Skip to main content

欢迎使用启航 AI API

启航 AI API 提供强大的人工智能能力,兼容 OpenAI 接口标准,支持多种编程语言和开发框架。

Base URL

所有 API 请求的基础 URL:
https://api.qhaigc.net/v1
或者使用香港节点:
https://api-hk.qhaigc.net

认证方式

所有 API 请求都需要在请求头中包含 API Key:
Authorization: Bearer sk-your-api-key-here

查看认证详情

了解如何获取和使用 API Key

API 分类

文本聊天

使用大语言模型进行对话、文本生成、代码编写等任务。

图像生成

语音处理

视频生成

Sora 视频生成

使用 Sora 模型生成视频内容

音乐生成

嵌入与重排序

其他接口

请求格式

所有 API 请求都使用 JSON 格式:
curl https://api.qhaigc.net/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-api-key-here" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "user", "content": "Hello"}
    ]
  }'

响应格式

API 响应也使用 JSON 格式:
{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! How can I help you today?"
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

错误处理

API 使用标准的 HTTP 状态码:
状态码说明
200请求成功
400请求参数错误
401认证失败(API Key 无效)
429请求频率超限
500服务器错误
错误响应示例:
{
  "error": {
    "message": "Invalid API key provided",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}

流式响应

大多数文本生成接口支持流式响应,实时返回生成的内容:
client = openai.OpenAI(
    api_key="sk-your-api-key-here",
    base_url="https://api.qhaigc.net/v1"
)

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "讲个故事"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

速率限制

为保证服务质量,API 设有速率限制:
  • 每分钟请求数(RPM)
  • 每天请求数(RPD)
  • 每分钟 Token 数(TPM)
具体限制根据您的账户等级而定。如需提高限制,请联系客服。

SDK 和工具

官方 SDK

启航 AI API 兼容 OpenAI SDK,可直接使用:
pip install openai

第三方工具

  • LangChain: 支持启航 AI API
  • LlamaIndex: 支持启航 AI API
  • Dify: 可配置启航 AI 作为模型提供商

最佳实践

建议实现指数退避的重试机制:
import time
from openai import OpenAI

def call_api_with_retry(max_retries=3):
    for i in range(max_retries):
        try:
            response = client.chat.completions.create(...)
            return response
        except Exception as e:
            if i == max_retries - 1:
                raise
            time.sleep(2 ** i)  # 指数退避
  • 设置 max_tokens 限制输出长度
  • 使用流式响应及时中断
  • 选择合适的模型(不同模型价格不同)
  • 在控制台设置预算提醒
  • 使用流式响应提升用户体验
  • 合理设置超时时间
  • 考虑使用缓存减少重复请求
  • 批量处理多个请求

需要帮助?