外观
Chat Completions
/v1/chat/completions 用于兼容 OpenAI Chat Completions 调用方式,适合直接复用现有 SDK、HTTP 客户端或第三方工具。
请求地址
text
POST https://mux.la/v1/chat/completions常用请求字段
| 字段 | 必填 | 说明 |
|---|---|---|
model | 是 | 模型名称,例如 gpt-4o |
messages | 是 | 对话消息数组,至少包含一条消息 |
temperature | 否 | 采样温度 |
max_tokens | 否 | 最大输出 token 数 |
stream | 否 | 是否使用流式返回,true 时返回 SSE 数据流 |
store | 否 | 是否存储该 completion |
metadata | 否 | 自定义元数据 |
请求示例
bash
curl https://mux.la/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}'返回结构示例
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1738960610,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 10,
"total_tokens": 18
}
}流式输出
将 stream 设置为 true:
bash
curl https://mux.la/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"stream": true,
"messages": [
{
"role": "user",
"content": "请用三句话介绍 Muxla"
}
]
}'OpenAI SDK 示例
ts
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.MUXLA_API_KEY,
baseURL: 'https://mux.la/v1'
})
const completion = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: '你好' }]
})
console.log(completion.choices[0]?.message?.content)