Skip to content

Commit 1b4b855

Browse files
Redirect user to warnings.html if assistant ID or OpenAI API key is missing
1 parent 783d204 commit 1b4b855

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

main.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import os
22
import logging
3-
import dotenv
3+
from dotenv import load_dotenv
44
from contextlib import asynccontextmanager
55
from fastapi import FastAPI, Request
66
from fastapi.staticfiles import StaticFiles
77
from fastapi.templating import Jinja2Templates
8-
from routers import messages, assistants_files, tool_outputs
8+
from fastapi.responses import RedirectResponse
9+
from routers import files, messages, tools
910

1011

1112
logger = logging.getLogger("uvicorn.error")
1213

13-
dotenv.load_dotenv()
14+
# Get the assistant ID from .env file
15+
load_dotenv()
1416

1517
@asynccontextmanager
1618
async def lifespan(app: FastAPI):
@@ -22,8 +24,8 @@ async def lifespan(app: FastAPI):
2224

2325
# Mount routers
2426
app.include_router(messages.router)
25-
app.include_router(assistants_files.router)
26-
app.include_router(tool_outputs.router)
27+
app.include_router(files.router)
28+
app.include_router(tools.router)
2729

2830

2931
# Mount static files (e.g., CSS, JS)
@@ -36,6 +38,12 @@ async def lifespan(app: FastAPI):
3638
async def read_home(request: Request):
3739
logger.info("Home page requested")
3840

41+
# Check if assistant ID is missing
42+
if not os.getenv("ASSISTANT_ID"):
43+
return RedirectResponse(url="/warnings", message="Assistant ID is missing")
44+
elif not os.getenv("OPENAI_API_KEY"):
45+
return RedirectResponse(url="/warnings", message="OpenAI API key is missing")
46+
3947
categories = {
4048
"Basic chat": "basic-chat",
4149
"File search": "file-search",
@@ -107,6 +115,14 @@ async def read_all(request: Request, messages: list = [], thread_id: str = None)
107115
}
108116
)
109117

118+
# Add new warnings route
119+
@app.get("/warnings")
120+
async def read_warnings(request: Request):
121+
return templates.TemplateResponse(
122+
"warnings.html",
123+
{"request": request}
124+
)
125+
110126
if __name__ == "__main__":
111127
import uvicorn
112128

0 commit comments

Comments
 (0)