Files
dsapi_pub/开发者接口文档(OpenAI兼容).md
2026-12-05 18:03:58 +08:00

293 lines
9.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 开发者接口文档(OpenAI兼容):模型提供商问答
> 实现已落地。Base URL:`http://ip:端口`
> **鉴权:Body `apiKey` 优先**;无 Body 时可用 `Authorization: Bearer`。支持 `stream=true`(伪流式 SSE,兼容 Chatbox)。
> 本接口为**新建**,不替代 `/v1/provider/chat` 与 `/v1/chat/completions`。
---
## 1. 接口基础信息
| 项 | 内容 |
|----|------|
| 接口名称 | 模型提供商问答(OpenAI 兼容) |
| 请求方式 | `POST` |
| 接口地址 | `{BASE}/v1/provider/chat/completions` |
| 完整地址示例 | `http://localhost:8080/v1/provider/chat/completions` |
| Content-Type | `application/json` |
| 鉴权 | API Key(Body 优先) |
| 流式 | 支持(伪流式:先完整生成再 SSE 切块,兼容 Chatbox 默认 stream=true) |
| Accept | 任意(含 `application/xml`);非流式响应始终为 `application/json`,不因 Accept 返回 406 |
| 成功响应 | OpenAI `chat.completion` JSON(无平台 `code/message/data` 包装) |
| 失败响应 | `{"error":{"message","type","code"}}` |
### 前置条件
1. 平台超管已登录 DeepSeek 且可聊天
2. 已创建有效 API Key,对应用户余额 ≥ 单价
### 兼容性说明(260906更新)
对于此OpenAI兼容接口,为兼容各个客户端的各种情况,这里只要路径开头是v1就能匹配。也就是说,v1后面能随便加任意路径。
---
## 2. 请求参数
### Header(可选)
| 参数名 | 类型 | 必填 | 说明 | 示例 |
|--------|------|------|------|------|
| Content-Type | string | 是 | JSON | application/json |
| Authorization | string | 否 | Body 无 apiKey 时的兜底:`Bearer <key>` | Bearer Ab12Cd34Ef |
### Body
| 参数名 | 类型 | 必填 | 说明 | 示例 |
|--------|------|------|------|------|
| apiKey | string | **推荐必填** | API Key,**优先于 Header** | Ab12Cd34Ef |
| messages | array | 是 | OpenAI 风格消息列表。**实际发到 DeepSeek 网页的是最后一条 `role=user` 的 `content` 归一化后的纯文本**(1~204000 字,按字符长度)。`content` 可为**字符串**或**数组**(见下) | 见下 |
| model | string | 否 | 回显到响应;默认 `deepseek-chat` | deepseek-chat |
| stream | boolean | 否 | `false`/省略:返回完整 JSON;`true`:SSE 伪流式(先调模型拿全文再切块推送,兼容 Chatbox) | true |
| webSearch | boolean | 否 | 是否联网(智能搜索)。默认 false。服务端会先读 DeepSeek 网页真实开关再切换 | true |
| historyMode | string | 否 | `save`:同一 Key 复用同一对话(**默认**,与 `/v1/provider/chat` 共用粘性会话);`none`:每次新对话 | save |
### 请求结构示例
```json
{
"apiKey": "Ab12Cd34Ef",
"model": "deepseek-chat",
"messages": [
{"role": "user", "content": "用一句话解释什么是 API"}
],
"stream": false,
"webSearch": false,
"historyMode": "none"
}
```
对上例,DeepSeek 网页端实际输入内容为:`用一句话解释什么是 API`。
### `content` 字符串与数组
| `content` 形态 | 行为 |
|----------------|------|
| 字符串 | 原样 trim 后发送 |
| 数组 | 按顺序抽取 `type=text`(或无 type)的 `text`,用换行 `\n` 拼成一段再发送;数组元素也可以是纯字符串 |
| 仅含图片等非文本 | **拒绝**(本接口非真多模态,无法把图发到网页) |
| 其它类型 | **拒绝** |
**数组示例(最终网页发送「第一段\\n第二段」):**
```json
{
"apiKey": "Ab12Cd34Ef",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "第一段"},
{"type": "text", "text": "第二段"}
]
}
],
"webSearch": false,
"historyMode": "none"
}
```
---
## 3. 响应结构
### 成功
| 字段 | 类型 | 说明 |
|------|------|------|
| id | string | `chatcmpl-{requestId}`,计费追踪可对应该后缀 |
| object | string | 固定 `chat.completion` |
| created | number | Unix 秒 |
| model | string | 请求中的 model 或默认值 |
| choices | array | 至少 1 条 |
| choices[0].index | number | 0 |
| choices[0].message.role | string | `assistant` |
| choices[0].message.content | string | 模型回复全文(对应 `/v1/provider/chat` 的 `data.answer`) |
| choices[0].finish_reason | string | `stop` |
```json
{
"id": "chatcmpl-a1b2c3d4e5f6",
"object": "chat.completion",
"created": 1710000000,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "API 是应用程序接口……"
},
"finish_reason": "stop"
}
]
}
```
### 失败
```json
{
"error": {
"message": "余额不足",
"type": "insufficient_quota",
"code": "402"
}
}
```
| code | type(参考) | 场景 |
|------|--------------|------|
| 400 | invalid_request_error | messages 空、无 user、content 非法/纯图片数组、historyMode 非法、超长 |
| 401 | authentication_error | 缺少/无效 API Key |
| 402 | insufficient_quota | 余额不足 |
| 503 / 503x | api_error | DeepSeek 未就绪、采集失败、开关切换失败 |
| 500 | server_error | 系统错误 |
> 业务失败多为 HTTP 200 + `error` 对象;系统错误可能为 HTTP 500。请用是否存在 `error` 字段判断失败,不要依赖平台 `code=="0"`。
---
## 4. 调用示例
### cURL
```bash
curl -s -X POST "http://localhost:8080/v1/provider/chat/completions" \
-H "Content-Type: application/json" \
-d '{
"apiKey": "YOUR_API_KEY",
"messages": [{"role": "user", "content": "用一句话解释什么是 API"}],
"webSearch": false,
"historyMode": "none"
}'
```
**响应示例:**
```json
{
"id": "chatcmpl-a1b2c3d4e5f6",
"object": "chat.completion",
"created": 1710000000,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "API(应用程序接口)是软件之间约定好的调用方式。"
},
"finish_reason": "stop"
}
]
}
```
### Python
```python
import requests
url = "http://localhost:8080/v1/provider/chat/completions"
resp = requests.post(
url,
json={
"apiKey": "YOUR_API_KEY",
"messages": [{"role": "user", "content": "用一句话解释什么是 API"}],
"webSearch": False,
"historyMode": "none",
},
timeout=300,
)
body = resp.json()
print(body)
if "error" in body:
raise RuntimeError(body["error"]["message"])
print(body["choices"][0]["message"]["content"])
```
**多轮(save):**
```python
payload = {"apiKey": "YOUR_API_KEY", "historyMode": "save", "webSearch": False}
requests.post(
url,
json={**payload, "messages": [{"role": "user", "content": "我叫小明"}]},
timeout=300,
)
r2 = requests.post(
url,
json={**payload, "messages": [{"role": "user", "content": "我叫什么?"}]},
timeout=300,
)
print(r2.json()["choices"][0]["message"]["content"])
```
### Java
```java
import java.net.URI;
import java.net.http.*;
String json = """
{
"apiKey": "YOUR_API_KEY",
"messages": [{"role": "user", "content": "用一句话解释什么是 API"}],
"webSearch": false,
"historyMode": "none"
}
""";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/v1/provider/chat/completions"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient()
.send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(resp.body());
```
**响应示例:**
```text
{"id":"chatcmpl-a1b2c3d4e5f6","object":"chat.completion","created":1710000000,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"API(应用程序接口)是软件之间约定好的调用方式。"},"finish_reason":"stop"}]}
```
---
## 5. 行为说明
| 参数 | 行为 |
|------|------|
| messages | 取**最后一条** `role=user` 的 `content`:字符串原样;数组则拼接其中文本段后填入 DeepSeek 输入框(非真多模态) |
| webSearch | 对应 Web「智能搜索」;发送前读网页真态并差分切换,失败则整次调用失败 |
| historyMode=none | 每次新建 DeepSeek 对话与本系统会话(须显式传入) |
| historyMode=save | 同一 apiKey 绑定一个会话;首次创建,之后复用(**默认**;与 `/v1/provider/chat` 共用) |
| stream | 支持伪流式 SSE;Chatbox 等默认开流式的客户端可正常显示 |
---
## 6. 与其它接口对比
| 项 | `/v1/provider/chat` | `/v1/chat/completions` | **本接口** `/v1/provider/chat/completions` |
|----|---------------------|-------------------------|---------------------------------------------|
| Key | Body 优先 | 仅 Header | Body 优先 |
| 入参 | question / webSearch / historyMode | messages | messages + webSearch / historyMode |
| 联网 | 可开 | 固定关 | 可开 |
| 历史模式 | 有 | 无 | 有(与 provider/chat 共用 sticky) |
| 流式 | 无 | 有(伪流式) | 有(伪流式,兼容 Chatbox) |
| 响应 | code/message/data | OpenAI JSON | OpenAI JSON |
| 取回复 | `data.answer` | `choices[0].message.content` | `choices[0].message.content` |
---