Welcome! Today, we’ll see how to build a simple AI agent that answers questions using external knowledge. Even advanced AI models sometimes need to look up information — just as you might check your notes or Google before answering a tough question.
Retrieval-Augmented Generation (RAG) helps AI agents find and use relevant information from a knowledge base for more accurate answers. By the end, you’ll know how to create a basic agent that uses RAG to answer user queries more effectively.
First, the agent needs access to knowledge. Here, our knowledge base is a JSON file with study tips and resources.
Here’s how to load it in Python:
This code locates and opens data.json, loading its content as a list of dictionaries. Each dictionary is a document, for example:
This simple structure works well for small knowledge sets. Larger, real-world knowledge bases follow the same principle: load and access information for the agent.
With the knowledge base loaded, the next step is retrieval — finding the most relevant document for a user’s question. We use a basic method: word overlap. We count how many words from the user’s question appear in each document. The document with the most overlap is chosen.
Here’s the code:
The query and each document are split into lowercase words, and the code counts overlapping words. The document with the highest overlap is returned. This method is simple and fast. More advanced systems use semantic search, but word overlap is a great way to start understanding retrieval.
After finding the most relevant document, we give this context to the agent. The agent uses both the user’s question and the retrieved document to answer. We build a prompt that combines both:
If a document is found, the prompt includes its content. If not, the agent just gets the original question. Providing context helps the agent focus on useful information, leading to better answers. This is the core of RAG: retrieval plus generation.
Now, let’s see how the agent uses the prompt to generate a response. The agent is defined with instructions and run using a helper:
The agent gets the prompt (with or without context), generates a response using its instructions and the context, and prints the response. This workflow — retrieval, prompt building, and response generation — is the foundation of a simple RAG agent.
You’ve learned how to build a simple agent using Retrieval-Augmented Generation (RAG) for better answers. We covered loading a knowledge base from JSON, retrieving the most relevant document using word overlap, building a prompt with the user’s question and context, and running the agent to generate a response. This approach makes AI agents more helpful, especially for specific topics or documents.
Now it’s your turn! Next, you’ll practice building and running your own RAG-powered agent. You’ll load data, retrieve relevant information, and generate answers using an agent. This hands-on work will reinforce your understanding and prepare you for more advanced RAG techniques.
