Spaces:
Runtime error
Runtime error
Eric Michael Martinez
commited on
Commit
·
8086001
1
Parent(s):
9884bb9
update
Browse files- app/app.py +67 -2
- requirements.txt +1 -0
app/app.py
CHANGED
@@ -8,6 +8,7 @@ from app.db import User, create_db_and_tables
|
|
8 |
from app.schemas import UserCreate, UserRead, UserUpdate
|
9 |
from app.users import auth_backend, current_active_user, fastapi_users
|
10 |
from dotenv import load_dotenv
|
|
|
11 |
|
12 |
import examples as chatbot_examples
|
13 |
|
@@ -244,6 +245,7 @@ async def authenticated_route(user: User = Depends(current_active_user)):
|
|
244 |
return {"message": f"Hello {user.email}!"}
|
245 |
|
246 |
|
|
|
247 |
@app.post("/v1/embeddings")
|
248 |
async def openai_api_embeddings_passthrough(
|
249 |
request: Request,
|
@@ -273,6 +275,7 @@ async def openai_api_embeddings_passthrough(
|
|
273 |
return response.json()
|
274 |
|
275 |
|
|
|
276 |
@app.post("/v1/engines/text-embedding-ada-002/embeddings")
|
277 |
async def openai_api_embeddings_passthrough(
|
278 |
request: Request,
|
@@ -302,6 +305,7 @@ async def openai_api_embeddings_passthrough(
|
|
302 |
return response.json()
|
303 |
|
304 |
|
|
|
305 |
@app.post("/v1/completions")
|
306 |
async def openai_api_completions_passthrough(
|
307 |
request: Request,
|
@@ -331,6 +335,7 @@ async def openai_api_completions_passthrough(
|
|
331 |
return response.json()
|
332 |
|
333 |
|
|
|
334 |
@app.post("/v1/chat/completions")
|
335 |
async def openai_api_chat_completions_passthrough(
|
336 |
request: Request,
|
@@ -344,8 +349,8 @@ async def openai_api_chat_completions_passthrough(
|
|
344 |
request_headers = request.headers
|
345 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
346 |
|
347 |
-
if "gpt-4" in request_data["model"]:
|
348 |
-
|
349 |
|
350 |
# Forward the request to the OpenAI API
|
351 |
async with AsyncClient() as client:
|
@@ -363,6 +368,66 @@ async def openai_api_chat_completions_passthrough(
|
|
363 |
return response.json()
|
364 |
|
365 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
366 |
@app.on_event("startup")
|
367 |
async def on_startup():
|
368 |
# Not needed if you setup a migration system like Alembic
|
|
|
8 |
from app.schemas import UserCreate, UserRead, UserUpdate
|
9 |
from app.users import auth_backend, current_active_user, fastapi_users
|
10 |
from dotenv import load_dotenv
|
11 |
+
from tenacity import retry, stop_after_attempt, wait_exponential
|
12 |
|
13 |
import examples as chatbot_examples
|
14 |
|
|
|
245 |
return {"message": f"Hello {user.email}!"}
|
246 |
|
247 |
|
248 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
249 |
@app.post("/v1/embeddings")
|
250 |
async def openai_api_embeddings_passthrough(
|
251 |
request: Request,
|
|
|
275 |
return response.json()
|
276 |
|
277 |
|
278 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
279 |
@app.post("/v1/engines/text-embedding-ada-002/embeddings")
|
280 |
async def openai_api_embeddings_passthrough(
|
281 |
request: Request,
|
|
|
305 |
return response.json()
|
306 |
|
307 |
|
308 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
309 |
@app.post("/v1/completions")
|
310 |
async def openai_api_completions_passthrough(
|
311 |
request: Request,
|
|
|
335 |
return response.json()
|
336 |
|
337 |
|
338 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
339 |
@app.post("/v1/chat/completions")
|
340 |
async def openai_api_chat_completions_passthrough(
|
341 |
request: Request,
|
|
|
349 |
request_headers = request.headers
|
350 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
351 |
|
352 |
+
# if "gpt-4" in request_data["model"]:
|
353 |
+
# request_data["model"] = "gpt-3.5-turbo"
|
354 |
|
355 |
# Forward the request to the OpenAI API
|
356 |
async with AsyncClient() as client:
|
|
|
368 |
return response.json()
|
369 |
|
370 |
|
371 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
372 |
+
@app.post("/v1/images/generations")
|
373 |
+
async def openai_api_chat_completions_passthrough(
|
374 |
+
request: Request,
|
375 |
+
user: User = Depends(fastapi_users.current_user()),
|
376 |
+
):
|
377 |
+
if not user:
|
378 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
379 |
+
|
380 |
+
# Get the request data and headers
|
381 |
+
request_data = await request.json()
|
382 |
+
request_headers = request.headers
|
383 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
384 |
+
|
385 |
+
# Forward the request to the OpenAI API
|
386 |
+
async with AsyncClient() as client:
|
387 |
+
response = await client.post(
|
388 |
+
"https://api.openai.com/v1/images/generations",
|
389 |
+
json=request_data,
|
390 |
+
headers={
|
391 |
+
"Content-Type": request_headers.get("Content-Type"),
|
392 |
+
"Authorization": f"Bearer {openai_api_key}",
|
393 |
+
},
|
394 |
+
timeout=120.0,
|
395 |
+
)
|
396 |
+
|
397 |
+
# Return the OpenAI API response
|
398 |
+
return response.json()
|
399 |
+
|
400 |
+
|
401 |
+
@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, min=2, max=30))
|
402 |
+
@app.post("/v1/audio/speech")
|
403 |
+
async def openai_api_audio_speech_passthrough(
|
404 |
+
request: Request,
|
405 |
+
user: User = Depends(fastapi_users.current_user()),
|
406 |
+
):
|
407 |
+
if not user:
|
408 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
409 |
+
|
410 |
+
# Get the request data and headers
|
411 |
+
request_data = await request.json()
|
412 |
+
request_headers = request.headers
|
413 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
414 |
+
|
415 |
+
# Forward the request to the OpenAI API
|
416 |
+
async with AsyncClient() as client:
|
417 |
+
response = await client.post(
|
418 |
+
"https://api.openai.com/v1/audio/speech",
|
419 |
+
json=request_data,
|
420 |
+
headers={
|
421 |
+
"Content-Type": request_headers.get("Content-Type"),
|
422 |
+
"Authorization": f"Bearer {openai_api_key}",
|
423 |
+
},
|
424 |
+
timeout=120.0,
|
425 |
+
)
|
426 |
+
|
427 |
+
# Return the OpenAI API response
|
428 |
+
return response.json()
|
429 |
+
|
430 |
+
|
431 |
@app.on_event("startup")
|
432 |
async def on_startup():
|
433 |
# Not needed if you setup a migration system like Alembic
|
requirements.txt
CHANGED
@@ -8,3 +8,4 @@ Requests
|
|
8 |
sqlalchemy[asyncio]
|
9 |
uvicorn
|
10 |
asyncpg
|
|
|
|
8 |
sqlalchemy[asyncio]
|
9 |
uvicorn
|
10 |
asyncpg
|
11 |
+
tenacity
|