Chat Completions
POST /v1/chat/completions
OpenAI 兼容对话接口,推荐作为默认接入方式。
请求示例
bash
curl https://api.hoyoai.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "用一句话介绍 HOYOAI"}
],
"temperature": 0.7
}'常用字段
| 字段 | 类型 | 说明 |
|---|---|---|
model | string | 必填,模型名以模型广场为准 |
messages | array | 必填,对话消息列表 |
temperature | number | 可选,采样温度 |
max_tokens | integer | 可选,最大生成长度 |
stream | boolean | 可选,是否 SSE 流式 |
top_p | number | 可选 |
stop | string / array | 可选,停止序列 |
部分模型还支持工具调用、视觉等多模态字段,以模型能力为准。
流式输出
bash
curl https://api.hoyoai.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "claude-sonnet-4-6",
"stream": true,
"messages": [{"role": "user", "content": "写一首短诗"}]
}'响应为 text/event-stream,按 OpenAI SSE 格式逐块返回,以 data: [DONE] 结束。
响应要点
非流式响应通常包含:
id/object/created/modelchoices[].message.role/contentusage.prompt_tokens/completion_tokens/total_tokens
SDK
python
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.hoyoai.cn/v1")
print(
client.chat.completions.create(
model="claude-sonnet-4-6",
messages=[{"role": "user", "content": "Hello"}],
)
.choices[0]
.message.content
)