Start learning free

LESSON 64 OF 129 · AI ENGINEERING: RETRIEVAL (RAG)

Nothing Relevant: Say So in Python

Practice

Customers don't only ask what the shop's documents can answer. But the search always returns three chunks, the three least bad ones, even when none of them has anything to do with the question. The model is then paid to read them, and it tells the customer about "passages", a word the customer has never seen.

The score decides: the search gives every chunk a score, and the best score tells you whether there is a real match. A threshold is the lowest score you still accept as a match. For a library's help desk it could work like this:

"When do you open?"            best score 0.71  →  ask the model
"Who painted the Mona Lisa?"   best score 0.18  →  "Sorry, I can't help with that."

Below the threshold, your code answers by itself and the model is never called: an honest sentence, at no cost, in words the shop chose. The right threshold depends on the embedding model and on your documents. You find it by printing the scores of questions you can answer and of questions you can't.

When your program is finished, it prints the first five lines below. Then the check counts the calls and asks two more questions through your function:

Best score: 0.825
Yes, in most cases. You can return any item within 14 days of delivery if it is unused and in its original packaging. Gift cards and items bought on sale can't be returned or refunded.
Source: returns.md
Best score: 0.32
I don't know, let me find someone who does.
Calls to the model: 1
Best score: 0.802
Do you ship to Canada? -> Yes. Shipping to Canada costs a flat $15 and takes 7 to 10 business days. ['shipping.md'] | calls to the model: 1
Best score: 0.248
Who won the football game? -> I don't know, let me find someone who does. [] | calls to the model: 0

Your task: ask "What's the weather tomorrow?" and print the answer. In answer, print the best score, rounded to 3 decimals. Add a constant THRESHOLD with the value 0.4, and when the best score is below it, return "I don't know, let me find someone who does." with an empty list of sources, without calling the model.


Assignment
Follow the Coding Tutorial to print the score that decides and to answer "I don't know" yourself when nothing relevant was found.


Hint
The passages are sorted with the best one first, so the best score is on the first passage. Return a dictionary with the same two keys the model's answers have, so the rest of the program works unchanged.


STEP 1 OF 7
    Ctrl + Enter to check

    Ai Say So in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free