Answer only from a retrieved source and say which one. When nothing matches, say so instead of making something up.
AI · intermediate · 7 min · 36 steps · free
DOCS = {'returns': 'refunds are free for 30 days', 'shipping': 'we ship to india in 5 days'}
def retrieve(question):
words = set(question.lower().split())
best, best_score = None, 0
for name, text in DOCS.items():
score = len(words & set(text.split()))
if score > best_score:
best, best_score = name, score
return best
def answer(question):
source = retrieve(question)
if source is None:
return "I don't know: none of our documents cover that."
return f'{DOCS[source]} [source: {source}]'
print(answer('are refunds free'))
print(answer('do you sell gift cards'))DOCS is created and points to a new dict: {returns: 'refunds are free for 30 days', shipping: 'we ship to india in 5 days'}.retrieve(). Its body does not run yet: it runs each time the function is called.answer(). Its body does not run yet: it runs each time the function is called.answer(), with question = 'are refunds free'. It gets its own names.retrieve(), with question = 'are refunds free'. It gets its own names.words is created and points to a new set: ['are', 'free', 'refunds'].best is created but does not point to any object yet (null). best_score is created with the value 0.name is created with the value 'returns'. text is created with the value 'refunds are free for 30 days'.score is created with the value 3.best changes from null to 'returns'. best_score changes from 0 to 3.name changes from 'returns' to 'shipping'. text changes from 'refunds are free for 30 days' to 'we ship to india in 5 days'.score changes from 3 to 0.score > best_score is false, so the block under it is skipped.for name, text in DOCS.items().best.retrieve() has finished, so its names are gone. The value it returned, 'returns', is stored in source.source is None is false, so the block under it is skipped.answer() has finished, so its names are gone. It prints refunds are free for 30 days [source: returns].retrieve(), with question = 'do you sell gift cards'. It gets its own names.words is created and points to a new set: ['cards', 'do', 'gift', 'sell', 'you'].best is created but does not point to any object yet (null). best_score is created with the value 0.name is created with the value 'returns'. text is created with the value 'refunds are free for 30 days'.score is created with the value 0.score > best_score is false, so the block under it is skipped.name changes from 'returns' to 'shipping'. text changes from 'refunds are free for 30 days' to 'we ship to india in 5 days'.score = len(words & set(text.split())).score > best_score is false, so the block under it is skipped.for name, text in DOCS.items().best.source is None is true, so the block under it runs.answer() has finished, so its names are gone. It prints I don't know: none of our documents cover that..Answer: Giving the model real text to answer from
Answer: "I don't know"
← Building the Prompt Within a Budget · Evaluating Retrieval →