cryogenic22 commited on
Commit
0b9345a
·
verified ·
1 Parent(s): 4288a7d

Update components/chat.py

Browse files
Files changed (1) hide show
  1. components/chat.py +44 -32
components/chat.py CHANGED
@@ -279,38 +279,50 @@ def display_chat_interface():
279
  st.warning("Please enter a valid question.")
280
  return
281
 
282
- # Create and append human message
283
- human_message = HumanMessage(content=prompt)
284
- st.session_state.messages.append(human_message)
285
-
286
- # Get response from QA system
287
- response = st.session_state.qa_system.invoke({
288
- "input": prompt,
289
- "chat_history": st.session_state.messages
290
- })
291
-
292
- # Handle response
293
- if response:
294
- ai_message = AIMessage(content=str(response))
295
- st.session_state.messages.append(ai_message)
296
 
297
- # Save chat history
298
- if 'current_session_id' in st.session_state:
299
- st.session_state.persistence.save_chat_history(
300
- st.session_state.messages,
301
- st.session_state.current_session_id,
302
- metadata={
303
- 'last_question': prompt,
304
- 'last_updated': datetime.now().isoformat()
305
- }
306
- )
307
 
308
- st.rerun()
309
- else:
310
- st.error("No valid response received. Please try again.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
 
312
- except Exception as e:
313
- st.error("An error occurred during chat processing.")
314
- st.error(f"Error details: {str(e)}")
315
- import traceback
316
- st.error(traceback.format_exc())
 
279
  st.warning("Please enter a valid question.")
280
  return
281
 
282
+ try:
283
+ # Create and append human message
284
+ human_message = HumanMessage(content=prompt)
285
+ st.session_state.messages.append(human_message)
 
 
 
 
 
 
 
 
 
 
286
 
287
+ # Format chat history properly
288
+ chat_history = []
289
+ for msg in st.session_state.messages[:-1]: # Exclude the current message
290
+ if isinstance(msg, (HumanMessage, AIMessage)):
291
+ chat_history.append({
292
+ "role": "user" if isinstance(msg, HumanMessage) else "assistant",
293
+ "content": msg.content
294
+ })
 
 
295
 
296
+ # Get response from QA system
297
+ response = st.session_state.qa_system.invoke({
298
+ "input": prompt,
299
+ "chat_history": [] # Start with empty history if causing issues
300
+ })
301
+
302
+ # Handle response
303
+ if response:
304
+ ai_message = AIMessage(content=str(response))
305
+ st.session_state.messages.append(ai_message)
306
+
307
+ # Save chat history
308
+ if 'current_session_id' in st.session_state:
309
+ st.session_state.persistence.save_chat_history(
310
+ st.session_state.messages,
311
+ st.session_state.current_session_id,
312
+ metadata={
313
+ 'last_question': prompt,
314
+ 'last_updated': datetime.now().isoformat()
315
+ }
316
+ )
317
+
318
+ st.rerun()
319
+ else:
320
+ st.error("No valid response received. Please try again.")
321
+
322
+ except Exception as e:
323
+ st.error(f"Error processing response: {str(e)}")
324
+ st.error(traceback.format_exc())
325
+ # Remove the failed message attempt
326
+ if st.session_state.messages:
327
+ st.session_state.messages.pop()
328