Skip to content

Commit 640db6e

Browse files
committed
feat: Add proxy support
1 parent 149817d commit 640db6e

6 files changed

+98
-24
lines changed

.env.example

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ MODEL_MAPPING='{
44
"gpt-3.5-turbo": "GPT-3.5-Turbo",
55
"gpt-4o": "GPT-4o",
66
"gpt-4-turbo": "GPT-4-Turbo"
7-
}'
7+
}'
8+
PROXY_TYPE=socks
9+
PROXY_HOST=127.0.0.1
10+
PROXY_PORT=6668
11+
PROXY_USERNAME=
12+
PROXY_PASSWORD=

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
This is a project that converts the official `poe.com` API to the OpenAI API. It only supports the `/v1/chat/completions` endpoint.
55

6+
## Changelog
7+
1.0.0 `/v1/chat/completions` endpoint support
8+
1.0.1 Environment variable custom model mapping
9+
1.1.0 Add proxy support
10+
611
## Usage
712
### Running Locally
813

@@ -67,4 +72,14 @@ MODEL_MAPPING='{
6772
"gpt-4o": "GPT-4o",
6873
"gpt-4-turbo": "GPT-4-Turbo"
6974
}'
75+
```
76+
77+
## Proxy Settings
78+
```shell
79+
# Please customize and edit in the .env file
80+
PROXY_TYPE=socks # socks/http, socks only supports socks5 proxy
81+
PROXY_HOST=127.0.0.1 # Proxy address
82+
PROXY_PORT=6668 # Proxy port
83+
PROXY_USERNAME= # Proxy username, optional
84+
PROXY_PASSWORD= # Proxy password, optional
7085
```

README_ZH.md

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
这是一个`poe.com`官方API转openai的API的项目
33
仅支持接口`/v1/chat/completions`
44

5+
## 更新日志
6+
1.0.0 `/v1/chat/completions`接口支持
7+
1.0.1 环境变量自定义模型映射
8+
1.1.0 添加proxy支持
9+
510
## 使用方式
611
### 本地运行
712

@@ -66,4 +71,14 @@ MODEL_MAPPING='{
6671
"gpt-4o": "GPT-4o",
6772
"gpt-4-turbo": "GPT-4-Turbo"
6873
}'
74+
```
75+
76+
## 代理设置
77+
```shell
78+
# 请在 .env 文件自定义编辑
79+
PROXY_TYPE=socks # socks/http,socks仅支持socks5代理
80+
PROXY_HOST=127.0.0.1 # 代理地址
81+
PROXY_PORT=6668 # 代理端口
82+
PROXY_USERNAME= # 代理用户名,可选
83+
PROXY_PASSWORD= # 代理密码,可选
6984
```

docker-compose.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ services:
1212
"gpt-3.5-turbo": "GPT-3.5-Turbo",
1313
"gpt-4o": "GPT-4o",
1414
"gpt-4-turbo": "GPT-4-Turbo"
15-
}'
15+
}'
16+
PROXY_TYPE: socks
17+
PROXY_HOST:
18+
PROXY_PORT:
19+
PROXY_USERNAME:
20+
PROXY_PASSWORD:

poe_api.py

+53-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import logging
33
import os
44

5-
from fastapi import WebSocket, Form
5+
import httpx
6+
from fastapi import Form
67
from fastapi.responses import JSONResponse
78
from fastapi_poe.client import get_bot_response, get_final_response, QueryRequest
89
from fastapi_poe.types import ProtocolMessage
@@ -13,6 +14,7 @@
1314

1415
client_dict = {}
1516

17+
1618
async def get_responses(api_key, prompt=[], bot="gpt-4"):
1719
bot_name = get_bot(bot)
1820
# "system", "user", "bot"
@@ -30,14 +32,17 @@ async def get_responses(api_key, prompt=[], bot="gpt-4"):
3032
**additional_params
3133
)
3234

33-
return await get_final_response(query, bot_name=bot_name, api_key=api_key)
35+
session = create_client()
36+
return await get_final_response(query, bot_name=bot_name, api_key=api_key, session=session)
3437

3538

3639
async def stream_get_responses(api_key, prompt, bot):
3740
bot_name = get_bot(bot)
3841
messages = openai_message_to_poe_message(prompt)
42+
43+
session = create_client()
3944
async for partial in get_bot_response(messages=messages, bot_name=bot_name, api_key=api_key,
40-
skip_system_prompt=True):
45+
skip_system_prompt=True, session=session):
4146
yield partial.text
4247

4348

@@ -69,24 +74,6 @@ async def ask(token: str = Form(...), bot: str = Form(...), content: str = Form(
6974
return JSONResponse(status_code=400, content={"message": errmsg})
7075

7176

72-
# @app.websocket("/stream")
73-
async def websocket_endpoint(websocket: WebSocket):
74-
try:
75-
await websocket.accept()
76-
token = await websocket.receive_text()
77-
bot = await websocket.receive_text()
78-
content = await websocket.receive_text()
79-
add_token(token)
80-
async for ret in stream_get_responses(token, content, bot):
81-
await websocket.send_text(ret)
82-
except Exception as e:
83-
errmsg = f"An exception of type {type(e).__name__} occurred. Arguments: {e.args}"
84-
logging.info(errmsg)
85-
await websocket.send_text(errmsg)
86-
finally:
87-
await websocket.close()
88-
89-
9077
def get_bot(model):
9178
model_mapping = json.loads(os.environ.get("MODEL_MAPPING", "{}"))
9279
return model_mapping.get(model, "GPT-4o")
@@ -100,3 +87,48 @@ def openai_message_to_poe_message(messages=[]):
10087
new_messages.append(ProtocolMessage(role=role, content=message["content"]))
10188

10289
return new_messages
90+
91+
92+
def create_client():
93+
proxy_config = {
94+
"proxy_type": os.environ.get("PROXY_TYPE"),
95+
"proxy_host": os.environ.get("PROXY_HOST"),
96+
"proxy_port": os.environ.get("PROXY_PORT"),
97+
"proxy_username": os.environ.get("PROXY_USERNAME"),
98+
"proxy_password": os.environ.get("PROXY_PASSWORD"),
99+
}
100+
101+
proxy = create_proxy(proxy_config)
102+
client = httpx.AsyncClient(timeout=600, proxies=proxy)
103+
return client
104+
105+
106+
def create_proxy(proxy_config):
107+
proxy_type = proxy_config["proxy_type"]
108+
proxy_url = create_proxy_url(proxy_config)
109+
110+
if proxy_type in ["http", "socks"] and proxy_url:
111+
return {
112+
"http://": proxy_url,
113+
"https://": proxy_url,
114+
}
115+
else:
116+
return None
117+
118+
119+
def create_proxy_url(proxy_config):
120+
proxy_type = proxy_config["proxy_type"]
121+
proxy_host = proxy_config["proxy_host"]
122+
proxy_port = proxy_config["proxy_port"]
123+
proxy_username = proxy_config["proxy_username"]
124+
proxy_password = proxy_config["proxy_password"]
125+
126+
if not proxy_host or not proxy_port:
127+
return None
128+
129+
if proxy_type == "http":
130+
return f"http://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}"
131+
elif proxy_type == "socks":
132+
return f"socks5://{proxy_username}:{proxy_password}@{proxy_host}:{proxy_port}"
133+
else:
134+
return None

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ dataclasses-json==0.6.4
1212
exceptiongroup==1.2.0
1313
fastapi==0.109.2
1414
fastapi-cache2==0.2.1
15-
fastapi_poe==0.0.42
15+
fastapi_poe==0.0.46
16+
fp==0.2
1617
frozenlist==1.4.1
1718
greenlet==3.0.3
1819
gunicorn==21.2.0
@@ -43,6 +44,7 @@ regex==2023.12.25
4344
requests==2.31.0
4445
six==1.16.0
4546
sniffio==1.3.0
47+
socksio==1.0.0
4648
SQLAlchemy==2.0.27
4749
sse-starlette==2.1.0
4850
starlette==0.36.3

0 commit comments

Comments
 (0)