返回首页

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时间戳)

需要更多帮助?

查看我们的帮助中心或联系技术支持

帮助中心 联系我们