API 使用文档
1. 获取 API Key
前往 API Key 创建页面 创建你的专属 Key。
2. 接口地址
POST https://api.apify.run/v1/chat/completions
3. 请求参数
参数 | 类型 | 说明 |
---|---|---|
model | string | 模型名称(如 gpt-4) |
messages | array | 消息数组,每个消息包含 role 和 content |
temperature | number | 可选,控制随机性,范围 0-2,默认 1 |
max_tokens | number | 可选,最大生成 token 数 |
4. 示例代码
Python
import requests
headers = {
"Authorization": f"Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"model": "gpt-4",
"messages": [
{"role": "user", "content": "你好"}
],
"temperature": 0.7
}
response = requests.post(
"https://api.apify.run/v1/chat/completions",
headers=headers,
json=data
)
print(response.json())
Node.js
const fetch = require('node-fetch');
const response = await fetch('https://api.apify.run/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer YOUR_API_KEY`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-4',
messages: [
{ role: 'user', content: '你好' }
],
temperature: 0.7
})
});
const data = await response.json();
console.log(data);
cURL
curl https://api.apify.run/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "你好"
}
],
"temperature": 0.7
}'
5. 响应格式
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么我可以帮助你的吗?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
6. 常见问题
- API Key 严格保密,丢失请重新生成
- 模型名称请参考模型市场页面
- 所有请求都需要在 header 中携带 API Key
- 响应格式与 OpenAI API 保持一致
- 支持流式输出,添加
stream: true
参数即可