-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
117 lines (98 loc) · 3.21 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# import sys, os
# sys.path.insert(
# 0, os.path.abspath("../")
# ) # Adds the parent directory to the system path
from fastapi import FastAPI, Request, status, HTTPException, Depends
from fastapi.responses import StreamingResponse
from fastapi.security import OAuth2PasswordBearer
from fastapi.middleware.cors import CORSMiddleware
import uuid
import openai
from openai import AsyncOpenAI
import aiohttp
import litellm
from litellm.router import Router
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
litellm_client = AsyncOpenAI(
base_url="https://exampleopenaiendpoint-production.up.railway.app/",
api_key="sk-1234",
)
http_client: aiohttp.ClientSession = None
# for completion
@app.post("/chat/completions")
@app.post("/v1/chat/completions")
async def proxy_completion(request: Request):
global http_client
# Get the raw request body
body = await request.json()
# Get the authorization header
auth_header = request.headers.get('Authorization')
if not auth_header:
raise HTTPException(status_code=401, detail="Authorization header missing")
if http_client is None:
http_client = aiohttp.ClientSession()
headers = {
'Content-Type': 'application/json',
'Authorization': auth_header
}
async with http_client.post(
'https://example-openai-endpoint.onrender.com/chat/completions',
headers=headers,
json=body
) as response:
return await response.json()
router = Router(
model_list=[
{
"model_name": "fake-openai-endpoint",
"litellm_params": {
"model": "aiohttp_openai/any",
"api_key": "my-key",
"api_base": "https://example-openai-endpoint.onrender.com/v1/chat/completions",
},
}
]
)
@app.post("/lite/chat/completions")
@app.post("/lite/v1/chat/completions")
async def lite_completion(request: Request):
# Get the raw request body
body = await request.json()
body.pop("model", None)
# Get the authorization header
auth_header = request.headers.get('Authorization')
if not auth_header:
raise HTTPException(status_code=401, detail="Authorization header missing")
response = await router.acompletion(
model="fake-openai-endpoint",
**body,
)
return response
@app.post("/lite_sdk/chat/completions")
@app.post("/lite_sdk/v1/chat/completions")
async def lite_sdk_completion(request: Request):
# Get the raw request body
body = await request.json()
body.pop("model", None)
# Get the authorization header
auth_header = request.headers.get('Authorization')
if not auth_header:
raise HTTPException(status_code=401, detail="Authorization header missing")
response = await litellm.acompletion(
model="aiohttp_openai/fake-openai-endpoint",
api_base="https://example-openai-endpoint.onrender.com/v1/chat/completions",
api_key="my-key",
**body,
)
return response
if __name__ == "__main__":
import uvicorn
# run this on 8090, 8091, 8092 and 8093
uvicorn.run(app, host="0.0.0.0", port=8090)