2023-04-22 12:39:27 +00:00
|
|
|
|
<p align="center">
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api"><img src="https://raw.githubusercontent.com/songquanpeng/one-api/main/web/public/logo.png" width="150" height="150" alt="one-api logo"></a>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div align="center">
|
|
|
|
|
|
2023-04-22 14:29:00 +00:00
|
|
|
|
# One API
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
2023-04-22 14:29:00 +00:00
|
|
|
|
_✨ All in one 的 OpenAI 接口,整合各种 API 访问方式,开箱即用✨_
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<p align="center">
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://raw.githubusercontent.com/songquanpeng/one-api/main/LICENSE">
|
|
|
|
|
<img src="https://img.shields.io/github/license/songquanpeng/one-api?color=brightgreen" alt="license">
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</a>
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
|
|
|
|
<img src="https://img.shields.io/github/v/release/songquanpeng/one-api?color=brightgreen&include_prereleases" alt="release">
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</a>
|
2023-04-24 01:58:24 +00:00
|
|
|
|
<a href="https://hub.docker.com/repository/docker/justsong/one-api">
|
|
|
|
|
<img src="https://img.shields.io/docker/pulls/justsong/one-api?color=brightgreen" alt="docker pull">
|
|
|
|
|
</a>
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api/releases/latest">
|
|
|
|
|
<img src="https://img.shields.io/github/downloads/songquanpeng/one-api/total?color=brightgreen&include_prereleases" alt="release">
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</a>
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://goreportcard.com/report/github.com/songquanpeng/one-api">
|
|
|
|
|
<img src="https://goreportcard.com/badge/github.com/songquanpeng/one-api" alt="GoReportCard">
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</a>
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p align="center">
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api/releases">程序下载</a>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
·
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api#部署">部署教程</a>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
·
|
2023-04-22 13:14:09 +00:00
|
|
|
|
<a href="https://github.com/songquanpeng/one-api/issues">意见反馈</a>
|
2023-04-23 11:52:16 +00:00
|
|
|
|
·
|
|
|
|
|
<a href="https://github.com/songquanpeng/one-api#截图展示">截图展示</a>
|
|
|
|
|
·
|
|
|
|
|
<a href="https://openai.justsong.cn/">在线演示</a>
|
2023-04-22 12:39:27 +00:00
|
|
|
|
</p>
|
|
|
|
|
|
2023-05-16 03:36:00 +00:00
|
|
|
|
> **Warning**:从 `v0.2` 版本升级到 `v0.3` 版本需要手动迁移数据库,请手动执行[数据库迁移脚本](./bin/migration_v0.2-v0.3.sql)。
|
|
|
|
|
|
|
|
|
|
|
2023-04-22 12:39:27 +00:00
|
|
|
|
## 功能
|
2023-04-22 14:46:29 +00:00
|
|
|
|
1. 支持多种 API 访问渠道,欢迎 PR 或提 issue 添加更多渠道:
|
2023-05-13 03:48:37 +00:00
|
|
|
|
+ [x] OpenAI 官方通道
|
2023-05-14 11:37:15 +00:00
|
|
|
|
+ [x] **Azure OpenAI API**
|
2023-05-14 07:34:14 +00:00
|
|
|
|
+ [x] [API2D](https://api2d.com/r/197971)
|
2023-05-17 15:26:30 +00:00
|
|
|
|
+ [x] [OhMyGPT](https://aigptx.top?aff=uFpUl2Kf)
|
2023-04-23 11:28:43 +00:00
|
|
|
|
+ [x] [CloseAI](https://console.openai-asia.com)
|
|
|
|
|
+ [x] [OpenAI-SB](https://openai-sb.com)
|
|
|
|
|
+ [x] [OpenAI Max](https://openaimax.com)
|
2023-05-05 13:56:48 +00:00
|
|
|
|
+ [x] 自定义渠道:例如使用自行搭建的 OpenAI 代理
|
2023-05-14 11:37:15 +00:00
|
|
|
|
2. 支持通过**负载均衡**的方式访问多个渠道。
|
|
|
|
|
3. 支持 **stream 模式**,可以通过流式传输实现打字机效果。
|
2023-05-18 04:48:20 +00:00
|
|
|
|
4. 支持**多机部署**,[详见此处](#多机部署)。
|
|
|
|
|
5. 支持**令牌管理**,设置令牌的过期时间和使用次数。
|
|
|
|
|
6. 支持**兑换码管理**,支持批量生成和导出兑换码,可使用兑换码为令牌进行充值。
|
|
|
|
|
7. 支持**通道管理**,批量创建通道。
|
|
|
|
|
8. 支持发布公告,设置充值链接,设置新用户初始额度。
|
|
|
|
|
9. 支持丰富的**自定义**设置,
|
2023-05-14 11:37:15 +00:00
|
|
|
|
1. 支持自定义系统名称,logo 以及页脚。
|
|
|
|
|
2. 支持自定义首页和关于页面,可以选择使用 HTML & Markdown 代码进行自定义,或者使用一个单独的网页通过 iframe 嵌入。
|
2023-05-18 04:48:20 +00:00
|
|
|
|
10. 支持通过系统访问令牌访问管理 API。
|
|
|
|
|
11. 支持用户管理,支持**多种用户登录注册方式**:
|
2023-04-27 08:32:21 +00:00
|
|
|
|
+ 邮箱登录注册以及通过邮箱进行密码重置。
|
|
|
|
|
+ [GitHub 开放授权](https://github.com/settings/applications/new)。
|
|
|
|
|
+ 微信公众号授权(需要额外部署 [WeChat Server](https://github.com/songquanpeng/wechat-server))。
|
2023-05-18 04:48:20 +00:00
|
|
|
|
12. 未来其他大模型开放 API 后,将第一时间支持,并将其封装成同样的 API 访问方式。
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
## 部署
|
2023-04-22 14:29:00 +00:00
|
|
|
|
### 基于 Docker 进行部署
|
2023-04-25 06:31:02 +00:00
|
|
|
|
执行:`docker run -d --restart always -p 3000:3000 -v /home/ubuntu/data/one-api:/data justsong/one-api`
|
2023-04-22 14:29:00 +00:00
|
|
|
|
|
2023-04-25 06:50:35 +00:00
|
|
|
|
`-p 3000:3000` 中的第一个 `3000` 是宿主机的端口,可以根据需要进行修改。
|
|
|
|
|
|
|
|
|
|
数据将会保存在宿主机的 `/home/ubuntu/data/one-api` 目录,请确保该目录存在且具有写入权限,或者更改为合适的目录。
|
|
|
|
|
|
|
|
|
|
Nginx 的参考配置:
|
|
|
|
|
```
|
|
|
|
|
server{
|
2023-04-25 06:51:53 +00:00
|
|
|
|
server_name openai.justsong.cn; # 请根据实际情况修改你的域名
|
2023-04-25 06:50:35 +00:00
|
|
|
|
|
|
|
|
|
location / {
|
|
|
|
|
client_max_body_size 64m;
|
|
|
|
|
proxy_http_version 1.1;
|
2023-04-25 06:51:53 +00:00
|
|
|
|
proxy_pass http://localhost:3000; # 请根据实际情况修改你的端口
|
2023-04-25 06:50:35 +00:00
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
|
proxy_set_header X-Forwarded-For $remote_addr;
|
|
|
|
|
proxy_cache_bypass $http_upgrade;
|
|
|
|
|
proxy_set_header Accept-Encoding gzip;
|
2023-04-26 01:57:46 +00:00
|
|
|
|
proxy_buffering off; # 重要:关闭代理缓冲
|
2023-04-25 06:50:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-26 04:58:06 +00:00
|
|
|
|
注意,为了 SSE 正常工作,需要关闭 Nginx 的代理缓冲。
|
2023-04-26 01:57:46 +00:00
|
|
|
|
|
2023-04-25 06:56:04 +00:00
|
|
|
|
之后使用 Let's Encrypt 的 certbot 配置 HTTPS:
|
|
|
|
|
```bash
|
|
|
|
|
# Ubuntu 安装 certbot:
|
|
|
|
|
sudo snap install --classic certbot
|
|
|
|
|
sudo ln -s /snap/bin/certbot /usr/bin/certbot
|
|
|
|
|
# 生成证书 & 修改 Nginx 配置
|
|
|
|
|
sudo certbot --nginx
|
|
|
|
|
# 根据指示进行操作
|
|
|
|
|
# 重启 Nginx
|
|
|
|
|
sudo service nginx restart
|
|
|
|
|
```
|
2023-04-22 14:29:00 +00:00
|
|
|
|
|
2023-04-22 12:39:27 +00:00
|
|
|
|
### 手动部署
|
2023-04-22 13:14:09 +00:00
|
|
|
|
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
|
2023-04-22 12:39:27 +00:00
|
|
|
|
```shell
|
2023-04-22 13:14:09 +00:00
|
|
|
|
git clone https://github.com/songquanpeng/one-api.git
|
2023-04-24 12:55:22 +00:00
|
|
|
|
|
|
|
|
|
# 构建前端
|
|
|
|
|
cd one-api/web
|
|
|
|
|
npm install
|
|
|
|
|
npm run build
|
|
|
|
|
|
|
|
|
|
# 构建后端
|
|
|
|
|
cd ..
|
2023-04-22 12:39:27 +00:00
|
|
|
|
go mod download
|
2023-04-22 13:14:09 +00:00
|
|
|
|
go build -ldflags "-s -w" -o one-api
|
2023-04-22 12:39:27 +00:00
|
|
|
|
````
|
|
|
|
|
2. 运行:
|
|
|
|
|
```shell
|
2023-04-22 13:14:09 +00:00
|
|
|
|
chmod u+x one-api
|
|
|
|
|
./one-api --port 3000 --log-dir ./logs
|
2023-04-22 12:39:27 +00:00
|
|
|
|
```
|
|
|
|
|
3. 访问 [http://localhost:3000/](http://localhost:3000/) 并登录。初始账号用户名为 `root`,密码为 `123456`。
|
|
|
|
|
|
|
|
|
|
更加详细的部署教程[参见此处](https://iamazing.cn/page/how-to-deploy-a-website)。
|
|
|
|
|
|
2023-05-18 04:48:20 +00:00
|
|
|
|
### 多机部署
|
|
|
|
|
1. 所有服务器 `SESSION_SECRET` 设置一样的值。
|
2023-05-18 04:50:19 +00:00
|
|
|
|
2. 必须设置 `SQL_DSN`,使用 MySQL 数据库而非 SQLite,请自行配置主备数据库同步。
|
2023-05-18 04:48:20 +00:00
|
|
|
|
3. 所有从服务器必须设置 `SYNC_FREQUENCY`,以定期从数据库同步配置。
|
2023-05-18 04:50:47 +00:00
|
|
|
|
4. 从服务器可以选择设置 `FRONTEND_BASE_URL`,以重定向页面请求到主服务器。
|
2023-05-18 04:48:20 +00:00
|
|
|
|
|
|
|
|
|
环境变量的具体使用方法详见[此处](#环境变量)。
|
|
|
|
|
|
2023-04-22 12:39:27 +00:00
|
|
|
|
## 配置
|
|
|
|
|
系统本身开箱即用。
|
|
|
|
|
|
|
|
|
|
你可以通过设置环境变量或者命令行参数进行配置。
|
|
|
|
|
|
|
|
|
|
等到系统启动后,使用 `root` 用户登录系统并做进一步的配置。
|
|
|
|
|
|
2023-04-23 12:09:07 +00:00
|
|
|
|
## 使用方式
|
|
|
|
|
在`渠道`页面中添加你的 API Key,之后在`令牌`页面中新增一个访问令牌。
|
|
|
|
|
|
|
|
|
|
之后就可以使用你的令牌访问 One API 了,使用方式与 [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) 一致。
|
|
|
|
|
|
|
|
|
|
可以通过在令牌后面添加渠道 ID 的方式指定使用哪一个渠道处理本次请求,例如:`Authorization: Bearer ONE_API_KEY-CHANNEL_ID`。
|
2023-04-26 06:49:27 +00:00
|
|
|
|
注意,需要是管理员用户创建的令牌才能指定渠道 ID。
|
2023-04-23 12:09:07 +00:00
|
|
|
|
|
|
|
|
|
不加的话将会使用负载均衡的方式使用多个渠道。
|
|
|
|
|
|
2023-04-22 12:39:27 +00:00
|
|
|
|
### 环境变量
|
|
|
|
|
1. `REDIS_CONN_STRING`:设置之后将使用 Redis 作为请求频率限制的存储,而非使用内存存储。
|
|
|
|
|
+ 例子:`REDIS_CONN_STRING=redis://default:redispw@localhost:49153`
|
|
|
|
|
2. `SESSION_SECRET`:设置之后将使用固定的会话密钥,这样系统重新启动后已登录用户的 cookie 将依旧有效。
|
|
|
|
|
+ 例子:`SESSION_SECRET=random_string`
|
|
|
|
|
3. `SQL_DSN`:设置之后将使用指定数据库而非 SQLite。
|
2023-04-22 13:14:09 +00:00
|
|
|
|
+ 例子:`SQL_DSN=root:123456@tcp(localhost:3306)/one-api`
|
2023-05-18 04:26:18 +00:00
|
|
|
|
4. `FRONTEND_BASE_URL`:设置之后将使用指定的前端地址,而非后端地址。
|
|
|
|
|
+ 例子:`FRONTEND_BASE_URL=https://openai.justsong.cn`
|
2023-05-18 04:48:20 +00:00
|
|
|
|
5. `SYNC_FREQUENCY`:设置之后将定期与数据库同步配置,单位为秒,未设置则不进行同步。
|
|
|
|
|
+ 例子:`SYNC_FREQUENCY=60`
|
2023-04-22 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
### 命令行参数
|
|
|
|
|
1. `--port <port_number>`: 指定服务器监听的端口号,默认为 `3000`。
|
|
|
|
|
+ 例子:`--port 3000`
|
|
|
|
|
2. `--log-dir <log_dir>`: 指定日志文件夹,如果没有设置,日志将不会被保存。
|
|
|
|
|
+ 例子:`--log-dir ./logs`
|
2023-04-23 11:52:16 +00:00
|
|
|
|
3. `--version`: 打印系统版本号并退出。
|
2023-04-25 08:23:17 +00:00
|
|
|
|
4. `--help`: 查看命令的使用帮助和参数说明。
|
2023-04-23 11:52:16 +00:00
|
|
|
|
|
|
|
|
|
## 演示
|
|
|
|
|
### 在线演示
|
|
|
|
|
注意,该演示站不提供对外服务:
|
|
|
|
|
https://openai.justsong.cn
|
|
|
|
|
|
|
|
|
|
### 截图展示
|
|
|
|
|

|
|
|
|
|

|