@@ -115,61 +115,48 @@ async def upload_file(file: UploadFile = File(...), request: Request = None):
115115 request .session ["history" ] = history
116116 return JSONResponse ({"filename" : file .filename , "content_type" : file .content_type })
117117
118- @app .post ("/chat/stream" )
119- async def chat_stream (request : Request ):
118+ @app .get ("/chat/stream" )
119+ async def chat_stream (request : Request , user_message : str = None ):
120120 """Stream chat responses using Server-Sent Events."""
121121 try :
122- # Get form data from request
123- form_data = await request .form ()
124- user_message = form_data .get ("user_message" )
125-
126122 if not user_message :
127- return JSONResponse ({"error" : "No message provided" }, status_code = 400 )
123+ history_check = request .session .get ("history" , [])
124+ if history_check and history_check [- 1 ]["role" ] == "user" :
125+ user_message = history_check [- 1 ]["content" ]
126+ else :
127+ return JSONResponse ({"error" : "No message provided in query parameters" }, status_code = 400 )
128128
129- # Get history from session
130129 history = request .session .get ("history" , [])
131130
132- # Add user message to history
133131 user_msg = {"role" : "user" , "content" : user_message , "timestamp" : now_str ()}
134132 history .append (user_msg )
135133
136- # Update session with user message
137134 request .session ["history" ] = history
138135
139- # Create an event stream generator
140136 async def event_generator ():
141- # Send user message confirmation first
142137 yield f"data: { json .dumps ({'type' : 'user_msg_received' , 'message' : user_msg })} \n \n "
143- await asyncio .sleep (0.1 ) # Small delay to allow frontend rendering
138+ await asyncio .sleep (0.1 )
144139
145- # Start assistant message
146140 assistant_msg = {"role" : "assistant" , "content" : "" , "timestamp" : now_str ()}
147141 yield f"data: { json .dumps ({'type' : 'assistant_msg_start' , 'message' : assistant_msg })} \n \n "
148142
149143 try :
150- # Start streaming response
151144 collected_content = []
152145 async for content_chunk in client .async_chat_completion_stream (history ):
153- # Add chunk to the collected content
154146 collected_content .append (content_chunk )
155147 assistant_msg ["content" ] = '' .join (collected_content )
156148
157- # Send the chunk to the client
158149 yield f"data: { json .dumps ({'type' : 'content_chunk' , 'chunk' : content_chunk })} \n \n "
159150
160- # Stream is complete - update the history in the session
161151 complete_msg = {"role" : "assistant" , "content" : '' .join (collected_content ), "timestamp" : now_str ()}
162152 history .append (complete_msg )
163153 request .session ["history" ] = history
164154
165- # Send completion message
166155 yield f"data: { json .dumps ({'type' : 'complete' , 'message' : complete_msg })} \n \n "
167156 except Exception as e :
168- # Handle errors during streaming
169157 error_msg = str (e )
170158 yield f"data: { json .dumps ({'type' : 'error' , 'error' : error_msg })} \n \n "
171159
172- # Add error message to history
173160 error_response = f"Error: { error_msg } "
174161 history .append ({"role" : "assistant" , "content" : error_response , "timestamp" : now_str ()})
175162 request .session ["history" ] = history
0 commit comments