对话生成(Chat Completions)
用于多轮对话和文本生成,是推荐使用的主入口。
接口地址
POST /v1/chat/completions
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型名称(在控制台查看) |
| messages | array | 是 | 对话上下文,包含 role 与 content |
| temperature | number | 否 | 随机性,数值越大越发散 |
| max_tokens | number | 否 | 最大输出长度 |
| stream | boolean | 否 | 是否使用流式输出 |
请求示例
curl https://api.anideaai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_NAME",
"messages": [
{"role": "system", "content": "你是一个客服助手。"},
{"role": "user", "content": "请介绍一下你们的计费方式。"}
],
"temperature": 0.2,
"max_tokens": 200
}'
import requests
api_key = "YOUR_API_KEY"
payload = {
"model": "MODEL_NAME",
"messages": [
{"role": "user", "content": "你好"}
],
"temperature": 0.2,
"max_tokens": 100
}
resp = requests.post(
"https://api.anideaai.com/v1/chat/completions",
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
json=payload,
)
print(resp.json())
const apiKey = "YOUR_API_KEY";
const payload = {
model: "MODEL_NAME",
messages: [{ role: "user", content: "你好" }],
temperature: 0.2,
max_tokens: 100,
};
fetch("https://api.anideaai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
.then((res) => res.json())
.then((data) => console.log(data));
响应示例
{
"id": "chatcmpl_xxxxx",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "我们按令牌计费,控制台会显示倍率与明细。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 25,
"completion_tokens": 30,
"total_tokens": 55
}
}
响应中的 usage 可用于统计成本。