Appearance
快速开始
注册与登录
- 访问平台首页,点击「注册」
- 填写邮箱和密码,完成注册
- 也可使用第三方登录:GitHub / Discord / LinuxDo / OIDC / 微信
- 注册后建议绑定邮箱验证,以便找回密码
获取 API Key
- 登录后进入「令牌」页面
- 点击「创建令牌」
- 填写配置:
- 名称:自定义令牌名称,便于管理
- 配额:设置令牌可用的最大配额(0 表示无限)
- 过期时间:设置令牌有效期(可选)
- 模型限制:可设置该令牌仅允许调用指定模型
- 创建后复制生成的 API Key(格式:
sk-xxxxxxxxxx) - ⚠️ 请妥善保管 API Key,不要暴露在公开代码或仓库中
第一次调用
Python 示例
python
from openai import OpenAI
client = OpenAI(
api_key="sk-xxxxxxxxxx", # 替换为你的 API Key
base_url="https://你的域名/v1" # 替换为平台 Base URL
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,请介绍一下你自己"}
]
)
print(response.choices[0].message.content)Node.js 示例
javascript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-xxxxxxxxxx',
baseURL: 'https://你的域名/v1',
});
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: '你好,请介绍一下你自己' },
],
});
console.log(response.choices[0].message.content);curl 示例
bash
curl https://你的域名/v1/chat/completions \
-H "Authorization: Bearer sk-xxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "你好,请介绍一下你自己"}
]
}'流式调用示例
python
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "写一首关于春天的诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)Base URL 说明
| 场景 | Base URL |
|---|---|
| 正式环境 | https://你的域名/v1 |
| Playground | https://你的域名/pg(仅 Web 界面) |
💡 所有 API 路径均兼容 OpenAI 规范,只需将
api.openai.com替换为你的平台域名即可,无需修改代码中的路径部分。