本站API完全兼容OpenAI格式,使用任何支持OpenAI的SDK或工具即可无缝接入。
只需三步即可开始使用全球顶尖AI模型:
免费注册并充值余额
在控制台创建 API Key
替换 Base URL 即可使用
所有API请求都需要在HTTP请求头中携带API Key进行身份认证。使用 Authorization 头,格式为 Bearer Token:
Authorization: Bearer sk-relay-your-api-key
API Key 可以在控制台中创建和管理。请妥善保管你的 API Key,不要在公开代码中暴露。
POST /v1/chat/completions
创建一个聊天补全请求。这是最常用的接口,支持所有可用的AI模型。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model |
string | 是 | 模型名称,如 deepseek-chat、gpt-4o |
messages |
array | 是 | 消息数组,每条消息包含 role 和 content |
stream |
boolean | 否 | 是否使用流式输出,默认 false |
temperature |
number | 否 | 温度参数,取值范围 0-2,默认 1 |
max_tokens |
integer | 否 | 最大输出 token 数量 |
curl /v1/chat/completions \ -H "Authorization: Bearer sk-relay-your-key" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-chat", "messages": [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ], "temperature": 0.7 }'
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1700000000,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 9,
"total_tokens": 29
}
}
GET /v1/models
获取所有可用模型的列表。此接口不需要认证。
{
"object": "list",
"data": [
{
"id": "deepseek-chat",
"object": "model",
"created": 1700000000,
"owned_by": "deepseek"
},
{
"id": "gpt-4o",
"object": "model",
"created": 1700000000,
"owned_by": "openai"
}
]
}
设置 stream: true 即可启用流式输出。服务器将以 Server-Sent Events (SSE) 格式逐步返回生成内容。
每个事件的格式如下:
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":"Hello"},"index":0}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"delta":{"content":" world"},"index":0}]}
data: [DONE]
from openai import OpenAI client = OpenAI( api_key="sk-relay-your-key", base_url="/v1" ) stream = client.chat.completions.create( model="deepseek-chat", messages=[{"role": "user", "content": "Hello!"}], stream=True ) for chunk in stream: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")
当请求发生错误时,API会返回对应的HTTP状态码和错误信息:
| 状态码 | 说明 | 建议处理 |
|---|---|---|
400 |
请求参数错误 | 检查请求参数格式 |
401 |
API Key 无效 | 检查 API Key 是否正确 |
402 |
余额不足 | 前往控制台充值 |
429 |
请求过于频繁 | 降低请求频率,稍后重试 |
502 |
上游服务错误 | 重试请求或联系支持 |
503 |
无可用渠道 | 该模型暂时不可用,稍后重试 |
错误响应格式:
{
"error": {
"message": "API Key无效或已过期",
"type": "authentication_error"
}
}
以下是常见语言和工具的接入示例。只需将 base_url 替换为本站地址即可。
from openai import OpenAI client = OpenAI( api_key="sk-relay-your-key", base_url="/v1" ) response = client.chat.completions.create( model="deepseek-chat", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"} ] ) print(response.choices[0].message.content)
import OpenAI from 'openai'; const client = new OpenAI({ apiKey: 'sk-relay-your-key', baseURL: '/v1', }); const response = await client.chat.completions.create({ model: 'deepseek-chat', messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: 'Hello!' }, ], }); console.log(response.choices[0].message.content);
curl /v1/chat/completions \ -H "Authorization: Bearer sk-relay-your-key" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-chat", "messages": [ {"role": "user", "content": "Hello!"} ] }'