聊天(Chat)
聊天接口用于与 AI 模型进行对话交互。
接口地址
POST https://ai-tokenhub.ai/api/v1/chat/completions授权 (Authorizations)
BearerAuth
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
| Authorization | string | 是 | Bearer token 认证。使用你的API密钥或平台认证token,虚拟密钥(以 sk-bf- 为前缀)也可以在此处传递。格式为:Bearer $YOUR_API_KEY |
| Content-Type | string | 是 | application/json |
请求体
application/json
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| model | string | 是 | - | 模型标识符,填写平台支持的模型 ID 示例: "dola-seed-2-1-turbo-260628" |
| messages | object[] | 是 | - | 对话消息数组 |
| fallbacks | string[] | 否 | - | 故障转移模型列表,填写模型 ID |
| stream | boolean | 否 | false | 是否启用流式响应 |
| temperature | number | 否 | 0.7 | 生成温度,取值范围:-2 <= x <= 2 |
| frequency_penalty | number | 否 | 0 | 频率惩罚,取值范围:-2 <= x <= 2 |
| presence_penalty | number | 否 | 0 | 存在惩罚,取值范围:-2 <= x <= 2 |
| max_tokens | integer | 否 | - | 最大生成 token 数 |
| max_completion_tokens | integer | 否 | - | 最大补全 token 数 |
| logit_bias | object | 否 | - | 词表偏置设置 |
| logprobs | boolean | 否 | false | 是否返回 token 概率 |
| metadata | object | 否 | - | 用户自定义元数据 |
| modalities | string[] | 否 | ["text"] | 响应模态类型 |
| parallel_tool_calls | boolean | 否 | true | 是否启用并行工具调用 |
| prompt_cache_key | string | 否 | - | 语义缓存自定义键 |
| stop | array/string | 否 | - | 停止词 |
| user | string | 否 | - | 用户标识 |
Messages 格式
json
{
"messages": [
{
"role": "system",
"content": "You are a professional assistant"
},
{
"role": "user",
"content": "Please introduce yourself"
}
]
}Role 类型
system: 系统角色,用于设定 AI 行为user: 用户角色,用户的输入assistant: 助手角色,AI 的回复
请求示例
bash
curl https://ai-tokenhub.ai/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d '{
"model": "dola-seed-2-1-turbo-260628",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
],
"temperature": 0.7,
"max_tokens": 100
}'python
from openai import OpenAI
client = OpenAI(
api_key="$YOUR_API_KEY",
base_url="https://ai-tokenhub.ai/api/v1"
)
response = client.chat.completions.create(
model="dola-seed-2-1-turbo-260628",
messages=[
{"role": "user", "content": "Hello, how are you?"}
],
temperature=0.7,
max_tokens=100
)
print(response.choices[0].message.content)javascript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: '$YOUR_API_KEY',
baseURL: 'https://ai-tokenhub.ai/api/v1'
});
const response = await client.chat.completions.create({
model: 'dola-seed-2-1-turbo-260628',
messages: [
{ role: 'user', content: 'Hello, how are you?' }
],
temperature: 0.7,
max_tokens: 100
});
console.log(response.choices[0].message.content);响应示例
json
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "dola-seed-2-1-turbo-260628",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! I'm doing well, thank you for asking."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}流式响应
将 stream 设为 true 即可启用 Server-Sent Events 格式的流式响应。
错误码
| 错误码 | 说明 |
|---|---|
| 400 | 请求参数无效 |
| 401 | API Key 无效 |
| 403 | 无权访问该模型 |
| 429 | 超出速率限制 |
| 500 | 内部服务错误 |
| 503 | 服务不可用 |
应用场景
- 智能客服:自动回复用户咨询
- 内容创作:写作辅助
- 代码助手:编程辅助
- 教育辅导:答疑与反馈
