Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.5 — Retrieval in a Conversation
RAG and Vector Search · Example 1 of 2

Standalone questions from history

The model is scripted, one reply per follow-up. A real one reads the history and writes the reply itself.

In a chat, the latest message is often not a question on its own: "what about the second one?". Turn the history plus the follow-up into a standalone question before retrieval, and skip retrieval when the message does not need the documents.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write needs_rewrite(history, message) that returns True when the history is non-empty and the message has four words or fewer, or contains one of it, that, one. Print yes or no for three cases.

02

Write last_turns(history, n, limit) returning the last n messages, each cut to limit characters. Print the result for six short messages with n = 4 and limit = 10, joined with " | ".

Explain it without notes

01

Why can't a retriever answer "how much is it?"?

02

What goes into a standalone-question prompt, and what do you leave out?

03

Why not make a model call on every turn?

04

Why must the rule for skipping retrieval be narrow?

Standalone questions from history
Sign in to run this example in your browser.

Expected output

message:     and on holidays?
standalone:  What time do shops close on public holidays? (model called)
raw search:  D2 (1)   standalone search: D2 (4)
message:     How much is it?
standalone:  How much is the monthly pass? (model called)
raw search:  none   standalone search: D8 (2)
message:     Do I pay for the other one?
standalone:  Do I pay for another bike after a flat tyre? (model called)
raw search:  D4 (1)   standalone search: D10 (4)
message:     What does the monthly pass cost?
standalone:  What does the monthly pass cost? (no history, no call)
raw search:  D8 (2)   standalone search: D8 (2)