聊天(Chat)
聊天接口用于与 AI 模型进行对话交互。
接口地址
POST https://ai-tokenhub.com/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 | 是 | - | 模型标识符,格式为 provider/model (例如:openai/gpt-4)示例: "openai/gpt-4" |
| messages | object[] | 是 | - | 对话消息数组 |
| fallbacks | string[] | 否 | - | 故障转移模型列表,格式为 provider/model |
| 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": "你是一个专业的助手"
},
{
"role": "user",
"content": "请介绍一下自己"
}
]
}Role 类型
system: 系统角色,用于设定 AI 行为user: 用户角色,用户的输入assistant: 助手角色,AI 的回复
请求示例
bash
curl https://ai-tokenhub.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4o",
"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.com/v1"
)
response = client.chat.completions.create(
model="gpt-4o",
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.com/v1'
});
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'Hello, how are you?' }
],
temperature: 0.7,
max_tokens: 100
});
console.log(response.choices[0].message.content);