文本生成/OpenAI Chat Completions API

OpenAI Chat Completions 接口,向主流模型家族发送文本请求。

聊天补全

通过 OpenAI 兼容的聊天补全 API 实现文本生成和多轮对话。

接口说明

项目
方法POST
路径/v1/chat/completions
Base URLhttps://api.unigateway.ai/v1
鉴权Authorization: Bearer $UNIGATEWAY_API_KEY
Content-Typeapplication/json

最小请求

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {"role": "user", "content": "用三点说明 AI 网关的价值。"}
    ]
  }'

Python(OpenAI SDK)

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.4",
    messages=[{"role": "user", "content": "用三点总结。"}],
)

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

TypeScript(OpenAI SDK)

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.UNIGATEWAY_API_KEY,
  baseURL: "https://api.unigateway.ai/v1",
});

const resp = await client.chat.completions.create({
  model: "gpt-5.4",
  messages: [{ role: "user", content: "用三点总结。" }],
});

console.log(resp.choices[0].message.content);

参数说明

字段类型必填说明
modelstringGET /v1/models 获取的精确模型 ID
messagesarray聊天消息数组,每个元素包含 rolecontent
temperaturenumber控制随机性;范围取决于模型
max_tokensnumber生成 token 的上限
streamboolean设为 true 启用 SSE 流式输出
top_pnumber核采样参数
frequency_penaltynumber抑制重复用词
presence_penaltynumber鼓励话题多样性
stopstring/array停止序列
userstring终端用户标识,便于风控

跨模型示例

GPT

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {"role": "system", "content": "你是简洁的技术助手。"},
      {"role": "user", "content": "写一份从单一 Provider 迁移到 UniGateway 的说明。"}
    ],
    "temperature": 0.3
  }'

Claude(通过 OpenAI 兼容接口)

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": "user", "content": "用一句话解释量子计算。"}
    ],
    "max_tokens": 200
  }'

Gemini(通过 OpenAI 兼容接口)

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-pro-preview",
    "messages": [
      {"role": "user", "content": "用三点说明 AI 网关路由。"}
    ]
  }'

使用 Claude 特有能力如 thinkingcache_control,请用 POST /v1/messages。使用 Gemini 特有能力如 responseModalitiesimageConfig,请用 POST /v1beta/models/{model}:generateContent

响应格式

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1760000000,
  "model": "gpt-5.4",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "AI 网关统一模型接入、简化计费,并实现智能路由。"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 18,
    "total_tokens": 42
  }
}
响应字段说明
choices[].message.content生成文本
choices[].finish_reason结束原因:stoplength
model实际处理请求的模型
usageToken 用量

常见错误

状态码原因处理方式
400请求体格式错误检查 JSON 结构和参数类型
401API Key 无效或缺失检查 Authorization 请求头
404模型 ID 不存在或不在套餐内重新确认 GET /v1/models
429触发限流增加退避并重试
5xx服务或上游异常指数退避重试;若持续则切换模型

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.4",
    messages=[{"role": "user", "content": "Summarize the benefits of a unified AI gateway in 3 bullets."}],
    temperature=0.2,
)

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