Skip to content

Commit b49e128

Browse files
authored
Merge pull request ricklamers#73 from Civitasv/feat-config-proxy
feat. add configuration for proxy.
2 parents 65ef925 + 818a8ea commit b49e128

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

config.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
},
77
"openai_key": "sk-...",
88

9-
"openai_api_base": "https://api.openai.com"
10-
}
9+
"openai_api_base": "https://api.openai.com",
10+
"proxy": {
11+
"enable": true,
12+
"http": "127.0.0.1:7890",
13+
"https": "127.0.0.1:7890"
14+
}
15+
}

server/backend.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def __init__(self, app, config: dict) -> None:
1616
self.app = app
1717
self.openai_key = os.getenv("OPENAI_API_KEY") or config['openai_key']
1818
self.openai_api_base = os.getenv("OPENAI_API_BASE") or config['openai_api_base']
19+
self.proxy = config['proxy']
1920
self.routes = {
2021
'/backend-api/v2/conversation': {
2122
'function': self._conversation,
@@ -55,12 +56,27 @@ def _conversation(self):
5556
_conversation + [prompt]
5657

5758
url = f"{self.openai_api_base}/v1/chat/completions"
58-
gpt_resp = post(url,
59-
headers = {'Authorization': 'Bearer %s' % self.openai_key},
59+
60+
proxies = None
61+
if self.proxy['enable']:
62+
proxies = {
63+
'http': self.proxy['http'],
64+
'https': self.proxy['https'],
65+
}
66+
67+
gpt_resp = post(
68+
url = url,
69+
proxies = proxies,
70+
headers = {
71+
'Authorization': 'Bearer %s' % self.openai_key
72+
},
6073
json = {
6174
'model' : request.json['model'],
6275
'messages' : conversation,
63-
'stream' : True}, stream = True)
76+
'stream' : True
77+
},
78+
stream = True
79+
)
6480

6581
def stream():
6682
for chunk in gpt_resp.iter_lines():

0 commit comments

Comments
 (0)