-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (58 loc) · 1.78 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from flask import Flask, request, Response
import requests
from libs.logger import logger
app = Flask(__name__)
OPENAI_BASE_URL = "https://api.openai.com/v1"
# OPENAI_BASE_URL = "https://fake.test.com/v1"
@app.before_request
def proxy():
method = request.method
path = request.full_path
data = request.get_data()
params = request.args
headers = {"Content-Type": request.content_type}
authorization = request.authorization
token = "invalid token."
if authorization is not None and authorization.token is not None:
token = authorization.token
elif authorization is not None and "password" in authorization:
token = authorization.password
else:
logger.error(token)
isStream = False
if (
request.json is not None
and "stream" in request.json
and type(request.json["stream"]) is bool
):
isStream = request.json["stream"]
res = requests.request(
method=method,
url=OPENAI_BASE_URL + path,
headers=headers,
data=data,
auth=(str('bearer'), str(token)),
stream=isStream,
)
# res_headers = list(res.headers)
# final_headers = {}
# for one_header in res_headers:
# final_headers[one_header] = res.headers[one_header]
final_headers = res.headers.items()
if isStream:
return Response(
response=res.iter_content(chunk_size=1024),
status=res.status_code,
headers=final_headers,
)
else:
return Response(
response=res.content,
status=res.status_code,
)
# @app.route("/", methods=["GET", "POST"])
# def hello_world():
# print(request)
# return "<p>Hello, World!</p>"
if __name__ == "__main__":
app.run(port=8885, host="0.0.0.0")