拒绝黑箱:从 OpenAI API 协议看 Agent 是如何长出“手脚”的

openai-api协议

为什么做这篇

我在直接使用框架如langchian4jlangchainspring AI黑箱感太重了,不了解是怎么给agent装上"手脚"的

体验openai-api协议

各家大模型基本都遵守了openai-api协议,用哪家都差不多,这里以deepseek为例。

注意: reasoning_contentthinking 字段是 DeepSeek 特有的扩展,并非 OpenAI 标准协议的一部分。

创建api keys

  1. 前往deepseek开放平台:platform.deepseek.com/
  2. 确保你的账户有充值余额
  3. 点击api keys,创建api keys

四个role

截至2026/5/16,role字段目前共有四个值:systemuserassistanttool。每个值的作用详情可查看deepseek api文档

基础对话

以deepseek为例,按照deepseek api文档给的示例,先来简单的对话(这里Bearer Token处输入你的api key)

点击SEND API REQUEST,我们能得到以下RESPONSE

从上面示例可以看到,我们需要维护请求体中的messages字段才能实现让大模型根据输入的上下文补全对话内容

Function calling功能

这部分是很多Agent Pattern的基石(如React Agent, Reflction agent, plan-execute agent, 支持MCP的Agent)我们编写出以下请求体,主要关注tools字段和tool_choice字段,此处不做过多赘述,在deepseek api官方文档对每个字段都有详细描述

json 复制代码
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "帮我查一下北京今天的天气,顺便看看上海的。"
    }
  ],
  "model": "deepseek-v4-pro",
  "thinking": {
    "type": "enabled"
  },
  "reasoning_effort": "high",
  "max_tokens": 4096,
  "response_format": {
    "type": "text"
  },
  "stop": null,
  "stream": false,
  "temperature": 0.7,
  "top_p": 1,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "获取指定城市的实时天气情况",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string",
              "description": "城市名称,例如:北京, 上海"
            },
            "unit": {
              "type": "string",
              "enum": ["celsius", "fahrenheit"],
              "description": "温度单位"
            }
          },
          "required": ["city"]
        }
      }
    }
  ],
  "tool_choice": "auto",
  "logprobs": false
}

得到以下响应

我们得到响应后,观察tool_calls字段(模型生成的 tool 调用,例如 function 调用),我们需要在代码里执行llm想要调用的函数,然后在messages字段中添加tool角色相关的消息再次发送请求给llm,示例如下

json 复制代码
{
  "model": "deepseek-v4-pro",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "帮我查一下北京今天的天气,顺便看看上海的。"
    },
    {
      "role": "assistant",
      "reasoning_content": "用户想查询北京和上海今天的天气。我需要调用 get_weather 函数两次,一次查询北京,一次查询上海。两个调用是独立的,可以同时进行。",
      "content": "好的,我同时为您查询北京和上海的天气。",
      "tool_calls": [
        {
          "index": 0,
          "id": "call_00_5GY4mVTH0JJ5AKezn5GI6260",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\": \"北京\", \"unit\": \"celsius\"}"
          }
        },
        {
          "index": 1,
          "id": "call_01_oJOHge7zD3tTuBo0BwT68516",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\": \"上海\", \"unit\": \"celsius\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_00_5GY4mVTH0JJ5AKezn5GI6260",
      "content": "{\"city\": \"北京\", \"temperature\": \"22°C\", \"condition\": \"晴\"}"
    },
    {
      "role": "tool",
      "tool_call_id": "call_01_oJOHge7zD3tTuBo0BwT68516",
      "content": "{\"city\": \"上海\", \"temperature\": \"18°C\", \"condition\": \"多云\"}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "获取指定城市的实时天气情况",
        "parameters": {
          "type": "object",
          "properties": {
            "city": { "type": "string" },
            "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
          },
          "required": ["city"]
        }
      }
    }
  ],
  "tool_choice": "auto"
}

得到的响应如下:

上述的两次请求响应让大模型装上了手脚,让agent能真正地干活

各agent pattern实现

脱离各种框架(langchain, langgraph,spring ai等),只使用基础的openai库,实现的agent pattern有ReAct Agent, plan-execute Agent, Reflection Agent

github仓库:PersonalViolet/openai-agents(小白轻喷QAQ)

agent相关教程

github仓库:datawhalechina/hello-agents: 📚 《从零开始构建智能体》------从零开始的智能体原理与实践教程

菜鸟教程:Agent 架构 | 菜鸟教程

tutorialq:ReAct Agents --- Reasoning + Acting in One Loop | tutorialQ

相关推荐
付玉祥1 小时前
Agent 不只是会调用工具:Echo Agent 的认知级记忆系统解析
agent
xuco1 小时前
如何让流式输出的 Markdown 渲染更丝滑
前端·agent
SuAluvfy2 小时前
不存在“全能第一模型”,存在“任务空间中的局部最优模型”
人工智能·chatgpt·agent
闲人编程3 小时前
Agent的安全边界:如何防止AI失控(对齐问题)
网络·python·ai·agent·权限·智能体·cai
小李子呢021115 小时前
什么是MCP?
agent
Mr. zhihao17 小时前
Agentic 知识库:Agent Wiki不是取代向量数据库,而是让 Agent 学会“多模态思考”
数据库·agent·angetic
小李子呢021117 小时前
什么是Function Call?
agent
dunky17 小时前
AI Agent 的 2026:从"能干活"到"会思考",中间还差什么
人工智能·agent
有才不一定有德19 小时前
动态生成 vs 静态预制:复杂 Agent 系统的 Sub-Agent 架构选择
架构·agent