← Forward Deployed Engineering Lesson 5 of 12

Answers from client documents

Why not paste everything?

Northstar has fee sheets, course pages and policies: about 120 pages. You could paste all of it into every model call.

But that's expensive, slow, and the model can miss the one line that matters.

Instead, for each question, find the few pieces that answer it, and send only those. This is called retrieval-augmented generation, or RAG: retrieve first, then generate the reply.

Compare the two ways for one question

First, the documents need cutting up.

Cut documents into chunks

You can only send pieces if the documents are cut into pieces. Each piece is a chunk.

Too big, and each chunk carries lots of text that doesn't matter. Too small, and a chunk loses what it's about.

Neighbouring chunks often share a few words, called overlap, so an idea split at a boundary isn't lost.

Change the chunk size for this course page
Sentences per chunk2

How do you find the right chunk for a question?

Meaning as numbers

Searching for exact words fails: a parent types "mern course fees", and the page says "Full Stack Web Development".

So a model turns every chunk into a list of numbers that captures its meaning, called an embedding. Texts that mean similar things get similar numbers, so they sit close together.

Real embeddings have hundreds of numbers. Here, just two, so you can see them on a map.

Tap a question to find its nearest chunks
Tap a question.

"Close" needs a number.

Score the matches

To measure how close two embeddings are, multiply their numbers pair by pair and add them up:

score = a₁ × b₁ + a₂ × b₂

For embeddings of length 1, as most are, this is called cosine similarity. Higher means closer. You give the model the top few chunks.

Question: "can i pay monthly?" = (0.6, 0.8). Score each chunk.

Your turn to score.

✏️ Practice 5.1

Remember:
score = a₁ × b₁ + a₂ × b₂
Higher score = closer meaning.
1. Question (0.6, 0.8). Chunk A is (0.8, 0.6). What's the score?
2. Chunk B is (1, 0). What's its score for the same question?
3. Which chunk should go to the model first, A or B?

Answer only from what was found

Finding the right chunks isn't enough. The model must answer only from them, and say so when they don't contain the answer.

So the rules say: use only these pieces, and if the answer isn't there, say you don't know and offer a counsellor. Answering only from given sources is called grounding.

"is hostel available near city centre campus? for girls". No chunk mentions hostels. Which reply?
Tap a reply.

What if the documents themselves are wrong?

Bad documents, bad answers

Remember the two fee sheets? Both get cut into chunks, and both match "fees for full stack". The model gets ₹1,35,000 and ₹1,45,000.

No prompt fixes that. Fix it at the source: decide which document is the truth, and keep labels on each chunk, like its date and owner. Labels like these are called metadata.

How would you fix it?
Tap a fix.

Time to count some chunks.

✏️ Practice 5.2

Remember:
With chunk size c words and overlap o, each new chunk starts c − o words after the last one:
chunks = (words − o) ÷ (c − o), rounded up
1. A 1,200-word policy, chunks of 300 words, no overlap. How many chunks?
2. The same policy, chunks of 300 words with 50 words of overlap. How many chunks?
3. Northstar's 120 pages have about 400 words each. With 400-word chunks and no overlap, how many chunks?

Do it for real

Now build it on Northstar's own course pages and policies. In the notebook you'll cut them into chunks, turn the chunks into embeddings, and search them.

Then you'll make the agent answer only from what it found, catch the old fee list, and measure how often your search finds the right page.

Build answers from documents in Google Colab

Colab runs Python in your browser for free. The embeddings model is free too, and needs no key. About 45 minutes.

Then check what stuck.

Lesson test

Five questions, all on new examples.

Question 1A law firm has 5,000 pages of contracts. Why not paste them all into each question?
Question 2A customer asks about "getting my money back", and the policy says "refunds". Which search finds it?
Question 3Question (1, 0), chunk (0.5, 0.9). What's the score?
Question 4A bank's bot is asked about a loan type that isn't in any retrieved chunk. What should it do?
Question 5A company's search holds two versions of its leave policy, and the bot quotes both. What's the real fix?

What's next?

This leaves a question
How does it take actions, not just answer?
Lesson 6 answers it.