跳转到主要内容

简介

AI Chat 是 Ace Data Cloud 提供的AI 聊天服务,多模型 AI 对话服务。通过 Ace Data Cloud 统一 API,你可以使用 Python 快速集成 AI Chat,实现多模型支持、有状态对话、预设角色等功能。

前置条件

  • 拥有 Ace Data Cloud 账号并获取 API Token
  • Python 3.7 及以上环境
  • 安装 requests 库:pip install requests

基础用法

调用 AI Chat API 的主端点为:
POST https://api.acedata.cloud/aichat/conversations
本示例使用 gpt-4o 模型。 可用模型包括:gpt-4ogpt-4deepseek-r1grok-3 完整 Python 代码示例:
import requests

url = "https://api.acedata.cloud/aichat/conversations"
headers = {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json"
}
data = {
    "model": "gpt-4o",
    "question": "什么是人工智能?",
    "stateful": true
}

response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result)
请将 YOUR_API_TOKEN 替换为你在 Ace Data Cloud 平台获取的实际 Token。

响应处理

成功调用后,API 返回 JSON 格式数据。建议检查 HTTP 状态码:
if response.status_code == 200:
    result = response.json()
    print("调用成功:", result)
else:
    print(f"调用失败,状态码: {response.status_code}")
    print(response.text)

进阶用法

对于支持异步任务的 API,可以通过回调 URL 获取结果:
data['callback_url'] = 'https://your-server.com/callback'
response = requests.post(url, headers=headers, json=data)
# 结果将通过回调 URL 推送到你的服务器

错误处理

常见错误码:
状态码说明
401认证失败,请检查 API Token
403余额不足或无权访问
429请求频率超限
500服务器内部错误

下一步