razan-dakkak commited on
Commit
7b64db3
·
verified ·
1 Parent(s): 1fbeb0e

Create retriver.py

Browse files
Files changed (1) hide show
  1. retriver.py +63 -0
retriver.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ]
20
+
21
+ from smolagents import Tool
22
+ from langchain_community.retrievers import BM25Retriever
23
+
24
+ class GuestInfoRetrieverTool(Tool):
25
+ name = "guest_info_retriever"
26
+ description = "Retrieves detailed information about gala guests based on their name or relation."
27
+ inputs = {
28
+ "query": {
29
+ "type": "string",
30
+ "description": "The name or relation of the guest you want information about."
31
+ }
32
+ }
33
+ output_type = "string"
34
+
35
+ def __init__(self, docs):
36
+ self.is_initialized = False
37
+ self.retriever = BM25Retriever.from_documents(docs)
38
+
39
+ def forward(self, query: str):
40
+ results = self.retriever.get_relevant_documents(query)
41
+ if results:
42
+ return "\n\n".join([doc.page_content for doc in results[:3]])
43
+ else:
44
+ return "No matching guest information found."
45
+
46
+ # Initialize the tool
47
+ guest_info_tool = GuestInfoRetrieverTool(docs)
48
+
49
+
50
+ from smolagents import CodeAgent, InferenceClientModel
51
+
52
+ # Initialize the Hugging Face model
53
+ model = InferenceClientModel()
54
+
55
+ # Create Alfred, our gala agent, with the guest info tool
56
+ alfred = CodeAgent(tools=[guest_info_tool], model=model)
57
+
58
+ # Example query Alfred might receive during the gala
59
+ response = alfred.run("Tell me about our guest named 'Lady Ada Lovelace'.")
60
+
61
+ print("🎩 Alfred's Response:")
62
+ print(response)
63
+