Commit
·
4d28021
1
Parent(s):
6170174
updated tools
Browse files
agent.py
CHANGED
@@ -4,6 +4,7 @@ from langchain.prompts import PromptTemplate
|
|
4 |
from langgraph.graph import START, StateGraph, MessagesState
|
5 |
from langgraph.prebuilt import ToolNode, tools_condition
|
6 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
|
|
7 |
from langchain_core.messages import HumanMessage
|
8 |
from langchain.tools import tool
|
9 |
from langchain_core.prompts import ChatPromptTemplate
|
@@ -270,6 +271,33 @@ def build_graph():
|
|
270 |
str: The weather information.
|
271 |
"""
|
272 |
return get_weather(location, search_tool)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
@tool
|
275 |
def recommendation_tool(weather_condition: str) -> str:
|
@@ -284,7 +312,8 @@ def build_graph():
|
|
284 |
"""
|
285 |
return get_recommendation(weather_condition, recommendation_chain)
|
286 |
|
287 |
-
tools = [weather_tool, recommendation_tool,
|
|
|
288 |
|
289 |
llm_with_tools = llm.bind_tools(tools)
|
290 |
|
@@ -313,7 +342,7 @@ def build_graph():
|
|
313 |
|
314 |
if __name__ == "__main__":
|
315 |
graph = build_graph()
|
316 |
-
question = "
|
317 |
messages = [HumanMessage(content=question)]
|
318 |
result = graph.invoke({"messages": messages})
|
319 |
for msg in result["messages"]:
|
|
|
4 |
from langgraph.graph import START, StateGraph, MessagesState
|
5 |
from langgraph.prebuilt import ToolNode, tools_condition
|
6 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
7 |
+
from langchain_community.document_loaders import WikipediaLoader
|
8 |
from langchain_core.messages import HumanMessage
|
9 |
from langchain.tools import tool
|
10 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
271 |
str: The weather information.
|
272 |
"""
|
273 |
return get_weather(location, search_tool)
|
274 |
+
|
275 |
+
@tool
|
276 |
+
def web_search(query: str) -> str:
|
277 |
+
"""Search the web for a given query and return the summary.
|
278 |
+
Args:
|
279 |
+
query (str): The search query.
|
280 |
+
"""
|
281 |
+
|
282 |
+
search_tool = TavilySearchResults()
|
283 |
+
result = search_tool.run(query)
|
284 |
+
return result[0]['content']
|
285 |
+
|
286 |
+
@tool
|
287 |
+
def wiki_search(query : str) -> str:
|
288 |
+
"""Search Wikipedia for a given query and return the summary.
|
289 |
+
Args:
|
290 |
+
query (str): The search query.
|
291 |
+
"""
|
292 |
+
|
293 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=1).load()
|
294 |
+
formatted_search_docs = "\n\n----\n\n".join(
|
295 |
+
[
|
296 |
+
f'<Document Source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}">\n{doc.page_content}\n</Document>'
|
297 |
+
for doc in search_docs
|
298 |
+
]
|
299 |
+
)
|
300 |
+
return formatted_search_docs
|
301 |
|
302 |
@tool
|
303 |
def recommendation_tool(weather_condition: str) -> str:
|
|
|
312 |
"""
|
313 |
return get_recommendation(weather_condition, recommendation_chain)
|
314 |
|
315 |
+
tools = [weather_tool, recommendation_tool, wiki_search, web_search,
|
316 |
+
add, subtract, multiply, divide, square, cube, power, factorial, mean, standard_deviation]
|
317 |
|
318 |
llm_with_tools = llm.bind_tools(tools)
|
319 |
|
|
|
342 |
|
343 |
if __name__ == "__main__":
|
344 |
graph = build_graph()
|
345 |
+
question = "How many albums were pulished by Mercedes Sosa?"
|
346 |
messages = [HumanMessage(content=question)]
|
347 |
result = graph.invoke({"messages": messages})
|
348 |
for msg in result["messages"]:
|