Chat Completions
使用 OpenAI 兼容协议创建 chat completion。同一接口可调用 OpenAI、Claude、Gemini、Qwen、DeepSeek 等多种后端 — BUZZ 根据 model 字段路由上游,必要时自动完成协议转换。
https://api.openai.com/v1/chat/completions 的代码,只需替换 base URL 和 API key。OpenAI 官方 35 个请求字段全部接受,包括 tools、response_format、web_search_options、reasoning_effort、stream_options、verbosity。POST /v1/completions 也走同一 OpenAI 格式。
鉴权
BUZZ 接受标准的 OpenAI bearer token 鉴权:
| Header | 说明 |
|---|---|
Authorization: Bearer <KEY> | 推荐。OpenAI SDK 默认约定。 |
Authorization: Bearer sk-<KEY> | sk- 前缀会被自动剥离。 |
Anthropic 风格的 x-api-key 在 /v1/messages 与 /v1/models 路径上生效;/v1/chat/completions 请使用 Authorization: Bearer。
Authorization: Bearer <YOUR_BUZZ_KEY>
Content-Type: application/json
请求示例
curl -X POST https://buzzai.cc/v1/chat/completions \
-H "Authorization: Bearer $BUZZ_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-4-5-20251001",
"messages": [
{"role": "user", "content": "reply with exactly: hello world"}
],
"max_tokens": 32
}'from openai import OpenAI
client = OpenAI(
base_url="https://buzzai.cc/v1",
api_key="",
)
resp = client.chat.completions.create(
model="claude-haiku-4-5-20251001",
messages=[
{"role": "user", "content": "reply with exactly: hello world"}
],
max_tokens=32,
)
print(resp.choices[0].message.content) import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://buzzai.cc/v1",
apiKey: process.env.BUZZ_API_KEY,
});
const resp = await client.chat.completions.create({
model: "claude-haiku-4-5-20251001",
messages: [
{ role: "user", content: "reply with exactly: hello world" },
],
max_tokens: 32,
});
console.log(resp.choices[0].message.content);响应
{
"id": "chatcmpl-AbCdEf...",
"object": "chat.completion",
"created": 1748246400,
"model": "claude-haiku-4-5-20251001",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "hello world"},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 14, "completion_tokens": 2, "total_tokens": 16}
}
请求体参数
BUZZ 接受 OpenAI CreateChatCompletionRequest 定义的所有字段。下表列出 35 个官方字段以及 top_k(非官方,但 Claude / Gemini / Qwen 等多个后端常用)。
核心字段
| 字段 | 类型 | 说明 |
|---|---|---|
| model必填 | string | 使用的模型。从 GET /v1/models 拉取实时列表。 |
| messages必填 | array | 对话轮次。Role 取值:system / developer / user / assistant / tool。非空。 |
| max_tokens | integer | 最大输出 token 数。OpenAI 已弃用,但仍接受。 |
| max_completion_tokens | integer | 新版 token 上限,亦覆盖 reasoning token。 |
| stream | boolean | true 时返回 Server-Sent Events 流。默认 false。 |
| stream_options | object | {"include_usage": true} 在最后一个 chunk 输出 usage。 |
| n | integer | 生成数量,1..128。默认 1。 |
| stop | string | array | 最多 4 个停止序列。 |
采样
| 字段 | 类型 | 说明 |
|---|---|---|
| temperature | number | 采样温度,0..2,默认 1。 |
| top_p | number | Nucleus 采样,0..1,默认 1。和 temperature 二选一。 |
| top_k | integer | 非 OpenAI 官方扩展;支持的后端(Claude、Gemini、Qwen)会原样接收。 |
| seed | integer | 尽力保持确定性。OpenAI 处于 beta 状态。 |
| presence_penalty | number | -2..2,默认 0。 |
| frequency_penalty | number | -2..2,默认 0。 |
| logit_bias | object | token id → bias(-100..100)。 |
| logprobs | boolean | 返回 token 对数概率,默认 false。 |
| top_logprobs | integer | 0..20,需 logprobs: true。 |
工具与结构化输出
| 字段 | 类型 | 说明 |
|---|---|---|
| tools | array | Function 或 custom 工具定义。详见 Tool Use。 |
| tool_choice | string | object | "auto"、"required"、"none",或 {"type":"function","function":{"name":"..."}}。 |
| parallel_tool_calls | boolean | 允许一轮内并行调用多个工具。 |
| response_format | object | {"type":"text"}、{"type":"json_object"},或 {"type":"json_schema","json_schema":{...}}。 |
| function_call | string | object | 已弃用。Tools API 之前的格式;仍接受。 |
| functions | array | 已弃用,改用 tools。 |
| prediction | object | 预测输出,用于加速重写型生成。 |
推理与多模态
| 字段 | 类型 | 说明 |
|---|---|---|
| reasoning_effort | string | minimal / low / medium / high。在 Claude 路径下,BUZZ 把 low/medium/high 映射为 thinking.budget_tokens 1280 / 2048 / 4096。 |
| verbosity | string | GPT-5 系列的输出冗余度:low / medium / high。 |
| modalities | array | 输出模态,如 ["text"]、["text","audio"]。 |
| audio | object | {"voice":"...","format":"..."},音频输出配置。 |
| web_search_options | object | 原生联网检索提示。search_context_size 取值 low/medium/high(默认 medium)。Claude 路径下转为 web_search_20250305 工具,max_uses 对应 1 / 5 / 10。 |
缓存、计费与身份
| 字段 | 类型 | 说明 |
|---|---|---|
| prompt_cache_key | string | 路由提示,用于提升 prompt-cache 命中率。 |
| prompt_cache_retention | string | in_memory 或 24h。 |
| store | boolean | 是否在上游存储请求/响应。默认透传;通道可通过 disable_store 关闭。 |
| metadata | object | 自由形式的字符串映射。 |
| user | string | 已弃用的终端用户标识。 |
| safety_identifier | string | 默认被过滤。见下方说明。 |
| service_tier | string | 默认被过滤。见下方说明。 |
service_tier、safety_identifier、stream_options.include_obfuscation,以及 Claude 专属的 inference_geo、speed。store 默认透传,但通道可单独关闭。若通道或网关启用 pass-through 模式,则跳过过滤、原样透传。
当 model 是 Claude 模型时
同一 /v1/chat/completions 端点也可以指向 Claude 模型(如 claude-haiku-4-5-20251001、claude-sonnet-4-6、claude-opus-4-7)。BUZZ 会把请求改写为 Anthropic Messages API、把响应转回 OpenAI 格式 — 但若干 OpenAI 独有字段在 Anthropic 协议里没有等价物,会被静默丢弃。
Claude 路径下被静默丢弃的字段
Claude 请求构造器不会把这些字段塞进出站 payload,显式传也无效。请预先规避:
| 字段 | 对 Claude 的影响 |
|---|---|
| n | 恒为单条结果,n > 1 不支持。 |
| presence_penalty | 丢弃。 |
| frequency_penalty | 丢弃。 |
| logit_bias | 丢弃。 |
| logprobs / top_logprobs | 丢弃。响应中 logprobs 永为 null。 |
| seed | 丢弃。 |
| response_format | 丢弃。JSON 模式 / JSON-Schema 强制不会传到 Claude。需要时改用 prompt-engineered JSON 或调用 GPT 模型。 |
| function_call / functions | 丢弃(请改用现代 tools)。 |
| prediction / modalities / audio | 丢弃。 |
| verbosity | 丢弃。 |
| user / safety_identifier | 丢弃。 |
| prompt_cache_key | 丢弃。 |
| metadata | 丢弃。 |
| service_tier / store | 丢弃。 |
Claude 上的采样覆盖
Claude 模型在思考模式下,或者模型为 opus-4-7 时,采样参数会被 BUZZ 在出站前覆盖:
opus-4-7加 thinking 后缀:temperature/top_p/top_k全部清空,thinking.type=adaptive,output_config.effort=high。- 其他模型加
-thinking后缀:thinking.type=enabled,budget_tokens=max_tokens的 80%,top_p清空,temperature强制为1.0。 - 非思考模式:
temperature/top_p/top_k正常透传。
字段映射
| OpenAI 输入 | Claude 表达 |
|---|---|
role 为 system 的 messages | 抽取到 Claude 顶层 system: [{type:"text",...}] |
| tools / tool_choice / parallel_tool_calls | 转为 Anthropic tools,字段为 name / description / input_schema |
| image_url / input_audio / file / video_url 内容块 | 由网关拉取并转码为 Claude base64 image / document 源 |
| stop(字符串或数组) | stop_sequences 数组 |
| max_tokens / max_completion_tokens | 取较大值,落到 Claude max_tokens |
| web_search_options | web_search_20250305 工具,max_uses 1 / 5 / 10 |
| reasoning_effort | thinking.budget_tokens 1280 / 2048 / 4096 |
| reasoning(OpenRouter 风格) | 解析后,reasoning.max_tokens 覆盖 thinking budget |
响应
非流式响应遵循 OpenAI chat.completion 标准格式。
| 字段 | 类型 | 说明 |
|---|---|---|
| id | string | OpenAI:chatcmpl-...。Claude:直接复用上游 msg_...。 |
| object | string | 恒为 "chat.completion"。 |
| created | integer | Unix 时间戳。 |
| model | string | 上游实际使用的模型,可能是带日期的别名。 |
| choices[].index | integer | 从 0 起的 choice 序号。 |
| choices[].message.role | string | 恒为 "assistant"。 |
| choices[].message.content | string | null | 拼接后的文本。仅返回 tool call 时为 null。 |
| choices[].message.tool_calls | array | 函数工具调用;Claude 路径下从 tool_use 块构造。 |
| choices[].message.reasoning_content | string | BUZZ 扩展。o-系列、gpt-5 thinking、Claude thinking 块下生成。 |
| choices[].finish_reason | string | stop / length / tool_calls / content_filter / function_call。Claude 映射:end_turn→stop、max_tokens→length、tool_use→tool_calls、refusal→content_filter。 |
| choices[].logprobs | object | null | OpenAI 透传;Claude 永为 null。 |
| system_fingerprint | string | 仅当上游返回时透传。Claude 响应永不出现。 |
| service_tier | string | null | OpenAI 透传;Claude 不返回。 |
| usage | object | Token 计数(见下)。 |
usage 子字段
OpenAI 字段原样返回。BUZZ 增加若干扩展字段,用于缓存与推理可观测性。
| 字段 | 说明 |
|---|---|
| prompt_tokens | 输入 token 数(Claude 路径下为 input_tokens + cached_tokens + cached_creation_tokens 合计)。 |
| completion_tokens | 输出 token 数。 |
| total_tokens | prompt_tokens + completion_tokens。 |
| prompt_tokens_details | {cached_tokens, cached_creation_tokens, text_tokens, audio_tokens, image_tokens}。 |
| completion_tokens_details | {text_tokens, audio_tokens, image_tokens, reasoning_tokens}。 |
| prompt_cache_hit_tokens | BUZZ 扩展。缓存命中 token 的便捷标量。 |
| input_tokens / output_tokens | BUZZ 扩展。Anthropic 风格的别名,方便跨协议消费。 |
| claude_cache_creation_5m_tokens / _1h_tokens | BUZZ 扩展。拆分 Claude 1 小时缓存。 |
| usage_semantic / usage_source | BUZZ 扩展。usage_source = "anthropic" 表示该次调用走的是 Claude 路径。 |
流式
请求体加 "stream": true 启用 Server-Sent Events 流。每个事件是一行 data: {...},以 data: [DONE] 结束。
Stream chunk 结构
{
"id": "chatcmpl-...",
"object": "chat.completion.chunk",
"created": 1748246400,
"model": "claude-haiku-4-5-20251001",
"choices": [
{
"index": 0,
"delta": {"role": "assistant", "content": "Hello"},
"logprobs": null,
"finish_reason": null
}
]
}
设置 stream_options.include_usage = true,会在最后追加一个 choices 为空、usage 已填充的 chunk。
Claude 流的转译规则
当 model 是 Claude 模型,BUZZ 把 Anthropic event: 流转为 OpenAI chunk:
| Claude 事件 | OpenAI chunk delta |
|---|---|
| message_start | 首个 chunk,delta.role="assistant",delta.content="" |
| content_block_start(text) | 文本起始 chunk |
| content_block_start(tool_use) | delta.tool_calls[].id 与 function.name |
| content_block_delta(text_delta) | delta.content 增量 |
| content_block_delta(input_json_delta) | delta.tool_calls[].function.arguments 增量 |
| content_block_delta(thinking_delta) | delta.reasoning_content 增量 |
| content_block_delta(signature_delta) | 用换行符占位写入 reasoning_content(签名段不暴露明文) |
| message_delta | 设置 finish_reason(经 OpenAI 映射) |
| message_stop | 跳过;data: [DONE] 由网关补发 |
如何识别本次响应来自哪个上游
同一接口可命中 OpenAI 或 Claude,你不需要解析 model 名,以下信号即可指纹化判断:
| 信号 | OpenAI / GPT | Claude |
|---|---|---|
id 前缀 | chatcmpl-... | msg_... |
system_fingerprint | 上游返回时存在 | 从不出现 |
choices[].logprobs | 请求时返回对象 | 恒为 null |
usage.usage_source | 空 | "anthropic" |
service_tier | 可能存在 | 不返回 |
choices[].message.reasoning_content | 仅 o-系列、gpt-5 thinking | thinking 模式必有 |
错误码
OpenAI 风格错误包络:
{
"error": {
"code": "",
"message": "Invalid token (request id: 202605260708...)",
"type": "buzz_error"
}
}
| HTTP | error.type | 典型场景 |
|---|---|---|
| 400 | invalid_request_error | JSON 错、缺 model、messages 为空、max_tokens 越界 |
| 401 | buzz_error / authentication_error | API key 缺失或不识别 |
| 403 | permission_error | Key 对该模型或 group 没权限 |
| 413 | request_too_large | 请求体超过网关上限 |
| 429 | rate_limit_error | 命中 key / 模型 / 通道级限流,遵守 retry-after |
| 500 | api_error | 内部错误,带退避重试 |
| 503 | buzz_error / model_not_found | BUZZ 特有:该 model 在你的 group 下无可用通道 |
| 529 | overloaded_error | 上游过载,带较长退避 |
相关链接
POST /v1/messages— Anthropic 原生格式GET /v1/models— 查询可用模型- Tool Use 概念
- Prompt Caching 概念
- 流式处理指南