精品欧美一区二区三区在线观看 _久久久久国色av免费观看性色_国产精品久久在线观看_亚洲第一综合网站_91精品又粗又猛又爽_小泽玛利亚一区二区免费_91亚洲精品国偷拍自产在线观看 _久久精品视频在线播放_美女精品久久久_欧美日韩国产成人在线

一文徹底搞懂智能體Agent基于ReAct的工具調用

人工智能
AI智能體是指具備一定自主性、能感知環境并通過智能決策執行特定任務的軟件或硬件實體。它結合了人工智能技術,能夠獨立或協作完成目標。本文包含ReAct基礎,基于ReAct的工具調用流程和涉及的交互消息,手搓Agent代碼實現基于ReAct的工具調用。

前言

AI智能體是指具備一定自主性、能感知環境并通過智能決策執行特定任務的軟件或硬件實體。它結合了人工智能技術(如機器學習、自然語言處理、計算機視覺等),能夠獨立或協作完成目標。

基于大語言模型(LLM)的Function Calling可以令智能體實現有效的工具使用和與外部API的交互。支持Function Calling的模型(如gpt-4,qwen-plus等)能夠檢測何時需要調用函數,并輸出調用函數的函數名和所需參數的JSON格式結構化數據。

但并非所有的LLM模型都支持Function Calling(如deepseek-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\"]}}\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/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\"]}}\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/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\"]}}\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 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", cnotallow=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", cnotallow=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大模型應用開發
相關推薦

2025-07-18 08:46:33

2025-09-30 07:05:22

2020-03-18 14:00:47

MySQL分區數據庫

2022-06-07 10:13:22

前端沙箱對象

2021-06-30 08:45:02

內存管理面試

2021-07-08 10:08:03

DvaJS前端Dva

2020-12-07 06:19:50

監控前端用戶

2024-05-29 12:13:50

2019-11-06 17:30:57

cookiesessionWeb

2024-08-08 14:57:32

2022-04-11 10:56:43

線程安全

2021-10-20 08:49:30

Vuexvue.js狀態管理模式

2023-04-12 08:38:44

函數參數Context

2021-08-05 06:54:05

觀察者訂閱設計

2020-12-18 09:36:01

JSONP跨域面試官

2023-11-23 06:50:08

括號

2023-09-20 16:20:20

2023-09-13 22:39:23

Minikube開源

2025-08-01 02:43:00

2024-04-12 12:19:08

語言模型AI
點贊
收藏

51CTO技術棧公眾號

欧美激情2020午夜免费观看| 欧美在线观看你懂的| 成人免费91在线看| 亚洲 欧美 视频| 精品视频黄色| 日韩一区二区视频| 欧美女人性生活视频| 日本中文字幕在线视频| 蜜桃av噜噜一区二区三区小说| 久久久精品免费视频| 大乳护士喂奶hd| 日韩毛片免费看| 欧美日韩国产一区二区| 在线精品日韩| 深夜福利视频一区| 老汉av免费一区二区三区| 久久久噜久噜久久综合| 日本免费网站视频| 日韩大胆成人| 欧美成人一级视频| 鲁一鲁一鲁一鲁一av| а√天堂8资源中文在线| 国产精品久久久久久福利一牛影视 | 国内精品福利| 亚洲无av在线中文字幕| 中文字幕乱妇无码av在线| 视频二区不卡| 狠狠躁夜夜躁人人爽天天天天97| 欧美一级中文字幕| 午夜视频在线免费观看| 国产午夜精品久久| 久久久久久欧美精品色一二三四| 国产精品无码白浆高潮| 国产精品尤物| 97香蕉超级碰碰久久免费的优势| 欧美黄色一区二区三区| 91亚洲国产成人久久精品| 日韩精品免费视频| 亚洲少妇中文字幕| 亚洲开心激情| 日韩精品福利一区二区三区| 国产美女娇喘av呻吟久久| 国产精品91在线观看| 日韩在线视频免费播放| 最新亚洲视频| 国精产品一区一区三区有限在线| 欧美丰满熟妇bbbbbb| 欧美激情欧美| 北条麻妃久久精品| 任我爽在线视频| 日韩影院二区| 日韩在线视频播放| 一级二级黄色片| 波多野结衣的一区二区三区| 亚洲天堂第一页| 男人的天堂官网| 成人vr资源| 日韩在线观看免费全集电视剧网站| 中文字幕精品亚洲| 99re久久最新地址获取| 久久九九精品99国产精品| 娇小11一12╳yⅹ╳毛片| 日韩片欧美片| 不卡中文字幕av| 欧美高清视频一区二区三区| 欧美性久久久| 欧美在线一级视频| 中文字幕+乱码+中文| 开心九九激情九九欧美日韩精美视频电影 | 狠狠色综合播放一区二区| 好吊成人免视频| 91在线观看欧美日韩| 国产高清免费av| 成人av在线资源网站| 精品一区久久久久久| 蝌蚪视频在线播放| 国产精品成人免费在线| 黑人巨茎大战欧美白妇| 国内激情视频在线观看| 在线精品视频一区二区| www.国产视频.com| julia中文字幕一区二区99在线| 亚洲精品电影网| 日本少妇xxxxx| 久久久久久久久久久久久久| 欧美激情欧美激情在线五月| 天堂在线免费观看视频| 日本aⅴ免费视频一区二区三区| 成人a在线视频| 天堂网2014av| 国产精品久久久久久亚洲伦| 800av在线免费观看| 免费电影日韩网站| 欧美一级精品大片| 西西444www无码大胆| 亚洲综合中文| 日本精品一区二区三区在线播放视频| 亚洲一区 中文字幕| www.欧美日韩| 五月天国产一区| 92久久精品| 欧美日韩成人在线一区| 亚洲欧美中文字幕在线一区| 国产一二三区av| 一区二区三区欧洲区| 亚洲欧美成人网| 加勒比av在线播放| 日韩电影免费一区| 国产女主播一区二区三区| 香蕉视频在线看| 欧美日韩国产中文字幕 | 九九九九九九精品任你躁 | 日本激情在线观看| 婷婷开心激情综合| 亚洲理论中文字幕| 国产一卡不卡| 97视频人免费观看| 国产av无码专区亚洲av| 国产欧美精品一区二区三区四区| 日韩精品在线中文字幕| 久久久精品区| 久久久999国产精品| 91视频久久久| 91老师国产黑色丝袜在线| wwwwww欧美| 国产精品国产三级在线观看| 亚洲天堂网在线观看| 中文字幕日韩一级| 国产成人精品免费| 国内自拍中文字幕| 日韩黄色三级在线观看| 在线中文字幕日韩| 亚洲中文一区二区| 久久久久国产成人精品亚洲午夜| 免费国产a级片| 成午夜精品一区二区三区软件| 久久综合久久88| 一级片视频播放| 国产人成亚洲第一网站在线播放| 亚洲 高清 成人 动漫| 国产ts一区| 久久人人爽人人爽人人片av高清| 国产激情无套内精对白视频| 亚洲色大成网站www久久九九| 在线观看国产福利| 久久一本综合| 国产在线精品成人一区二区三区| 亚洲精品传媒| 欧美精品久久一区二区三区| 国产日产在线观看| 精品一区二区三区视频 | 中国女人特级毛片| 日韩精品久久久久久| 日韩免费毛片| 久久亚洲精品人成综合网| 中文日韩在线观看| 91丨九色丨蝌蚪丨对白| 亚洲色大成网站www久久九九| 亚洲综合伊人久久| 激情欧美丁香| 精品欧美日韩在线| 3d性欧美动漫精品xxxx软件| 国产亚洲在线播放| 99国产精品一区二区三区| 亚洲激情av在线| 国产视频久久久久久| 国产精品日韩久久久| 日本一区免费在线观看| 国产精品99精品一区二区三区∴| 久久精品亚洲国产| 天堂网在线资源| 色婷婷久久综合| 福利视频第一页| 国产999精品久久久久久绿帽| 欧美 国产 综合| 精品国产精品国产偷麻豆| 成人乱人伦精品视频在线观看| 丰满人妻一区二区三区53视频| 黄色网页在线看| 欧美一区二区三区免费| 国产污视频在线看| 国产女主播一区| 手机av在线免费| 日韩视频精品在线观看| 视频一区二区在线| 2023国产精华国产精品| 国产va免费精品高清在线观看| 免费在线观看黄| 亚洲国产99精品国自产| 日韩xxx视频| 一区二区成人在线视频| www.超碰97| 精品亚洲porn| 久久国产亚洲精品无码| 久久免费大视频| 精品国产福利| 成人污污www网站免费丝瓜| 69久久夜色精品国产69| 蜜桃视频在线观看免费视频网站www| 欧美丰满一区二区免费视频| 麻豆久久久久久久久久| 综合分类小说区另类春色亚洲小说欧美| 香蕉视频污视频| 国内精品伊人久久久久av一坑| 你懂的av在线| 欧美激情视频一区二区三区在线播放 | 五月天激情播播| 亚洲中字在线| av在线免费观看国产| 国产高清一区| 欧美一区激情视频在线观看| 国产成人高清精品免费5388| 亚洲精品欧美日韩专区| 69堂精品视频在线播放| 午夜免费日韩视频| 少妇av在线| 日韩有码在线播放| 国产中文在线观看| 日韩精品极品在线观看播放免费视频 | 搞黄网站在线看| 久久久精品一区| av福利精品| 亚洲视频第一页| 五月婷婷伊人网| 精品国产一区二区三区久久久蜜月| 在线观看xxxx| av中文在线资源库| 久久精品这里热有精品| av网在线观看| 中文字幕欧美国内| 国产在线视频福利| 亚洲欧美日韩在线高清直播| 无码国产精品高潮久久99| 精品国产乱码久久久久久久| av网站在线观看免费| 69堂亚洲精品首页| 国产乱淫片视频| 欧美久久久影院| 一级做a爱片久久毛片| 欧美日韩一卡二卡三卡| 成人一级免费视频| 欧美亚洲一区二区在线| 无码一区二区三区在线观看| 日韩欧美精品网址| www毛片com| 91极品视觉盛宴| 亚洲免费视频二区| 欧美日韩黄色影视| 国产免费一区二区三区免费视频| 欧美肥胖老妇做爰| 国产男男gay网站| 日韩一区二区精品在线观看| 午夜精品一二三区| 亚洲国产婷婷香蕉久久久久久| 天堂在线视频免费| 亚洲美女av在线| 97在线观看免费观看高清| 色悠悠国产精品| 污污的网站在线看| 欧美精品aaa| 在线免费日韩片| 国产精品国产亚洲伊人久久| 草莓视频成人appios| 国产在线久久久| 9l亚洲国产成人精品一区二三| 国产区日韩欧美| 最新国产一区| 手机成人av在线| 一区二区亚洲| 日韩精品一区二区三区不卡| 久久精品二区亚洲w码| 麻豆网站免费观看| 99久久久精品| 国产精品1区2区3区4区| 亚洲精品免费在线播放| 久久露脸国语精品国产91| 91国产成人在线| 国产绿帽一区二区三区| 亚洲国内精品在线| 2019中文字幕在线视频| 久久国产精品影视| www.成人影院| 91九色视频导航| 天天操综合520| 制服丝袜综合日韩欧美| 激情文学一区| 污版视频在线观看| 高清视频一区二区| 欧美自拍偷拍网| 亚洲成人动漫一区| 最新中文字幕免费| 亚洲精品成人网| caoporn免费在线| 日本精品久久久| 日韩精品中文字幕一区二区| 欧美一区二区三区四区在线观看地址 | av在线资源观看| 亚洲免费一级电影| 四虎亚洲精品| 国产男人精品视频| 亚洲另类av| 大荫蒂性生交片| 国内精品久久久久影院一蜜桃| 麻豆国产精品一区| 亚洲欧美日韩一区二区 | 日韩三级视频在线看| 国产在线中文字幕| 国模叶桐国产精品一区| 亚洲人体在线| 日韩理论片在线观看| 亚洲综合丁香| caopor在线| 亚洲欧美经典视频| 一区二区的视频| 亚洲视频自拍偷拍| 亚洲优女在线| 国产一区二区三区av在线 | 男人的天堂日韩| 99麻豆久久久国产精品免费优播| 成人在线观看免费完整| 欧美亚洲国产bt| 极品白浆推特女神在线观看 | 无码毛片aaa在线| 美女性感视频久久| 日本一道本视频| 在线免费亚洲电影| 久久精品a一级国产免视看成人| 色综合久久悠悠| 精品国产鲁一鲁****| 一区二区视频在线免费| 麻豆视频一区二区| 卡一卡二卡三在线观看| 一本色道a无线码一区v| 人成在线免费视频| 欧美一性一乱一交一视频| 麻豆精品99| 国产美女网站在线观看| 不卡免费追剧大全电视剧网站| 免费人成视频在线| 精品日韩一区二区| 免费看电影在线| 国产午夜精品一区| 亚洲精选成人| 久久精品一区二区免费播放| 欧美日韩另类字幕中文| 日本在线视频1区| 国产成人小视频在线观看| 国产亚洲欧美日韩在线观看一区二区| 99999精品视频| 久久精品免费在线观看| 中文字幕人妻色偷偷久久| 中文字幕亚洲综合久久筱田步美| 四虎在线精品| 一二三在线视频| 成人av在线一区二区| 欧美bbbbbbbbbbbb精品| 亚洲人成电影网| 国产精品蜜月aⅴ在线| 亚洲最大免费| 国产成人精品影视| www成人在线| 亚洲午夜性刺激影院| 久久国产三级| 成人免费在线视频播放| 91麻豆福利精品推荐| 中文精品久久久久人妻不卡| 久久久成人精品视频| 精品国产导航| 男人女人黄一级| 亚洲精品一二三| 天堂成人在线视频| 国产日韩欧美黄色| 亚洲调教视频在线观看| 欧美 日本 国产| 欧美麻豆精品久久久久久| 国产在线xxx| 日韩精品欧美专区| 国产精品99久久久久久似苏梦涵| 91精品国产高潮对白| 亚洲社区在线观看| 日本一区二区乱| 无码人妻h动漫| 亚洲精品综合在线| 日韩a在线观看| 97久草视频| 日韩精品免费视频人成| 九九热国产精品视频| 亚洲欧美999| 成人av激情人伦小说| 手机看片福利日韩| 亚洲成人免费av| 国产一二区在线| 欧美连裤袜在线视频| 国产成a人无v码亚洲福利| 久久久久久亚洲av无码专区| 欧美激情第6页| 久久影视一区| 毛片网站免费观看| 精品日韩99亚洲| 精品国产乱码一区二区三区|