前言

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个阶段可能迭代多次,直到问题得到解决或者达到迭代次数上限。

img

基于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 answerThought: you should always think about what to doAction:```$JSON_BLOB```Observation: the result of the action... (this Thought/Action/Observation can repeat N times)Thought: I now know the final answerFinal 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。查询天气的流程如下图:

img

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/bashexport 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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"    },    {      "role": "user",      "content": "Question: 北京和广州天气怎么样\n\n"    }  ],  "stop": "Observation:"}'

2. LLM返回Action获取北京天气

LLM经过推理,发现需要先调用函数获取北京天气。

Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。Action:```{  "action": "get_weather",  "action_input": {    "location": "北京"  }}```

完整的JSON响应如下:

{  "choices": [    {      "message": {        "content": "Thought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。\n\nAction:\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/bashexport 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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"    },    {      "role": "user",      "content": "Question: 北京和广州天气怎么样\n\nThought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。\n\nAction:\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: 我已经获取了北京的天气信息。接下来,我将获取广州的天气信息。\n\nAction:\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/bashexport 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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"    },    {      "role": "user",      "content": "Question: 北京和广州天气怎么样\n\nThought: 我需要获取北京和广州的天气信息。首先,我将获取北京的天气。\n\nAction:\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: 现在我已经获取了北京的天气信息,接下来我将获取广州的天气信息。\n\nAction:\n```\n{\n\"action\": \"get_weather\",\n\"action_input\": {\n\"location\": \"广州\"\n}\n}\n```\nObservation\nObservation: {\"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: 我已经获取了北京和广州的天气信息,现在可以回答用户的问题了。\n\nFinal 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
.venv\Scripts\activate

uv add openai requests python-dotenv

2. 设置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 jsonimport reimport requestsimport urllib.parsefrom typing import Iterablefrom openai import OpenAIfrom openai.types.chat.chat_completion_message_param import ChatCompletionMessageParamfrom 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_dotenvload_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 answerThought: you should always think about what to doAction:```$JSON_BLOB```Observation: the result of the action... (this Thought/Action/Observation can repeat N times)Thought: I now know the final answerFinal 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}\n\n{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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"}, {"role": "user", "content": "Question: \u5317\u4eac\u548c\u5e7f\u5dde\u5929\u6c14\u600e\u4e48\u6837\n\n"}]>>> 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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"}, {"role": "user", "content": "Question: \u5317\u4eac\u548c\u5e7f\u5dde\u5929\u6c14\u600e\u4e48\u6837\n\nThought: \u6211\u9700\u8981\u83b7\u53d6\u5317\u4eac\u548c\u5e7f\u5dde\u7684\u5929\u6c14\u4fe1\u606f\u3002\u9996\u5148\uff0c\u6211\u5c06\u83b7\u53d6\u5317\u4eac\u7684\u5929\u6c14\u3002\n\nAction:\n```\n{\n\"action\": \"get_weather\",\n\"action_input\": {\n\"location\": \"\u5317\u4eac\"\n}\n}\n```\nObservation: {\"msg\":\"success\",\"code\":0,\"data\":{\"location\":{\"id\":\"54511\",\"name\":\"\u5317\u4eac\",\"path\":\"\u4e2d\u56fd, \u5317\u4eac, \u5317\u4eac\"},\"now\":{\"precipitation\":0.0,\"temperature\":23.4,\"pressure\":1005.0,\"humidity\":43.0,\"windDirection\":\"\u897f\u5357\u98ce\",\"windDirectionDegree\":216.0,\"windSpeed\":2.7,\"windScale\":\"\u5fae\u98ce\",\"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\"]}}\n\n\nThe 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).\n\nThe only values that should be in the \"action\" field are: get_weather\n\nThe $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:\n\n```\n{{\n\"action\": $TOOL_NAME,\n\"action_input\": $INPUT\n}}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding. \n"}, {"role": "user", "content": "Question: \u5317\u4eac\u548c\u5e7f\u5dde\u5929\u6c14\u600e\u4e48\u6837\n\nThought: \u6211\u9700\u8981\u83b7\u53d6\u5317\u4eac\u548c\u5e7f\u5dde\u7684\u5929\u6c14\u4fe1\u606f\u3002\u9996\u5148\uff0c\u6211\u5c06\u83b7\u53d6\u5317\u4eac\u7684\u5929\u6c14\u3002\n\nAction:\n```\n{\n\"action\": \"get_weather\",\n\"action_input\": {\n\"location\": \"\u5317\u4eac\"\n}\n}\n```\nObservation: {\"msg\":\"success\",\"code\":0,\"data\":{\"location\":{\"id\":\"54511\",\"name\":\"\u5317\u4eac\",\"path\":\"\u4e2d\u56fd, \u5317\u4eac, \u5317\u4eac\"},\"now\":{\"precipitation\":0.0,\"temperature\":23.4,\"pressure\":1005.0,\"humidity\":43.0,\"windDirection\":\"\u897f\u5357\u98ce\",\"windDirectionDegree\":216.0,\"windSpeed\":2.7,\"windScale\":\"\u5fae\u98ce\",\"feelst\":23.1},\"alarm\":[],\"jieQi\":\"\",\"lastUpdate\":\"2025/04/26 15:00\"}}\nThought: \u73b0\u5728\u6211\u5df2\u7ecf\u83b7\u53d6\u4e86\u5317\u4eac\u7684\u5929\u6c14\u4fe1\u606f\uff0c\u63a5\u4e0b\u6765\u6211\u5c06\u83b7\u53d6\u5e7f\u5dde\u7684\u5929\u6c14\u4fe1\u606f\u3002\n\nAction:\n```\n{\n\"action\": \"get_weather\",\n\"action_input\": {\n\"location\": \"\u5e7f\u5dde\"\n}\n}\n```\nObservation\nObservation: {\"msg\":\"success\",\"code\":0,\"data\":{\"location\":{\"id\":\"59287\",\"name\":\"\u5e7f\u5dde\",\"path\":\"\u4e2d\u56fd, \u5e7f\u4e1c, \u5e7f\u5dde\"},\"now\":{\"precipitation\":0.0,\"temperature\":24.2,\"pressure\":1005.0,\"humidity\":79.0,\"windDirection\":\"\u4e1c\u5317\u98ce\",\"windDirectionDegree\":31.0,\"windSpeed\":1.3,\"windScale\":\"\u5fae\u98ce\",\"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复杂
  • 推理过程可见,更高的可解释性

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

在这里插入图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐