6
6
from fastapi .staticfiles import StaticFiles
7
7
from fastapi .templating import Jinja2Templates
8
8
from fastapi .responses import RedirectResponse
9
- from routers import files , messages , tools , api_keys , assistants
9
+ from routers import chat , files , api_keys , assistants
10
10
from utils .threads import create_thread
11
11
12
12
@@ -21,9 +21,8 @@ async def lifespan(app: FastAPI):
21
21
app = FastAPI (lifespan = lifespan )
22
22
23
23
# Mount routers
24
- app .include_router (messages .router )
24
+ app .include_router (chat .router )
25
25
app .include_router (files .router )
26
- app .include_router (tools .router )
27
26
app .include_router (api_keys .router )
28
27
app .include_router (assistants .router )
29
28
@@ -33,124 +32,31 @@ async def lifespan(app: FastAPI):
33
32
# Initialize Jinja2 templates
34
33
templates = Jinja2Templates (directory = "templates" )
35
34
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
36
37
@app .get ("/" )
37
- async def read_home (request : Request ):
38
+ async def read_home (request : Request , thread_id : str = None , messages : list = [] ):
38
39
logger .info ("Home page requested" )
39
40
40
41
# Check if environment variables are missing
41
42
load_dotenv (override = True )
42
43
if not os .getenv ("OPENAI_API_KEY" ) or not os .getenv ("ASSISTANT_ID" ):
43
44
return RedirectResponse (url = "/setup" )
44
45
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
-
67
46
# Create a new assistant chat thread if no thread ID is provided
68
47
if not thread_id or thread_id == "None" or thread_id == "null" :
69
48
thread_id : str = await create_thread ()
70
49
71
50
return templates .TemplateResponse (
72
- "examples/basic-chat .html" ,
51
+ "index .html" ,
73
52
{
74
53
"request" : request ,
75
- "assistant_id" : assistant_id ,
54
+ "assistant_id" : os . getenv ( "ASSISTANT_ID" ) ,
76
55
"messages" : messages ,
77
56
"thread_id" : thread_id
78
57
}
79
58
)
80
59
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
- )
154
60
155
61
# Add new setup route
156
62
@app .get ("/setup" )
0 commit comments