-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentTools.py
More file actions
24 lines (21 loc) · 828 Bytes
/
agentTools.py
File metadata and controls
24 lines (21 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain_core.messages import ToolMessage
import json
class BasicToolNode:
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", []):
messages = messages[-1]
else:
raise ValueError("Messages not found in Inputs")
outputs = []
for tool_call in messages.tool_calls:
tool_result = self.tools_by_name[tool_call["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}