Skip to content

Commit fdf279c

Browse files
Created a setup page and pointed it to api key and assistants routers
1 parent 8662358 commit fdf279c

File tree

8 files changed

+136
-66
lines changed

8 files changed

+136
-66
lines changed

__pycache__/main.cpython-312.pyc

-3.89 KB
Binary file not shown.

main.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
from fastapi.staticfiles import StaticFiles
77
from fastapi.templating import Jinja2Templates
88
from fastapi.responses import RedirectResponse
9-
from routers import files, messages, tools
9+
from routers import files, messages, tools, api_keys, assistants
1010

1111

1212
logger = logging.getLogger("uvicorn.error")
1313

14-
# Get the assistant ID from .env file
15-
load_dotenv()
16-
1714
@asynccontextmanager
1815
async def lifespan(app: FastAPI):
1916
# Optional startup logic
@@ -26,7 +23,8 @@ async def lifespan(app: FastAPI):
2623
app.include_router(messages.router)
2724
app.include_router(files.router)
2825
app.include_router(tools.router)
29-
26+
app.include_router(api_keys.router)
27+
app.include_router(assistants.router)
3028

3129
# Mount static files (e.g., CSS, JS)
3230
app.mount("/static", StaticFiles(directory=os.path.join(os.getcwd(), "static")), name="static")
@@ -38,11 +36,10 @@ async def lifespan(app: FastAPI):
3836
async def read_home(request: Request):
3937
logger.info("Home page requested")
4038

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")
39+
# Check if environment variables are missing
40+
load_dotenv(override=True)
41+
if not os.getenv("OPENAI_API_KEY") or not os.getenv("ASSISTANT_ID"):
42+
return RedirectResponse(url="/setup")
4643

4744
categories = {
4845
"Basic chat": "basic-chat",
@@ -115,12 +112,21 @@ async def read_all(request: Request, messages: list = [], thread_id: str = None)
115112
}
116113
)
117114

118-
# Add new warnings route
119-
@app.get("/warnings")
120-
async def read_warnings(request: Request):
115+
# Add new setup route
116+
@app.get("/setup")
117+
async def read_setup(request: Request, message: str = None):
118+
# Check if assistant ID is missing
119+
load_dotenv(override=True)
120+
if not os.getenv("OPENAI_API_KEY"):
121+
message="OpenAI API key is missing."
122+
elif not os.getenv("ASSISTANT_ID"):
123+
message="Assistant ID is missing."
124+
else:
125+
message="All set up!"
126+
121127
return templates.TemplateResponse(
122-
"warnings.html",
123-
{"request": request}
128+
"setup.html",
129+
{"request": request, "message": message}
124130
)
125131

126132
if __name__ == "__main__":

routers/api-keys.py renamed to routers/api_keys.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
import logging
2-
from fastapi import APIRouter, HTTPException
3-
from pydantic import BaseModel
2+
from fastapi import APIRouter, HTTPException, Form
3+
from fastapi.responses import RedirectResponse
44
from utils.create_assistant import update_env_file
55

66
# Configure logger
77
logger: logging.Logger = logging.getLogger("uvicorn.error")
88

99
router = APIRouter(prefix="/api-keys", tags=["API Keys"])
1010

11-
class APIKeyRequest(BaseModel):
12-
api_key: str
13-
14-
@router.post("")
15-
async def set_openai_api_key(request: APIKeyRequest):
11+
@router.post("/set")
12+
async def set_openai_api_key(api_key: str = Form()):
1613
"""
1714
Set the OpenAI API key in the application's environment variables.
1815
1916
Args:
20-
request: APIKeyRequest containing the API key
17+
api_key: OpenAI API key received from form submission
2118
2219
Returns:
23-
dict: Success message
20+
RedirectResponse: Redirects to home page on success
2421
2522
Raises:
2623
HTTPException: If there's an error updating the environment file
2724
"""
2825
try:
29-
update_env_file("OPENAI_API_KEY", request.api_key, logger)
30-
return {"message": "API key updated successfully"}
26+
update_env_file("OPENAI_API_KEY", api_key, logger)
27+
return RedirectResponse(url="/", status_code=303)
3128
except Exception as e:
3229
raise HTTPException(
3330
status_code=500,

routers/assistants.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import APIRouter, Depends
2+
from fastapi.responses import RedirectResponse
23
from openai import AsyncOpenAI
34
from utils.create_assistant import create_or_update_assistant, request
45
import logging
@@ -34,6 +35,4 @@ async def create_update_assistant(
3435
logger=logger
3536
)
3637

37-
return {
38-
"message": f"Assistant {assistant_id} successfully created/updated"
39-
}
38+
return RedirectResponse(url="/", status_code=303)

static/styles.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,42 @@ pre {
452452
color: #b8b8b8;
453453
margin-right: 8px;
454454
}
455+
456+
.setupContainer {
457+
display: flex;
458+
flex-direction: column;
459+
align-items: center;
460+
gap: 24px;
461+
max-width: 600px;
462+
width: 100%;
463+
padding: 40px 20px;
464+
}
465+
466+
.setupForm {
467+
display: flex;
468+
flex-direction: column;
469+
align-items: center;
470+
gap: 20px;
471+
width: 100%;
472+
}
473+
474+
.inputWrapper {
475+
margin-bottom: 20px;
476+
width: 100%;
477+
max-width: 400px;
478+
display: flex;
479+
justify-content: center;
480+
}
481+
482+
.setupForm form {
483+
display: flex;
484+
flex-direction: column;
485+
align-items: center;
486+
width: 100%;
487+
}
488+
489+
.setupMessage {
490+
font-size: 1.1em;
491+
text-align: center;
492+
margin-bottom: 8px;
493+
}

templates/components/warnings.html

Lines changed: 0 additions & 22 deletions
This file was deleted.

templates/index.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
{% extends 'layout.html' %}
2-
3-
{% block content %}
4-
<main>
5-
<div class="title">
6-
Explore sample apps built with Assistants API
7-
</div>
8-
<div class="container">
9-
{% for name, url in categories.items() %}
10-
<a class="category" href="/{{ url }}">
11-
{{ name }}
12-
</a>
13-
{% endfor %}
14-
</div>
15-
</main>
1+
{% extends 'layout.html' %}
2+
3+
{% block content %}
4+
<main class="main">
5+
<div class="title">
6+
Explore sample apps built with Assistants API
7+
</div>
8+
<div class="container">
9+
{% for name, url in categories.items() %}
10+
<a class="category" href="/{{ url }}">
11+
{{ name }}
12+
</a>
13+
{% endfor %}
14+
</div>
15+
</main>
1616
{% endblock %}

templates/setup.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{% extends "layout.html" %}
2+
3+
{% block content %}
4+
<main class="main">
5+
<div class="container">
6+
<div class="setupContainer">
7+
<h1 class="title">Setup Required</h1>
8+
9+
{% if "API key" in message %}
10+
<div class="setupForm">
11+
<p class="setupMessage">Please enter your OpenAI API key to continue:</p>
12+
<form action="/api-keys/set" method="POST">
13+
<div class="inputWrapper">
14+
<input
15+
type="password"
16+
name="api_key"
17+
placeholder="sk-..."
18+
required
19+
class="input"
20+
>
21+
</div>
22+
<button
23+
type="submit"
24+
class="button"
25+
>
26+
Save API Key
27+
</button>
28+
</form>
29+
</div>
30+
{% elif "Assistant ID" in message %}
31+
<div class="setupForm">
32+
<p class="setupMessage">You need to create an assistant to continue:</p>
33+
<form action="/assistants/create-update" method="POST">
34+
<button
35+
type="submit"
36+
class="button"
37+
>
38+
Create Assistant
39+
</button>
40+
</form>
41+
</div>
42+
{% elif message == "All set up!" %}
43+
<div class="setupForm">
44+
<p class="setupMessage">Your application is already configured and ready to use.</p>
45+
<a href="/" class="button">Return to Home</a>
46+
</div>
47+
{% endif %}
48+
</div>
49+
</div>
50+
</main>
51+
{% endblock %}

0 commit comments

Comments
 (0)