Spaces:
Runtime error
Runtime error
Kseniya Zborovskaya
commited on
Commit
·
e24c44c
1
Parent(s):
2add2d9
commit
Browse files- app.py +15 -0
- retriever.py +28 -0
- tools.py +19 -0
app.py
CHANGED
@@ -5,3 +5,18 @@ def greet(name):
|
|
5 |
|
6 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
demo.launch()
|
8 |
+
|
9 |
+
from smolagents import CodeAgent, HfApiModel
|
10 |
+
from retriever import guest_info_tool
|
11 |
+
|
12 |
+
# Initialize the Hugging Face model
|
13 |
+
model = HfApiModel()
|
14 |
+
|
15 |
+
# Create Alfred, our gala agent, with the guest info tool
|
16 |
+
alfred = CodeAgent(tools=[guest_info_tool], model=model)
|
17 |
+
|
18 |
+
# Example query Alfred might receive during the gala
|
19 |
+
response = alfred.run("Tell me about our guest named 'Lady Ada Lovelace'.")
|
20 |
+
|
21 |
+
print("🎩 Alfred's Response:")
|
22 |
+
print(response)
|
retriever.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import Tool
|
2 |
+
from langchain_community.retrievers import BM25Retriever
|
3 |
+
from tools import docs
|
4 |
+
|
5 |
+
class GuestInfoRetrieverTool(Tool):
|
6 |
+
name = "guest_info_retriever"
|
7 |
+
description = "Retrieves detailed information about gala guests based on their name or relation."
|
8 |
+
inputs = {
|
9 |
+
"query": {
|
10 |
+
"type": "string",
|
11 |
+
"description": "The name or relation of the guest you want information about."
|
12 |
+
}
|
13 |
+
}
|
14 |
+
output_type = "string"
|
15 |
+
|
16 |
+
def __init__(self, docs):
|
17 |
+
self.is_initialized = False
|
18 |
+
self.retriever = BM25Retriever.from_documents(docs)
|
19 |
+
|
20 |
+
def forward(self, query: str):
|
21 |
+
results = self.retriever.get_relevant_documents(query)
|
22 |
+
if results:
|
23 |
+
return "\n\n".join([doc.page_content for doc in results[:3]])
|
24 |
+
else:
|
25 |
+
return "No matching guest information found."
|
26 |
+
|
27 |
+
# Initialize the tool
|
28 |
+
guest_info_tool = GuestInfoRetrieverTool(docs)
|
tools.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import datasets
|
2 |
+
from langchain.docstore.document import Document
|
3 |
+
|
4 |
+
# Load the dataset
|
5 |
+
guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
6 |
+
|
7 |
+
# Convert dataset entries into Document objects
|
8 |
+
docs = [
|
9 |
+
Document(
|
10 |
+
page_content="\n".join([
|
11 |
+
f"Name: {guest['name']}",
|
12 |
+
f"Relation: {guest['relation']}",
|
13 |
+
f"Description: {guest['description']}",
|
14 |
+
f"Email: {guest['email']}"
|
15 |
+
]),
|
16 |
+
metadata={"name": guest["name"]}
|
17 |
+
)
|
18 |
+
for guest in guest_dataset
|
19 |
+
]
|