三代 LLM API 技术演进白皮书
1. 演进总览
LLM API 的三代迭代,本质上是模型能力边界与开发者编排复杂度之间的重新分配:
| 代际 | 时间 | 代表模型 | 核心范式 | 编排责任方 |
|---|---|---|---|---|
| V1 Completions | 2020 | GPT-3 (Davinci) | Prompt → Text | 开发者(100%) |
| V2 Chat Completions | 2023.03 | GPT-3.5 Turbo | 结构化对话 + 工具声明 | 开发者(80%) |
| V3 Responses | 2025.03 | GPT-4.1 / GPT-5 系列 | 有状态 Agent Loop | 服务端(60%) |
核心规律:模型每新增一项原生能力(对话、工具调用、状态记忆),API 就向上抽象一层,将对应的编排复杂度从客户端迁移到服务端。
2. V1 Completions API:文本生成的原始形态
2.1 接口语义
Completions API 诞生于 GPT-3 时代,其设计哲学是**"无状态文本补全"**——模型仅作 为概率分布生成器,对输入 prompt 进行续写。
POST /v1/completions
Content-Type: application/json
{
"model": "text-davinci-003",
"prompt": "为一家冰淇淋店写一句标语:",
"max_tokens": 50,
"temperature": 0.7
}
2.2 响应结构
{
"id": "cmpl-xxx",
"object": "text_completion",
"choices": [
{
"text": ""甜蜜每一刻,冰爽每一口!"",
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 15,
"total_tokens": 27
}
}
2.3 技术局限
| 维度 | 状态 | 技术影响 |
|---|---|---|
| 角色系统 | ❌ 不存在 | 无法区分系统指令、用户输入、模型输出 |
| 对话状态 | ❌ 无状态 | 多轮对话需客户端手动拼接历史 |
| 工具调用 | ❌ 不支持 | 无法与外部系统交互 |
| 上下文管理 | 手动 | 开发者自行计算 token 上限并截断 |
| 约束机制 | 前缀注入 | 仅能在 prompt 开头写指令,易被用户输入覆盖 |
2.4 对话模拟的脆弱性
在 V1 时代实现多轮对话,完全依赖字符串约定:
系统:你是一名手机客服助手,请礼貌回答。
用户:我的手机开不了机。
客服:请长按电源键 10 秒以上。
用户:还是不行。
技术缺陷:
- 模型无法语义化区分 "系统"、"用户"、"客服" 边界
- 角色漂移(Role Drift)概率极高
- 无原生机制防止提示词注入攻击
3. V2 Chat Completions API:对话原语与工具链的奠基
3.1 架构升级
2023 年 3 月随 GPT-3.5 Turbo 发布,Chat Completions 将对话建模为一等公民数据结构,引入四种原生角色:
┌─────────────────────────────────────────┐
│ Chat Completions │
├─────────────────────────────────────────┤
│ Role: system → 全局行为约束 │
│ Role: user → 人类输入 │
│ Role: assistant → 模型输出 │
│ Role: tool → 工具执行结果 │
└─────────────────────────────────────────┘
关键突破:模型在后训练阶段针对这些角色做了专门强化,具备语义化的角色区分能力。
3.2 Function Calling 交互协议
Chat Completions 首次标准化了模型 → 工具 → 模型的三角交互协议:
Step 1:工具声明
POST /v1/chat/completions
Content-Type: application/json
{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "你是一名天气助手。"},
{"role": "user", "content": "北京今天天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}
Step 2:模型返回工具调用意图
{
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": "北京\"}"
}
}]
},
"finish_reason": "tool_calls"
}]
}
Step 3:客户端执行并回填
import json
# 解析第一层嵌套
tool_call = response.choices[0].message.tool_calls[0]
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
# 本地执行工具
weather_result = get_weather(**arguments) # {"temperature": 32, "condition": "晴"}
# 手动重建完整对话历史
messages.append(response.choices[0].message) # assistant 的调用消息
messages.append({
"role": "tool",
"tool_call_id": "call_abc123",
"content": json.dumps(weather_result)
})
# 第二轮请求
second_response = client.chat.completions.create(
model="gpt-4",
messages=messages
)
Step 4:模型给出最终回答
{
"choices": [{
"message": {
"role": "assistant",
"content": "北京今天天气晴朗,气温 32°C,适合外出。"
},
"finish_reason": "stop"
}]
}
3.3 关键技术约束
约束 1:assistant / tool 消息必须成对
✅ 合法序列:
assistant(tool_calls) → tool(result) → assistant(text)
❌ 非法序列:
assistant(tool_calls) → user(text) [模型会将用户输入误判为工具结果]
模型在后训练时专门学习了这种成对模式,打破该模式会导致严重的语义错乱。
约束 2:content 与 tool_calls 互斥
{
"message": {
"content": null, // 有工具调用时强制为 null
"tool_calls": [...] // 与 content 互斥
}
}
这导致类型系统无法提供编译期保证,开发者必须在运行时进行分支判断。