A model answers from what it remembers, and it has never read the Corner Shop's rules. A support assistant must answer from the shop's own documents. This is its knowledge, the first of the five parts you build in this module.
The way to do it is retrieval. Each document is cut into chunks, and each chunk has a vector. You embed the question, give every chunk a score (how close it is to the question), sort the chunks with the highest score first and look at the top three.
The best three are not always good. A threshold drops the ones that score too low. For example, a café's assistant with a threshold of 0.5:
Question: "When do you open on Sunday?" 0.71 hours.md kept 0.38 parking.md dropped, below 0.5 0.22 menu.md dropped, below 0.5
If no chunk is left, the honest answer is "I don't know", and the model is not called at all. Otherwise the passages go to the model with the question, and the answer comes back with its sources: the files it was found in. This is what your finished program prints:
Customer: Can I get my money back?
Assistant: {'answer': "Yes. You can return any item within 14 days of delivery, …", 'sources': ['returns.md']}
Customer: Do you ship to Canada?
Assistant: {'answer': 'Yes, we ship to Canada. It costs a flat $15 and takes 7 to 10 business days.', 'sources': ['shipping.md']}
Customer: What's the weather tomorrow?
Assistant: {'answer': "I don't know, let me find someone who does.", 'sources': []}
Model calls: 2Your task: the starting code already cuts the files in docs/ into chunks and gives each chunk its vector. Finish top_matches (the scores, the sorting, the top three, the threshold), write answer(question), and print the answers for the three waiting customers.
Assignment
Follow the Coding Tutorial to finish top_matches, write answer and answer the three customers.
Hint
Both returns of answer give a dictionary with the same two keys, "answer" and "sources". The return for an empty matches comes before the call to the model.
Free plan: 30 tutor messages. PRO is unlimited.
