文本生成(OpenAI Chat Completions 兼容)
使用 OpenAI Chat Completions 格式进行文本对话生成,支持多轮对话、系统提示词、函数调用等模型能力;参数语义与 OpenAI 官方一致。
POST https://openai.jw-info.com/v1/chat/completions
| 请求头 | 是否必填 | 说明 |
|---|
| Content-Type | 是 | application/json |
| Authorization | 是 | Bearer {API_KEY},也可用 x-api-key: {API_KEY} |
| 参数名 | 类型 | 是否必填 | 默认值 | 说明 |
|---|
| model | string | 是 | - | 模型名,见模型广场 |
| messages | array | 是 | - | 对话消息列表,按时间顺序排列 |
| temperature | float | 否 | 1.0 | 采样温度 [0, 2] |
| top_p | float | 否 | 1.0 | 核采样 [0, 1] |
| max_tokens | integer | 否 | 模型默认值 | 最大生成 token 数(旧参数) |
| max_completion_tokens | integer | 否 | 模型默认值 | 最大生成 token 数(新参数,优先级更高) |
| n | integer | 否 | 1 | 候选回复数量 |
| stop | string / array | 否 | null | 停止词 |
| seed | integer | 否 | null | 随机种子,用于复现 |
| presence_penalty | float | 否 | 0 | 存在惩罚 [-2, 2] |
| tools | array | 否 | null | 工具/函数定义 |
| tool_choice | string / object | 否 | "auto" | 工具选择策略 |
| response_format | object | 否 | null | 输出格式约束,如 {"type":"json_object"} |
| stream | boolean | 否 | false | 传 true 时以 SSE 流式返回(OpenAI 兼容) |
| stream_options | object | 否 | 自动开启 | 流式输出选项。流式响应末尾固定返回 usage 统计,无需调用方设置 |
| 字段 | 类型 | 说明 |
|---|
| role | string | system / developer / user / assistant / tool |
| content | string | 消息内容(视觉理解场景为数组,见「视觉理解」) |
curl https://openai.jw-info.com/v1/chat/completions \
-H "Authorization: Bearer sk-你的密钥" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-r1",
"messages": [
{"role": "system", "content": "你是云梯TokenHub的助手"},
{"role": "user", "content": "用一句话说明什么是向量检索"}
],
"temperature": 0.7,
"max_tokens": 1024
}'
| 字段 | 类型 | 说明 |
|---|
| id | string | 响应标识 |
| choices | array | 候选回复列表,取 choices[0].message.content |
| choices[].finish_reason | string | 结束原因(stop / length / tool_calls) |
| usage.prompt_tokens | integer | 输入 token 数 |
| usage.prompt_tokens_details.cached_tokens | integer | 命中缓存的输入 token 数(计费按缓存价) |
| usage.completion_tokens | integer | 输出 token 数 |
| usage.total_tokens | integer | 总 token 数 |
stream: true 时返回 text/event-stream,逐帧 data: {...},最后以 data: [DONE] 结束,与 OpenAI 完全一致,可直接用官方 SDK:
from openai import OpenAI
client = OpenAI(base_url="https://openai.jw-info.com/v1", api_key="sk-你的密钥")
stream = client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": "你好"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
| 说明 | 细节 |
|---|
| 事件流格式 | data: {chunk json},末尾 data: [DONE] |
| usage | 末尾有一帧只带 usage 的 chunk(仅含用量统计),无需设置 stream_options |
| 费用 | 流式调用在流结束后结算,本次费用可在控制台「费用中心」查看 |
| 中途断开 | 客户端断开时中止本次生成;按已收到的用量(含估算)计费,完全拿不到用量时记 0 费用并标记失败 |
| 超时 | 流式读超时放宽到 300 秒 |
{
"id": "chatcmpl-...",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "向量检索是把文本转成向量后按相似度找最近邻。" },
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 32,
"prompt_tokens_details": { "cached_tokens": 0 },
"completion_tokens": 24,
"total_tokens": 56
}
}