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 2 of 2

When not to retrieve

Rules for "do not retrieve" are narrow on purpose. Anything unrecognised retrieves.

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?

When not to retrieve
Sign in to run this example in your browser.

Expected output

hello                             no retrieval (small talk)
Thanks a lot!                     no retrieval (small talk)
Can you make that shorter?        no retrieval (about the last answer)
hi, how do I reset my password?   retrieve
What does the monthly pass cost?  retrieve