hibam1 commited on
Commit
089ba5f
·
1 Parent(s): 369bfa5

add PartyPlanningRetrieverTool

Browse files
Files changed (1) hide show
  1. partyPlanningRetrieverTool.py +73 -0
partyPlanningRetrieverTool.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.docstore.document import Document
2
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
3
+ from smolagents import Tool
4
+ from langchain_community.retrievers import BM25Retriever
5
+ from smolagents import CodeAgent, InferenceClientModel
6
+ from model import model
7
+
8
+ class PartyPlanningRetrieverTool(Tool):
9
+ name = "party_planning_retriever"
10
+ description = "Uses semantic search to retrieve relevant party planning ideas for Alfred’s superhero-themed party at Wayne Manor."
11
+ inputs = {
12
+ "query": {
13
+ "type": "string",
14
+ "description": "The query to perform. This should be a query related to party planning or superhero themes.",
15
+ }
16
+ }
17
+ output_type = "string"
18
+
19
+ def __init__(self, docs, **kwargs):
20
+ super().__init__(**kwargs)
21
+ self.retriever = BM25Retriever.from_documents(
22
+ docs, k=5 # Retrieve the top 5 documents
23
+ )
24
+
25
+ def forward(self, query: str) -> str:
26
+ assert isinstance(query, str), "Your search query must be a string"
27
+
28
+ docs = self.retriever.invoke(
29
+ query,
30
+ )
31
+ return "\nRetrieved ideas:\n" + "".join(
32
+ [
33
+ f"\n\n===== Idea {str(i)} =====\n" + doc.page_content
34
+ for i, doc in enumerate(docs)
35
+ ]
36
+ )
37
+
38
+ # Simulate a knowledge base about party planning
39
+ party_ideas = [
40
+ {"text": "A superhero-themed masquerade ball with luxury decor, including gold accents and velvet curtains.", "source": "Party Ideas 1"},
41
+ {"text": "Hire a professional DJ who can play themed music for superheroes like Batman and Wonder Woman.", "source": "Entertainment Ideas"},
42
+ {"text": "For catering, serve dishes named after superheroes, like 'The Hulk's Green Smoothie' and 'Iron Man's Power Steak.'", "source": "Catering Ideas"},
43
+ {"text": "Decorate with iconic superhero logos and projections of Gotham and other superhero cities around the venue.", "source": "Decoration Ideas"},
44
+ {"text": "Interactive experiences with VR where guests can engage in superhero simulations or compete in themed games.", "source": "Entertainment Ideas"}
45
+ ]
46
+
47
+ source_docs = [
48
+ Document(page_content=doc["text"], metadata={"source": doc["source"]})
49
+ for doc in party_ideas
50
+ ]
51
+
52
+ # Split the documents into smaller chunks for more efficient search
53
+ text_splitter = RecursiveCharacterTextSplitter(
54
+ chunk_size=500,
55
+ chunk_overlap=50,
56
+ add_start_index=True,
57
+ strip_whitespace=True,
58
+ separators=["\n\n", "\n", ".", " ", ""],
59
+ )
60
+ docs_processed = text_splitter.split_documents(source_docs)
61
+
62
+ # Create the retriever tool
63
+ party_planning_retriever = PartyPlanningRetrieverTool(docs_processed)
64
+
65
+ # Initialize the agent
66
+ agent = CodeAgent(tools=[party_planning_retriever], model=model)
67
+
68
+ # Example usage
69
+ response = agent.run(
70
+ "Find ideas for a luxury superhero-themed party, including entertainment, catering, and decoration options."
71
+ )
72
+
73
+ print(response)