RAG in Python · 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, after it when the reply cites nothing) and measure what each refusal costs in rupees. 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
  • 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

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.

  1. Task 1

    refuse.py: score_summary, how close is the best chunk?

    Kabir has an Apple Pay answer, with two citations, for something Paisa Setu does not support. First measure what search scores tell you. Write score_summary(conn, questions=None, table="chunks") in refuse.py: for each question (merchant_questions() when questions is None), take the score of its best chunk (retrieve with k 1), and put it in one of two piles, questions whose kind is unanswerable and all the others (answerable). Return Scores(answerable=Summary, unanswerable=Summary), each Summary holding count, min, median and max of its pile.

    With the default questions, answerable.count is 62 and unanswerable.count is 10. The check passes a smaller sample: use the questions you are given. The median of an even count is the mean of the two middle values.

  2. Task 2

    refuse.py: answer, refuse before the model

    Write answer(conn, question, threshold=THRESHOLD, table="chunks"): retrieve the 5 best chunks; score is the best one's score. When score is under threshold, return Reply(status="refused", reason="weak-retrieval", score=score) without calling the model at all. Otherwise send build_messages(question, hits) to MODEL with the given client, and return Reply(status="answered", text=the reply's text, cited=cited_ids(text, the ids sent), score=score).

    answer(conn, "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. Use the threshold parameter, so answer(conn, question, 0) always asks the model.

  3. Task 3

    refuse.py: answer, refuse when the reply cites nothing

    Finish answer: when the reply cites none of the chunks you sent (cited_ids of the reply and the ids of the hits is empty), return Reply(status="refused", reason="model", score=score); otherwise the answered Reply as before.

    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. The check also tries a model that refuses in its own words: do not look for the words "I don't know"; a missing citation is the sign you can rely on.

  4. Task 4 · interview question

    Interview: refuse.py, evaluate, what refusing costs

    Write evaluate(conn, threshold, questions=None, table="chunks"): run answer(conn, q.question, threshold, table) for every question (merchant_questions() when questions is None) and return Evaluation(questions=how many you ran, model_calls=all but the weak-retrieval refusals, wrong=unanswerable questions that got an answer, missed=answerable questions that were refused for either reason, cost=wrong * WRONG_COST + missed * MISSED_COST).

    With the default questions, evaluate(conn, 0) makes 72 model calls and evaluate(conn, 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, 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.