返回首頁

API文檔

完整的API參考文檔和代碼示例

快速開始

基礎URL

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"
}

文本生成API

ChatGPT-4 Turbo

端點: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
    }
  }
}

CodeLlama 70B

端點: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
  }'

圖像生成API

SeedDream4.0

端點: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,最大4
  • size (可選): 圖像尺寸,支持 "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
    }
  }
}

音頻生成API

AudioGen

端點: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"
  }'

視頻生成API

VideoGen Pro

端點: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"
  }'

SDK和代碼示例

Python SDK

安裝

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)

JavaScript/Node.js SDK

安裝

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();

錯誤處理

HTTP狀態碼

狀態碼 說明
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請求實施了速率限制:

  • 免費賬戶:每分鐘100次請求
  • 開發者賬戶:每分鐘1,000次請求
  • 企業賬戶:自定義限制

當超出速率限制時,API會返回429狀態碼。響應頭中包含以下信息:

  • X-RateLimit-Limit: 限制數量
  • X-RateLimit-Remaining: 剩餘請求數
  • X-RateLimit-Reset: 重置時間(Unix時間戳)

需要更多幫助?

查看我們的幫助中心或聯繫技術支持

幫助中心 聯繫我們