Pydantic AI · Chapter 10 of 23
RAG as a tool
By the end of this chapter you can turn KhetBazaar's policy into embeddings with Pydantic AI's Embedder, store them in Postgres with pgvector, give the agent a search tool that finds the rules nearest a farmer's question, show the rules behind each answer, and explain why the claim question from chapter 1 is now answered right. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Real-world agents
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.
Why you need this
In chapter 1 a farmer asked how long he had to report hail damage, and the agent said 15 days. Sneha still thinks about that answer. KhetBazaar's rule 3.2 says 72 hours. A farmer who believed the agent would have reported on day 10, and lost his claim.
The agent was not lying. It had never seen the policy, so it wrote what such answers usually say. Kedar's fix is the one this lab promised: give the agent KhetBazaar's facts. The policy has 14 rules today and will have more; pasting all of it into every request costs tokens and gets worse as it grows. So the agent will search the policy, and read only the rules that answer the question. That pattern is called RAG: retrieval augmented generation.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
embed_store.py: the policy in pgvector
Embed every rule of /workspace/data/policy.md (the lines that start with a digit, like 3.2 A claim must be...) with Pydantic AI's Embedder for openai:lab-embed, all in one call, and store them in Postgres (psycopg.connect() finds the lab's database).
Make the pgvector extension if it is missing, and a table policy_rules if it is missing, with the columns rule (text, the primary key: the rule's number, like 3.2), text (the rule without its number) and embedding (vector(384), the embedding of the whole line). Insert one row per rule; when the rule is already there, replace its text and embedding. Then print Stored and the number of rows in the table, then rules: Stored 14 rules.
The Check runs your program twice: the table must still hold each rule once.
- Task 2
search_tool.py: a search tool the agent calls
Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. Answer only from the policy rules the tool finds, and give the rule number.
Give it an async tool search_policy that takes only the run context, with the docstring Search KhetBazaar's farmer policy: selling, charge, payment, cash, collection, grading, credit, seeds, return, claim, loss, hail and spray advice. The tool embeds the farmer's question (the run's prompt) with the Embedder's method for queries, finds the 3 rules of policy_rules nearest to it by cosine distance, and returns them as text, one rule per line: its number, a space, its text.
Run the agent on Is there a limit on credit for seeds? and print the output. Use the table task 1 made.
- Task 3
sources.py: answers with their sources
Make the agent and search_policy tool of task 2 again, with a dependency: a dataclass Sources with one field, rules, a list of strings that starts empty. Each time search_policy runs, it adds the numbers of the rules it found to the deps' rules, in order.
Run the agent on Do I get cash on the day of weighing? with a new Sources as its deps, and print the output, then a last line Sources: and the rule numbers, joined by a comma and a space (like Sources: 3.2, 3.5, 3.1).
- Task 4 · interview question
Interview: claim_right.py, the claim question, now right
Chapter 1's agent answered this confidently and wrongly: Hail damaged my grapes. How long do I have to report the loss?
Ask it twice. First an agent for openai-chat:lab-chat with no instructions and no tools, as in chapter 1. Then the agent and search_policy tool of task 2. Print Before: and the first output, then After: and the second (the second may run over several lines).
The interviewer will ask: why is the second answer right, and what could still make it wrong?
What you will learn
- Embeddings: text as a list of numbers, near for near meanings, made with Pydantic AI's Embedder.
- pgvector: embeddings in a Postgres table, searched by distance.
- A search tool: the agent calls it, the tool embeds the farmer's question and returns the nearest rules.
- Sources: the rules behind an answer, collected through deps.
More in Real-world agents
Build "RAG as a tool" yourself
KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.