HollowVoice commited on
Commit
0c8d19f
·
1 Parent(s): 52237c6

Added web search 4 out of 20

Browse files
Files changed (2) hide show
  1. agent.py +52 -8
  2. requirements.txt +2 -1
agent.py CHANGED
@@ -13,6 +13,10 @@ from langchain_community.utilities import WikipediaAPIWrapper
13
  from langchain_community.document_loaders import WikipediaLoader
14
  from langchain_community.retrievers import WikipediaRetriever
15
 
 
 
 
 
16
  load_dotenv()
17
 
18
 
@@ -43,20 +47,49 @@ def wikipedia_search(query: str) -> str:
43
  load_all_available_meta=False,
44
  )
45
  documents = loader.load()
 
46
 
47
- formatted_search_docs = "\n\n---\n\n".join(
48
- [
49
- f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"/>\n{next_doc.page_content}\n</Document>'
50
- for next_doc in documents
51
- ]
52
- )
53
 
54
  result = f"{{wiki_results: {formatted_search_docs}}}"
55
 
56
  return result
57
 
58
 
59
- tools = [meaning_of_life, wikipedia_search]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  # --- GRAPH ---
@@ -121,6 +154,7 @@ def create_graph():
121
  if __name__ == "__main__":
122
  # Build the graph
123
  graph = create_graph()
 
124
  # Run the graph
125
  """
126
  print(f"******** TEST NORMAL LLM CALL ********")
@@ -137,7 +171,7 @@ if __name__ == "__main__":
137
  for m in messages["messages"]:
138
  m.pretty_print()
139
 
140
- """
141
  print("******** TESTING WIKEPEDIA TOOL ********")
142
  # expected answer is "Samuel"
143
  question = "Search Wikipedia and find out who is the recipient of the malko competition in 2024"
@@ -145,3 +179,13 @@ if __name__ == "__main__":
145
  messages = graph.invoke({"messages": messages})
146
  for m in messages["messages"]:
147
  m.pretty_print()
 
 
 
 
 
 
 
 
 
 
 
13
  from langchain_community.document_loaders import WikipediaLoader
14
  from langchain_community.retrievers import WikipediaRetriever
15
 
16
+ # Tavily web search
17
+ from langchain_community.tools.tavily_search import TavilySearchResults
18
+
19
+
20
  load_dotenv()
21
 
22
 
 
47
  load_all_available_meta=False,
48
  )
49
  documents = loader.load()
50
+ formatted_search_docs = "\n\n---\n\n"
51
 
52
+ for next_doc in documents:
53
+ formatted_doc = f'<Document source="{next_doc.metadata["source"]}" title="{next_doc.metadata.get("title", "")}"/>\n{next_doc.page_content}\n</Document>'
54
+ formatted_search_docs.join(formatted_doc)
 
 
 
55
 
56
  result = f"{{wiki_results: {formatted_search_docs}}}"
57
 
58
  return result
59
 
60
 
61
+ def web_search(query: str) -> str:
62
+ """Search Web with Tavily for a query and return results.
63
+
64
+ Args:
65
+ query: The search query."""
66
+ tool = TavilySearchResults(
67
+ max_results=3,
68
+ include_answer=True,
69
+ include_raw_content=True,
70
+ include_images=True,
71
+ # search_depth="advanced",
72
+ # include_domains = []
73
+ # exclude_domains = []
74
+ )
75
+
76
+ documents = tool.invoke(input=query)
77
+ formatted_search_docs = "\n\n---\n\n"
78
+ for next_doc in documents:
79
+ url = next_doc["url"]
80
+ title = next_doc["title"]
81
+ content = next_doc["content"]
82
+ formatted_doc = (
83
+ f'<Document source="{url}" title="{title}"/>\n{content}\n</Document>'
84
+ )
85
+ formatted_search_docs = formatted_search_docs + formatted_doc
86
+
87
+ result = f"{{web_results: {formatted_search_docs}}}"
88
+
89
+ return result
90
+
91
+
92
+ tools = [meaning_of_life, wikipedia_search, web_search]
93
 
94
 
95
  # --- GRAPH ---
 
154
  if __name__ == "__main__":
155
  # Build the graph
156
  graph = create_graph()
157
+
158
  # Run the graph
159
  """
160
  print(f"******** TEST NORMAL LLM CALL ********")
 
171
  for m in messages["messages"]:
172
  m.pretty_print()
173
 
174
+
175
  print("******** TESTING WIKEPEDIA TOOL ********")
176
  # expected answer is "Samuel"
177
  question = "Search Wikipedia and find out who is the recipient of the malko competition in 2024"
 
179
  messages = graph.invoke({"messages": messages})
180
  for m in messages["messages"]:
181
  m.pretty_print()
182
+
183
+ """
184
+
185
+ print("******** TESTING WEB SEARCH TOOL ********")
186
+ # expected answer is "Samuel"
187
+ question = "Search web for information about mozart"
188
+ messages = [HumanMessage(content=question)]
189
+ messages = graph.invoke({"messages": messages})
190
+ for m in messages["messages"]:
191
+ m.pretty_print()
requirements.txt CHANGED
@@ -7,4 +7,5 @@ langchain-core
7
  langchain-community
8
  langchain-openai
9
  langgraph-cli[inmem]
10
- wikipedia
 
 
7
  langchain-community
8
  langchain-openai
9
  langgraph-cli[inmem]
10
+ wikipedia
11
+ tavily-python