核心 API/聊天补全

通过 UniGateway 统一的聊天补全接口向主流模型家族发送文本请求。

聊天补全(Chat Completions)

该接口用于文本生成和多轮对话。

  • 方法:POST
  • 路径:/v1/chat/completions
  • Base URL:https://api.unigateway.ai/v1

最小请求

{
  "model": "gpt-5.2",
  "messages": [
    { "role": "user", "content": "用 3 点说明统一 AI Gateway 的好处。" }
  ]
}

cURL 示例

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-6",
    "messages": [
      {"role": "system", "content": "你是一个简洁的助手。"},
      {"role": "user", "content": "写一段从单一供应商迁移到 UniGateway 的说明。"}
    ],
    "temperature": 0.3
  }'

关键参数

字段类型说明
modelstring必须使用 /v1/models 返回的精确值。
messagesarray主流兼容 SDK 通用的消息数组结构。
temperaturenumber控制随机性,范围按模型而异。
max_tokensnumber限制最大输出 token。
streambooleantrue 时开启 SSE 流式输出。

部分高级参数存在模型家族差异,建议按目标模型实测验证。

响应结构示例

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "model": "gemini-3-pro-preview",
  "choices": [
    {
      "index": 0,
      "message": { "role": "assistant", "content": "..." },
      "finish_reason": "stop"
    }
  ]
}

Example request

Run it in your stack

Pick the SDK style that matches your app and copy the snippet directly into your project.

from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_UNIGATEWAY_API_KEY>",
    base_url="https://api.unigateway.ai/v1",
)

resp = client.chat.completions.create(
    model="gpt-5.2",
    messages=[{"role": "user", "content": "Summarize the benefits of a unified AI gateway in 3 bullets."}],
    temperature=0.2,
)

print(resp.choices[0].message.content)