Fix the error of name not defined
Browse filesAdd support for configuring CORS middleware
- main.py +15 -1
- request.py +4 -0
main.py
CHANGED
@@ -14,7 +14,8 @@ from response import fetch_response, fetch_response_stream
|
|
14 |
|
15 |
from typing import List, Dict
|
16 |
from urllib.parse import urlparse
|
17 |
-
from fastapi.responses import StreamingResponse
|
|
|
18 |
|
19 |
@asynccontextmanager
|
20 |
async def lifespan(app: FastAPI):
|
@@ -27,6 +28,15 @@ async def lifespan(app: FastAPI):
|
|
27 |
|
28 |
app = FastAPI(lifespan=lifespan)
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
async def process_request(request: RequestModel, provider: Dict):
|
31 |
print("provider: ", provider['provider'])
|
32 |
url = provider['base_url']
|
@@ -144,6 +154,10 @@ model_handler = ModelRequestHandler()
|
|
144 |
async def request_model(request: RequestModel, token: str = Depends(verify_api_key)):
|
145 |
return await model_handler.request_model(request, token)
|
146 |
|
|
|
|
|
|
|
|
|
147 |
@app.post("/v1/models")
|
148 |
async def list_models(token: str = Depends(verify_api_key)):
|
149 |
models = get_all_models(token)
|
|
|
14 |
|
15 |
from typing import List, Dict
|
16 |
from urllib.parse import urlparse
|
17 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
18 |
+
from fastapi.middleware.cors import CORSMiddleware
|
19 |
|
20 |
@asynccontextmanager
|
21 |
async def lifespan(app: FastAPI):
|
|
|
28 |
|
29 |
app = FastAPI(lifespan=lifespan)
|
30 |
|
31 |
+
# 配置 CORS 中间件
|
32 |
+
app.add_middleware(
|
33 |
+
CORSMiddleware,
|
34 |
+
allow_origins=["*"], # 允许所有来源
|
35 |
+
allow_credentials=True,
|
36 |
+
allow_methods=["*"], # 允许所有 HTTP 方法
|
37 |
+
allow_headers=["*"], # 允许所有头部字段
|
38 |
+
)
|
39 |
+
|
40 |
async def process_request(request: RequestModel, provider: Dict):
|
41 |
print("provider: ", provider['provider'])
|
42 |
url = provider['base_url']
|
|
|
154 |
async def request_model(request: RequestModel, token: str = Depends(verify_api_key)):
|
155 |
return await model_handler.request_model(request, token)
|
156 |
|
157 |
+
@app.options("/v1/chat/completions")
|
158 |
+
async def options_handler():
|
159 |
+
return JSONResponse(status_code=200, content={"detail": "OPTIONS allowed"})
|
160 |
+
|
161 |
@app.post("/v1/models")
|
162 |
async def list_models(token: str = Depends(verify_api_key)):
|
163 |
models = get_all_models(token)
|
request.py
CHANGED
@@ -46,6 +46,7 @@ async def get_gemini_payload(request, engine, provider):
|
|
46 |
|
47 |
messages = []
|
48 |
for msg in request.messages:
|
|
|
49 |
if isinstance(msg.content, list):
|
50 |
content = []
|
51 |
for item in msg.content:
|
@@ -117,6 +118,7 @@ async def get_gpt_payload(request, engine, provider):
|
|
117 |
|
118 |
messages = []
|
119 |
for msg in request.messages:
|
|
|
120 |
if isinstance(msg.content, list):
|
121 |
content = []
|
122 |
for item in msg.content:
|
@@ -164,6 +166,7 @@ async def get_openrouter_payload(request, engine, provider):
|
|
164 |
|
165 |
messages = []
|
166 |
for msg in request.messages:
|
|
|
167 |
if isinstance(msg.content, list):
|
168 |
content = []
|
169 |
for item in msg.content:
|
@@ -247,6 +250,7 @@ async def get_claude_payload(request, engine, provider):
|
|
247 |
|
248 |
messages = []
|
249 |
for msg in request.messages:
|
|
|
250 |
if isinstance(msg.content, list):
|
251 |
content = []
|
252 |
for item in msg.content:
|
|
|
46 |
|
47 |
messages = []
|
48 |
for msg in request.messages:
|
49 |
+
name = None
|
50 |
if isinstance(msg.content, list):
|
51 |
content = []
|
52 |
for item in msg.content:
|
|
|
118 |
|
119 |
messages = []
|
120 |
for msg in request.messages:
|
121 |
+
name = None
|
122 |
if isinstance(msg.content, list):
|
123 |
content = []
|
124 |
for item in msg.content:
|
|
|
166 |
|
167 |
messages = []
|
168 |
for msg in request.messages:
|
169 |
+
name = None
|
170 |
if isinstance(msg.content, list):
|
171 |
content = []
|
172 |
for item in msg.content:
|
|
|
250 |
|
251 |
messages = []
|
252 |
for msg in request.messages:
|
253 |
+
name = None
|
254 |
if isinstance(msg.content, list):
|
255 |
content = []
|
256 |
for item in msg.content:
|