青城数智
AI模型市场
模型中心
帮助中心
登录后可使用更多功能
立即登录
API文档
用户

青城数智 AI模型市场API 集成文档

简单、易用、稳定的AI模型API接口

快速开始

1. 获取API密钥

访问 API密钥管理页面 创建您的专属API密钥。

2. 发送请求

使用以下接口发送AI对话请求:

POST http://localhost:9090/v1/chat/completions cURL
curl -X POST "http://localhost:9090/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -H "X-API-Key: your-api-key-here" \
  -H "X-AI-Provider: deepseek" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {
        "role": "user",
        "content": "你好,请介绍一下青城数智平台"
      }
    ],
    "temperature": 0.7,
    "max_tokens": 1000,
    "stream": true
  }'

请求参数

参数 类型 必填 说明
model string 使用的模型名称
messages array 对话消息列表
temperature number 控制回答的随机性,范围0-2,默认0.7
max_tokens number 最大输出Token数量
stream boolean 是否启用流式响应,默认false

请求头

请求头 必填 说明
Content-Type application/json
X-API-Key 您的API密钥
X-AI-Provider AI提供商:deepseek

响应示例

JSON 响应 Response
{
  "id": "chatcmpl-123456",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "deepseek-chat",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!青城数智是一个先进的AI模型平台..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 20,
    "total_tokens": 30
  }
}

错误代码

状态码 错误类型 说明
400 Bad Request 请求参数错误
401 Unauthorized API密钥无效或缺失
403 Forbidden 账户余额不足
429 Too Many Requests 请求频率超限
500 Internal Server Error 服务器内部错误

SDK 示例

Python

python_example.py Python
import requests
import json

url = "http://localhost:9090/v1/chat/completions"
headers = {
    "Content-Type": "application/json",
    "X-API-Key": "your-api-key-here",
    "X-AI-Provider": "deepseek"
}

data = {
    "model": "deepseek-chat",
    "messages": [
        {
            "role": "user",
            "content": "你好,请介绍一下青城数智平台"
        }
    ],
    "temperature": 0.7
}

response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result)

JavaScript

javascript_example.js JavaScript
const response = await fetch('http://localhost:9090/v1/chat/completions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-Key': 'your-api-key-here',
    'X-AI-Provider': 'deepseek'
  },
  body: JSON.stringify({
    model: 'deepseek-chat',
    messages: [
      {
        role: 'user',
        content: '你好,请介绍一下青城数智平台'
      }
    ],
    temperature: 0.7
  })
});

const result = await response.json();
console.log(result);

流式响应处理(JavaScript)

streaming_example.js JavaScript Stream
async function streamChat(message) {
  const response = await fetch('http://localhost:9090/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-API-Key': 'your-api-key-here',
      'X-AI-Provider': 'deepseek'
    },
    body: JSON.stringify({
      model: 'deepseek-chat',
      messages: [{ role: 'user', content: message }],
      stream: true,
      temperature: 0.7
    })
  });

  const reader = response.body.getReader();
  const decoder = new TextDecoder();

  try {
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      
      const chunk = decoder.decode(value);
      const lines = chunk.split('\n');
      
      for (const line of lines) {
        if (line.startsWith('data: ')) {
          const data = line.slice(6);
          if (data === '[DONE]') return;
          
          try {
            const parsed = JSON.parse(data);
            const content = parsed.choices[0]?.delta?.content;
            if (content) {
              console.log(content); // 实时输出内容
            }
          } catch (e) {
            // 忽略解析错误
          }
        }
      }
    }
  } finally {
    reader.releaseLock();
  }
}

// 使用示例
streamChat('你好,请介绍一下青城数智平台');

最佳实践

性能优化

使用流式响应获得更好的用户体验
合理设置max_tokens避免不必要的消耗
复用连接以减少建连开销

安全建议

定期轮换API密钥
不要在客户端暴露API密钥
使用HTTPS确保传输安全