Skip to content

Commit

Permalink
whisper support
Browse files Browse the repository at this point in the history
  • Loading branch information
KenyonY committed Jul 18, 2023
1 parent 896fa4c commit cb46e67
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@
> https://render.openai-forward.com
> https://railway.openai-forward.com

<details >
<summary> 👉Tips </summary>

🎉🎉🎉近期GPT-4 API 已经全面可用! 但它需要付费api账户,也就是需要先绑定信用卡。
目前比较推荐开源加密钱包[OneKey](https://github.com/OneKeyHQ)的VISA虚拟卡:[https://card.onekey.so](https://card.onekey.so/?i=O163GB)

</details>


## 功能

**基础功能**
Expand All @@ -95,27 +85,26 @@

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


提供以下几种部署方式
**有海外vps方案**

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

1. [Railway部署](deploy.md#Railway-一键部署)
> https://railway.openai-forward.com
2. [Render一键部署](deploy.md#render-一键部署)
> https://render.openai-forward.com

---
下面的部署仅提供单一转发功能

3. [一键Vercel部署](deploy.md#vercel-一键部署)
> https://vercel.openai-forward.com
4. [cloudflare部署](deploy.md#cloudflare-部署)
4. [cloudflare部署](deploy.md#cloudflare-部署)
> https://cloudflare.page.openai-forward.com
## 应用
Expand Down Expand Up @@ -200,6 +189,7 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \
另一种为读取环境变量的方式指定。

### 命令行参数

可通过 `openai-forward run --help` 查看

<details open>
Expand All @@ -217,10 +207,10 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \
| --route_prefix | 同 ROUTE_PREFIX | `None` |
| --log_chat | 同 LOG_CHAT | `False` |


</details>

### 环境变量配置项

支持从运行目录下的`.env`文件中读取

| 环境变量 | 说明 | 默认值 |
Expand All @@ -231,7 +221,6 @@ curl --location 'https://api.openai-forward.com/v1/images/generations' \
| ROUTE_PREFIX | 路由前缀 ||
| LOG_CHAT | 是否记录聊天内容 | `false` |


## 高级配置

**设置openai api_key为自定义的forward key**
Expand All @@ -245,8 +234,8 @@ FORWARD_KEY=fk-****** # 这里fk-token由我们自己定义
```

这里我们配置了FORWARD_KEY为`fk-******`, 那么后面客户端在调用时只需设置OPENAI_API_KEY为我们自定义的`fk-******` 即可。
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用自定义的api-key`fk-******`,
无需担心真正的OPENAI_API_KEY被泄露。并且可以对外分发`fk-******`
这样的好处是在使用一些需要输入OPENAI_API_KEY的第三方应用时,我们可以使用自定义的api-key`fk-******`,
无需担心真正的OPENAI_API_KEY被泄露。并且可以对外分发`fk-******`

**用例:**

Expand Down
2 changes: 1 addition & 1 deletion openai_forward/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def run(
)

@staticmethod
def convert(log_folder: str = "./Log", target_path: str = "./Log/chat.json"):
def convert(log_folder: str = "./Log/chat", target_path: str = "./Log/chat.json"):
"""Convert log folder to jsonl file"""
from openai_forward.tool import convert_folder_to_jsonl

Expand Down
41 changes: 23 additions & 18 deletions openai_forward/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .config import print_startup_info, setting_log
from .content.chat import ChatSaver
from .content.whisper import WhisperSaver
from .tool import env2list


Expand Down Expand Up @@ -36,6 +37,7 @@ class OpenaiBase:
if _LOG_CHAT:
setting_log(save_file=False)
chatsaver = ChatSaver()
whispersaver = WhisperSaver()

def validate_request_host(self, ip):
if self.IP_WHITELIST and ip not in self.IP_WHITELIST:
Expand All @@ -56,10 +58,13 @@ async def aiter_bytes(cls, r: httpx.Response, route_path: str, uid: str):
bytes_ += chunk
yield chunk
try:
target_info = cls.chatsaver.parse_bytes_to_content(bytes_, route_path)
cls.chatsaver.add_chat(
{target_info["role"]: target_info["content"], "uid": uid}
)
if route_path == "/v1/chat/completions":
target_info = cls.chatsaver.parse_bytes_to_content(bytes_, route_path)
cls.chatsaver.add_chat(
{target_info["role"]: target_info["content"], "uid": uid}
)
elif route_path.startswith("/v1/audio/"):
cls.whispersaver.add_log(bytes_)
except Exception as e:
logger.debug(f"log chat (not) error:\n{e=}")

Expand All @@ -71,23 +76,27 @@ async def _reverse_proxy(cls, request: Request):
url = httpx.URL(path=url_path, query=request.url.query.encode("utf-8"))
headers = dict(request.headers)
auth = headers.pop("authorization", "")
auth_headers_dict = {"Content-Type": "application/json", "Authorization": auth}
content_type = headers.pop("content-type", "application/json")
auth_headers_dict = {"Content-Type": content_type, "Authorization": auth}
auth_prefix = "Bearer "
if cls._no_auth_mode or auth and auth[len(auth_prefix) :] in cls._FWD_KEYS:
auth = auth_prefix + next(cls._cycle_api_key)
auth_headers_dict["Authorization"] = auth

log_chat_completions = False
if_log = False
uid = None
if cls._LOG_CHAT and request.method == "POST":
try:
chat_info = await cls.chatsaver.parse_payload_to_content(
request, route_path=url_path
)
if chat_info:
cls.chatsaver.add_chat(chat_info)
uid = chat_info.get("uid")
log_chat_completions = True
if url_path.startswith("/v1/audio/"):
if_log = True
else:
chat_info = await cls.chatsaver.parse_payload_to_content(
request, route_path=url_path
)
if chat_info:
cls.chatsaver.add_chat(chat_info)
uid = chat_info.get("uid")
if_log = True
except Exception as e:
logger.debug(
f"log chat error:\n{request.client.host=} {request.method=}: {e}"
Expand Down Expand Up @@ -117,11 +126,7 @@ async def _reverse_proxy(cls, request: Request):
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=e
)

aiter_bytes = (
cls.aiter_bytes(r, url_path, uid)
if log_chat_completions
else r.aiter_bytes()
)
aiter_bytes = cls.aiter_bytes(r, url_path, uid) if if_log else r.aiter_bytes()
return StreamingResponse(
aiter_bytes,
status_code=r.status_code,
Expand Down
9 changes: 8 additions & 1 deletion openai_forward/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,19 @@ def setting_log(save_file=False, log_name="openai_forward", multi_process=True):
config_handlers = [
{"sink": sys.stdout, "level": "DEBUG"},
{
"sink": f"./Log/chat.log",
"sink": f"./Log/chat/chat.log",
"enqueue": multi_process,
"rotation": "50 MB",
"filter": lambda record: "chat" in record["extra"],
"format": "{message}",
},
{
"sink": f"./Log/whisper/whisper.log",
"enqueue": multi_process,
"rotation": "30 MB",
"filter": lambda record: "whisper" in record["extra"],
"format": "{message}",
},
]
if save_file:
config_handlers += [
Expand Down
10 changes: 10 additions & 0 deletions openai_forward/content/whisper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from loguru import logger


class WhisperSaver:
def __init__(self):
self.logger = logger.bind(whisper=True)

def add_log(self, bytes_: bytes):
text_content = bytes_.decode("utf-8")
self.logger.debug(text_content)
1 change: 0 additions & 1 deletion openai_forward/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def get_matches(messages: List[Dict], assistants: List[Dict]):
print(f"message({msg_len}) 与 assistant({ass_len}) 长度不匹配")

cvt = lambda msg, ass: {
"auth": msg.get("auth"),
"datetime": msg.get('datetime'),
"forwarded-for": msg.get("forwarded-for"),
"model": msg.get("model"),
Expand Down

0 comments on commit cb46e67

Please sign in to comment.