前言
AI智能体是指具备一定自主性、能感知环境并通过智能决策执行特定任务的软件或硬件实体。它结合了人工智能技术(如机器学习、自然语言处理、计算机视觉等),能够独立或协作完成目标。
基于大语言模型(LLM)的Function Calling可以令智能体实现有效的工具使用和与外部API的交互。支持Function Calling的模型(如gpt-4,qwen-plus等)能够检测何时需要调用函数,并输出调用函数的函数名和所需参数的JSON格式结构化数据。
但并非所有的LLM模型都支持Function Calling(如deepseel-v3)。对于不支持Function Calling的模型,可通过ReAct的相对较为复杂的提示词工程,要求模型返回特定格式的响应,以便区分不同的阶段(思考、行动、观察)。
工具调用主要有两个用途:
获取数据:
例如根据关键字从知识库检索内容、通过特定API接口获取业务数据 执行行动:
例如通过API接口修改业务状态数据、执行预定业务操作
本文包含如下内容:
-
ReAct基础 -
详细介绍基于ReAct的工具调用流程和涉及的交互消息 -
手搓Agent代码实现基于ReAct的工具调用
ReAct基础
ReAct源于经典论文: REACT: SYNERGIZING REASONING AND ACTING IN LANGUAGE MODELS
(链接:https://arxiv.org/pdf/2210.03629)
基于ReAct的智能体为了解决问题,需要经过几个阶段
-
Thought: 思考推理 -
Action:作出行动,决定要调用的工具和参数 -
Observation:行动的结果(工具输出)
以上3个阶段可能迭代多次,直到问题得到解决或者达到迭代次数上限。

基于ReAct的工具调用依赖于复杂的提示词工程。系统提示词参考langchain的模板:
Answer the following questions as best you can. You have access to the following tools:
{tool_strings}
The way you use the tools is by specifying a json blob.
Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).
The only values that should be in the "action" field are: {tool_names}
The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
```
{{{{
"action": $TOOL_NAME,
"action_input": $INPUT
}}}}
```
ALWAYS use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action:
```
$JSON_BLOB
```
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin! Reminder to always use the exact characters `Final Answer` when responding.
基于ReAct的工具调用流程和交互消息
我们以查询北京和广州天气为例,LLM采用阿里云的DeepSeek-v3
。查询天气的流程如下图:

1. 发起查询请求
向LLM发起查询时,messages列表有2条messages:
-
第1条role为 system
,定义了系统提示词(含工具定义) -
第2条role为 user
,包含如下内容: -
Question: 北京和广州天气怎么样
我们用curl发起POST请求,body的JSON结构可参考https://platform.openai.com/docs/api-reference/chat/create 。
请求里的
stop
字段需要设置为Observation:
,否则LLM会直接输出整个Thought/Action/Observation流程并给出虚构的最终答案。我们仅需要LLM输出Thought/Action即可
#!/bin/bash
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
export OPENAI_API_KEY="sk-xxx" # 替换为你的key
curl ${OPENAI_BASE_URL}/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer $OPENAI_API_KEY"
-d '{
"model": "deepseek-v3",
"messages": [
{
"role": "system",
"content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"
},
{
"role": "user",
"content": "Question: 北京和广州天气怎么样nn"
}
],
"stop": "Observation:"
}'
2. LLM返回Action获取北京天气
LLM经过推理,发现需要先调用函数获取北京天气。
Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。Action:```{ "action": "get_weather", "action_input": { "location": "北京" }}```
完整的JSON响应如下:
{ "choices": [ { "message": { "content": "Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。nnAction:n```n{n "action": "get_weather",n "action_input": {n "location": "北京"n }n}n```", "role": "assistant" }, "finish_reason": "stop", "index": 0, "logprobs": null } ], "object": "chat.completion", "usage": { "prompt_tokens": 305, "completion_tokens": 49, "total_tokens": 354 }, "created": 1745651748, "system_fingerprint": null, "model": "deepseek-v3", "id": "chatcmpl-697b0627-4fca-975b-954c-7304386ac224"}
3. 处理函数调用获取北京天气
解析处理LLM的Action获得函数名和参数列表,调用相应的API接口获得结果。
例如:通过http://weather.cma.cn/api/now/54511
可获得北京的天气情况。
完整的JSON响应如下:
{ "msg": "success", "code": 0, "data": { "location": { "id": "54511", "name": "北京", "path": "中国, 北京, 北京" }, "now": { "precipitation": 0.0, "temperature": 23.4, "pressure": 1005.0, "humidity": 43.0, "windDirection": "西南风", "windDirectionDegree": 216.0, "windSpeed": 2.7, "windScale": "微风", "feelst": 23.1 }, "alarm": [], "jieQi": "", "lastUpdate": "2025/04/26 15:00" }}
4. 把上下文信息以及函数调用结果发给LLM
发给LLM的messages列表有2条messages:
-
第1条role为 system
,定义了系统提示词(含工具定义) -
第2条role为 user
,包含如下内容: -
Question: 北京和广州天气怎么样 -
Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气 -
Action: {"action":"get_weather","action_input":{"location":"北京"}} -
Observation: 工具调用 get_weather('北京')
的结果
#!/bin/bash
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
export OPENAI_API_KEY="sk-xxx" # 替换为你的key
curl ${OPENAI_BASE_URL}/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer $OPENAI_API_KEY"
-d '{
"model": "deepseek-v3",
"messages": [
{
"role": "system",
"content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"
},
{
"role": "user",
"content": "Question: 北京和广州天气怎么样nnThought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "北京"n}n}n```nObservation: {"msg":"success","code":0,"data":{"location":{"id":"54511","name":"北京","path":"中国, 北京, 北京"},"now":{"precipitation":0.0,"temperature":23.4,"pressure":1005.0,"humidity":43.0,"windDirection":"西南风","windDirectionDegree":216.0,"windSpeed":2.7,"windScale":"微风","feelst":23.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}n"
}
],
"stop": "Observation:"
}'
5. LLM返回Action获取广州天气
LLM经过推理,发现还需要调用函数获取广州天气。
Thought: 我已经获取了北京的天气信息。接下来,我将获取广州的天气信息。Action:```{ "action": "get_weather", "action_input": { "location": "广州" }}```
完整的JSON响应如下:
{ "choices": [ { "message": { "content": "Thought: 我已经获取了北京的天气信息。接下来,我将获取广州的天气信息。nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "广州"n}n}n```nObservation", "role": "assistant" }, "finish_reason": "stop", "index": 0, "logprobs": null } ], "object": "chat.completion", "usage": { "prompt_tokens": 472, "completion_tokens": 46, "total_tokens": 518 }, "created": 1745651861, "system_fingerprint": null, "model": "deepseek-v3", "id": "chatcmpl-a822b8d7-9105-9dc2-8e98-4327afb50b3a"}
6. 处理函数调用获取广州天气
解析处理LLM的Action获得函数名和参数列表,调用相应的API接口获得结果。
例如:通过http://weather.cma.cn/api/now/59287
可获得广州的天气情况。
完整的JSON响应如下:
{ "msg": "success", "code": 0, "data": { "location": { "id": "59287", "name": "广州", "path": "中国, 广东, 广州" }, "now": { "precipitation": 0.0, "temperature": 24.2, "pressure": 1005.0, "humidity": 79.0, "windDirection": "东北风", "windDirectionDegree": 31.0, "windSpeed": 1.3, "windScale": "微风", "feelst": 27.1 }, "alarm": [], "jieQi": "", "lastUpdate": "2025/04/26 15:00" }}
7. 把上下文信息以及函数调用结果发给LLM
发给LLM的messages列表有2条messages:
-
第1条role为 system
,定义了系统提示词(含工具定义) -
第2条role为 user
,包含如下内容: -
Question: 北京和广州天气怎么样 -
Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气 -
Action: {"action":"get_weather","action_input":{"location":"北京"}} -
Observation: 工具调用 get_weather('北京')
的结果 -
Thought: 现在我已经获取了北京的天气信息,接下来我将获取广州的天气信息。 -
Action: {"action":"get_weather","action_input":{"location":"广州"}} -
Observation: 工具调用 get_weather('广州')
的结果
#!/bin/bash
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
export OPENAI_API_KEY="sk-xxx" # 替换为你的key
curl ${OPENAI_BASE_URL}/chat/completions
-H "Content-Type: application/json"
-H "Authorization: Bearer $OPENAI_API_KEY"
-d '{
"model": "deepseek-v3",
"messages": [
{
"role": "system",
"content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"
},
{
"role": "user",
"content": "Question: 北京和广州天气怎么样nnThought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "北京"n}n}n```nObservation: {"msg":"success","code":0,"data":{"location":{"id":"54511","name":"北京","path":"中国, 北京, 北京"},"now":{"precipitation":0.0,"temperature":23.4,"pressure":1005.0,"humidity":43.0,"windDirection":"西南风","windDirectionDegree":216.0,"windSpeed":2.7,"windScale":"微风","feelst":23.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}nThought: 现在我已经获取了北京的天气信息,接下来我将获取广州的天气信息。nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "广州"n}n}n```nObservationnObservation: {"msg":"success","code":0,"data":{"location":{"id":"59287","name":"广州","path":"中国, 广东, 广州"},"now":{"precipitation":0.0,"temperature":24.2,"pressure":1005.0,"humidity":79.0,"windDirection":"东北风","windDirectionDegree":31.0,"windSpeed":1.3,"windScale":"微风","feelst":27.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}n"
}
],
"stop": "Observation:"
}'
8. LLM生成最终回复
LLM生成最终的回复:
Thought: 我已经获取了北京和广州的天气信息,现在可以回答用户的问题了。
Final Answer: 北京的天气温度为23.4°C,湿度为43%,风向为西南风,风速为2.7米/秒。广州 的天气温度为24.2°C,湿度为79%,风向为东北风,风速为1.3米/秒。
完整的JSON响应如下:
{ "choices": [ { "message": { "content": "Thought: 我已经获取了北京和广州的天气信息,现在可以回答用户的问题了。nnFinal Answer: 北京的天气温度为23.4°C,湿度为43%,风向为西南风,风速为2.7米/秒。广州 的天气温度为24.2°C,湿度为79%,风向为东北风,风速为1.3米/秒。", "role": "assistant" }, "finish_reason": "stop", "index": 0, "logprobs": null } ], "object": "chat.completion", "usage": { "prompt_tokens": 641, "completion_tokens": 79, "total_tokens": 720 }, "created": 1745652025, "system_fingerprint": null, "model": "deepseek-v3", "id": "chatcmpl-d9b85f31-589e-9c6f-8694-cf813344e464"}
手搓Agent代码实现基于ReAct的工具调用
1. 创建python环境
uv init agent
cd agent
uv venv
.venvScriptsactivate
uv add openai requests python-dotenv2. 设置API Key
创建.env,.env内容如下(注意修改OPENAI_API_KEY为您的key)
OPENAI_API_KEY=your_api_key_here
OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1把.env添加到.gitignore
3. 实现Agent代码
基于openai sdk实现ReAct agent的伪代码主体逻辑如下:
maxIter = 5 # 最大迭代次数agent_scratchpad = "" # agent思考过程(Thought/Action/Observation)for iterSeq in range(1, maxIter+1): 构造chat completion请求 messages有2条 第1条为系统提示词消息(含工具定义) 第2条为用户消息:Question + agent思考过程(Thought/Action/Observation) stop参数设置为"Observation:" 获取chat completion结果 如果chat completion结果带有"Final Answer:" 返回最终答案 如果chat completion结果带有Action 解析并调用相应函数 更新agent思考过程:把本次LLM的输出(Though/Action)和工具调用结果(Observation)添加到agent_scratchpad 继续迭代
完整的main.py代码如下:
import json
import re
import requests
import urllib.parse
from typing import Iterable
from openai import OpenAI
from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
from openai.types.chat.chat_completion_user_message_param import (
ChatCompletionUserMessageParam,
)
from openai.types.chat.chat_completion_system_message_param import (
ChatCompletionSystemMessageParam,
)
# 加载环境变量
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
model = "deepseek-v3"
# 工具定义
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "location"}
},
"required": ["location"],
},
},
}
]
# 系统提示词
def get_system_prompt():
tool_strings = "n".join([json.dumps(tool["function"]) for tool in tools])
tool_names = ", ".join([tool["function"]["name"] for tool in tools])
systemPromptFormat = """
Answer the following questions as best you can. You have access to the following tools:
{tool_strings}
The way you use the tools is by specifying a json blob.
Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).
The only values that should be in the "action" field are: {tool_names}
The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:
```
{{{{
"action": $TOOL_NAME,
"action_input": $INPUT
}}}}
```
ALWAYS use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action:
```
$JSON_BLOB
```
Observation: the result of the action
... (this Thought/Action/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin! Reminder to always use the exact characters `Final Answer` when responding.
"""
return systemPromptFormat.format(tool_strings=tool_strings, tool_names=tool_names)
# 实现获取天气
def get_weather(location: str) -> str:
url = "http://weather.cma.cn/api/autocomplete?q=" + urllib.parse.quote(location)
response = requests.get(url)
data = response.json()
if data["code"] != 0:
return "没找到该位置的信息"
location_code = ""
for item in data["data"]:
str_array = item.split("|")
if (
str_array[1] == location
or str_array[1] + "市" == location
or str_array[2] == location
):
location_code = str_array[0]
break
if location_code == "":
return "没找到该位置的信息"
url = f"http://weather.cma.cn/api/now/{location_code}"
return requests.get(url).text
# 实现工具调用
def invoke_tool(toolName: str, toolParamaters) -> str:
result = ""
if toolName == "get_weather":
result = get_weather(toolParamaters["location"])
else:
result = f"函数{toolName}未定义"
return result
def main():
query = "北京和广州天气怎么样"
systemMsg = ChatCompletionSystemMessageParam(
role="system", content=get_system_prompt()
)
maxIter = 5 # 最大迭代次数
agent_scratchpad = "" # agent思考过程
action_pattern = re.compile(r"nAction:n`{3}(?:json)?n(.*?)`{3}.*?$", re.DOTALL)
for iterSeq in range(1, maxIter + 1):
messages: Iterable[ChatCompletionMessageParam] = list()
messages.append(systemMsg)
messages.append(
ChatCompletionUserMessageParam(
role="user", content=f"Question: {query}nn{agent_scratchpad}"
)
)
print(f">> iterSeq:{iterSeq}")
print(f">>> messages: {json.dumps(messages)}")
# 向LLM发起请求,注意需要设置stop参数
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
stop="Observation:",
)
content = chat_completion.choices[0].message.content
print(f">>> content:n{content}")
final_answer_match = re.search(r"nFinal Answer:s*(.*)", content)
if final_answer_match:
final_answer = final_answer_match.group(1)
print(f">>> 最终答案: {final_answer}")
return
action_match = action_pattern.search(content)
if action_match:
obj = json.loads(action_match.group(1))
toolName = obj["action"]
toolParameters = obj["action_input"]
print(f">>> tool name:{toolName}")
print(f">>> tool parameters:{toolParameters}")
result = invoke_tool(toolName, toolParameters)
print(f">>> tool result: {result}")
# 把本次LLM的输出(Though/Action)和工具调用结果(Observation)添加到agent_scratchpad
agent_scratchpad += content + f"nObservation: {result}n"
else:
print(">>> ERROR: detect invalid response")
return
print(">>> 迭代次数达到上限,我无法得到最终答案")
main()
运行代码:
uv run .main.py
>> iterSeq:1
>>> messages: [{"role": "system", "content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"}, {"role": "user", "content": "Question: u5317u4eacu548cu5e7fu5ddeu5929u6c14u600eu4e48u6837nn"}]
>>> content:
Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。
Action:
```
{
"action": "get_weather",
"action_input": {
"location": "北京"
}
}
```
>>> tool name:get_weather
>>> tool parameters:{'location': '北京'}
>>> tool result: {"msg":"success","code":0,"data":{"location":{"id":"54511","name":"北京","path":"中国, 北京, 北京"},"now":{"precipitation":0.0,"temperature":23.4,"pressure":1005.0,"humidity":43.0,"windDirection":"西南风","windDirectionDegree":216.0,"windSpeed":2.7,"windScale":"微风","feelst":23.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}
>> iterSeq:2
>>> messages: [{"role": "system", "content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"}, {"role": "user", "content": "Question: u5317u4eacu548cu5e7fu5ddeu5929u6c14u600eu4e48u6837nnThought: u6211u9700u8981u83b7u53d6u5317u4eacu548cu5e7fu5ddeu7684u5929u6c14u4fe1u606fu3002u9996u5148uff0cu6211u5c06u83b7u53d6u5317u4eacu7684u5929u6c14u3002nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "u5317u4eac"n}n}n```nObservation: {"msg":"success","code":0,"data":{"location":{"id":"54511","name":"u5317u4eac","path":"u4e2du56fd, u5317u4eac, u5317u4eac"},"now":{"precipitation":0.0,"temperature":23.4,"pressure":1005.0,"humidity":43.0,"windDirection":"u897fu5357u98ce","windDirectionDegree":216.0,"windSpeed":2.7,"windScale":"u5faeu98ce","feelst":23.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}n"}]
>>> content:
Thought: 现在我已经获取了北京的天气信息,接下来我将获取广州的天气信息。
Action:
```
{
"action": "get_weather",
"action_input": {
"location": "广州"
}
}
```
Observation
>>> tool name:get_weather
>>> tool parameters:{'location': '广州'}
>>> tool result: {"msg":"success","code":0,"data":{"location":{"id":"59287","name":"广州","path":"中国, 广东, 广州"},"now":{"precipitation":0.0,"temperature":24.2,"pressure":1005.0,"humidity":79.0,"windDirection":"东北风","windDirectionDegree":31.0,"windSpeed":1.3,"windScale":"微风","feelst":27.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}
>> iterSeq:3
>>> messages: [{"role": "system", "content": "nAnswer the following questions as best you can. You have access to the following tools:n{"name": "get_weather", "description": "Get weather", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "the name of the location"}}, "required": ["location"]}}nnnThe way you use the tools is by specifying a json blob.nSpecifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).nnThe only values that should be in the "action" field are: get_weathernnThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:nn```n{{n"action": $TOOL_NAME,n"action_input": $INPUTn}}n```nnALWAYS use the following format:nnQuestion: the input question you must answernThought: you should always think about what to donAction:n```n$JSON_BLOBn```nObservation: the result of the actionn... (this Thought/Action/Observation can repeat N times)nThought: I now know the final answernFinal Answer: the final answer to the original input questionnnnBegin! Reminder to always use the exact characters `Final Answer` when responding. n"}, {"role": "user", "content": "Question: u5317u4eacu548cu5e7fu5ddeu5929u6c14u600eu4e48u6837nnThought: u6211u9700u8981u83b7u53d6u5317u4eacu548cu5e7fu5ddeu7684u5929u6c14u4fe1u606fu3002u9996u5148uff0cu6211u5c06u83b7u53d6u5317u4eacu7684u5929u6c14u3002nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "u5317u4eac"n}n}n```nObservation: {"msg":"success","code":0,"data":{"location":{"id":"54511","name":"u5317u4eac","path":"u4e2du56fd, u5317u4eac, u5317u4eac"},"now":{"precipitation":0.0,"temperature":23.4,"pressure":1005.0,"humidity":43.0,"windDirection":"u897fu5357u98ce","windDirectionDegree":216.0,"windSpeed":2.7,"windScale":"u5faeu98ce","feelst":23.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}nThought: u73b0u5728u6211u5df2u7ecfu83b7u53d6u4e86u5317u4eacu7684u5929u6c14u4fe1u606fuff0cu63a5u4e0bu6765u6211u5c06u83b7u53d6u5e7fu5ddeu7684u5929u6c14u4fe1u606fu3002nnAction:n```n{n"action": "get_weather",n"action_input": {n"location": "u5e7fu5dde"n}n}n```nObservationnObservation: {"msg":"success","code":0,"data":{"location":{"id":"59287","name":"u5e7fu5dde","path":"u4e2du56fd, u5e7fu4e1c, u5e7fu5dde"},"now":{"precipitation":0.0,"temperature":24.2,"pressure":1005.0,"humidity":79.0,"windDirection":"u4e1cu5317u98ce","windDirectionDegree":31.0,"windSpeed":1.3,"windScale":"u5faeu98ce","feelst":27.1},"alarm":[],"jieQi":"","lastUpdate":"2025/04/26 15:00"}}n"}]
>>> content:
Thought: 我已经获取了北京和广州的天气信息,现在可以回答用户的问题了。
Final Answer: 北京的天气情况为:温度23.4°C,湿度43%,西南风,风速2.7米/秒,微风。广州的天气情况为:温度24.2°C,湿度79%,东北风,风速1.3米/秒,微风。
>>> 最终答案: 北京的天气情况为:温度23.4°C,湿度43%,西南风,风速2.7米/秒,微风。广州的天气情况为:温度24.2°C,湿度79%,东北风,风速1.3米/秒,微风。
总结
基于Function Calling和基于ReAct的工具调用有各自的优缺点:
1. Function Calling
-
无需设定系统提示词,LLM根据tools定义即可触发工具调用,token消耗较少 -
模型参数量相对较大。模型的训练数据必须包含Function Calling相关的内容,以确保模型能够理解和生成结构化输出,结构化输出更稳定 -
输出结果较为容易处理 -
隐藏了推理过程,缺乏可解释性
2. ReAct
-
需要设置复杂的系统提示词,token消耗较多 -
对模型参数要求较低 -
输出结果处理比Function Calling复杂 -
推理过程可见,更高的可解释性