Skip to content

Chat Completions ​

对话补全接口,OpenAI 兼容。

POST https://www.zzcraft.cn/v1/chat/completions

请求头 ​

头值
AuthorizationBearer sk-你的令牌
Content-Typeapplication/json

请求参数 ​

参数类型必填说明
modelstring是模型名,见 支持的模型
messagesarray是对话消息列表
max_tokensinteger否最大输出 token 数,不填由模型决定
temperaturenumber否0–2,越大越随机,默认 1
top_pnumber否核采样,与 temperature 二选一
streamboolean否true 时以 SSE 流式返回
toolsarray否可供模型调用的工具定义
reasoning_effortstring否思考等级: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 都能直接用。

基于开源项目 New API 与 SearXNG 构建