Stateful by Design: Build a RAG Search Tool
In the previous lesson, you saw how a vector-based knowledge base of appliance manuals can be built and searched using embeddings.
Now, you'll build a custom tool that wraps this search logic, so an agent can use it to answer appliance-related questions.
The tool you're writing will subclass the Tool
base class and expose one input: a question about appliance operation
You already have access to:
- A variable called
vector_store
, which contains your pre-built FAISS index - Document chunks with appliance manual content, embedded and ready for search
Your job is to implement the structure and logic of the tool that will make this knowledge base accessible to an agent.
Cet exercice fait partie du cours
AI Agents with Hugging Face smolagents
Instructions
- Accept a
vector_store
parameter in the__init__()
method. - Add
query
as the parameter to theforward()
method. - Use
self.k
to set how many relevant documents should be returned from the similarity search.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
class ApplianceSearchTool(Tool):
name = "appliance_manual_search"
description = "Search appliance manuals for maintenance and usage information"
inputs = {"query": {"type": "string", "description": "Question about appliance operation"}}
output_type = "string"
# Pass the vector store into the constructor
def __init__(self, ____, k=3):
super().__init__()
self.vector_store = vector_store
self.k = k
# Accept the query string as input to the forward method
def forward(self, ____):
# Use self.k here to specify how many chunks to return
docs = self.vector_store.similarity_search(query, k=____)
return "\n\n".join(doc.page_content for doc in docs) or "No relevant manual sections found."