RAG in Python · 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
- Python
- In the all labs plan
The first 2 chapters of RAG in Python 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 a net banking refund takes, gets an answer, and then writes "and for cards?". Searched alone, those three words find articles about accepting cards, and nothing about refunds.
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
rewriting.py: rewrite, a chatty message as a search query
Kabir's merchants write greetings and stories before the question. Write rewrite(question): ask MODEL (lab-chat) to rewrite it, with REWRITE as the system message and the question as a user message, and return the reply's text stripped of spaces at both ends, or the question itself when the reply is empty or None.
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
rewriting.py: multi_query, several queries, one result
Write multi_query(conn, question, n=3, k=10, table): ask MODEL for n queries in one request, with multi_prompt(n) as the system message and the question as a user message. The reply has one query per line: strip every line, drop empty lines and repeats (keep the first), and keep at most n. Search each query with vector_search for CANDIDATES (20) chunks, fuse the lists with rrf, and return MultiQuery(queries, ids), where ids are the k best fused ids.
multi_query(conn, "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. The check also gives it a reply with a blank line and a repeat.
- Task 3
rewriting.py: hyde_search, search with a hypothetical answer
Write hyde_search(conn, question, k=10, table): ask MODEL for a passage that would answer the question, with HYDE as the system message and the question as a user message. Strip the reply, search with the passage (not the question) using vector_search, and return Hyde(passage, ids), the k best chunk ids for the passage.
hyde_search(conn, "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 vector_search of that passage, which are not the ids the question finds.
- Task 4 · interview question
Interview: rewriting.py, standalone
Write standalone(history, question): send REWRITE as the system message, then every message of history in order, then the question as a user message. Return the rewritten query, stripped, 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."], 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, with sources. 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 in Python are free. This one comes with the all labs plan.