Skip to content

Commit

Permalink
feat: add convert log tool && switch chat saver && cancel fk- prefi…
Browse files Browse the repository at this point in the history
…x limit (#34)

* feat: switch log saver

* Update README

* fix: fix except error && add load_chat func

* cancel `fk-` prefix limit

* 🔥 Cancel commit-lint check

* add tool.py

* fix pytest
  • Loading branch information
KenyonY authored May 27, 2023
1 parent 4e1359b commit af3a05f
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 334 deletions.
7 changes: 5 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
LOG_CHAT=True
LOG_CHAT=false
OPENAI_BASE_URL=https://api.openai.com
OPENAI_API_KEY=
FORWARD_KEY=
ROUTE_PREFIX=
ROUTE_PREFIX=

# 设定时区
TZ=Asia/Shanghai
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ OPENAI_API_KEY=sk-xxx1 sk-xxx2 sk-xxx3

# FORWARD_KEY: 当前面的OPENAI_API_KEY被设置,就可以设置这里的FORWARD_KEY,客户端调用时可以使用FORWARD_KEY作为api key
FORWARD_KEY=fk-xxx1

# ROUTE_PREFIX: 可指定整个转发服务的根路由前缀
ROUTE_PREFIX=

# 设定时区
TZ=Asia/Shanghai
11 changes: 1 addition & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,11 @@ on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'README_*.md'
- '*.md'
branches:
- main

jobs:
commit-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v4

check-black:
runs-on: ubuntu-latest
steps:
Expand Down
189 changes: 135 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,67 @@
</a>
</p>

<div align="center">

[功能](#功能) |
[部署指南](#部署指南) |
[应用](#应用) |
[配置选项](#配置选项) |
[聊天日志](#聊天日志)

[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/tejCum?referralCode=U0-kXv)
[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/beidongjiedeguang/openai-forward)

</div>

本项目用于解决一些地区无法直接访问OpenAI的问题,将该服务部署在可以正常访问openai
api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代理服务
api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代理服务

---

由本项目搭建的长期代理地址:
> https://api.openai-forward.com
> https://api.openai-forward.com
## 功能

## 目录

- [功能](#功能)
- [部署指南](#部署指南)
- [应用](#应用)
- [配置选项](#配置选项)
- [聊天日志](#聊天日志)
- [高级配置](#高级配置)
**基础功能**

## 功能
**基础功能**
- [x] 支持转发OpenAI所有接口
- [x] 支持流式响应
- [x] 支持指定转发路由前缀
- [x] docker部署
- [x] pip 安装部署
- [x] cloudflare 部署
- [x] [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fbeidongjiedeguang%2Fopenai-forward&project-name=openai-forward&repository-name=openai-forward&framework=other) ~~Vercel一键部署(不建议)~~
- [x] [![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/tejCum?referralCode=U0-kXv) Railway 一键部署
- [x] [![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/beidongjiedeguang/openai-forward) Render 一键部署
- [x] ~~Vercel一键部署(不建议)~~
- [x] Railway 一键部署
- [x] Render 一键部署

**高级功能**

**高级功能**
- [x] 实时记录聊天记录(包括流式响应的聊天内容)
- [x] 允许输入多个openai api key 组成轮询池
- [x] 自定义 api key (见高级配置)
- [x] 自定义 转发api key ([高级配置](#高级配置))

## 部署指南

提供以下几种部署方式
提供以下几种部署方式

**有海外vps方案**

**有海外vps方案**
1. [pip 安装部署](deploy.md#pip-推荐) (推荐)
2. [Docker部署](deploy.md#docker-推荐) (推荐)
> https://api.openai-forward.com
2. [Docker部署](deploy.md#docker-推荐) (推荐)
> https://api.openai-forward.com
**无vps免费部署方案**

**无vps免费部署方案**
1. [一键Vercel部署](deploy.md#vercel-一键部署) (不推荐)
> ~~https://vercel.openai-forward.com~~
> ~~https://vercel.openai-forward.com~~
2. [cloudflare部署](deploy.md#cloudflare-部署) (推荐)
> https://cloudflare.openai-forward.com
3. [Railway部署](deploy.md#Railway-一键部署)
> https://railway.openai-forward.com
4. [Render一键部署](deploy.md#render-一键部署) (最佳推荐)
> https://render.openai-forward.com
4. [Render一键部署](deploy.md#render-一键部署) (推荐)
> https://render.openai-forward.com
## 应用

Expand All @@ -99,6 +104,9 @@ api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代
基于开源项目[ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web)搭建自己的chatgpt服务
替换docker启动命令中的 `BASE_URL`为我们自己搭建的代理服务地址

<details markdown="1">
<summary>Click for more details</summary>

```bash
docker run -d \
-p 3000:3000 \
Expand All @@ -107,11 +115,24 @@ docker run -d \
-e CODE="kunyuan" \
yidadaa/chatgpt-next-web
```

这里部署了一个,供大家轻度使用:
https://chat.beidongjiedeguang.top , 访问密码: `kunyuan`
https://chat.beidongjiedeguang.top , 访问密码: `kunyuan`
</details>

### 在代码中使用

**Python**

```diff
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
openai.api_key = "sk-******"
```

<details markdown="1">
<summary>More Examples</summary>

**JS/TS**

```diff
Expand All @@ -123,15 +144,8 @@ docker run -d \
});
```

**Python**

```diff
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
openai.api_key = "sk-******"
```

**gpt-3.5-turbo**

```bash
curl https://api.openai-forward.com/v1/chat/completions \
-H "Content-Type: application/json" \
Expand All @@ -143,6 +157,7 @@ curl https://api.openai-forward.com/v1/chat/completions \
```

**Image Generation (DALL-E)**

```bash
curl --location 'https://api.openai-forward.com/v1/images/generations' \
--header 'Authorization: Bearer sk-******' \
Expand All @@ -154,44 +169,64 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \
}'
```

</details>

## 配置选项

配置的设置方式支持两种
一种为在shell中运行`openai-forward run --port=8000`的命令行方式指定;
另一种为读取环境变量的方式指定。

<details markdown="1">
<summary>Click for more details</summary>

**`openai-forward run`参数配置项**

| 配置项 | 说明 | 默认值 |
|-----------| --- | :---: |
| --port | 服务端口号 | 8000 |
| --workers | 工作进程数 | 1 |
| 配置项 | 说明 | 默认值 |
|-----------------|-------------------|:----------------------:|
| --port | 服务端口号 | 8000 |
| --workers | 工作进程数 | 1 |
| --base_url | 同 OPENAI_BASE_URL | https://api.openai.com |
| --api_key | 同 OPENAI_API_KEY | `None` |
| --forward_key | 同 FORWARD_KEY | `None` |
| --route_prefix | 同 ROUTE_PREFIX | `None` |
| --log_chat | 同 LOG_CHAT | `False` |

更多参数 `openai-forward run --help` 查看
也可通过 `openai-forward run --help` 查看

**环境变量配置项**
支持从运行目录下的`.env`文件中读取:
支持从运行目录下的`.env`文件中读取

| 环境变量 | 说明 | 默认值 |
|-----------------|-----------------------------------------------------------------|:------------------------:|
| OPENAI_API_KEY | 默认openai api key,支持多个默认api key, 以 `sk-` 开头, 以空格分割 ||
| FORWARD_KEY | 允许调用方使用该key代替openai api key,支持多个forward key, 以`fk-` 开头, 以空格分割 ||
| OPENAI_BASE_URL | 转发base url | `https://api.openai.com` |
| LOG_CHAT | 是否记录聊天内容 | `true` |
| ROUTE_PREFIX | 路由前缀 ||
| 环境变量 | 说明 | 默认值 |
|-----------------|-----------------------------------------------------------------------------------------------------------------------------------|:------------------------:|
| OPENAI_BASE_URL | 默认 openai官方 api 地址 | https://api.openai.com |
| OPENAI_API_KEY | 默认openai api key,支持多个默认api key, 以 `sk-` 开头, 以空格分割 ||
| FORWARD_KEY | 允许调用方使用该key代替openai api key,支持多个forward key, 以空格分割; 如果设置了OPENAI_API_KEY,而没有设置FORWARD_KEY, 则客户端调用时无需提供密钥, 此时出于安全考虑不建议FORWARD_KEY置空 ||
| OPENAI_BASE_URL | 转发base url | `https://api.openai.com` |
| ROUTE_PREFIX | 路由前缀 ||
| LOG_CHAT | 是否记录聊天内容 | `false` |

</details>

## 高级配置

**设置api_key为自己设置的forward key**
需要配置 OPENAI_API_KEY 和 FORWARD_KEY, 例如
<details markdown="1">
<summary>Click for more details</summary>

```bash
OPENAI_API_KEY=sk-*******
FORWARD_KEY=fk-****** # 这里fk-token由我们自己定义
```

这里我们配置了FORWARD_KEY为`fk-******`, 那么后面客户端在调用时只需设置OPENAI_API_KEY为我们自定义的`fk-******` 即可。
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用`fk-******`搭配代理服务使用(如下面的例子) 而无需担心OPENAI_API_KEY被泄露。
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用`fk-******`搭配代理服务使用(如下面的例子)
而无需担心OPENAI_API_KEY被泄露。
并且可以对外分发`fk-******`

**用例:**

```bash
curl https://api.openai-forward.com/v1/chat/completions \
-H "Content-Type: application/json" \
Expand All @@ -201,14 +236,18 @@ curl https://api.openai-forward.com/v1/chat/completions \
"messages": [{"role": "user", "content": "Hello!"}]
}'
```

**Python**

```diff
import openai
+ openai.api_base = "https://api.openai-forward.com/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"
```

**Web application**

```bash
docker run -d \
-p 3000:3000 \
Expand All @@ -218,21 +257,63 @@ docker run -d \
yidadaa/chatgpt-next-web
```

</details>

## 聊天日志

默认不记录聊天日志,若要开启需设置环境变量`LOG_CHAT=true`
<details markdown="1">
<summary>Click for more details</summary>

保存路径在当前目录下的`Log/chat.log`路径中。
记录格式为

```text
[{'host': xxx, 'model': xxx, 'message': [{'user': xxx}, {'assistant': xxx}]},
{'assistant': xxx}]
{'messages': [{'user': 'hi'}], 'model': 'gpt-3.5-turbo', 'host': '', 'uid': '467a17ec-bf39-4b65-9ebd-e722b3bdd5c3'}
{'assistant': 'Hello! How can I assist you today?', 'uid': '467a17ec-bf39-4b65-9ebd-e722b3bdd5c3'}
{'messages': [{'user': 'Hello!'}], 'model': 'gpt-3.5-turbo', 'host': '', 'uid': 'f844d156-e747-4887-aef8-e40d977b5ee7'}
{'assistant': 'Hi there! How can I assist you today?', 'uid': 'f844d156-e747-4887-aef8-e40d977b5ee7'}
...
```

[{'host': ...},
{'assistant': ...}]
转换为`jsonl`格式:

```bash
openai-forward convert
```

即可转换为以下格式:

```json lines
[
{
"messages": [
{
"user": "hi!"
}
]
},
{
"assistant": "Hello! How can I assist you today?"
}
]
[
{
"messages": [
{
"user": "Hello!"
}
]
},
{
"assistant": "Hi there! How can I assist you today?"
}
]
...
```

</details>

## Backer and Sponsor

<a href="https://www.jetbrains.com/?from=beidongjiedeguang/openai-forward" target="_blank">
Expand All @@ -241,4 +322,4 @@ docker run -d \

## License

Openai-forward is licensed under the [MIT](https://opensource.org/license/mit/) license.
OpenAI-Forward is licensed under the [MIT](https://opensource.org/license/mit/) license.
Loading

1 comment on commit af3a05f

@vercel
Copy link

@vercel vercel bot commented on af3a05f May 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.