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
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)}')INDEX is created and points to a new dict: {returns: 'refund time is 30 days', shipping: 'we ship to india', gifts: 'cards for birthdays'}.search(). Its body does not run yet: it runs each time the function is called.hits is created with the value 0.query is created with the value 'refund time'. expected is created with the value 'returns'.search(), with query = 'refund time'. It gets its own names.words is created and points to a new set: ['refund', 'time'].max(scores, key=scores.get).search() has finished, so its names are gone. The value it returned, 'returns', is stored in found.found == expected is true, so the block under it runs.hits changes from 0 to 1.query changes from 'refund time' to 'ship to india'. expected changes from 'returns' to 'shipping'.search(), with query = 'ship to india'. It gets its own names.words is created and points to a new set: ['india', 'ship', 'to'].scores is created and points to a new dict: {returns: 0, shipping: 3, gifts: 0}.max(scores, key=scores.get).search() has finished, so its names are gone. The value it returned, 'shipping', is stored in found.found == expected is true, so the block under it runs.hits changes from 1 to 2.query changes from 'ship to india' to 'present ideas'. expected changes from 'shipping' to 'gifts'.search(), with query = 'present ideas'. It gets its own names.words is created and points to a new set: ['ideas', 'present'].max(scores, key=scores.get).search() has finished, so its names are gone. The value it returned, 'returns', is stored in found.found == expected is false, so the block under it is skipped.for query, expected in tests.Answer: 2/3
Answer: Meaning-based (embedding) search
← Grounded Answers and "I Don't Know" · Tools, Schemas and Validation →