RAG: search that answers · Chapter 26 of 37
Saying I do not know
By the end of this chapter you can make the assistant refuse when it has nothing to answer from, before the model when search finds nothing close and after it when the reply cites nothing, and measure what each refusal costs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Answers
- 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
Kabir brings a reply from the trial. A merchant asked "can customers pay with Apple Pay on my checkout?" Paisa Setu does not support Apple Pay, and no document says it does. The assistant answered anyway, with two citations, about the hosted checkout page. The merchant spent a day setting it up. A wrong answer like that costs Paisa Setu a callback, an apology and sometimes money; "I don't know, an agent will reply" costs an agent a few minutes.
The rules from chapter 24 tell the model to say it does not know, and the citations from chapter 25 show where an answer came from. Neither helped here: search always returns five chunks, even for a question no document answers, and the model found words in them to answer from.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
scoreSummary: how close is the best chunk?
For each question, take the score of its best chunk (retrieve with k of 1), and sort the scores into two piles: questions whose kind is unanswerable, and all the others (answerable). Return { answerable, unanswerable }, each a Summary: count, min, median and max of its scores.
With the default questions (the 72 merchant questions), answerable.count is 62 and unanswerable.count is 10. Use the questions parameter: the Check passes a smaller sample.
The median is the middle value of the sorted scores; with an even count, it is the mean of the two middle values.
- Task 2
answer: refuse before the model
answer(question, threshold) retrieves the 5 best chunks; score is the best one's score. When score is under the threshold, return { status: 'refused', reason: 'weak-retrieval', score } without calling the model at all.
answer('do you have an office in Dubai I can visit?') is refused for weak-retrieval with score 0.2625..., and so is the Apple Pay question (0.471), with no request to the model. THRESHOLD (0.5) is the default: use the threshold parameter, so answer(question, 0) always asks the model.
- Task 3
answer: refuse when the reply cites nothing
Otherwise, send buildMessages(question, hits) to MODEL. When the reply cites none of the chunks you sent (citedIds of the reply and the ids of the hits is empty), return { status: 'refused', reason: 'model', score }. Otherwise return { status: 'answered', text, cited, score }, with text the reply's text and cited its citedIds.
The customer care phone number question scores 0.678, so it goes to the model, which cites nothing: refused, reason model. 'how long is a payment link valid?' is answered, with the ids it cites.
Do not look for the words "I don't know": real models refuse in many ways, and a missing citation is the sign you can rely on.
- Task 4 · interview question
Interview: evaluate, what refusing costs
Run answer(q.question, threshold) for every question given and count: questions (how many you ran), modelCalls (all but the weak-retrieval refusals), wrong (unanswerable questions that got an answer), missed (answerable questions that were refused, for either reason), and cost, which is wrong * WRONG_COST + missed * MISSED_COST.
With the default questions, evaluate(0) makes 72 model calls and evaluate(0.5) makes 59. Use the threshold and the questions you are given.
Arjun will ask why you need a threshold when the model already says it does not know, and where you put it.
What you will learn
- Retrieval scores as evidence: how close the best chunk is, measured on answerable and unanswerable questions.
- A threshold: refusing before the model when search is weak.
- The model's refusal: a reply that cites nothing is not an answer.
- The cost: wrong answers against missed ones, in rupees.
More in Answers
Build "Saying I do not know" 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.