Lab 05 — RAG from scratch

Scaffold: 3/5. No managed vector store, no Knowledge Base. Just a model, a handful of documents, and the retrieval step, which you write.

The scenario

You have used a Knowledge Base in the reading; now build the thing it hides. A support assistant must answer questions about Greenbox (a fictional product, so the model cannot know the answers from training) using only five short documents. The point is to see retrieval with nothing in the way: embed the question, compare it to the documents, pick the closest, ground the answer in them.

The requirement

What’s provided

Your task

Implement retrieve(query, k) in src/handler.py:

  1. Embed the query with _embed(query).
  2. Score it against every document vector with _cosine().
  3. Return the top k documents, best first.

Four lines. Everything else is wired. This is the exact loop a vector store runs for you at scale, an approximate-nearest-neighbour search over embeddings; here you do it by brute force so the shape is clear.

Run it

# Prerequisite: Model access enabled for BOTH the generation model and the
# Titan embedding model, in your region.
./scripts/deploy.sh
./scripts/test.sh
./scripts/teardown.sh

What success looks like

Before you fill retrieve(), the function raises NotImplementedError. After, “when does my box arrive?” returns Tuesdays and Fridays with sources naming the delivery-days document, and the carbon-footprint question returns an honest refusal because no document supports an answer.

If it fails

Reveal the solution

SRC=solution ./scripts/deploy.sh && ./scripts/test.sh

What you just learned

Next

Lab 06 — Wire a tool the model can call. You move from stuffing retrieved text into a prompt to letting the model call a tool, run it, and read the result back: function calling with a real execution loop, the mechanics a managed agent automates.