Spaces:
Sleeping
Sleeping
Create tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Step 2: Create the Retriever Tool
|
| 2 |
+
# Now, let’s create a custom tool that Alfred can use to search through our guest information.
|
| 3 |
+
|
| 4 |
+
# We will use the BM25Retriever from the langchain_community.retrievers module to create a retriever tool.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from smolagents import Tool
|
| 8 |
+
from langchain_community.retrievers import BM25Retriever
|
| 9 |
+
|
| 10 |
+
class GuestInfoRetrieverTool(Tool):
|
| 11 |
+
name = "guest_info_retriever"
|
| 12 |
+
description = "Retrieves detailed information about gala guests based on their name or relation."
|
| 13 |
+
inputs = {
|
| 14 |
+
"query": {
|
| 15 |
+
"type": "string",
|
| 16 |
+
"description": "The name or relation of the guest you want information about."
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
output_type = "string"
|
| 20 |
+
|
| 21 |
+
def __init__(self, docs):
|
| 22 |
+
self.is_initialized = False
|
| 23 |
+
self.retriever = BM25Retriever.from_documents(docs)
|
| 24 |
+
|
| 25 |
+
def forward(self, query: str):
|
| 26 |
+
results = self.retriever.get_relevant_documents(query)
|
| 27 |
+
if results:
|
| 28 |
+
return "\n\n".join([doc.page_content for doc in results[:3]])
|
| 29 |
+
else:
|
| 30 |
+
return "No matching guest information found."
|
| 31 |
+
|
| 32 |
+
# Initialize the tool
|
| 33 |
+
guest_info_tool = GuestInfoRetrieverTool(docs)
|