Function Calling 执行流程和历史消息结构

在 function calling(工具调用)的场景中,传递给大语言模型(LLM)的历史消息需要包含完整的对话上下文,包括用户输入、模型生成的工具调用请求(tool_calls),以及工具执行后的返回结果。以下是详细的格式说明和示例:

核心流程与消息结构

  1. 用户提问 → 2. 模型生成工具调用请求 → 3. 工具执行并返回结果 → 4. 模型根据结果生成最终回答

消息历史需要按顺序包含这些步骤,格式如下:

1. 用户提问(User Message)

  • 用户的问题或请求。
  • 格式
{
  "role": "user",
  "content": "What's the weather in Beijing tomorrow?"
}

2. 模型生成工具调用请求(Assistant Message with tool_calls

  • 模型决定调用工具时,返回一个包含 tool_calls 字段的消息,描述需要调用的工具和参数。
  • 关键字段
    • tool_calls: 工具调用列表,每个调用包含唯一 id、工具名称 name 和参数 arguments
  • 格式
{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_abc123",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"location\": \"Beijing\", \"date\": \"2023-10-05\"}"
      }
    }
  ]
}

3. 工具执行结果(Tool Message)

  • 将工具执行的结果以 tool 角色的消息返回,需关联对应的 tool_call_id
  • 关键字段
    • tool_call_id: 与 tool_calls 中的 id 对应。
    • content: 工具返回的结果(通常是 JSON 字符串或纯文本)。
  • 格式
{
  "role": "tool",
  "content": "{\"temperature\": 22, \"condition\": \"sunny\"}",
  "tool_call_id": "call_abc123"
}

4. 模型生成最终回答(Assistant Message)

  • 模型根据工具返回的结果生成最终回答。
  • 格式

  "role": "assistant",
  "content": "The weather in Beijing tomorrow will be sunny with 22°C."
}

完整示例对话历史

// 用户提问
  {
    "role": "user",
    "content": "What's the weather in Beijing tomorrow?"
  },
// 模型请求调用工具
  {
    "role": "assistant",
    "content": null,
    "tool_calls": [
      {
        "id": "call_abc123",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"location\": \"Beijing\", \"date\": \"2023-10-05\"}"
        }
      }
    ]
  },
// 工具返回结果
  {
    "role": "tool",
    "content": "{\"temperature\": 22, \"condition\": \"sunny\"}",
    "tool_call_id": "call_abc123"
  },
// 模型生成最终回答
  {
    "role": "assistant",
    "content": "The weather in Beijing tomorrow will be sunny with 22°C."
  }
]

关键注意事项

  1. 顺序必须严格:消息需按时间顺序排列,确保模型能正确理解上下文。
  2. 关联 tool_call_id:工具的返回结果必须与对应的工具调用请求通过 tool_call_id 匹配。
  3. 工具结果格式:工具返回的结果可以是 JSON 字符串或纯文本,但需与模型预期的输入格式一致。
  4. 支持多工具调用:如果模型同时调用多个工具(如 tool_calls 列表有多个条目),需为每个工具调用单独返回对应的 tool 消息。

通过这种方式,模型能够根据完整的对话历史和工具结果生成准确的回答。

历史消息样例(deepseek-v3)

{
    "model": "deepseek-chat",
    "messages": [
        {
            "role": "user",
            "content": "今天是星期几?"
        },
        {
            "role": "assistant",
            "content": "",
            "tool_calls": [
                {
                    "index": 0,
                    "id": "call_0_a762209f-0498-4166-a95c-5b8c5302dcaa",
                    "type": "function",
                    "function": {
                        "name": "get_current_datetime",
                        "arguments": "{}"
                    }
                }
            ]
        },
        {
            "tool_call_id": "call_0_a762209f-0498-4166-a95c-5b8c5302dcaa",
            "role": "tool",
            "content": "2025-03-26 10:16:20 星期三"
        }
    ],
    "stream": false,
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_datetime",
                "description": "Get current datetime and day of week"
            }
        }
    ]
}

👉 持续分享AI工具,AI应用场景,AI学习资源 ❤️

📢 创作不易,如果这篇文章对你有帮助,欢迎❤️关注、👍点赞支持,并️转发给那些需要的朋友!关注 [拓荒者IT] 公众号获取更多精彩内容!

posted @ 2025-03-27 20:20  拓荒者IT  阅读(28)  评论(1)    收藏  举报
皮肤配置 参考地址:https://www.yuque.com/awescnb/user