Stateful by Design: एक RAG सर्च टूल बनाएँ
पिछले लेसन में, आपने देखा था कि कैसे appliance manuals के लिए embeddings का उपयोग करके एक vector-आधारित knowledge base बनाया और सर्च किया जा सकता है.
अब, आप एक कस्टम टूल बनाएँगे जो इस सर्च logic को wrap करेगा, ताकि कोई agent इसका उपयोग करके appliance-संबंधित प्रश्नों के उत्तर दे सके.
जो टूल आप लिख रहे हैं, वह Tool बेस क्लास से subclass होगा और एक इनपुट एक्सपोज़ करेगा: appliance operation से जुड़ा कोई प्रश्न.
आपके पास पहले से उपलब्ध है:
vector_storeनाम का एक वैरिएबल, जिसमें आपका pre-built FAISS index है- appliance manual सामग्री वाले डॉक्यूमेंट chunks, जो embed किए गए हैं और सर्च के लिए तैयार हैं
आपका काम है टूल की structure और logic को implement करना, ताकि यह knowledge base किसी agent के लिए सुलभ हो सके.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Hugging Face smolagents के साथ AI Agents
अभ्यास निर्देश
__init__()मेथड मेंvector_storeपैरामीटर स्वीकार करें.forward()मेथड के पैरामीटर के रूप मेंqueryजोड़ें.- similarity search से कितने प्रासंगिक डॉक्यूमेंट लौटाने हैं, इसके लिए
self.kका उपयोग करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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."