Introduction to Agentic RAG
1. Introduction to Agentic RAG
Great work so far! Now, let's move beyond traditional RAG.2. Agentic RAG: Iterative Retrieval + Reasoning
Agentic RAG blends retrieval with iterative agent reasoning: instead of one search, the agent retrieves results, spots gaps, and issues refined queries until it has enough evidence to provide a complete answer. So let's turn our RAG pipeline from the previous lesson into an agentic RAG one by wrapping the similarity search step into a custom tool.3. Stateless vs. Stateful Tools in smolagents
In Chapter 1, you used the @tool decorator on simple functions for creating custom tools. But that won't work here, as function-based tools are stateless. This means they can't remember your vector store between calls. Instead, you need class-based tools that inherit from the Tool class. These maintain state and hold references to complex objects like vector stores.4. The Anatomy of a Class-Based Tool
Let's break down a class-based tool step by step. The name identifies the tool to the agent. The description explains what it does, so the agent knows when to use it. The inputs define parameters with their types and purposes, and the output_type specifies the return format. The .__init__() method sets up any persistent state your tool needs to remember between calls. The super().__init()__ initializes the parent Tool class before you add your custom parameters. The .forward() method contains the tool's actual logic, which the agent calls with the right parameters. With this structure, the agent can understand what the tool does, when to use it, and how to interact with it.5. Build a Recipe Search Tool
Now let's build a recipe search tool, providing a name, description, inputs, and output type. The .__init__() method keeps a reference to the vector store and sets how many documents to retrieve with the k parameter. Defaulting to 6 documents provides good coverage without overwhelming the agent. The .forward() method performs similarity search, then joins all retrieved documents with double newlines to create readable, separated text blocks. If no matches are found, it returns "Nothing found." so the agent always gets a clear response.6. Cooking Assistant Agent: Putting It All Together
Now let's create an agent with this tool. Here, the instructions parameter acts as the system prompt that guides agent behavior. It tells the agent to be thorough and search multiple times if needed. The verbosity_level controls how much detail you see during execution. 0 means printing only the final answer, 1 shows the agent's reasoning steps and tool calls, and 2 provides full debugging output including internal processing. The max_steps parameter limits how many actions the agent can take. This prevents infinite loops where an agent might keep searching without reaching a conclusion.7. Agent Run Example
When this agent responds, it has thoroughly examined the cooking documentation to provide complete answers.8. Let's practice!
Time to practice building your own agentic RAG tools!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.