一、角色枚举

role含义使用场景
system系统提示词(人设、规则)首轮必带,全局指令
userHuman 用户输入人类提问、指令
assistantAI 模型回复模型普通文本回答 / 工具调用请求
tool工具执行结果函数调用后,把工具返回数据塞给模型

二、基础单条 Message JSON 标准结构

1. system 消息

{
  "role": "system",
  "content": "你是专业数据分析助手,只能使用提供工具查询数据,禁止编造信息"
}

2. human (user) 用户消息

{
  "role": "user",
  "content": "查询2026年6月全国销售额数据"
}

3. ai (assistant) 普通纯文本回复(无工具调用)

{
  "role": "assistant",
  "content": "我将为你调取2026年6月销售数据,请稍等"
}

4. ai (assistant) 带工具调用

tool_calls 存在时,content 通常为空字符串

{
  "role": "assistant",
  "content": "",
  "tool_calls": [
    {
      "id": "call_0123xyz",
      "type": "function",
      "function": {
        "name": "get_sales_data",
        "arguments": "{\"month\":\"2026-06\",\"region\":\"全国\"}"
      }
    }
  ]
}

5. tool 工具结果消息

必须携带 tool_call_id 关联上方 tool_calls.id

{
  "role": "tool",
  "tool_call_id": "call_0123xyz",
  "name": "get_sales_data",
  "content": "{\"total\":125800000,\"growth_rate\":0.086,\"top_province\":\"山东\"}"
}

三、请求体完整规范

完整 Request 示例(带工具定义)

{
  "model": "gpt-4o",
  "temperature": 0.7,
  "messages": [
    {
      "role": "system",
      "content": "你是销售数据查询助手,有数据需求必须调用get_sales_data工具,禁止虚构数据"
    },
    {
      "role": "user",
      "content": "查2026年6月全国销售总额及增速"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_sales_data",
        "description": "按月份、地区查询销售统计数据",
        "parameters": {
          "type": "object",
          "required": ["month"],
          "properties": {
            "month": {
              "type": "string",
              "description": "年月,格式YYYY-MM,例:2026-06"
            },
            "region": {
              "type": "string",
              "description": "地区,不传默认全国",
              "enum": ["全国", "山东", "广东", "江苏"]
            }
          }
        }
      }
    }
  ],
  "tool_choice": "auto"
  "stream":"false"
}

tool_choice:控制工具调用模式:

  • auto:模型自主判断是否调用工具(默认)
  • none:禁止调用工具,只输出文本
  • {“type”:“function”,“function”:{“name”:“xxx”}}:强制调用指定函数

四、模型返回 Response

{
  "id": "chatcmpl-9xVmYdef456",
  "object": "chat.completion",
  "created": 1782123789,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "",
        "tool_calls": [
          {
            "id": "call_789xyz",
            "type": "function",
            "function": {
              "name": "get_sales_data",
              "arguments": "{\"month\":\"2026-06\",\"region\":\"全国\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 182,
    "completion_tokens": 41,
    "total_tokens": 223
  }
}

五、流式传输

5.1 完整请求体

5.2 流式响应json

这里为了便于理解,抽象压缩成5个块
流式传输中 content和工具调用tool_calls 不会再一个块中返回,而是会互斥分布在不同的块中返回

步骤阶段名称核心动作content 状态tool_calls 状态
初始化角色接收第一个块,声明 role: assistant不存在不存在
处理文本回复(内容增量)循环接收并拼接 delta.content 片段存在(逐步累加)不存在
处理工具调用(参数增量)循环接收并拼接 delta.tool_calls 参数片段不存在(至此为 null)存在(逐步累加)
终止与执行收到 finish_reason,解析完整数据拼接完成拼接完成,解析 JSON 并执行函数

包 1(属于步骤 ①):

{
  "choices": [{
    "delta": { "role": "assistant" },
    "finish_reason": null
  }]
}

包 2(属于步骤 ②):

{ "choices": [{ "delta": { "content": "正在查询北京天气。" }, "finish_reason": null }] }

包 3(步骤 ②结束,步骤 ③开始):

{ 
  "choices": [{ 
    "delta": { 
      "tool_calls": [{ 
        "index": 0, 
        "id": "call_123", 
        "function": { "name": "get_weather", "arguments": "{\"loc" } 
      }] 
    }, 
    "finish_reason": null 
  }] 
}

包 4(属于步骤 ③):

{ "choices": [{ "delta": { "tool_calls": [{ "index": 0, "function": { "arguments": "ation\":\"北京\"}" } }] }, "finish_reason": null }] }

包 5(步骤 ④ 终止):

{ "choices": [{ "delta": {}, "finish_reason": "tool_calls" }] }
Logo

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

更多推荐