Skip to content

Commit d5a0343

Browse files
Merge pull request #20 from Promptly-Technologies-LLC/event-handling
Working custom tool call stream
2 parents 53276d3 + dedafa3 commit d5a0343

23 files changed

+707
-403
lines changed

main.py

Lines changed: 7 additions & 101 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

@@ -33,124 +32,31 @@ async def lifespan(app: FastAPI):
3332
# Initialize Jinja2 templates
3433
templates = Jinja2Templates(directory="templates")
3534

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

4041
# Check if environment variables are missing
4142
load_dotenv(override=True)
4243
if not os.getenv("OPENAI_API_KEY") or not os.getenv("ASSISTANT_ID"):
4344
return RedirectResponse(url="/setup")
4445

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-
6746
# Create a new assistant chat thread if no thread ID is provided
6847
if not thread_id or thread_id == "None" or thread_id == "null":
6948
thread_id: str = await create_thread()
7049

7150
return templates.TemplateResponse(
72-
"examples/basic-chat.html",
51+
"index.html",
7352
{
7453
"request": request,
75-
"assistant_id": assistant_id,
54+
"assistant_id": os.getenv("ASSISTANT_ID"),
7655
"messages": messages,
7756
"thread_id": thread_id
7857
}
7958
)
8059

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-
)
15460

15561
# Add new setup route
15662
@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 = [

0 commit comments

Comments
 (0)