跳到主要内容

AI API

聊天补全。认证方式:Authorization: Bearer <token>x-api-key: <your-api-key>。非流式响应为 JSON(choices[].message.content);流式响应为 SSE 分块(choices[].delta.content)。

  • 版本: 1.0
  • Base URL: https://api.b.ai
  • 兼容旧域名: https://api.bankofai.io
  • OpenAPI: 3.1.0

认证

Bearer Token

  • 类型: HTTP Bearer Token
  • 请求头: Authorization: Bearer <token>
  • 格式: 使用平台签发的 API Key 风格密钥,例如 sk-xxx
  • 示例: Bearer sk-xxx

API Key

  • 类型: API Key
  • 请求头: x-api-key: <your-api-key>
  • 说明: Chat CompletionsMessages 端点都同时支持 x-api-keyAuthorization: Bearer <token>。两者本质上都使用平台签发的同一种密钥。

端点

1. 获取模型列表

GET /v1/models

获取可用模型列表。认证方式:Bearer Token。

认证: Bearer Token 或 API Key(x-api-key

200 响应:

{
"object": "list",
"success": true,
"data": [
{
"id": "gpt-5.2",
"object": "model",
"created": 1626777600,
"owned_by": "openai",
"supported_endpoint_types": ["openai", "anthropic"]
}
]
}
状态码描述
200成功 - 返回模型列表
400错误请求 - 参数无效或请求体格式错误
401未授权 - 认证无效或缺失
403禁止访问 - 无权限、额度不足或账号被封禁
429请求过多 - 超出速率限制
500服务器内部错误

2. 聊天补全(OpenAI 兼容)

POST /v1/chat/completions

接收一组消息并返回模型生成的回复。支持单轮和多轮对话。响应既可以是单个 JSON 对象,也可以通过流式(SSE)返回。

认证: Bearer Token

请求体

参数类型必填描述
modelstring要使用的模型 ID(例如 gpt-5.2)。
messagesarray对话中的消息列表。参见 ChatMessage
streamboolean若为 true,将通过 Server-Sent Events 返回部分消息增量。默认值为 false
max_tokensinteger本次补全最多可生成的 token 数。
temperaturenumber采样温度,范围 0 到 2。值越高,结果越随机。默认 1
top_pnumberNucleus Sampling,仅考虑累计概率达到 top_p 的 token。默认 1
stopstring | string[]最多 4 个停止序列,命中后 API 将停止生成。
ninteger生成多少个补全选项。默认 1
frequency_penaltynumber范围 -2.0 到 2.0。用于惩罚重复 token。默认 0
presence_penaltynumber范围 -2.0 到 2.0。用于惩罚已在文本中出现过的 token。默认 0
seedinteger随机种子,用于确定性采样(如果模型支持)。
response_formatobject指定输出格式:{ "type": "text" }{ "type": "json_object" }json_schema
toolsarray模型可调用的工具列表。参见 ChatTool
tool_choicestring | object可选值:"auto""none""required",或 { "type": "function", "function": { "name": "..." } }
userstring可选的终端用户标识,用于滥用监控。
web_search_optionsobject为支持的模型开启网页搜索。参见 WebSearchOptions

请求示例

{
"model": "gpt-5.2",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Hello" }
],
"stream": false,
"max_tokens": 1024,
"temperature": 1
}

响应(非流式)

{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-5.2",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you?",
"refusal": null,
"annotations": []
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 8,
"total_tokens": 20,
"prompt_tokens_details": { "cached_tokens": 0, "audio_tokens": 0 },
"completion_tokens_details": {
"reasoning_tokens": 0,
"audio_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
}
}
}

响应(流式)

每个 SSE 分块的 object 都是 "chat.completion.chunk",其中 choices[].delta.content 包含增量文本。最后一个分块会包含 usagefinish_reason

状态码描述
200成功
400错误请求 - 参数无效、请求体格式错误或请求非法
401未授权 - 认证无效或缺失
403禁止访问 - 无权限、额度不足或模型访问受限
429请求过多 - 超出速率限制
500服务器内部错误
502网关错误 - 上游服务错误
503服务不可用 - 服务过载或无可用通道

3. Messages(Claude 兼容)

POST /v1/messages

接收一组消息并返回模型生成的回复。支持单轮和多轮对话。可通过 x-api-key 请求头或 Bearer Token 进行认证。响应既可以是单个 JSON 对象,也可以通过流式(SSE)返回。

认证: API Key(x-api-key)或 Bearer Token

请求体

参数类型必填描述
modelstring模型 ID(例如 claude-sonnet-4.6claude-opus-4.6claude-haiku-4.5)。
max_tokensinteger最多生成的 token 数。不同模型的最大值不同。
messagesarray输入消息。用户与助手轮流出现。上限:100,000 条消息。参见 MessagesMessageItem
systemstring | array系统提示词。可以是纯字符串,也可以是文本块数组(用于 cache_control)。
streamboolean是否使用 SSE 流式返回。默认 false
temperaturenumber随机性(0.0 - 1.0)。分析型任务建议接近 0.0,创意型任务建议接近 1.0。默认 1
top_pnumberNucleus Sampling。默认 1
top_kinteger仅从概率最高的前 K 个选项中采样。默认关闭。
stop_sequencesstring[]自定义停止文本序列,命中后停止生成。
metadataobject请求元数据。支持 user_id(不透明标识符)。
thinkingobject扩展思考配置。参见 ThinkingConfig
toolsarray模型可调用的工具定义。参见 Tool
tool_choiceobject模型如何使用工具:autoanytoolnone

请求示例

{
"model": "claude-sonnet-4.6",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "Hello, Claude!" }
],
"system": "You are a helpful assistant.",
"temperature": 1.0
}

响应(非流式)

{
"id": "chatcmpl-xxx",
"type": "message",
"role": "assistant",
"content": [
{ "type": "text", "text": "Hello! How can I help you?" }
],
"stop_reason": "end_turn",
"model": "claude-sonnet-4.6",
"usage": {
"input_tokens": 4,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"output_tokens": 12,
"claude_cache_creation_5_m_tokens": 0,
"claude_cache_creation_1_h_tokens": 0
}
}

响应(流式 - SSE 事件)

流式响应会发出以下事件类型:

事件类型描述关键字段
message_start初始消息元数据message(id、model、role、usage)
content_block_start开始新的内容块indexcontent_block(type、text)
content_block_delta增量内容indexdelta(type: text_delta、text)
content_block_stop内容块结束index
message_stop消息完成-
状态码描述
200成功
400错误请求 - 参数无效、请求体格式错误或请求非法
401未授权 - API Key 无效或缺失
403禁止访问 - 无权限、额度不足或模型访问受限
429请求过多 - 超出速率限制
500服务器内部错误
502网关错误 - 上游服务错误
503服务不可用 - 服务过载或无可用通道

数据模型

ChatMessage

字段类型必填描述
rolestring"system""user""assistant""tool"
contentstring消息内容。对于 tool 角色,这里是工具调用结果。
namestring消息作者的可选名称。
tool_call_idstringrole"tool" 时,对应的工具调用 ID。
tool_callsarrayrole"assistant" 且模型调用了工具时使用。格式为 { id, type, function: { name, arguments } } 的数组。

MessagesMessageItem

字段类型必填描述
rolestring"user""assistant"(不支持 "system",请使用顶层 system 参数)。
contentstring | array文本字符串,或内容块数组(text、image、tool_use、tool_result)。

内容块类型(Messages API)

TextBlockParam

{ "type": "text", "text": "Hello, Claude!", "cache_control": { "type": "ephemeral" } }

ImageBlockParam

Base64 来源:

{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}
}

URL 来源:

{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
}
}

支持的媒体类型:image/jpegimage/pngimage/gifimage/webp

ToolUseBlockParam(来自 assistant)

{
"type": "tool_use",
"id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
"name": "get_stock_price",
"input": { "ticker": "AAPL" }
}

ToolResultBlockParam(来自 user)

{
"type": "tool_result",
"tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV",
"content": "259.75 USD",
"is_error": false
}

ThinkingConfig

启用扩展思考,让 Claude 展示其推理过程。

启用:

{ "type": "enabled", "budget_tokens": 1024 }
  • budget_tokens:必须大于等于 1024,并且小于 max_tokens

禁用:

{ "type": "disabled" }

Tool(Anthropic)

{
"name": "get_stock_price",
"description": "Get the current stock price for a given ticker symbol.",
"input_schema": {
"type": "object",
"properties": {
"ticker": { "type": "string" }
},
"required": ["ticker"]
}
}

ToolChoice(Anthropic)

类型描述
{ "type": "auto" }模型自行决定是否使用工具。支持 disable_parallel_tool_use
{ "type": "any" }模型将使用任意可用工具。支持 disable_parallel_tool_use
{ "type": "tool", "name": "..." }模型将使用指定工具。支持 disable_parallel_tool_use
{ "type": "none" }模型不会使用工具。

ChatTool

{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string" }
},
"required": ["location"]
}
}
}

WebSearchOptions

字段类型描述
search_context_sizestring"low""medium""high",表示网页搜索结果占用的上下文大小。
user_locationobject用户的大致位置(国家 ISO 3166-1 alpha-2、城市、地区、时区)。

ChatResponseFormat

字段类型描述
typestring"text""json_object"
json_schemaobjecttypejson_schema 时,指定可选的输出结构 schema。

错误响应

所有错误响应都遵循以下格式:

{
"error": {
"message": "Error message",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
字段类型描述
messagestring错误信息
typestring错误类型(例如 invalid_request_error
paramstring | null相关参数
codestring | null错误代码