Skip to content

创建对话补全

向模型发送一组对话消息,生成文本回复。适用于文本对话、多轮聊天、工具调用,以及图片、音频、文件等多模态输入理解。

需要实时返回时,将请求体中的 stream 设置为 true,网关会通过 Server-Sent Events(SSE)持续返回增量内容。

适用范围

本页说明的是文本对话补全能力。图片生成虽然也使用 /chat/completions 路径,但需要 modalitiesimage_config,请参考 创建图像生成


Endpoint

MethodURL
POST{TRINITY_BASE_URL}/chat/completions

Authentication

Authorization · Bearer

Authorization 请求头传入 API Key:Bearer <TRINITY_API_KEY>(前缀一般为 xh-...)。详见 管理 API 密钥


Headers

请求头必填说明
AuthorizationBearer <TRINITY_API_KEY>
Content-Typeapplication/json
Accept流式时text/event-streamstream: true

追踪与结算(X-Request-IdX-Idempotency-KeyX-Conversation-Id)见 API 概述 · 追踪与结算


最小请求示例

bash
curl -sS "${TRINITY_BASE_URL}/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TRINITY_API_KEY}" \
  -d '{
    "model": "doubao-seed-1-6-thinking-agent-preview",
    "messages": [{ "role": "user", "content": "你好" }]
  }'

Body

字段类型必填说明
modelstring模型 ID,见 模型广场
messagesarray{ role, content }content 可为 string 或 Part 数组
streamboolean默认 falsetrue 时返回 SSE 增量事件

INFO

temperaturethinking_enabledtoolsstream_options、多模态 Part 等高级字段见 对话补全 · 高级参数


Response

非流式请求成功时返回 OpenAI 风格 JSON(choices[]usage 等)。流式请求返回 text/event-stream,从 choices[0].delta.content 读取增量内容,详见 流式输出(SSE)。响应头含 X-Request-IdX-Settlement-Key。错误见 错误与调试

json
{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "..." },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 20,
    "total_tokens": 30
  }
}

SDK 代码示例

python
import json, os, requests

url = f"{os.environ['TRINITY_BASE_URL']}/chat/completions"
payload = {
    "model": "doubao-seed-1-6-thinking-agent-preview",
    "messages": [{"role": "user", "content": "你好"}],
    "stream": False,
}
headers = {
    "Authorization": f"Bearer {os.environ['TRINITY_API_KEY']}",
    "Content-Type": "application/json",
}
r = requests.post(url, headers=headers, data=json.dumps(payload))
print(r.json()["choices"][0]["message"]["content"])
typescript
const res = await fetch(`${process.env.TRINITY_BASE_URL}/chat/completions`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.TRINITY_API_KEY}`,
  },
  body: JSON.stringify({
    model: "doubao-seed-1-6-thinking-agent-preview",
    messages: [{ role: "user", content: "你好" }],
  }),
});
const data = await res.json();
console.log(data.choices[0]?.message?.content);
bash
curl -sS "${TRINITY_BASE_URL}/chat/completions" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${TRINITY_API_KEY}" \
  -d '{
    "model": "doubao-seed-1-6-thinking-agent-preview",
    "messages": [{ "role": "user", "content": "你好" }],
    "stream": false
  }'

相关

© Trinity AI