RAG: search that answers · Chapter 21 of 37
Better questions
By the end of this chapter you can rewrite a merchant's message into a search query, search several versions of one question, search with a hypothetical answer, and make a follow-up searchable, and you know from measurements which of them to switch on. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Better retrieval
- 4 tasks
- In the all labs plan
The first 2 chapters of RAG: search that answers are free. This one comes with the all labs plan.
Why you need this
Search gets exactly what the merchant typed. Kabir pastes two real chats. One starts "Hello team, good morning. I run a small sweets shop in Indore and I am new to all this", and the question comes at the end. In the other, the merchant asks how long card refunds take, gets an answer, and then writes "and for UPI?". Searched alone, those three words find nothing about refunds at all.
The common fix is to put a model before the search: let it rewrite the question. Every rewrite is a model call on the critical path of every question, so it has to earn its place. This chapter builds four ways of doing it and measures each one, honestly: on Paisa Setu's questions, most of them do not help.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
rewrite: a chatty message as a search query
Ask MODEL (lab-chat) to rewrite the question: REWRITE as the system message, then the question as a user message. Return the reply's text with the spaces at both ends trimmed, or the question itself when the reply is empty.
await rewrite('Hi team, urgently need help: how long is a payment link valid?') is 'need help long payment link valid': no greeting, no filler.
- Task 2
multiQuery: several queries, one result
Ask MODEL for n queries: multiPrompt(n) as the system message, the question as a user message, in one request. The reply has one query per line: trim every line, drop empty lines and repeats, and keep at most n. Search each query with vectorSearch for CANDIDATES (20) chunks, fuse the lists with rrf, and return { queries, ids: the k best fused ids }.
await multiQuery(db, 'my money has not come since Friday', 3) has the queries 'my money has not come since Friday', 'my settlement not received since Friday' and 'money has come since Friday'. Each query is embedded once.
- Task 3
hydeSearch: search with a hypothetical answer
Ask MODEL for a passage that would answer the question: HYDE as the system message, the question as a user message. Trim the reply, search with the passage (not the question) using vectorSearch, and return { passage, ids: the k best chunk ids }.
await hydeSearch(db, 'how long do refunds take?', 5) gets the passage 'Refunds are typically processed within 5 to 7 working days and credited back to the original payment method.', wrong for Paisa Setu, and its ids are vectorSearch of that passage, which are not the ids the question finds.
- Task 4 · interview question
Interview: standalone, a follow-up made whole
Send REWRITE as the system message, then every message of history in order, then the question as a user message. Return the rewritten query, trimmed, or the question itself when the reply is empty. Do not change history.
With the history [user: 'how long do card refunds take?', assistant: 'Card refunds take 5 to 7 working days to reach the customer.'], await standalone(history, 'and for UPI?') is 'long card refunds take UPI': the follow-up gets the words of the question before it.
Arjun will ask: a merchant writes 'and for UPI?'. What does your search look for?
What you will learn
- Rewriting: a chatty message into a short search query.
- Multi-query: several versions of a question, searched and fused.
- HyDE: searching with a hypothetical answer instead of the question.
- Follow-ups: a standalone query built from the conversation.
More in Better retrieval
Build "Better questions" yourself
Paisa Setu, a payments company, needs its help centre to answer merchants correctly. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of RAG: search that answers are free. This one comes with the all labs plan.