跳到主要内容

对话生成(Chat Completions)

用于多轮对话和文本生成,是推荐使用的主入口。

接口地址

POST /v1/chat/completions

请求参数

参数类型必填说明
modelstring模型名称(在控制台查看)
messagesarray对话上下文,包含 role 与 content
temperaturenumber随机性,数值越大越发散
max_tokensnumber最大输出长度
streamboolean是否使用流式输出

请求示例

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 可用于统计成本。