-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multi-reverse proxys support (#56)
* 多接口服务转发支持(用例见.env.example) * 环境变量读取格式更变:多值分隔符由空格分隔更变为逗号 , 分 (用例见.env.example) * 本地请求代理支持 (用例见.env.example) * 请求性能优化 * 潜在bug修复 * 依赖缩减
- Loading branch information
Showing
41 changed files
with
641 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
# 示例见 .env.example | ||
|
||
# LOG_CHAT: 是否开启日志 | ||
LOG_CHAT=false | ||
|
||
# OPENAI_BASE_URL: 转发openai风格的任何服务地址,允许指定多个, 以逗号隔开。 | ||
# 如果指定超过一个,则任何OPENAI_ROUTE_PREFIX/EXTRA_ROUTE_PREFIX都不能为根路由/ | ||
OPENAI_BASE_URL=https://api.openai.com | ||
|
||
# OPENAI_ROUTE_PREFIX: 可指定所有openai风格(为记录日志)服务的转发路由前缀 | ||
OPENAI_ROUTE_PREFIX=/ | ||
|
||
OPENAI_API_KEY= | ||
FORWARD_KEY= | ||
ROUTE_PREFIX= | ||
|
||
# EXTRA_BASE_URL: 可指定任意服务转发 | ||
EXTRA_BASE_URL= | ||
# EXTRA_ROUTE_PREFIX: 与 EXTRA_BASE_URL 匹配的路由前缀 | ||
EXTRA_ROUTE_PREFIX= | ||
|
||
# 设定时区 | ||
TZ=Asia/Shanghai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
# LOG_CHAT: 是否Log 对话记录 | ||
LOG_CHAT=True | ||
# LOG_CHAT: 是否开启日志 | ||
LOG_CHAT=true | ||
|
||
OPENAI_BASE_URL=https://api.openai.com | ||
|
||
# OPENAI_API_KEY:允许输入多个api key 形成轮询池 | ||
OPENAI_API_KEY=sk-xxx1 sk-xxx2 sk-xxx3 | ||
# OPENAI_BASE_URL: 转发openai风格的任何服务地址,允许指定多个, 以逗号隔开。 | ||
# 如果指定超过一个,则任何OPENAI_ROUTE_PREFIX/EXTRA_ROUTE_PREFIX都不能为根路由/ | ||
OPENAI_BASE_URL=https://api.openai.com, http:localhost:8080 | ||
|
||
# OPENAI_ROUTE_PREFIX: 可指定所有openai风格(为记录日志)服务的转发路由前缀 | ||
OPENAI_ROUTE_PREFIX=/openai, /localai | ||
|
||
# OPENAI_API_KEY:允许输入多个api key, 以逗号隔开, 形成轮询池 | ||
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= | ||
|
||
# EXTRA_BASE_URL: 可指定任意服务转发 | ||
EXTRA_BASE_URL=http://localhost:8882, http://localhost:8881 | ||
|
||
# EXTRA_ROUTE_PREFIX: 与 EXTRA_BASE_URL 匹配的路由前缀 | ||
EXTRA_ROUTE_PREFIX=/tts,/translate | ||
|
||
# PROXY 配置代理 | ||
PROXY=http://localhost:7890 | ||
|
||
# 设定时区 | ||
TZ=Asia/Shanghai |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Issue Translator | ||
on: | ||
issue_comment: | ||
types: [created] | ||
issues: | ||
types: [opened] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: usthe/[email protected] | ||
with: | ||
IS_MODIFY_TITLE: false | ||
CUSTOM_BOT_NOTE: Bot detected the issue body's language is not English, translate it automatically. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,34 @@ | ||
import time | ||
|
||
import openai | ||
from rich import print | ||
from sparrow import yaml_load | ||
|
||
config = yaml_load("config.yaml", rel_path=True) | ||
print(f"{config=}") | ||
openai.api_base = config["api_base"] | ||
openai.api_key = config["api_key"] | ||
|
||
openai.api_base = "https://api.openai-forward.com/v1" | ||
openai.api_key = "sk-******" | ||
stream = True | ||
user_content = """ | ||
用c实现快速平方根算法 | ||
""" | ||
|
||
resp = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{"role": "user", "content": "Who won the world series in 2020?"}, | ||
{"role": "user", "content": user_content}, | ||
], | ||
stream=stream, | ||
) | ||
print(resp.choices) | ||
|
||
if stream: | ||
chunk_message = next(resp)['choices'][0]['delta'] | ||
print(f"{chunk_message['role']}: ") | ||
for chunk in resp: | ||
chunk_message = chunk['choices'][0]['delta'] | ||
content = chunk_message.get("content", "") | ||
print(content, end="") | ||
print() | ||
else: | ||
print(resp.choices) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
curl http://localhost:8000/openai/v1/chat/completions \ | ||
-H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer sk-******" \ | ||
-d '{ | ||
"model": "gpt-3.5-turbo", | ||
"messages": [{"role": "user", "content": "Hello!"}] | ||
}' & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
api_base: http://localhost:8000/v1 | ||
api_key: sk-****** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Note: you need to be using OpenAI Python v0.27.0 for the code below to work | ||
import openai | ||
from sparrow import relp | ||
|
||
openai.api_base = "https://api.openai-forward.com/v1" | ||
openai.api_key = "sk-******" | ||
from sparrow import relp, yaml_load | ||
|
||
config = yaml_load("config.yaml") | ||
openai.api_base = config["api_base"] | ||
openai.api_key = config["api_key"] | ||
audio_file = open(relp("../.github/data/whisper.m4a"), "rb") | ||
transcript = openai.Audio.transcribe("whisper-1", audio_file) | ||
print(transcript) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__version__ = "0.3.0-alpha" | ||
__version__ = "0.4.0" | ||
|
||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
load_dotenv(override=False) |
Oops, something went wrong.