-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
39 lines (28 loc) · 1.15 KB
/
tools.py
File metadata and controls
39 lines (28 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from langchain_community.tools.tavily_search import TavilySearchResults
import json
from langchain_core.messages import ToolMessage
from langgraph.prebuilt import ToolNode, tools_condition
import dotenv
dotenv.load_dotenv()
tool = TavilySearchResults(max_results=2)
tools = [tool]
class BasicToolNode:
"""A node that runs the tools requested in the last AI message."""
def __init__(self, tools: list) -> None:
self.tools_by_name = {tool.name: tool for tool in tools}
def __call__(self, inputs:dict):
if messages := inputs.get("messages", []):
message = messages[-1]
else:
raise ValueError("No messages found in inputs")
outputs = []
for tool_call in message.tool_calls:
tool_result = self.tools_by_name[tool_call.tool_name].invoke(tool_call['args'])
outputs.append(ToolMessage(
content=json.dumps(tool_result),
name=tool_call['name'],
tool_call_id=tool_call['id'],
))
return {"messages": outputs}
# tool_node = BasicToolNode(tools=tools)
tool_node = ToolNode(tools=tools)