API 文档
OpenAI 兼容的 AI 接口,快速集成到您的应用
快速开始
1. 获取 API Key
登录后进入仪表板,创建您的 API Key。
前往创建 API Key →2. API 端点
https://www.sozeer.com/api/v1
3. 发起请求
curl https://www.sozeer.com/api/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer YOUR_API_KEY" \\
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "你好!"}],
"stream": false
}'
支持的接口
Chat Completions
/v1/chat/completions
对话式 AI 接口,支持流式响应
Embeddings
/v1/embeddings
文本嵌入,用于语义搜索和分类
Images
/v1/images/generations
文本生成图像
聊天接口
接口地址
POST https://www.sozeer.com/api/v1/chat/completions
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 是 | 模型名称 |
| messages | array | 是 | 对话历史 |
| stream | boolean | 否 | 是否流式响应,默认 false |
| temperature | number | 否 | 采样温度 (0-2) |
| max_tokens | integer | 否 | 最大输出 token 数 |
| top_p | number | 否 | 核采样 (0-1) |
请求示例
{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"temperature": 0.7,
"max_tokens": 500,
"stream": false
}
响应示例
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-3.5-turbo",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 8,
"total_tokens": 18
}
}
代码示例
Python
import requests
url = "https://www.sozeer.com/api/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
}
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "你好!"}],
"stream": True # 流式响应
}
response = requests.post(url, json=data, headers=headers, stream=True)
for line in response.iter_lines():
if line:
decoded = line.decode('utf-8')
if decoded.startswith('data: '):
data_str = decoded[6:]
if data_str != '[DONE]':
import json
chunk = json.loads(data_str)
content = chunk['choices'][0]['delta'].get('content', '')
print(content, end='', flush=True)
Node.js
const fetch = require('node-fetch');
const url = "https://www.sozeer.com/api/v1/chat/completions";
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_API_KEY"
};
const data = {
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "你好!" }],
stream: true
};
async function chat() {
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(data),
stream: true
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
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 dataStr = line.slice(6);
if (dataStr !== '[DONE]') {
const parsed = JSON.parse(dataStr);
const content = parsed.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
}
}
}
}
chat();
Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type Request struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
}
func main() {
url := "https://www.sozeer.com/api/v1/chat/completions"
reqBody := Request{
Model: "gpt-3.5-turbo",
Messages: []Message{
{Role: "user", Content: "你好!"},
},
Stream: true,
}
body, _ := json.Marshal(reqBody)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
reader := io.NewSectionReader(resp.Body, 0, 1e9)
buf := make([]byte, 1024)
for {
n, err := reader.Read(buf)
if n > 0 {
fmt.Print(string(buf[:n]))
}
if err == io.EOF {
break
}
}
}
cURL
curl https://www.sozeer.com/api/v1/chat/completions \\
-H "Content-Type: application/json" \\
-H "Authorization: Bearer YOUR_API_KEY" \\
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}],
"stream": false
}'
SDK 下载
我们推荐以下 SDK 快速集成:
在线测试
直接在浏览器中测试 API,无需编写代码。请在下方输入您的 API Key。
错误码
| 状态码 | 错误码 | 说明 | 解决方案 |
|---|---|---|---|
| 401 | invalid_api_key | API Key 无效 | 检查 API Key 是否正确 |
| 401 | key_expired | API Key 已过期 | 创建新的 API Key |
| 402 | insufficient_balance | 余额不足 | 前往仪表板充值 |
| 403 | key_disabled | API Key 已禁用 | 在仪表板重新启用 |
| 429 | rate_limit | 请求频率超限 | 降低请求频率或联系管理员 |
| 422 | model_not_found | 模型不可用 | 检查模型名称是否正确 |
| 500 | server_error | 服务器错误 | 请稍后重试或联系客服 |
错误响应格式
{
"error": {
"message": "Insufficient balance",
"type": "insufficient_balance",
"code": "insufficient_balance"
}
}