Evaluating Retrieval

Run test questions with known answers and measure the hit rate. One miss shows exactly where keyword matching fails.

AI · intermediate · 7 min · 30 steps · free

What you'll learn

retrieval_eval.py

INDEX = {'returns': 'refund time is 30 days', 'shipping': 'we ship to india', 'gifts': 'cards for birthdays'}

def search(query):
    words = set(query.split())
    scores = {name: len(words & set(text.split())) for name, text in INDEX.items()}
    return max(scores, key=scores.get)

tests = [('refund time', 'returns'), ('ship to india', 'shipping'), ('present ideas', 'gifts')]
hits = 0
for query, expected in tests:
    found = search(query)
    if found == expected:
        hits += 1
    else:
        print('missed:', query, '->', found)
print(f'hit rate: {hits}/{len(tests)}')

Step by step

  1. INDEX is created and points to a new dict: {returns: 'refund time is 30 days', shipping: 'we ship to india', gifts: 'cards for birthdays'}.
  2. Python defines the function search(). Its body does not run yet: it runs each time the function is called.
  3. Each test pairs a question with the document that should come back.
  4. hits is created with the value 0.
  5. query is created with the value 'refund time'. expected is created with the value 'returns'.
  6. Python calls search(), with query = 'refund time'. It gets its own names.
  7. words is created and points to a new set: ['refund', 'time'].
  8. "refund time" scores 2 against the returns document.
  9. The function returns max(scores, key=scores.get).
  10. search() has finished, so its names are gone. The value it returned, 'returns', is stored in found.
  11. found == expected is true, so the block under it runs.
  12. hits changes from 0 to 1.
  13. query changes from 'refund time' to 'ship to india'. expected changes from 'returns' to 'shipping'.
  14. Python calls search(), with query = 'ship to india'. It gets its own names.
  15. words is created and points to a new set: ['india', 'ship', 'to'].
  16. scores is created and points to a new dict: {returns: 0, shipping: 3, gifts: 0}.
  17. The function returns max(scores, key=scores.get).
  18. search() has finished, so its names are gone. The value it returned, 'shipping', is stored in found.
  19. found == expected is true, so the block under it runs.
  20. hits changes from 1 to 2.
  21. query changes from 'ship to india' to 'present ideas'. expected changes from 'shipping' to 'gifts'.
  22. Python calls search(), with query = 'present ideas'. It gets its own names.
  23. words is created and points to a new set: ['ideas', 'present'].
  24. "present ideas" shares no words with any document: every score is 0.
  25. The function returns max(scores, key=scores.get).
  26. search() has finished, so its names are gone. The value it returned, 'returns', is stored in found.
  27. found == expected is false, so the block under it is skipped.
  28. The miss: "present" should match "gifts". Meaning-based search would fix it.
  29. Python moves to the next item of the loop: for query, expected in tests.
  30. Hit rate 2/3: a number to beat after the next change.

Common mistakes

Best practices

Key takeaways

Check yourself

3 tests, 2 found. Hit rate?

Answer: 2/3

What fixes "present ideas"?

Answer: Meaning-based (embedding) search

·

More in The RAG Pipeline