LangGraph第二章 toolNode使用
是一个 LangChain Runnable,它将图形状态(带有消息列表)作为输入,并使用工具调用的结果输出状态更新。注意ToolNode是通过AIMessage的tool_calls字段绑定。query_by_tag_and_question被调用。query_by_tag_and_question被调用。tool_call_id_1: 大雾。什么内容都没有查询到。
·
ToolNode是一个 LangChain Runnable,它将图形状态(带有消息列表)作为输入,并使用工具调用的结果输出状态更新。注意ToolNode是通过AIMessage的tool_calls字段绑定
@tool
def query_by_tag_and_question(question: str, tag: str):
"""根据问题和标签查询相关信息"""
print("query_by_tag_and_question被调用")
if tag == "复变函数" and question == "示例问题":
return "查询到的相关信息"
return "什么内容都没有查询到"
tool_node = ToolNode(tools=[query_by_tag_and_question])
message_with_tool_calls = AIMessage(
content="What is the derivative of a function?",
tool_calls=[
{
"name": "query_by_tag_and_question",
"args": {
"question": "猜猜我要什么内容",
"tag": "复变函数"
},
"id": "tool_call_id_1",
"type":"tool_call"
}
]
)
result = tool_node.invoke({"messages": [message_with_tool_calls]})
print("预期不命中")
print(result["messages"][-1].content)
print("---------------------------------------------------------")
message_with_tool_calls = AIMessage(
content="What is the derivative of a function?",
tool_calls=[
{
"name": "query_by_tag_and_question",
"args": {
"question": "示例问题",
"tag": "复变函数"
},
"id": "tool_call_id_1",
"type":"tool_call"
}
]
)
result = tool_node.invoke({"messages": [message_with_tool_calls]})
print("预期命中")
print(result["messages"][-1].content)
print("---------------------------------------------------------")
query_by_tag_and_question被调用
预期不命中
什么内容都没有查询到
---------------------------------------------------------
query_by_tag_and_question被调用
预期命中
查询到的相关信息
---------------------------------------------------------
@tool
def get_weather(location: str):
"""Call to get the current weather."""
if location.lower() in ["sf", "san francisco"]:
return "大雾"
else:
return "阳光明媚"
@tool
def get_coolest_cities():
"""Get a list of coolest cities"""
return "nyc, sf"
tools = [get_weather, get_coolest_cities]
tool_node = ToolNode(tools)
message_with_multiple_tool_calls = AIMessage(
content="",
tool_calls=[
{
"name": "get_coolest_cities",
"args": {},
"id": "tool_call_id_2",
"type": "tool_call",
},
{
"name": "get_weather",
"args": {"location": "sf"},
"id": "tool_call_id_1",
"type": "tool_call",
},
],
)
result=tool_node.invoke({"messages": [message_with_multiple_tool_calls]})
for m in result["messages"]:
print(f"{m.tool_call_id}: {m.content}")
tool_call_id_2: nyc, sf
tool_call_id_1: 大雾
火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。
更多推荐
所有评论(0)