help.khb.com 文本系列 API: 创建对话请求 (Chat Completions)

API: 创建对话请求 (Chat Completions)

Create a Chat Completion

Creates a model response for the given chat conversation.

端点:POST /v1/chat/completions

Authorization

设置请求头 Authorization: Bearer {your API key} 进行身份验证。

Request Body

model (string, required)

对应模型名称。平台会定期更新模型以提升服务质量,变化可能包括模型上下线或能力调整。完整可用模型列表请查看 模型广场

示例:"Pro/zai-org/GLM-4.7"

messages (array<object>, required)

由消息组成的对话列表。每个消息包含 role(system/user/assistant/tool) 和 content 字段。

stream (boolean)

如果设置,token 将以 Server-Sent Events 形式实时返回,流式输出以 data: [DONE] 结束。可选值:false | true

max_tokens (integer)

单次请求最大生成长度。确保输入 token + max_tokens 不超过模型上下文窗口。由于部分服务仍在更新中,请勿将 max_tokens 设置为窗口上限;为输入和系统开销预留 ~10k token 空间。

enable_thinking (boolean)

在 thinking 与 non-thinking 模式之间切换。该字段支持以下模型:

  • Pro/zai-org/GLM-5
  • Pro/zai-org/GLM-4.7
  • deepseek-ai/DeepSeek-V3.2
  • Pro/deepseek-ai/DeepSeek-V3.2
  • zai-org/GLM-4.6
  • Qwen/Qwen3-8B / 14B / 32B / 30B-A3B
  • tencent/Hunyuan-A13B-Instruct
  • zai-org/GLM-4.5V
  • deepseek-ai/DeepSeek-V3.1-Terminus / Pro
  • Qwen/Qwen3.5-397B-A17B / 122B-A10B / 35B-A3B / 27B / 9B / 4B

thinking_budget (integer)

思维链最大长度(仅在 enable_thinking=true 时生效)。

temperature (number)

采样温度,范围 0.0~2.0。值越高,输出越发散;值越低,输出越发集中。默认 1.0。

top_p (number)

核采样阈值,范围 0.0~1.0。默认 1.0。

frequency_penalty (number)

频率惩罚,范围 -2.0~2.0。值越高,越抑制重复用词。

tools (array<object>)

Function Calling 工具列表,详见 Function Calling

response_format (object)

输出格式约束,如 {"type": "json_object"} 强制 JSON 输出,详见 JSON 模式

代码示例

cURL

curl --request POST \
  --url https://khb.net.cn/v1/chat/completions \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "Pro/zai-org/GLM-4.7",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "你好,请介绍一下自己"}
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": false
  }'

Python

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://khb.net.cn/v1"
)

response = client.chat.completions.create(
    model="Pro/zai-org/GLM-4.7",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "你好,请介绍一下自己"}
    ],
    temperature=0.7,
    max_tokens=1024,
    stream=False
)

print(response.choices[0].message.content)

JavaScript

const options = {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model: 'Pro/zai-org/GLM-4.7',
    messages: [
      {role: 'system', content: 'You are a helpful assistant.'},
      {role: 'user', content: '你好,请介绍一下自己'}
    ],
    temperature: 0.7,
    max_tokens: 1024,
    stream: false
  })
};

fetch('https://khb.net.cn/v1/chat/completions', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Response Body

成功响应(200):

{
  "id": "chatcmpl-xxx",
  "object": "chat.completion",
  "created": 1741685396,
  "model": "Pro/zai-org/GLM-4.7",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!我是 KHB 算力平台的大语言模型助手..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 32,
    "completion_tokens": 156,
    "total_tokens": 188
  }
}

错误码

状态码 说明
200 成功
400 参数错误(模型不存在、参数越界等)
401 API Key 无效
403 权限不足(余额不足、未实名认证等)
404 资源不存在
429 触发 Rate Limits
500/503/504 服务端错误,稍后重试

Related Post