Grounded Answers and "I Don't Know"

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

What you'll learn

grounded_answer.py

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'))

Step by step

  1. DOCS is created and points to a new dict: {returns: 'refunds are free for 30 days', shipping: 'we ship to india in 5 days'}.
  2. Python defines the function retrieve(). Its body does not run yet: it runs each time the function is called.
  3. Python defines the function answer(). Its body does not run yet: it runs each time the function is called.
  4. Python calls answer(), with question = 'are refunds free'. It gets its own names.
  5. Python calls retrieve(), with question = 'are refunds free'. It gets its own names.
  6. words is created and points to a new set: ['are', 'free', 'refunds'].
  7. best is created but does not point to any object yet (null). best_score is created with the value 0.
  8. name is created with the value 'returns'. text is created with the value 'refunds are free for 30 days'.
  9. score is created with the value 3.
  10. The returns document shares 3 words with the question: the best so far.
  11. best changes from null to 'returns'. best_score changes from 0 to 3.
  12. name changes from 'returns' to 'shipping'. text changes from 'refunds are free for 30 days' to 'we ship to india in 5 days'.
  13. score changes from 3 to 0.
  14. score > best_score is false, so the block under it is skipped.
  15. Python moves to the next item of the loop: for name, text in DOCS.items().
  16. The function returns best.
  17. retrieve() has finished, so its names are gone. The value it returned, 'returns', is stored in source.
  18. source is None is false, so the block under it is skipped.
  19. The answer comes with its source.
  20. answer() has finished, so its names are gone. It prints refunds are free for 30 days [source: returns].
  21. A question the documents do not cover.
  22. Python calls retrieve(), with question = 'do you sell gift cards'. It gets its own names.
  23. words is created and points to a new set: ['cards', 'do', 'gift', 'sell', 'you'].
  24. best is created but does not point to any object yet (null). best_score is created with the value 0.
  25. name is created with the value 'returns'. text is created with the value 'refunds are free for 30 days'.
  26. score is created with the value 0.
  27. score > best_score is false, so the block under it is skipped.
  28. name changes from 'returns' to 'shipping'. text changes from 'refunds are free for 30 days' to 'we ship to india in 5 days'.
  29. Line runs: score = len(words & set(text.split())).
  30. score > best_score is false, so the block under it is skipped.
  31. Python moves to the next item of the loop: for name, text in DOCS.items().
  32. The function returns best.
  33. No document scored above 0, so retrieve returns None.
  34. source is None is true, so the block under it runs.
  35. Saying "I don't know" is the correct, grounded answer here.
  36. answer() has finished, so its names are gone. It prints I don't know: none of our documents cover that..

Common mistakes

Best practices

Key takeaways

Check yourself

RAG reduces hallucination by?

Answer: Giving the model real text to answer from

Retrieval finds nothing. Best answer?

Answer: "I don't know"

·

More in The RAG Pipeline