完整的API参考文档和代码示例
https://api.aimodels.com/v1
所有API请求都需要在请求头中包含您的API密钥:
Authorization: Bearer YOUR_API_KEY
您可以在控制台获取您的API密钥。
API支持JSON格式的请求。请求头应包含:
Content-Type: application/json
Authorization: Bearer YOUR_API_KEY
所有API响应均为JSON格式,包含以下字段:
{
"success": true,
"data": { ... },
"message": "操作成功",
"timestamp": "2024-01-15T10:30:00Z"
}
端点:POST /chat/completions
描述:使用ChatGPT-4 Turbo模型生成对话响应
curl -X POST https://api.aimodels.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4-turbo",
"messages": [
{
"role": "user",
"content": "你好,请介绍一下人工智能。"
}
],
"temperature": 0.7,
"max_tokens": 1000
}'
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model |
string | 是 | 模型ID,使用 "gpt-4-turbo" |
messages |
array | 是 | 对话消息列表 |
temperature |
float | 否 | 控制随机性,范围0-2,默认0.7 |
max_tokens |
integer | 否 | 最大生成token数,默认2048 |
{
"success": true,
"data": {
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "gpt-4-turbo",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "人工智能(AI)是计算机科学的一个分支..."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 50,
"total_tokens": 60
}
}
}
端点:POST /code/completions
描述:代码生成和补全
curl -X POST https://api.aimodels.com/v1/code/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "codellama-70b",
"prompt": "def fibonacci(n):",
"language": "python",
"max_tokens": 200
}'
端点:POST /images/generations
描述:根据文本描述生成高质量图像
curl -X POST https://api.aimodels.com/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "seeddream-4.0",
"prompt": "a beautiful sunset over the ocean, digital art",
"n": 1,
"size": "1024x1024",
"style": "digital-art"
}'
model (必填): 模型ID,使用 "seeddream-4.0"prompt (必填): 图像描述文本n (可选): 生成图像数量,默认1,最大4size (可选): 图像尺寸,支持 "512x512", "1024x1024", "2048x2048"style (可选): 艺术风格,如 "digital-art", "photo-realistic", "anime"{
"success": true,
"data": {
"images": [
{
"url": "https://cdn.aimodels.com/images/xxx.jpg",
"width": 1024,
"height": 1024
}
],
"usage": {
"images": 1,
"cost": 0.0008
}
}
}
端点:POST /audio/generations
描述:根据文本描述生成音频
curl -X POST https://api.aimodels.com/v1/audio/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "audiogen",
"prompt": "gentle rain and thunder in the background",
"duration": 30,
"format": "mp3"
}'
端点:POST /video/generations
描述:根据文本或图像生成视频
curl -X POST https://api.aimodels.com/v1/video/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "videogen-pro",
"prompt": "a cat playing with a ball of yarn",
"duration": 10,
"resolution": "1920x1080"
}'
pip install aimodels-sdk
from aimodels import AIModels
# 初始化客户端
client = AIModels(api_key="YOUR_API_KEY")
# 文本生成
response = client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{"role": "user", "content": "你好"}
]
)
print(response.data.choices[0].message.content)
# 图像生成
image = client.images.create(
model="seeddream-4.0",
prompt="a beautiful landscape"
)
print(image.data.images[0].url)
npm install aimodels-sdk
const AIModels = require('aimodels-sdk');
const client = new AIModels({
apiKey: 'YOUR_API_KEY'
});
// 文本生成
async function generateText() {
const response = await client.chat.completions.create({
model: 'gpt-4-turbo',
messages: [
{ role: 'user', content: '你好' }
]
});
console.log(response.data.choices[0].message.content);
}
generateText();
| 状态码 | 说明 |
|---|---|
200 |
请求成功 |
400 |
请求参数错误 |
401 |
认证失败,API密钥无效 |
403 |
权限不足 |
429 |
请求频率超出限制 |
500 |
服务器内部错误 |
{
"success": false,
"error": {
"code": "invalid_api_key",
"message": "API密钥无效或已过期",
"type": "authentication_error"
},
"timestamp": "2024-01-15T10:30:00Z"
}
为了确保服务的稳定性和公平性,我们对API请求实施了速率限制:
当超出速率限制时,API会返回429状态码。响应头中包含以下信息:
X-RateLimit-Limit: 限制数量X-RateLimit-Remaining: 剩余请求数X-RateLimit-Reset: 重置时间(Unix时间戳)