Spaces:
Runtime error
Runtime error
Update retriever.py
Browse files- retriever.py +17 -52
retriever.py
CHANGED
|
@@ -1,53 +1,18 @@
|
|
| 1 |
-
from smolagents import Tool
|
| 2 |
from langchain_community.retrievers import BM25Retriever
|
| 3 |
-
from langchain.
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
name
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
self.retriever = BM25Retriever.from_documents(docs)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def forward(self, query: str):
|
| 24 |
-
results = self.retriever.get_relevant_documents(query)
|
| 25 |
-
if results:
|
| 26 |
-
return "\n\n".join([doc.page_content for doc in results[:3]])
|
| 27 |
-
else:
|
| 28 |
-
return "No matching guest information found."
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def load_guest_dataset():
|
| 32 |
-
# Load the dataset
|
| 33 |
-
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
| 34 |
-
|
| 35 |
-
# Convert dataset entries into Document objects
|
| 36 |
-
docs = [
|
| 37 |
-
Document(
|
| 38 |
-
page_content="\n".join([
|
| 39 |
-
f"Name: {guest['name']}",
|
| 40 |
-
f"Relation: {guest['relation']}",
|
| 41 |
-
f"Description: {guest['description']}",
|
| 42 |
-
f"Email: {guest['email']}"
|
| 43 |
-
]),
|
| 44 |
-
metadata={"name": guest["name"]}
|
| 45 |
-
)
|
| 46 |
-
for guest in guest_dataset
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
# Return the tool
|
| 50 |
-
return GuestInfoRetrieverTool(docs)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 1 |
from langchain_community.retrievers import BM25Retriever
|
| 2 |
+
from langchain.tools import Tool
|
| 3 |
+
|
| 4 |
+
bm25_retriever = BM25Retriever.from_documents(docs)
|
| 5 |
+
|
| 6 |
+
def extract_text(query: str) -> str:
|
| 7 |
+
"""Retrieves detailed information about gala guests based on their name or relation."""
|
| 8 |
+
results = bm25_retriever.invoke(query)
|
| 9 |
+
if results:
|
| 10 |
+
return "\n\n".join([doc.text for doc in results[:3]])
|
| 11 |
+
else:
|
| 12 |
+
return "No matching guest information found."
|
| 13 |
+
|
| 14 |
+
guest_info_tool = Tool(
|
| 15 |
+
name="guest_info_retriever",
|
| 16 |
+
func=extract_text,
|
| 17 |
+
description="Retrieves detailed information about gala guests based on their name or relation."
|
| 18 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|