🔧 API 接口文档
51API 完全兼容 OpenAI API 协议,你可以直接使用 OpenAI SDK 或任何兼容 OpenAI 的工具进行调用。
基础信息
| 配置项 | 值 |
|---|---|
| Base URL | https://www.51api.org/v1 |
| 鉴权方式 | Authorization: Bearer <你的API Key> |
| 请求格式 | application/json |
| 协议 | 完全兼容 OpenAI API |
Chat Completions(对话补全)
这是最常用的接口,用于与 AI 模型进行对话。
接口地址
POST https://www.51api.org/v1/chat/completions1
请求头
http
Content-Type: application/json
Authorization: Bearer sk-你的密钥1
2
2
请求参数
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
model | string | ✅ 是 | 模型 ID,如 gpt-4o |
messages | array | ✅ 是 | 对话消息列表 |
stream | boolean | 否 | 是否流式输出,默认 false |
max_tokens | integer | 否 | 最大输出 token 数 |
temperature | number | 否 | 随机性,0-2,默认 1 |
top_p | number | 否 | 核采样,0-1 |
messages 格式
json
[
{"role": "system", "content": "你是一个有帮助的助手"},
{"role": "user", "content": "你好!"},
{"role": "assistant", "content": "你好!有什么可以帮助你的?"},
{"role": "user", "content": "今天天气怎么样?"}
]1
2
3
4
5
6
2
3
4
5
6
| role | 说明 |
|---|---|
system | 系统指令,设定 AI 的行为 |
user | 用户消息 |
assistant | AI 的回复 |
基础调用示例
请求
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "你是一个有帮助的助手。"},
{"role": "user", "content": "你好!"}
]
}'1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
响应
json
{
"id": "chatcmpl-B9MBs8CjcvOU2jLn4n570S5qMJKcT",
"object": "chat.completion",
"created": 1741569952,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮助你的吗?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 19,
"completion_tokens": 10,
"total_tokens": 29
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
响应字段说明
| 字段 | 说明 |
|---|---|
id | 请求唯一标识 |
choices[0].message.content | AI 的回复内容 |
finish_reason | 结束原因:stop=正常结束,length=达到长度限制 |
usage.total_tokens | 总消耗 token 数(用于计费) |
流式输出
添加 "stream": true 参数,实现打字机效果的实时输出。
请求
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "讲个故事"}],
"stream": true
}'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
响应格式
流式响应使用 Server-Sent Events (SSE) 格式:
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"从"}}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"前"}}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"有"}}]}
data: [DONE]1
2
3
4
5
6
7
2
3
4
5
6
7
每个 data: 行包含一个 JSON 对象,choices[0].delta.content 是增量内容。
图像分析(多模态)
支持 GPT-4o、Gemini 等多模态模型分析图片。
使用 URL
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "这张图片里有什么?"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"}
}
]
}
],
"max_tokens": 300
}'1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
使用 Base64
json
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRg..."
}
}1
2
3
4
5
6
2
3
4
5
6
函数调用(Function Calling)
让 AI 调用你定义的函数,实现与外部系统的交互。
请求示例
bash
curl https://www.51api.org/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的密钥" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京今天的天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,如:北京"
}
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
响应示例
当 AI 决定调用函数时:
json
{
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [
{
"id": "call_xxx",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
]
}
}
]
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
错误处理
常见错误码
| HTTP 状态码 | 说明 | 解决方案 |
|---|---|---|
| 400 | 请求格式错误 | 检查 JSON 格式和参数 |
| 401 | 认证失败 | 检查 API Key 是否正确 |
| 403 | 权限不足 | 检查令牌分组是否包含该模型 |
| 404 | 接口不存在 | 检查 URL 是否包含 /v1 |
| 429 | 请求过于频繁 | 降低请求频率 |
| 500 | 服务器错误 | 稍后重试 |
错误响应格式
json
{
"error": {
"message": "Model not found",
"type": "invalid_request_error",
"code": "model_not_found"
}
}1
2
3
4
5
6
7
2
3
4
5
6
7
