Skip to content

Commit 6ca9c8a

Browse files
Removed some unnecessary abstractions
1 parent 72c9df4 commit 6ca9c8a

File tree

9 files changed

+58
-236
lines changed

9 files changed

+58
-236
lines changed

main.py

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ async def lifespan(app: FastAPI):
3333
# Initialize Jinja2 templates
3434
templates = Jinja2Templates(directory="templates")
3535

36+
# TODO: Implement some kind of thread id storage or management logic to allow
37+
# user to load an old thread, delete an old thread, etc. instead of start new
3638
@app.get("/")
3739
async def read_home(request: Request):
3840
logger.info("Home page requested")
@@ -42,115 +44,20 @@ async def read_home(request: Request):
4244
if not os.getenv("OPENAI_API_KEY") or not os.getenv("ASSISTANT_ID"):
4345
return RedirectResponse(url="/setup")
4446

45-
categories = {
46-
"Basic chat": "basic-chat",
47-
"File search": "file-search",
48-
"Function calling": "function-calling",
49-
"All": "all",
50-
}
51-
return templates.TemplateResponse(
52-
"index.html",
53-
{
54-
"request": request,
55-
"categories": categories
56-
}
57-
)
58-
59-
# TODO: Implement some kind of thread id storage or management logic to allow
60-
# user to load an old thread, delete an old thread, etc. instead of start new
61-
@app.get("/basic-chat")
62-
async def read_basic_chat(request: Request, messages: list = [], thread_id: str = None):
63-
# Get assistant ID from environment variables
64-
load_dotenv()
65-
assistant_id = os.getenv("ASSISTANT_ID")
66-
6747
# Create a new assistant chat thread if no thread ID is provided
6848
if not thread_id or thread_id == "None" or thread_id == "null":
6949
thread_id: str = await create_thread()
7050

7151
return templates.TemplateResponse(
72-
"examples/basic-chat.html",
52+
"index.html",
7353
{
7454
"request": request,
75-
"assistant_id": assistant_id,
55+
"assistant_id": os.getenv("ASSISTANT_ID"),
7656
"messages": messages,
7757
"thread_id": thread_id
7858
}
7959
)
8060

81-
@app.get("/file-search")
82-
async def read_file_search(request: Request, messages: list = [], thread_id: str = None):
83-
# Get assistant ID from environment variables
84-
load_dotenv()
85-
assistant_id = os.getenv("ASSISTANT_ID")
86-
87-
# Create a new assistant chat thread if no thread ID is provided
88-
if not thread_id or thread_id == "None" or thread_id == "null":
89-
thread_id: str = await create_thread()
90-
91-
return templates.TemplateResponse(
92-
"examples/file-search.html",
93-
{
94-
"request": request,
95-
"messages": messages,
96-
"thread_id": thread_id,
97-
"assistant_id": assistant_id, # Add assistant_id to template context
98-
}
99-
)
100-
101-
@app.get("/function-calling")
102-
async def read_function_calling(request: Request, messages: list = [], thread_id: str = None):
103-
# Get assistant ID from environment variables
104-
load_dotenv()
105-
assistant_id = os.getenv("ASSISTANT_ID")
106-
107-
# Create a new assistant chat thread if no thread ID is provided
108-
if not thread_id or thread_id == "None" or thread_id == "null":
109-
thread_id: str = await create_thread()
110-
111-
# Define the condition class map
112-
conditionClassMap = {
113-
"Cloudy": "weatherBGCloudy",
114-
"Sunny": "weatherBGSunny",
115-
"Rainy": "weatherBGRainy",
116-
"Snowy": "weatherBGSnowy",
117-
"Windy": "weatherBGWindy",
118-
}
119-
120-
return templates.TemplateResponse(
121-
"examples/function-calling.html",
122-
{
123-
"conditionClassMap": conditionClassMap,
124-
"location": "---",
125-
"temperature": "---",
126-
"conditions": "Sunny",
127-
"isEmpty": True,
128-
"thread_id": thread_id,
129-
"messages": messages,
130-
"assistant_id": assistant_id, # Add assistant_id to template context
131-
}
132-
)
133-
134-
135-
@app.get("/all")
136-
async def read_all(request: Request, messages: list = [], thread_id: str = None):
137-
# Get assistant ID from environment variables
138-
load_dotenv()
139-
assistant_id = os.getenv("ASSISTANT_ID")
140-
141-
# Create a new assistant chat thread if no thread ID is provided
142-
if not thread_id or thread_id == "None" or thread_id == "null":
143-
thread_id: str = await create_thread()
144-
145-
return templates.TemplateResponse(
146-
"examples/all.html",
147-
{
148-
"request": request,
149-
"assistant_id": assistant_id, # Add assistant_id to template context
150-
"thread_id": thread_id,
151-
"messages": messages
152-
}
153-
)
15461

15562
# Add new setup route
15663
@app.get("/setup")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "openai-assistants-python-quickstart"
33
version = "0.1.0"
4-
description = "Add your description here"
4+
description = "A quickstart template using the OpenAI Assistants API with Python, FastAPI, Jinja2, and HTMX"
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [

templates/components/chat.html

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

templates/examples/all.html

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

templates/examples/basic-chat.html

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

templates/examples/file-search.html

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

templates/examples/function-calling.html

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

templates/index.html

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
1-
{% extends 'layout.html' %}
1+
{% extends "layout.html" %}
22

33
{% 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>
16-
{% endblock %}
4+
<div class="chatContainer">
5+
<div id="messages" class="messages">
6+
7+
{% for msg in messages %}
8+
{% if msg.role == "user" %}
9+
<div class="userMessage">{{ msg.text }}</div>
10+
{% elif msg.role == "assistant" %}
11+
<div class="assistantMessage">
12+
{{ msg.text | safe }}
13+
</div>
14+
{% elif msg.role == "code" %}
15+
<div class="codeMessage">
16+
{% for line in msg.text.split('\n') %}
17+
<div>
18+
<span>{{ loop.index }}. </span>{{ line }}
19+
</div>
20+
{% endfor %}
21+
</div>
22+
{% endif %}
23+
{% endfor %}
24+
</div>
25+
<form id="chatForm" class="inputForm clearfix"
26+
hx-on::after-request="this.reset()">
27+
<input
28+
type="text"
29+
class="input"
30+
name="userInput"
31+
placeholder="Enter your question"
32+
id="userInput"
33+
/>
34+
<button
35+
type="submit"
36+
class="button"
37+
hx-post="/assistants/{{ assistant_id }}/messages/{{ thread_id }}/send"
38+
hx-target="#messages"
39+
hx-swap="beforeend"
40+
{% if inputDisabled %}disabled{% endif %}
41+
>
42+
Send
43+
</button>
44+
</form>
45+
</div>
46+
{% endblock %}

templates/layout.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Assistants API Quickstart</title>
6-
<meta name="description" content="A quickstart template using the Assistants API with OpenAI">
6+
<meta name="description" content="A quickstart template using the OpenAI Assistants API with Python, FastAPI, Jinja2, and HTMX">
77
<link rel="icon" href="{{ url_for('static', path='openai.svg') }}">
88
<link rel="stylesheet" href="{{ url_for('static', path='styles.css') }}">
99
<link rel="favicon" href="{{ url_for('static', path='favicon.png') }}">
10-
<script src="{{ url_for('static', path='htmx.min.js') }}" integrity="sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" crossorigin="anonymous"></script>
10+
<script src="{{ url_for('static', path='htmx.min.js') }}"></script>
1111
<script src="{{ url_for('static', path='sse.js') }}"></script>
1212
</head>
1313
<body>
14-
{% block content %}
15-
<!-- Content will be injected here -->
16-
{% endblock %}
14+
<main class="main">
15+
<div class="container">
16+
{% block content %}
17+
<!-- Content will be injected here -->
18+
{% endblock %}
19+
</div>
20+
</main>
1721
<img class="logo" src="{{ url_for('static', path='openai.svg') }}" alt="OpenAI Logo" />
1822
</body>
1923
</html>

0 commit comments

Comments
 (0)