返回文档首页

Chat 对话 - API 文档

与 AI 模型进行多轮对话,支持流式输出。这是最常用的接口,OpenAI 完全兼容。

接口说明 这是什么

Chat 对话 是 AI 应用的核心接口:把你的消息发给模型,模型返回回复。支持多轮对话(传历史消息)、角色设定(system)、流式打字机效果。

有什么用:聊天机器人、AI 助手、客服、写作、翻译、代码生成等一切需要 AI 生成文本的场景。

请求信息

项目值
请求方法POST
请求路径https://103.236.87.35/v1/chat/completions
请求头Authorization: Bearer <你的密钥>
Content-Type: application/json
是否需要鉴权必需
是否计费按 Token 计费

请求参数

参数名类型必填默认值说明
modelstring是-模型名称,如 deepseek-v4-flash。见「模型列表」接口
messagesarray是-对话消息数组,每项含 role 和 content
messages[].rolestring是-角色:system(系统设定)/ user(用户)/ assistant(助手)
messages[].contentstring是-消息内容文本
temperaturenumber否1随机性,0~2。越低越稳定,越高越有创造性
top_pnumber否1核采样,与 temperature 二选一调整
max_tokensinteger否模型默认回复最大 token 数
streamboolean否false是否流式返回(SSE),true 时逐块输出
stopstring/array否-遇到该字符串即停止生成
presence_penaltynumber否0话题新鲜度惩罚,-2~2
frequency_penaltynumber否0重复度惩罚,-2~2
userstring否-终端用户标识

怎么请求 请求示例

方式一:curl

curl https://103.236.87.35/v1/chat/completions \
  -H "Authorization: Bearer <你的密钥>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-flash",
    "messages": [
      {"role": "system", "content": "你是一个乐于助人的助手"},
      {"role": "user", "content": "你好,介绍一下你自己"}
    ],
    "temperature": 0.7,
    "max_tokens": 1024
  }'

方式二:Python (OpenAI SDK)

from openai import OpenAI

client = OpenAI(
    api_key="<你的密钥>",
    base_url="https://103.236.87.35/v1",
)

resp = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[
        {"role": "system", "content": "你是一个乐于助人的助手"},
        {"role": "user", "content": "你好,介绍一下你自己"},
    ],
)
print(resp.choices[0].message.content)

方式三:Node.js

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "<你的密钥>",
  baseURL: "https://103.236.87.35/v1",
});

const resp = await client.chat.completions.create({
  model: "deepseek-v4-flash",
  messages: [{ role: "user", content: "你好" }],
});
console.log(resp.choices[0].message.content);

返回示例 成功响应

{
  "id": "chatcmpl-0217888408922",
  "object": "chat.completion",
  "created": 1788840893,
  "model": "deepseek-v4-flash-ga-260731",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "你好!我是你的 AI 助手。"},
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 86,
    "completion_tokens": 35,
    "total_tokens": 121
  }
}

返回字段说明

字段名类型说明
idstring本次请求唯一 ID
objectstring对象类型,固定 chat.completion
createdinteger创建时间戳(秒)
modelstring实际使用的模型
choices[].message.rolestring回复角色,固定 assistant
choices[].message.contentstring模型回复的文本内容
choices[].finish_reasonstring结束原因:stop 正常结束 / length 达到上限 / content_filter 内容过滤
usage.prompt_tokensinteger输入 token 数(计费依据)
usage.completion_tokensinteger输出 token 数(计费依据)
usage.total_tokensinteger总 token 数

流式请求 (SSE)

设置 "stream": true,响应以 text/event-stream 逐块返回。网关会自动注入 stream_options.include_usage(流结束带计费数据),并剥离模型思考内容。

Python 流式示例

from openai import OpenAI

client = OpenAI(api_key="<你的密钥>", base_url="https://103.236.87.35/v1")

stream = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[{"role": "user", "content": "讲个笑话"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

错误返回示例

{
  "error": {
    "message": "Invalid API key provided",
    "type": "new_api_error",
    "param": "",
    "code": "invalid_api_key"
  }
}

常见错误码:401 invalid_api_key 密钥错误 · 402 insufficient_balance 余额不足 · 403 model_not_allowed 模型无权 · 429 rate_limit_exceeded 调用超限。完整对照见文档首页 · 错误格式。

其他接口

接口说明
POST /v1/chat/completionsChat 对话(流式 SSE)查看 →
POST /v1/completions文本补全查看 →
POST /v1/embeddings向量嵌入查看 →
POST /v1/responsesResponses API查看 →
GET /v1/models模型列表查看 →
GET /v1/me账号余额查询查看 →
GET/POST /v1/notifications平台通知查看 →
GET /v1/_install_check安装自检查看 →