算力宝

对话接口

平台把模型算力以对话接口对外提供,请求体与响应格式沿用 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 请求。

二、请求参数

参数类型必填说明
modelstring是模型标识,取模型广场展示的 model 值
messagesarray是对话消息数组
streamboolean是必须为 true,接口按流式返回
stream_optionsobject是需带 { "include_usage": true },否则不返回本次用量
temperaturenumber否采样温度
max_tokensnumber否最大生成 token 数
top_pnumber否核采样

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补上该参数
算粒不足钱包余额不够本次调用充值算粒,或购买覆盖该模型的权益套餐

相关文档