Skip to content

Commit fd4d64b

Browse files
Fixed a couple bugs
1 parent e154fbb commit fd4d64b

File tree

6 files changed

+57
-62
lines changed

6 files changed

+57
-62
lines changed

main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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, api_keys, assistants
9+
from routers import chat, files, api_keys, assistants
1010
from utils.threads import create_thread
1111

1212

@@ -21,9 +21,8 @@ async def lifespan(app: FastAPI):
2121
app = FastAPI(lifespan=lifespan)
2222

2323
# Mount routers
24-
app.include_router(messages.router)
24+
app.include_router(chat.router)
2525
app.include_router(files.router)
26-
app.include_router(tools.router)
2726
app.include_router(api_keys.router)
2827
app.include_router(assistants.router)
2928

@@ -36,7 +35,7 @@ async def lifespan(app: FastAPI):
3635
# TODO: Implement some kind of thread id storage or management logic to allow
3736
# user to load an old thread, delete an old thread, etc. instead of start new
3837
@app.get("/")
39-
async def read_home(request: Request):
38+
async def read_home(request: Request, thread_id: str = None, messages: list = []):
4039
logger.info("Home page requested")
4140

4241
# Check if environment variables are missing

routers/messages.py renamed to routers/chat.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from fastapi.templating import Jinja2Templates
33
from fastapi import APIRouter, Form, Depends, Request
4-
from fastapi.responses import StreamingResponse
4+
from fastapi.responses import StreamingResponse, HTMLResponse
55
from openai import AsyncOpenAI, AssistantEventHandler
66
from openai.resources.beta.threads.runs.runs import AsyncAssistantStreamManager
77
from openai.types.beta.threads.runs import RunStep, RunStepDelta
@@ -78,31 +78,32 @@ def on_tool_call_delta(self, delta, snapshot):
7878
# Route to submit a new user message to a thread and mount a component that
7979
# will start an assistant run stream
8080
@router.post("/send")
81-
async def post_message(
81+
async def send_message(
8282
request: Request,
8383
assistant_id: str,
8484
thread_id: str,
8585
userInput: str = Form(...),
8686
client: AsyncOpenAI = Depends(lambda: AsyncOpenAI())
87-
) -> dict:
87+
) -> HTMLResponse:
8888
# Create a new message in the thread
8989
await client.beta.threads.messages.create(
9090
thread_id=thread_id,
9191
role="user",
9292
content=userInput
93-
9493
)
9594

9695
# Render the component templates with the context
97-
user_message_html = templates.get_template("user_message.html").render(user_input=userInput)
98-
assistant_run_html = templates.get_template("assistant_run.html").render(
96+
user_message_html = templates.get_template("components/user-message.html").render(user_input=userInput)
97+
assistant_run_html = templates.get_template("components/assistant-run.html").render(
9998
assistant_id=assistant_id,
10099
thread_id=thread_id
101100
)
102101

103-
return (
104-
user_message_html +
105-
assistant_run_html
102+
return HTMLResponse(
103+
content=(
104+
user_message_html +
105+
assistant_run_html
106+
)
106107
)
107108

108109

@@ -124,7 +125,7 @@ async def event_generator():
124125

125126
async with stream_manager as event_handler:
126127
async for text in event_handler.text_deltas:
127-
yield f"data: {text.replace('\n', '<br>')}\n\n"
128+
yield f"data: {text.replace('\n', '&#10;')}\n\n"
128129

129130
# Send a done event when the stream is complete
130131
yield "event: EndMessage\ndata: DONE\n\n"
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- assistant-step.html -->
2-
<div class="{event_type}"
3-
sse-swap="{event_name}"
2+
<div class="{{ event_type }}"
3+
sse-swap="{{ event_name }}"
44
hx-swap="beforeend">
55
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<!-- user-message.html -->
22
<div class="userMessage">
3-
{user_input}
3+
{{ user_input }}
44
</div>

templates/setup.html

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,47 @@
22
{% extends "layout.html" %}
33

44
{% block content %}
5-
<main class="main">
6-
7-
<div class="container">
85
<div class="setupContainer">
9-
<h1 class="title">Setup Required</h1>
6+
<h1 class="title">Setup Required</h1>
107

11-
{% if "API key" in message %}
12-
<div class="setupForm">
13-
<p class="setupMessage">Please enter your OpenAI API key to continue:</p>
14-
<form action="/api-keys/set" method="POST">
15-
<div class="inputWrapper">
16-
<input
17-
type="password"
18-
name="api_key"
19-
placeholder="sk-..."
20-
required
21-
class="input"
22-
>
23-
</div>
24-
<button
25-
type="submit"
26-
class="button"
27-
>
28-
Save API Key
29-
</button>
30-
</form>
31-
</div>
32-
{% elif "Assistant ID" in message %}
33-
<div class="setupForm">
34-
<p class="setupMessage">You need to create an assistant to continue:</p>
35-
<form action="/assistants/create-update" method="POST">
36-
<button
37-
type="submit"
38-
class="button"
39-
>
40-
Create Assistant
41-
</button>
42-
</form>
43-
</div>
44-
{% elif message == "All set up!" %}
45-
<div class="setupForm">
46-
<p class="setupMessage">Your application is already configured and ready to use.</p>
47-
<a href="/" class="button">Return to Home</a>
48-
</div>
49-
{% endif %}
8+
{% if "API key" in message %}
9+
<div class="setupForm">
10+
<p class="setupMessage">Please enter your OpenAI API key to continue:</p>
11+
<form action="/api-keys/set" method="POST">
12+
<div class="inputWrapper">
13+
<input
14+
type="password"
15+
name="api_key"
16+
placeholder="sk-..."
17+
required
18+
class="input"
19+
>
20+
</div>
21+
<button
22+
type="submit"
23+
class="button"
24+
>
25+
Save API Key
26+
</button>
27+
</form>
28+
</div>
29+
{% elif "Assistant ID" in message %}
30+
<div class="setupForm">
31+
<p class="setupMessage">You need to create an assistant to continue:</p>
32+
<form action="/assistants/create-update" method="POST">
33+
<button
34+
type="submit"
35+
class="button"
36+
>
37+
Create Assistant
38+
</button>
39+
</form>
40+
</div>
41+
{% elif message == "All set up!" %}
42+
<div class="setupForm">
43+
<p class="setupMessage">Your application is already configured and ready to use.</p>
44+
<a href="/" class="button">Return to Home</a>
45+
</div>
46+
{% endif %}
5047
</div>
51-
</div>
52-
</main>
5348
{% endblock %}

0 commit comments

Comments
 (0)