对话补全接口

1. 接口地址

/v1/chat/completions

 

2. 请求方式

POST

 

3. 请求参数

Header

参数 类型 是否必填 说明
Authorization string 是 Bearer API Key,例如 Bearer sk-xxxxxxxx
Content-Type string 是 application/json

Body

参数 类型 是否必填 说明
model string 是 模型 ID,可选值见下方模型列表
messages array 是 对话消息列表,元素结构见 Message
temperature float 否 生成温度,控制随机性,取值范围 0-2,默认 1
top_p float 否 核采样参数,取值范围 0-1,默认 1
max_tokens integer 否 生成最大 token 数
stream boolean 否 是否启用流式输出,默认 false
stop string/array 否 停止生成的标记,最多 4 个
frequency_penalty float 否 频率惩罚,取值范围 -2-2,默认 0
presence_penalty float 否 存在惩罚,取值范围 -2-2,默认 0
n integer 否 生成回复数量,默认 1

模型列表

model 说明
qwen3.5-plus qwen3.5-plus
qwen3.6-plus qwen3.6-plus
qwen3.7-plus qwen3.7-plus
qwen3.7-max qwen3.7-max
kimi-k2.6 kimi-k2.6
deepseek-v3-2-251201 deepseek-v3-2-251201
deepseek-v4-flash deepseek-v4-flash
deepseek-v4-pro deepseek-v4-pro

对象结构

Message

参数 类型 是否必填 说明
role string 是 消息角色,可选值:system、user、assistant
content string 是 消息内容

4. 返回参数

非流式请求返回 chat.completion 对象。

参数 类型 说明
id string 请求唯一标识
object string 返回对象类型,固定为 chat.completion
created integer 创建时间戳,单位为秒
model string 使用的模型 ID
choices array 生成结果列表
usage object Token 用量统计

choices

参数 类型 说明
index integer 结果序号
message object 生成的消息对象
finish_reason string 结束原因,例如 stop 表示正常结束,length 表示达到最大长度

message

参数 类型 说明
role string 角色
content string 回复内容

usage

参数 类型 说明
prompt_tokens integer 输入 token 数
completion_tokens integer 输出 token 数
total_tokens integer 总 token 数

流式请求返回 text/event-stream,每个事件以 data: 开头,结束事件为 data: [DONE]。

5. 请求示例

非流式请求

curl -X POST https://hiharness.hidreamai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <YOUR_API_KEY>" \
  -d '{
    "model": "qwen3.5-plus",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
      {"role": "user", "content": "你好,请介绍一下你自己。"}
    ],
    "temperature": 0.7,
    "max_tokens": 1024
  }'

非流式响应

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1748236800,
  "model": "qwen3.5-plus",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!我是 AI 助手,有什么可以帮你的吗?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 18,
    "total_tokens": 33
  }
}

流式请求

curl -X POST https://hiharness.hidreamai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <YOUR_API_KEY>" \
  -d '{
    "model": "qwen3.5-plus",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
      {"role": "user", "content": "你好,请介绍一下你自己。"}
    ],
    "stream": true,
    "temperature": 0.7,
    "max_tokens": 1024
  }'

流式响应

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1748236800,"model":"qwen3.5-plus","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1748236800,"model":"qwen3.5-plus","choices":[{"index":0,"delta":{"content":"你好"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1748236800,"model":"qwen3.5-plus","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1748236800,"model":"qwen3.5-plus","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]

6. Python 示例

requests 非流式调用

import requests

BASE_URL = "https://hiharness.hidreamai.com"
API_KEY = "sk-xxxxxxxx"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}",
}

payload = {
    "model": "qwen3.5-plus",
    "messages": [
        {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
        {"role": "user", "content": "你好,请介绍一下你自己。"},
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
}

response = requests.post(
    f"{BASE_URL}/v1/chat/completions",
    headers=headers,
    json=payload,
)
response.raise_for_status()

result = response.json()
print(result["choices"][0]["message"]["content"])

requests 流式调用

import json
import requests

BASE_URL = "https://hiharness.hidreamai.com"
API_KEY = "sk-xxxxxxxx"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {API_KEY}",
}

payload = {
    "model": "qwen3.5-plus",
    "messages": [
        {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
        {"role": "user", "content": "你好,请介绍一下你自己。"},
    ],
    "stream": True,
    "temperature": 0.7,
    "max_tokens": 1024,
}

response = requests.post(
    f"{BASE_URL}/v1/chat/completions",
    headers=headers,
    json=payload,
    stream=True,
)
response.raise_for_status()

for line in response.iter_lines():
    if not line:
        continue
    line = line.decode("utf-8")
    if not line.startswith("data: "):
        continue
    data = line[6:]
    if data == "[DONE]":
        break
    chunk = json.loads(data)
    delta = chunk["choices"][0]["delta"]
    if "content" in delta:
        print(delta["content"], end="", flush=True)

print()

OpenAI Python Client 非流式调用

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://hiharness.hidreamai.com/v1",
)

response = client.chat.completions.create(
    model="qwen3.5-plus",
    messages=[
        {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
        {"role": "user", "content": "你好,请介绍一下你自己。"},
    ],
    temperature=0.7,
    max_tokens=1024,
)

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

OpenAI Python Client 流式调用

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://hiharness.hidreamai.com/v1",
)

stream = client.chat.completions.create(
    model="qwen3.5-plus",
    messages=[
        {"role": "system", "content": "你是一个有帮助的 AI 助手。"},
        {"role": "user", "content": "你好,请介绍一下你自己。"},
    ],
    stream=True,
    temperature=0.7,
    max_tokens=1024,
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)

print()

7. 错误码

HTTP 状态码 说明
400 请求参数错误
401 API Key 无效或缺失
429 请求频率超限
500 服务器内部错误
503 模型服务暂时不可用

results matching ""

    No results matching ""

    results matching ""

      No results matching ""