Skip to content

Commit

Permalink
chore:update configuration files && readme
Browse files Browse the repository at this point in the history
  • Loading branch information
KenyonY committed Aug 19, 2023
1 parent 54182b5 commit 47d8d65
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ EXTRA_ROUTE_PREFIX=
# ratelimit-string format [count] [per|/] [n (optional)] [second|minute|hour|day|month|year] :ref:`ratelimit-string`: https://limits.readthedocs.io/en/stable/quickstart.html#rate-limit-string-notation
ROUTE_RATE_LIMIT={"/healthz": "60/2minutes", "/v1/chat/completions": "15/minute;200/hour"}

# `GLOBAL_RATE_LIMIT`: 所有`RATE_LIMIT`没有指定的路由. 不填默认无限制
# `GLOBAL_RATE_LIMIT`: 所有`ROUTE_RATE_LIMIT`没有指定的路由. 不填默认无限制
GLOBAL_RATE_LIMIT=30/minute

#`RATE_LIMIT_STRATEGY` Options: (fixed-window, fixed-window-elastic-expiry, moving-window) :ref: https://limits.readthedocs.io/en/latest/strategies.html
Expand All @@ -32,7 +32,7 @@ RATE_LIMIT_STRATEGY=moving-window


# TPM: 返回的token速率限制
TOKEN_RATE_LIMIT=40/second
TOKEN_RATE_LIMIT=50/second


TIMEOUT=300
Expand Down
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ROUTE_RATE_LIMIT='{
"/localai/v1/chat/completions": "2/second"
}'

# `GLOBAL_RATE_LIMIT`: 所有`RATE_LIMIT`没有指定的路由. 不填默认无限制
# `GLOBAL_RATE_LIMIT`: 所有`ROUTE_RATE_LIMIT`没有指定的路由. 不填默认无限制
GLOBAL_RATE_LIMIT=2/5seconds

#`RATE_LIMIT_STRATEGY` Options: (fixed-window, fixed-window-elastic-expiry, moving-window) ref: https://limits.readthedocs.io/en/latest/strategies.html
Expand Down
Binary file added .github/images/startup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions .github/workflows/issue-close.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Close inactive issues
on:
schedule:
- cron: '0 9 * * *'

jobs:
close-issues:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v5
with:
days-before-issue-stale: 30
days-before-issue-close: 7
stale-issue-label: stale
stale-issue-message: This issue is stale because it has been open for 30 days with no activity.
close-issue-message: This issue was closed because it has been inactive for 7 days since being marked as stale.
days-before-pr-stale: -1
days-before-pr-close: -1
repo-token: ${{ secrets.GITHUB_TOKEN }}
31 changes: 31 additions & 0 deletions Examples/aifd.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

[log]
chat = true


[openai]
# 指定具有openai接口风格的服务的转发
base_url = ["https://api.openai.com", "http://localhost:8080"]
route_prefix = ["/openai", "/localai"]
api_key = ["sk-xxx1", "sk-xxx2", "sk-xxx3"]
forward_key = ["fk-xxx1"]


[extra]
# 可指定任意接口服务的转发
base_url = ["http://localhost:8882", "http://localhost:8881"]
route_prefix = ["/tts", "/translate"]


[rate_limit]
# format: route = limit value
'openai/v1/chat/completions' = '100/minute' # nums/second, nums/minute, nums/hour
'localai/v1/chat/completions' = '200/minute'


[proxy]
global = "http://localhost:7890"


[timezone]
tz = "Asia/Shanghai"
8 changes: 8 additions & 0 deletions Examples/aiohttp_test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import aiohttp

timeout = aiohttp.ClientTimeout(total=self.timeout)
session = aiohttp.ClientSession(base_url=self.BASE_URL, timeout=timeout)
url_path = '/v1/chat/completions'
request_url_query = "name=kunyuan"
aio_url = aiohttp.client.URL(f"{url_path}?{request_url_query}")
print(f"{aio_url=}")
2 changes: 0 additions & 2 deletions Examples/chat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import time

import openai
from rich import print
from sparrow import yaml_load
Expand Down
24 changes: 24 additions & 0 deletions Examples/debug_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from openai_forward.helper import retry, toml_load


# config = toml_load('aifd.toml')
# print(config)
class TestClass:
def __init__(self):
self.counter = 0

@retry(max_retries=5, delay=1, exceptions=(ValueError,))
def test_method(self):
self.counter += 1
print(f"Attempt {self.counter}")

# 为了演示,这里抛出一个ValueError异常
if self.counter < 4:
raise ValueError("This is a test error")
else:
print("Success on the 4th try!")


# 使用示例
obj = TestClass()
obj.test_method()
23 changes: 23 additions & 0 deletions Examples/global_request_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from contextvars import ContextVar

from fastapi import FastAPI, Request, status

app = FastAPI()
_request_ctx_var: ContextVar[str] = ContextVar("request_context", default=None)


@app.middleware("http")
async def request_context_middleware(request: Request, call_next):
try:
request_ctx = _request_ctx_var.set(request)
response = await call_next(request)
_request_ctx_var.reset(request_ctx)
return response
except Exception as e:
raise e


def dynamic_rate_limit(key: str):
request: Request = _request_ctx_var.get()
print(request.headers)
return None
97 changes: 62 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<h1 align="center">
<br>
AI forward
OpenAI forward
<br>
</h1>
<p align="center">
Expand Down Expand Up @@ -37,17 +37,20 @@

<div align="center">

[功能](#功能) |
[部署指南](#部署指南) |
[应用](#应用) |
[配置选项](#配置选项) |
[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/beidongjiedeguang/openai-forward)

[特点](#特点) |
[部署指南](deploy.md) |
[使用](#使用) |
[配置](#配置) |
[对话日志](#对话日志)

[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/beidongjiedeguang/openai-forward)

</div>

本项目是大模型与用户层之间的一道转发服务,可用于搭建反向代理,用户速率限制,token速率限制,自定义API KEY 等.
OpenAI-Forward是大模型与用户层之间的一道转发服务,
用于对请求模型的速率限制,模型返回的Token速率限制,自定义API KEY 等。



<a>
Expand All @@ -56,38 +59,53 @@

### 特点

AI-Forward支持以下功能:
OpenAI-Forward支持以下功能:

- **万能代理**: 几乎可以转发任何请求
- **用户速率限制**: 提供请求速率限制(**RPM**)与流式返回的Token速率限制(**TPM**)
- **自定义秘钥**: 支持用户使用自定义生成的秘钥代替原始api key使用。
- **自定义秘钥**: 支持用户使用自定义生成的秘钥代替原始api key使用。
- 流式响应的对话日志
- 可同时转发多个目标服务至不同路由
- 失败请求自动重试
- 一分钟内完成安装与部署
- 一分钟内完成安装与部署; 一键部署至云端
- ...


由本项目搭建的代理服务地址:
> https://api.openai-forward.com
> https://render.openai-forward.com
> https://render.openai-forward.com
<font size=3>
注:本项目中提供的所有代理服务仅供学习使用,请勿用作其它用途。
</font>

注:这里提供的代理地址服务仅限于学生/研究人员使用,若要长期使用请参考部署文档自行搭建。

## 部署指南

见 👉 [部署文档](deploy.md)

👉 [部署文档](deploy.md)


<a>
<img src="https://raw.githubusercontent.com/beidongjiedeguang/openai-forward/main/.github/images/separators/aqua.png" height=8px width="100%">
</a>

## 使用方式
## 使用
**安装**
```bash
pip install openai-forward
```
**运行**
```bash
aifd run
```
如果一切正常将会看到下面的启动信息

<div align="center">
<img src=.github/images/startup.png width="600" style="opacity:0.9;" alt="startup"/>
</div>

### 反向代理应用:

### 代理OpenAI API:
这也是`aifd run`的默认选项

#### 在第三方应用中使用

Expand All @@ -97,8 +115,6 @@ AI-Forward支持以下功能:
基于开源项目[ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web)搭建自己的chatgpt服务
替换docker启动命令中的 `BASE_URL`为自己搭建的代理服务地址



```bash
docker run -d \
-p 3000:3000 \
Expand All @@ -123,7 +139,6 @@ docker run -d \
openai.api_key = "sk-******"
```


**JS/TS**

```diff
Expand Down Expand Up @@ -162,14 +177,23 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \

</details>

### 与众大模型服务结合使用
### 代理本地模型

[LocalAI](https://github.com/go-skynet/LocalAI)
[api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm)
[claude-to-chatgpt](https://github.com/jtsang4/claude-to-chatgpt)
一起使用,赋予这些服务接口的RPM,TPM,日志等能力。
(待补充...)
[api-for-open-llm](https://github.com/xusenlinzy/api-for-open-llm)
一起使用,赋予这些服务接口的RPM限制,TPM限制,日志等能力。

以LocalAI为例:
假设部署的LocalAI服务运行在 `http://localhost:8080`
那么接下来只需修改.env配置中`OPENAI_BASE_URL=http://localhost:8080` 就可以完成对LocalAI的代理。

(待补充)

### 代理其它云端模型

例如可通过 [claude-to-chatgpt](https://github.com/jtsang4/claude-to-chatgpt)
将claude的api格式对齐为openai的格式,然后使用`openai-forward`进行代理。
(待补充)

## 配置

Expand All @@ -182,19 +206,18 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \

**`aifd run`参数配置项**

| 配置项 | 说明 | 默认值 |
|-----------------------|-----------------------|:----------------------:|
| --port | 服务端口号 | 8000 |
| --workers | 工作进程数 | 1 |
| --log_chat | 同 LOG_CHAT | `False` |
| 配置项 | 说明 | 默认值 |
|------------|------------|:-------:|
| --port | 服务端口号 | 8000 |
| --workers | 工作进程数 | 1 |
| --log_chat | 同 LOG_CHAT | `False` |

</details>

### 环境变量配置项

支持从运行目录下的`.env`文件中读取
配置示例见根目录下的 [.env.example](.env.example)

配置示例见根目录下的 [.env.example](.env.example)

| 环境变量 | 说明 | 默认值 |
|---------------------|-----------------------------------------------------------------------------------------------------------------------------------|:----------------------:|
Expand All @@ -204,12 +227,17 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \
| FORWARD_KEY | 允许调用方使用该key代替openai api key,支持多个forward key, 以逗号分隔; 如果设置了OPENAI_API_KEY,而没有设置FORWARD_KEY, 则客户端调用时无需提供密钥, 此时出于安全考虑不建议FORWARD_KEY置空 ||
| EXTRA_BASE_URL | 额外转发服务地址 ||
| EXTRA_ROUTE_PREFIX | 额外转发服务路由前缀 ||
| ROUTE_RATE_LIMIT | 指定路由的请求速率限制(区分用户) ||
| GLOBAL_RATE_LIMIT | 所有`RATE_LIMIT`没有指定的路由. 不填默认无限制 ||
| RATE_LIMIT_STRATEGY | 速率限制策略(fixed-window, fixed-window-elastic-expiry, moving-window) ||
| TOKEN_RATE_LIMIT | 对每一份流式返回的token速率限制 (这里的token并不严格等于gpt中定义的token,而是SSE的chunk) ||
| PROXY | http代理 ||
| LOG_CHAT | 是否记录聊天内容 | `false` |

更多见 [.env.example](.env.example) 中的说明。(待完善)


### 自定义秘钥

<details open>
<summary>Click for more details</summary>

Expand All @@ -236,7 +264,6 @@ FORWARD_KEY=fk-****** # 这里fk-token由我们自己定义
支持转发不同地址的服务至同一端口的不同路由下
用例见 `.env.example`


### 对话日志

默认不记录对话日志,若要开启需设置环境变量`LOG_CHAT=true`
Expand Down Expand Up @@ -298,4 +325,4 @@ aifd convert

## License

AI-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

0 comments on commit 47d8d65

Please sign in to comment.