Gateway API 文档

OpenAI 兼容
POST/openai/v1/chat/completions

Chat Completions

创建聊天补全。Gateway 将已发布的公开 model 改写为绑定的 LiteLLM model_name,再由 LiteLLM 处理协议兼容、Deployment 选择和上游弹性。支持流式和非流式响应。

需要认证支持流式

请求参数

参数类型必填说明
modelstring必填公开模型 ID。必须已发布并绑定 LiteLLM model_name,未命中当前模型投影会被拒绝。
messagesobject[]必填对话消息数组。
rolestring必填消息角色,可选值:"system"、"user"、"assistant"、"tool"。
contentstring | object[]必填消息内容,纯文本或多模态内容数组。
streamboolean可选是否启用流式输出,默认 false。
temperaturenumber可选采样温度,范围 0~2。值越高输出越随机。
max_tokensnumber可选生成的最大 token 数。
top_pnumber可选核采样参数,范围 0~1。与 temperature 二选一使用。
frequency_penaltynumber可选频率惩罚,范围 -2~2。正值降低重复 token 的概率。
presence_penaltynumber可选存在惩罚,范围 -2~2。正值鼓励讨论新话题。
stopstring | string[]可选停止序列,最多 4 个。遇到时停止生成。

调用示例

typescript
import OpenAI from "openai"

const client = new OpenAI({
  baseURL: "https://llmapi.memene.cn/openai/v1",
  apiKey: "gw_memene_<keyId>_<secret>",
})

const res = await client.chat.completions.create({
  "model": "gpt-4o",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "Hello!"
    }
  ],
  "stream": false
})
console.log(res.choices[0].message.content)

响应示例

json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1700000000,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! How can I help you today?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 9,
    "total_tokens": 29
  }
}

注意:流式模式下,响应以 SSE(Server-Sent Events)格式返回,每行以 `data: ` 开头。流结束时发送 `data: [DONE]`。