对话接口
平台把模型算力以对话接口对外提供,请求体与响应格式沿用 OpenAI Chat Completions 的约定。
一、端点与认证
| 项目 | 值 |
|---|---|
| 接入地址 | https://www.hgzscloud.fun/admin-api/ai/chat/completions |
| 方法 | POST |
| 认证 | Authorization: Bearer <你的 API Key> |
| 响应格式 | SSE 流式分片,末尾为 data: [DONE] |
API Key 在控制台创建,与网站登录令牌是两回事:调模型必须用 API Key。
注意:这里是完整端点,不是 OpenAI SDK 约定的{base_url}/chat/completions。 官方 SDK 会固定拼接/chat/completions,直接指向本站会 404; 用 SDK 时需要把 base_url 设为https://www.hgzscloud.fun/admin-api并覆写请求路径为/ai/chat/completions,或直接用原生 HTTP 请求。
二、请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型标识,取模型广场展示的 model 值 |
| messages | array | 是 | 对话消息数组 |
| stream | boolean | 是 | 必须为 true,接口按流式返回 |
| stream_options | object | 是 | 需带 { "include_usage": true },否则不返回本次用量 |
| temperature | number | 否 | 采样温度 |
| max_tokens | number | 否 | 最大生成 token 数 |
| top_p | number | 否 | 核采样 |
messages 里每条消息包含 role 与 content:
| role | 说明 |
|---|---|
| system | 系统提示词,设定助手角色与约束 |
| user | 用户输入 |
| assistant | 历史助手回复,多轮对话时回传 |
三、请求示例
curl -X POST https://www.hgzscloud.fun/admin-api/ai/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $SLB_API_KEY" \
-d '{
"model": "qwen3.7-flash",
"messages": [
{ "role": "system", "content": "你是一个严谨的技术助手" },
{ "role": "user", "content": "什么是算粒?" }
],
"stream": true,
"stream_options": { "include_usage": true }
}'
四、流式响应
响应是 SSE 分片,每个 data: 行一个 JSON,最后以 data: [DONE] 结束:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"算粒"}}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"是"}}]}
data: [DONE]
五、Python 示例
import httpx, json
API_KEY = "sk-..."
ENDPOINT = "https://www.hgzscloud.fun/admin-api/ai/chat/completions"
with httpx.stream(
"POST",
ENDPOINT,
headers={
"Authorization": "Bearer " + API_KEY,
"Content-Type": "application/json",
},
json={
"model": "qwen3.7-flash",
"messages": [{"role": "user", "content": "什么是算粒?"}],
"stream": True,
"stream_options": {"include_usage": True},
},
) as response:
for line in response.iter_lines():
if not line or not line.startswith("data: "):
continue
data = line[len("data: "):]
if data == "[DONE]":
break
chunk = json.loads(data)
for choice in chunk.get("choices", []):
print(choice.get("delta", {}).get("content", ""), end="")
六、Node.js 示例
const response = await fetch("https://www.hgzscloud.fun/admin-api/ai/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.SLB_API_KEY,
},
body: JSON.stringify({
model: "qwen3.7-flash",
messages: [{ role: "user", content: "什么是算粒?" }],
stream: true,
stream_options: { include_usage: true },
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice(6);
if (data === "[DONE]") break;
const chunk = JSON.parse(data);
for (const choice of chunk.choices || []) {
process.stdout.write(choice.delta && choice.delta.content ? choice.delta.content : "");
}
}
}
七、计费口径
- 按量计费:
grainCost = CEIL(((in × inputPrice + out × outputPrice) / 1000) × grainsPerYuan),从钱包扣算粒。 - 套餐权益:命中覆盖该模型的有效权益套餐时免扣算粒,本次调用的
grainCost为 0。 - 两种来源二选一,不会同时发生。详见计费说明。
八、常见错误
| 现象 | 原因 | 处理 |
|---|---|---|
返回流里带 API Key验证失败 | API Key 缺失、过期或已被禁用 | 检查 Authorization 头;必要时在控制台重新创建密钥 |
| 404 No static resource | 路径拼错或用了 OpenAI SDK 的默认拼接 | 用完整端点 /admin-api/ai/chat/completions |
| 不返回 usage | 没带 stream_options.include_usage | 补上该参数 |
| 算粒不足 | 钱包余额不够本次调用 | 充值算粒,或购买覆盖该模型的权益套餐 |