OpenAI 兼容接口
接收 OpenAI Chat Completions 请求格式,保持相应的响应、SSE 和错误结构。推荐给使用 OpenAI SDK 的项目和大多数新项目——已有 OpenAI SDK 代码只需修改 Base URL 和 API Key。
POST/api/v1/chat/completions
OpenAI Chat Completions
OpenAI Chat Completions 兼容入口,额外字段按模型能力透传。
🔑Bearer API Key请求头使用 Authorization: Bearer sk_live_...,也支持 x-api-key。
🧩先确认模型调用名请先在 模型广场 筛选支持当前接口格式的模型,打开模型详情页复制调用名。当前账号的最终可用范围还受组织权限影响。 请求结构
下方内容用于确认方法、地址和鉴权方式,属于 HTTP 结构片段,不是可独立执行示例。
POST https://www.silvamux.com/api/v1/chat/completions
Authorization: Bearer $SILVAMUX_API_KEY
Content-Type: application/json
请求体
application/json · OpenAIChatRequest · 必填
| 字段 | 类型 | 必填 | 说明 |
|---|
max_tokens | integer | 否 | — |
messages | array<Message> | 是 | — |
messages.content | string | array<FreeFormObject> | 是 | — |
messages.role | string (system | user | assistant | tool) | 是 | — |
model | string | 是 | SilvaMux 模型 id 或 alias。 |
stream | boolean | 否 | 默认值:false |
temperature | number | 否 | — |
tools | array<FreeFormObject> | 否 | — |
响应
200OpenAI Chat JSON 或 SSE 响应。
application/json · OpenAIChatResponse
| 字段 | 类型 | 必填 | 说明 |
|---|
choices | array<FreeFormObject> | 是 | — |
id | string | 是 | — |
model | string | 否 | — |
object | string | 否 | — |
usage | FreeFormObject | 否 | — |
defaultOpenAI 兼容错误。
application/json · OpenAIError
| 字段 | 类型 | 必填 | 说明 |
|---|
error | object | 是 | — |
error.code | string | integer | 否 | — |
error.message | string | 是 | — |
error.type | string | 否 | — |
curl https://www.silvamux.com/api/v1/chat/completions \
-H "Authorization: Bearer $SILVAMUX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "minimax-m2.5",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好"}
],
"stream": false
}'
# pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SILVAMUX_API_KEY"],
base_url="https://www.silvamux.com/api/v1",
)
response = client.chat.completions.create(
model="minimax-m2.5",
messages=[
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好"},
],
)
print(response.choices[0].message.content)
// npm install openai
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.SILVAMUX_API_KEY,
baseURL: "https://www.silvamux.com/api/v1",
});
const response = await client.chat.completions.create({
model: "minimax-m2.5",
messages: [
{ role: "system", content: "你是一个有帮助的助手" },
{ role: "user", content: "你好" },
],
});
console.log(response.choices[0].message.content);
// 仅依赖标准库
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
)
func main() {
payload := []byte(`{
"model": "minimax-m2.5",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好"}
],
"stream": false
}`)
req, err := http.NewRequest("POST", "https://www.silvamux.com/api/v1/chat/completions", bytes.NewReader(payload))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("SILVAMUX_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
}
响应(OpenAI 格式):
{
"id": "chatcmpl-xxxx",
"choices": [
{
"message": {"role": "assistant", "content": "你好!有什么可以帮你的吗?"},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 20, "completion_tokens": 15, "total_tokens": 35}
}
兼容入口允许透传对应协议的额外字段。流式输出与多模态输入见概述。