完整的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時間戳)