Chat Completions
对话补全接口,OpenAI 兼容。
POST https://www.zzcraft.cn/v1/chat/completions请求头
| 头 | 值 |
|---|---|
Authorization | Bearer sk-你的令牌 |
Content-Type | application/json |
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名,见 支持的模型 |
messages | array | 是 | 对话消息列表 |
max_tokens | integer | 否 | 最大输出 token 数,不填由模型决定 |
temperature | number | 否 | 0–2,越大越随机,默认 1 |
top_p | number | 否 | 核采样,与 temperature 二选一 |
stream | boolean | 否 | true 时以 SSE 流式返回 |
tools | array | 否 | 可供模型调用的工具定义 |
reasoning_effort | string | 否 | 思考等级:low / medium / high / max(支持该参数的模型) |
messages
json
[
{"role": "system", "content": "你是一个简洁的助手"},
{"role": "user", "content": "介绍一下 Go 语言的 goroutine"},
{"role": "assistant", "content": "goroutine 是 Go 的轻量级线程……"},
{"role": "user", "content": "它和线程有什么区别?"}
]role 可选值:system、user、assistant、tool。
多模态模型支持把 content 写成数组:
json
{
"role": "user",
"content": [
{"type": "text", "text": "这张图里是什么?"},
{"type": "image_url", "image_url": {"url": "https://example.com/cat.jpg"}}
]
}返回
json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1794000000,
"model": "deepseek-v4.1-flash",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "goroutine 是 Go 运行时管理的轻量级线程……"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 24,
"completion_tokens": 156,
"total_tokens": 180
}
}finish_reason 含义:
| 值 | 含义 |
|---|---|
stop | 正常结束 |
length | 达到 max_tokens 上限被截断 |
tool_calls | 模型要求调用工具 |
content_filter | 被上游内容策略拦截 |
流式输出
加 "stream": true,返回变成 SSE:
bash
curl https://www.zzcraft.cn/v1/chat/completions \
-H "Authorization: Bearer sk-你的令牌" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4.1-flash",
"messages": [{"role": "user", "content": "数到十"}],
"stream": true
}'data: {"choices":[{"delta":{"content":"1"}}]}
data: {"choices":[{"delta":{"content":"、"}}]}
...
data: [DONE]每个 data: 行是一个 JSON,取 choices[0].delta.content 拼接。
思考过程
支持思考的模型会把推理过程放在 reasoning_content 字段:
json
{
"choices": [{
"message": {
"role": "assistant",
"reasoning_content": "用户问的是……",
"content": "最终答案"
}
}]
}用 reasoning_effort 控制思考深度:
json
{"reasoning_effort": "low"}low 快而浅,max 慢而深。不是所有模型都支持,见 支持的模型 里的 reasoning_effort 列。
各语言示例
Python(openai SDK)
python
from openai import OpenAI
client = OpenAI(
base_url="https://www.zzcraft.cn/v1",
api_key="sk-你的令牌"
)
resp = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role": "user", "content": "你好"}]
)
print(resp.choices[0].message.content)Node.js
javascript
import OpenAI from 'openai'
const client = new OpenAI({
baseURL: 'https://www.zzcraft.cn/v1',
apiKey: 'sk-你的令牌'
})
const resp = await client.chat.completions.create({
model: 'deepseek-v4.1-flash',
messages: [{ role: 'user', content: '你好' }]
})
console.log(resp.choices[0].message.content)Go
go
package main
import (
"context"
"fmt"
"github.com/sashabaranov/go-openai"
)
func main() {
cfg := openai.DefaultConfig("sk-你的令牌")
cfg.BaseURL = "https://www.zzcraft.cn/v1"
client := openai.NewClientWithConfig(cfg)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "deepseek-v4.1-flash",
Messages: []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleUser, Content: "你好"},
},
},
)
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}因为协议完全兼容,任何支持自定义 base_url 的 OpenAI SDK 都能直接用。
