/openai/v1/chat/completions创建聊天补全。Gateway 将已发布的公开 model 改写为绑定的 LiteLLM model_name,再由 LiteLLM 处理协议兼容、Deployment 选择和上游弹性。支持流式和非流式响应。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 必填 | 公开模型 ID。必须已发布并绑定 LiteLLM model_name,未命中当前模型投影会被拒绝。 |
| messages | object[] | 必填 | 对话消息数组。 |
| role | string | 必填 | 消息角色,可选值:"system"、"user"、"assistant"、"tool"。 |
| content | string | object[] | 必填 | 消息内容,纯文本或多模态内容数组。 |
| stream | boolean | 可选 | 是否启用流式输出,默认 false。 |
| temperature | number | 可选 | 采样温度,范围 0~2。值越高输出越随机。 |
| max_tokens | number | 可选 | 生成的最大 token 数。 |
| top_p | number | 可选 | 核采样参数,范围 0~1。与 temperature 二选一使用。 |
| frequency_penalty | number | 可选 | 频率惩罚,范围 -2~2。正值降低重复 token 的概率。 |
| presence_penalty | number | 可选 | 存在惩罚,范围 -2~2。正值鼓励讨论新话题。 |
| stop | string | string[] | 可选 | 停止序列,最多 4 个。遇到时停止生成。 |
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){
"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]`。