基础文本对话
POST /v1/chat/completions
OpenAI 兼容的对话补全接口。所有模型(Claude / GPT / Gemini)均通过此端点调用,只需更改 model 参数即可切换。
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model |
string | 是 | 模型 ID,如 claude-opus-4-8 / gpt-5.5 / gemini-3.1-pro。 |
messages |
array | 是 | 对话消息列表,每项含 role(system/user/assistant/tool)和 content。 |
max_tokens |
integer | 否 | 生成的最大 token 数。超过模型上限时自动收敛(clamp)到上限。 |
temperature |
number | 否 | 采样温度,0–2,默认 1。 |
top_p |
number | 否 | 核采样,0–1,默认 1。 |
stream |
boolean | 否 | 是否流式返回(SSE),默认 false。详见流式响应。 |
stop |
string | array | 否 | 停止序列。 |
tools |
array | 否 | 可调用的工具(function calling)。 |
tool_choice |
string | object | 否 | 工具选择策略:auto / none / 指定工具。 |
response_format |
object | 否 | 响应格式,如 {"type": "json_object"}。 |
seed |
integer | 否 | 采样随机种子,便于复现。 |
请求示例
cURL
curl https://api.openbili.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "claude-opus-4-8",
"messages": [
{"role": "user", "content": "你好,请介绍一下自己"}
],
"max_tokens": 1024
}'Python
import openai
client = openai.OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.openbili.com/v1",
)
response = client.chat.completions.create(
model="claude-opus-4-8",
messages=[{"role": "user", "content": "你好,请介绍一下自己"}],
max_tokens=1024,
)
print(response.choices[0].message.content)Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.openbili.com/v1",
});
const response = await client.chat.completions.create({
model: "claude-opus-4-8",
messages: [{ role: "user", content: "你好,请介绍一下自己" }],
max_tokens: 1024,
});
console.log(response.choices[0].message.content);响应
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 本次补全的唯一 ID。 |
object |
string | 固定为 chat.completion。 |
created |
integer | Unix 时间戳。 |
model |
string | 实际使用的模型。 |
choices |
array | 补全结果,每项含 index / message / finish_reason。 |
usage |
object | token 用量:prompt_tokens / completion_tokens / total_tokens。 |
{
"id": "chatcmpl-...",
"object": "chat.completion",
"created": 1730000000,
"model": "claude-opus-4-8",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "你好!我是..." },
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 12, "completion_tokens": 28, "total_tokens": 40 }
}