Agentic RAG: Rewrite, Search, Decide

Rewrite the user's words into search terms, search, and decide whether the result is good enough to answer from.

AI · intermediate · 7 min · 26 steps · free

What you'll learn

agentic_rag.py

DOCS = {'refund': 'Refunds are free for 30 days.', 'shipping': 'We ship to India in 5 days.'}

def search_docs(query):
    for key, text in DOCS.items():
        if key in query:
            return text
    return None

def agent(question):
    query = question.lower().replace('money back', 'refund')
    found = search_docs(query)
    if found is None:
        return "I couldn't find that in our documents."
    return f'{found} (from our documents)'

print(agent('Can I get my money back?'))
print(agent('Do you have stores in Paris?'))

Step by step

  1. DOCS is created and points to a new dict: {refund: 'Refunds are free for 30 days.', shipping: 'We ship to India in 5 days.'}.
  2. Python defines the function search_docs(). Its body does not run yet: it runs each time the function is called.
  3. Python defines the function agent(). Its body does not run yet: it runs each time the function is called.
  4. Python calls agent(), with question = 'Can I get my money back?'. It gets its own names.
  5. "money back" is rewritten to "refund", the word the documents use.
  6. Python calls search_docs(), with query = 'can i get my refund?'. It gets its own names.
  7. key is created with the value 'refund'. text is created with the value 'Refunds are free for 30 days.'.
  8. "refund" is in the rewritten query: the refund document matches.
  9. The function returns text.
  10. search_docs() has finished, so its names are gone. The value it returned, 'Refunds are free for 30 days.', is stored in found.
  11. found is None is false, so the block under it is skipped.
  12. The answer is built from the document and says where it came from.
  13. agent() has finished, so its names are gone. It prints Refunds are free for 30 days. (from our documents).
  14. Python calls agent(), with question = 'Do you have stores in Paris?'. It gets its own names.
  15. query is created with the value 'do you have stores in paris?'.
  16. Python calls search_docs(), with query = 'do you have stores in paris?'. It gets its own names.
  17. key is created with the value 'refund'. text is created with the value 'Refunds are free for 30 days.'.
  18. key in query is false, so the block under it is skipped.
  19. key changes from 'refund' to 'shipping'. text changes from 'Refunds are free for 30 days.' to 'We ship to India in 5 days.'.
  20. key in query is false, so the block under it is skipped.
  21. Python moves to the next item of the loop: for key, text in DOCS.items().
  22. The function returns None.
  23. Nothing about stores in Paris.
  24. found is None is true, so the block under it runs.
  25. The function returns "I couldn't find that in our documents.".
  26. agent() has finished, so its names are gone. It prints I couldn't find that in our documents..

Common mistakes

Best practices

Key takeaways

Check yourself

Why rewrite "money back"?

Answer: Documents say "refund"

Plain RAG vs agentic RAG?

Answer: Agentic can search again or differently

·

More in Agents