Skip to content

Commit

Permalink
Feat: Support forward key (#23)
Browse files Browse the repository at this point in the history
* Support your own forward API key

* bug fix

* update README
  • Loading branch information
KenyonY authored May 9, 2023
1 parent 3855a80 commit 749e197
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 82 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ start:
--env-file .env \
-p 27001:8000 \
-v $(shell pwd)/Log-caloi-top:/home/openai-forward/Log \
-v $(shell pwd)/openai_forward:/home/openai-forward/openai_forward \
$(image)


Expand Down Expand Up @@ -38,11 +39,6 @@ twine:
@twine upload dist/*
@rm -rf dist/*

build-web:
@cd third-party/forward_web && npm run build
@cd third-party/forward_web/build && npx uglify-js index.js -m -o index.min.js
@mv third-party/forward_web/build/index.min.js openai_forward/web/index.js

start-web:
@openai_forward node --port=9099 --base_url="https://api.openai.com"

Expand Down
108 changes: 88 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
本项目用于解决一些地区无法直接访问OpenAI的问题,将该服务部署在可以正常访问openai
api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代理服务
测试访问:https://caloi.top/openai/v1/chat/completions 将等价于 https://api.openai.com/v1/chat/completions
或者说 https://caloi.top/openai 等价于 https://api.openai.com
或者说 https://caloi.top/openai 等价于 https://api.openai.com

# Table of Contents

Expand All @@ -50,16 +50,16 @@ api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代
- [服务调用](#服务调用)
- [配置选项](#配置选项)
- [聊天日志](#聊天日志)
- [高级配置](#高级配置)

# Features

- [x] 支持转发OpenAI所有接口
- [x] 支持流式响应
- [x] 实时记录聊天记录(包括流式响应的聊天内容)
- [x] 支持默认api key(多api key 循环调用)
- [x] pip安装部署
- [x] 支持默认openai api key(多api key 循环调用)
- [x] 转发api key (在已设置默认openai api key情况下使用)
- [x] docker部署
- [x] 支持多进程转发
- [x] 支持指定转发路由前缀
- [x] 支持请求IP验证

Expand All @@ -68,15 +68,23 @@ api的服务器上,通过该服务转发OpenAI的请求。即搭建反向代
> 这里以个人使用该项目搭建好的代理服务 https://caloi.top/openai 为例
### [caloi.top](https://caloi.top)

基于开源项目[ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web)搭建自己的chatgpt服务
替换docker启动命令中的 `BASE_URL`为我们自己搭建的代理服务地址

```bash
docker run -d -p 3000:3000 -e OPENAI_API_KEY="sk-xxx" -e CODE="<your password>" -e BASE_URL="caloi.top/openai" yidadaa/chatgpt-next-web
```
```bash
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="sk-******" \
-e BASE_URL="caloi.top/openai" \
-e CODE="<your password>" \
yidadaa/chatgpt-next-web
```

访问 https://caloi.top 。访问密码为 `beidongjiedeguang`

### 在模块中使用

**JS/TS**

```diff
Expand Down Expand Up @@ -109,13 +117,12 @@ curl --location 'https://caloi.top/openai/v1/images/generations' \
}'
```


# 安装部署

提供两种服务部署方式,选择一种即可

## pip
提供3种服务部署方式,选择一种即可

## pip
pip的安装方式目前在使用nginx反向代理时存在Bug, 建议使用Docker方式部署。
**安装**

```bash
Expand Down Expand Up @@ -149,6 +156,26 @@ docker run --name="openai-forward" -d -p 9999:8000 beidongjiedeguang/openai-forw
将映射宿主机的9999端口,通过`http://{ip}:9999`访问服务。
注:同样可以在启动命令中通过-e传入环境变量OPENAI_API_KEY=sk-xxx作为默认api key

## 源码部署

```bash
git clone https://github.com/beidongjiedeguang/openai-forward.git --depth=1
cd openai-forward
```

**使用 docker**

```bash
docker-compose up
```

**或使用pip**

```bash
pip install -e .
openai-forward run
```

# 服务调用

替换openai的api地址为该服务的地址即可,如:
Expand All @@ -175,20 +202,22 @@ http://{ip}:{port}/v1/chat/completions
**环境变量配置项**
参考项目根目录下`.env`文件

| 环境变量 | 说明 | 默认值 |
|-----------------|--------------------------------|:------------------------:|
| OPENAI_API_KEY | 默认api key,支持多个默认api key, 以空格分割 ||
| OPENAI_BASE_URL | 转发base url | `https://api.openai.com` |
| LOG_CHAT | 是否记录聊天内容 | `true` |
| ROUTE_PREFIX | 路由前缀 | |
| IP_WHITELIST | ip白名单, 空格分开 | |
| IP_BLACKLIST | ip黑名单, 空格分开 ||

| 环境变量 | 说明 | 默认值 |
|-----------------|-----------------------------------------------------------------|:------------------------:|
| 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 | 路由前缀 | |
| IP_WHITELIST | ip白名单, 空格分开 ||
| IP_BLACKLIST | ip黑名单, 空格分开 ||

# 聊天日志

保存路径在当前目录下的`Log/`路径中。
聊天日志以 `chat_`开头, 默认每5轮对话写入一次文件
记录格式为

```text
{'host': xxx, 'model': xxx, 'message': [{'user': xxx}, {'assistant': xxx}]}
{'assistant': xxx}
Expand All @@ -198,3 +227,42 @@ http://{ip}:{port}/v1/chat/completions
...
```

# 高级配置

**设置api_key为自己设置的forward key**
需要配置 OPENAI_API_KEY 和 FORWARD_KEY, 例如

```bash
OPENAI_API_KEY=sk-*******
FORWARD_KEY=fk-****** # 这里fk-token由我们自己定义
```
这里我们配置了FORWARD_KEY为`fk-******`, 那么后面客户端在调用时只需设置OPENAI_API_KEY为我们自定义的`fk-******` 即可。
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用`fk-******`搭配proxy使用(如下面的例子) 而无需担心OPENAI_API_KEY被泄露。

**用例:**
```bash
curl https://caloi.top/openai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer fk-******" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
```
**Python**
```diff
import openai
+ openai.api_base = "https://caloi.top/openai/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"
```
**Web application**
```bash
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="fk-******" \
-e BASE_URL="caloi.top/openai" \
-e CODE="<your password>" \
yidadaa/chatgpt-next-web
```
76 changes: 61 additions & 15 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@


[//]: # ( <a href="https://github.com/beidongjiedeguang">)

[//]: # ( <img alt="name" src="https://img.shields.io/badge/[email protected]?style=flat-square&logo=appveyor">)

[//]: # ( </a>)

[//]: # ( <a href="https://github.com/beidongjiedeguang/openai-forward/stargazers">)

[//]: # ( <img alt="starts" src=https://img.shields.io/github/stars/beidongjiedeguang/openai-forward?style=social>)

[//]: # ( </a>)

[//]: # ([![Downloads]&#40;https://static.pepy.tech/badge/openai-forward/month&#41;]&#40;https://pepy.tech/project/openai-forward&#41;)

<p align="center">
Expand Down Expand Up @@ -57,29 +63,37 @@ Or, to put it another way, https://caloi.top/openai is equivalent to https://api
- [Service Usage](#Service-Usage)
- [Configuration](#Configuration)
- [Chat Log](#Chat-log)
- [Advanced Configuration](#Advanced-Configuration)

# Features

- [x] Supports forwarding of all OpenAI interfaces
- [x] Request IP verification
- [x] Streaming Response
- [x] Real-time recording of chat records (including the chat contents of streaming responses).
- [x] Supports default API key (cyclic call with multiple API keys)
- [x] pip installation and deployment
- [x] Docker deployment
- [x] Support for multiple worker processes
- [x] Support for specifying the forwarding routing prefix
- [x] Request IP verification

# Usage

> Here, the proxy address set up by the individual, https://caloi.top/openai, is used as an example
### [caloi.top](https://caloi.top)
Build your own ChatGPT service based on the open source project [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web).

Build your own ChatGPT service based on the open source
project [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web).
Replace `BASE_URL` in the docker startup command with the address of the proxy service we set up:

```bash
docker run -d -p 3000:3000 -e OPENAI_API_KEY="sk-******" -e CODE="<your password>" -e BASE_URL="caloi.top/openai" yidadaa/chatgpt-next-web
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="sk-******" \
-e BASE_URL="caloi.top/openai" \
-e CODE="<your password>" \
yidadaa/chatgpt-next-web
```

Access to https://caloi.top. access code: `beidongjiedeguang`.

### Using in a module
Expand All @@ -103,7 +117,6 @@ Access to https://caloi.top. access code: `beidongjiedeguang`.
});
```


### Image Generation (DALL-E):

```bash
Expand All @@ -117,12 +130,11 @@ curl --location 'https://caloi.top/openai/v1/images/generations' \
}'
```


# Deploy

Two deployment methods are provided, just choose one.

## Use `pip`
## Use `pip`

**Installation**

Expand Down Expand Up @@ -164,6 +176,7 @@ Note: You can also pass in the environment variable OPENAI_API_KEY=sk-xxx as the
# Service Usage

Simply replace the OpenAI API address with the address of the service we set up, such as `Chat Completion`

```bash
https://api.openai.com/v1/chat/completions
```
Expand All @@ -186,19 +199,22 @@ http://{ip}:{port}/v1/chat/completions
**Environment Variable Configuration Options**
refer to the `.env` file in the project root directory

| Environment Variable | Description | Default Value |
| Environment Variable | Description | Default Value |
|-----------------|------------|:------------------------:|
| OPENAI_API_KEY | Default API key, supports multiple default API keys separated by space. | None |
| OPENAI_API_KEY | Default API key, supports multiple default API keys separated by space. | None |
| FORWARD_KEY | Allow the caller to use the key instead of the OpenAI API key, support multiple forward keys starting with "fk-" and separated by spaces. | None |
| OPENAI_BASE_URL | Forwarding base URL | `https://api.openai.com` |
|LOG_CHAT| Whether to log chat content | `true` |
|ROUTE_PREFIX| Route prefix | None |
| IP_WHITELIST | IP whitelist, separated by space. | None |
| IP_BLACKLIST | IP blacklist, separated by space. | None |
|LOG_CHAT| Whether to log chat content | `true` |
|ROUTE_PREFIX| Route prefix | None |
| IP_WHITELIST | IP whitelist, separated by space. | None |
| IP_BLACKLIST | IP blacklist, separated by space. | None |

# Chat Log

The saved path is in the `Log/` directory under the current directory.
The chat log starts with `chat_` and is written to the file every 5 rounds by default.
The recording format is as follows:

```text
{'host': xxx, 'model': xxx, 'message': [{'user': xxx}, {'assistant': xxx}]}
{'assistant': xxx}
Expand All @@ -207,4 +223,34 @@ The recording format is as follows:
{'assistant': ...}
...
```
```

# Advanced Configuration

**Set the api_key to your own forward key**
You need to configure OPENAI_API_KEY and FORWARD_KEY, for example:

```bash
OPENAI_API_KEY=sk-*******
FORWARD_KEY=fk-******
```
If you set FORWARD_KEY to fk-******, then the client only needs to set the OPENAI_API_KEY to fk-****** when calling the API later.

**Examples:**

**Python**
```diff
import openai
+ openai.api_base = "https://caloi.top/openai/v1"
- openai.api_key = "sk-******"
+ openai.api_key = "fk-******"
```
**Web application**
```bash
docker run -d \
-p 3000:3000 \
-e OPENAI_API_KEY="fk-******" \
-e BASE_URL="caloi.top/openai" \
-e CODE="<your password>" \
yidadaa/chatgpt-next-web
```
3 changes: 1 addition & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ services:
ports:
- "8000:8000"
volumes:
- ./Log:/home/openai-forward/Log
- ./Log-caloi-top:/home/openai-forward/Log
- ./openai_forward:/home/openai-forward/openai_forward
# - ./openai_forward:/opt/conda/lib/python3.10/site-packages/openai_forward
command:
- --port=8000
- --workers=1
2 changes: 1 addition & 1 deletion openai_forward/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.7"
__version__ = "0.1.8"

from dotenv import load_dotenv

Expand Down
Loading

0 comments on commit 749e197

Please sign in to comment.