



























在开发 AI 应用时,我们经常需要对接不同的大模型 API。其中 OpenAI 的 Chat Completions API 和 Anthropic 的 Messages API 是目前最流行的两种。虽然它们功能相似,但 请求/响应格式、消息角色、工具调用流程 存在显著差异。本文将从实际开发角度,为你详细对比两者,并提供可直接使用的代码示例。
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "回复内容"
},
"finish_reason": "stop"
}
],
"usage": { ... }
}
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "回复内容" }
],
"model": "claude-3-sonnet-...",
"stop_reason": "end_turn",
"usage": { ... }
}
| 特性 | OpenAI | Anthropic |
|---|---|---|
| 消息位置 | choices[0].message |
顶层 role + content |
content 类型 |
字符串(或数组) | 数组(纯文本也需数组形式) |
| 结束原因字段 | finish_reason |
stop_reason |
| 判断依据 | 存在 "choices" → OpenAI |
存在 "type": "message" → Anthropic |
{
"model": "gpt-4",
"messages": [
{ "role": "system", "content": "你是一个助手" },
{ "role": "user", "content": "你好" }
],
"temperature": 0.7,
"max_tokens": 1000
}
{
"model": "claude-3-sonnet-20240229",
"system": "你是一个助手",
"messages": [
{ "role": "user", "content": "你好" }
],
"temperature": 0.7,
"max_tokens": 1000 // 必填!
}
| 特性 | OpenAI | Anthropic |
|---|---|---|
| 系统提示 | 在 messages 中,role: "system" |
顶层 system 字段 |
| 消息角色 | system, user, assistant, tool |
仅 user, assistant |
max_tokens |
可选,有默认值 | 必填,无默认值 |
content 类型 |
字符串(可扩展为数组) | 必须是数组(如 [{"type":"text","text":"..."}]) |
| 角色顺序约束 | 无强制交替 | 严格交替,且第一条必须是 user |
| 特有参数 | presence_penalty, frequency_penalty |
top_k |
| 停止词字段 | stop |
stop_sequences |
system:设定助手行为(可出现在任意位置,但通常放在开头)user:用户输入assistant:模型回复tool:工具调用结果常见错误写法:
function_call)role 或 content 字段user 或两个 assistant(虽不强制,但影响逻辑)user 和 assistant 仅限这两种。系统指令放在顶层的 system 字段。严格约束:
user 开头 → 报错content 是字符串而非数组(某些模型会失败)assistant 消息的内容以空白字符结尾 → 特殊失败场景\n\nHuman: 格式 → 不兼容 Messages API第一次请求:定义工具,模型返回 tool_calls
{
"model": "gpt-4o",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "北京天气怎么样?" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名称" }
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}
模型返回(简化):
{
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
}]
}
}]
}
第二次请求:回传工具执行结果
{
"model": "gpt-4o",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "北京天气怎么样?" },
{
"role": "assistant",
"content": null,
"tool_calls": [{ "id": "call_abc123", "type": "function", "function": { "name": "get_weather", "arguments": "{\"city\":\"北京\"}" } }]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"content": "{\"temperature\": \"22°C\", \"condition\": \"晴\"}"
}
],
"tools": [ /* 同第一次定义 */ ]
}
关键点:工具结果使用
role: "tool",通过tool_call_id关联。
第一次请求:定义工具,模型返回 tool_use 块
{
"model": "claude-3-sonnet-20240229",
"system": "You are a helpful assistant.",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "北京天气怎么样?" }
]
}
],
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名称" }
},
"required": ["city"]
}
}
],
"tool_choice": { "type": "auto" },
"max_tokens": 1024
}
模型返回:
{
"id": "msg_xyz789",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "好的,我来查询北京的天气。" },
{
"type": "tool_use",
"id": "toolu_01AbC",
"name": "get_weather",
"input": { "city": "北京" }
}
],
"stop_reason": "tool_use"
}
第二次请求:回传工具执行结果
{
"model": "claude-3-sonnet-20240229",
"system": "You are a helpful assistant.",
"messages": [
{
"role": "user",
"content": [
{ "type": "text", "text": "北京天气怎么样?" }
]
},
{
"role": "assistant",
"content": [
{ "type": "text", "text": "好的,我来查询北京的天气。" },
{
"type": "tool_use",
"id": "toolu_01AbC",
"name": "get_weather",
"input": { "city": "北京" }
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01AbC",
"content": "{\"temperature\": \"22°C\", \"condition\": \"晴\"}"
}
]
}
],
"tools": [ /* 同第一次定义 */ ],
"max_tokens": 1024
}
关键点:
- 工具结果放在 user 消息中,类型为
tool_result。- 必须保留之前的
assistant完整content。- 消息仍然严格交替
user/assistant(工具结果本质上是一条新的user消息)。
| 对比维度 | OpenAI | Anthropic |
|---|---|---|
| 设计哲学 | messages 是统一的时序容器 |
分离系统指令与对话历史 |
| 角色丰富度 | 更灵活(4种角色) | 简洁(仅2种核心角色) |
| 严格程度 | 宽容,不强制交替 | 严格,强制交替且首消息为 user |
| 工具调用 | 独立 tool 角色 + tool_calls 字段 |
tool_use / tool_result 块嵌入 content |
| 易错点 | 角色名拼错、缺少字段 | 内容不是数组、未交替、尾随空格 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。