Rulga commited on
Commit
8f81d4d
·
1 Parent(s): 7827bc1

Refactor respond_and_clear function to improve error handling and response validation

Browse files
Files changed (1) hide show
  1. app.py +17 -38
app.py CHANGED
@@ -644,17 +644,13 @@ def save_chat_history(history, conversation_id):
644
 
645
  def respond_and_clear(message, history, conversation_id):
646
  """Handle chat message and clear input"""
647
- # Get model parameters from config
648
- max_tokens = ACTIVE_MODEL['parameters']['max_length']
649
- temperature = ACTIVE_MODEL['parameters']['temperature']
650
- top_p = ACTIVE_MODEL['parameters']['top_p']
651
-
652
- # Print debug information to help diagnose the issue
653
- print("Debug - Message type:", type(message), "Content:", message)
654
- print("Debug - History type:", type(history), "Content:", history)
655
-
656
  try:
657
- # Get response generator
 
 
 
 
 
658
  response_generator = respond(
659
  message=message,
660
  history=history if history else [],
@@ -665,43 +661,26 @@ def respond_and_clear(message, history, conversation_id):
665
  top_p=top_p
666
  )
667
 
668
- # Get first response from generator
669
- new_history, conv_id = next(response_generator)
670
-
671
- # Debug the response
672
- print("Debug - Final history:", new_history)
673
-
674
- # Check if the history contains errors (by looking for error message pattern)
675
- last_message = new_history[-1] if new_history else None
676
- is_error = last_message and isinstance(last_message.get('content', ''), str) and "⚠️ API Error" in last_message.get('content', '')
677
-
678
- # Save chat history after response (even with errors)
679
- save_chat_history(new_history, conv_id)
680
 
681
- return new_history, conv_id, "" # Clear message input
 
 
 
 
682
 
683
  except Exception as e:
684
  print(f"Error in respond_and_clear: {str(e)}")
685
 
686
- # Create a more readable error message
687
- if "incompatible with messages format" in str(e):
688
- error_message = (
689
- "⚠️ Message processing error: Problem with message format.\n\n"
690
- "Please try to clear the chat history using the 'Clear' button or "
691
- "switch to another model."
692
- )
693
- else:
694
- error_message = f"⚠️ Error: {str(e)}"
695
-
696
  # Create error history in the correct format
697
  error_history = history.copy() if history else []
698
  error_history.append({"role": "user", "content": message})
699
- error_history.append({"role": "assistant", "content": error_message})
 
 
 
700
 
701
- # Still try to save history with error
702
- if conversation_id:
703
- save_chat_history(error_history, conversation_id)
704
-
705
  return error_history, conversation_id, ""
706
 
707
  def update_model_info(model_key):
 
644
 
645
  def respond_and_clear(message, history, conversation_id):
646
  """Handle chat message and clear input"""
 
 
 
 
 
 
 
 
 
647
  try:
648
+ # Get model parameters from config
649
+ max_tokens = ACTIVE_MODEL['parameters']['max_length']
650
+ temperature = ACTIVE_MODEL['parameters']['temperature']
651
+ top_p = ACTIVE_MODEL['parameters']['top_p']
652
+
653
+ # Get response
654
  response_generator = respond(
655
  message=message,
656
  history=history if history else [],
 
661
  top_p=top_p
662
  )
663
 
664
+ # Get response from generator
665
+ new_history, conv_id = response_generator
 
 
 
 
 
 
 
 
 
 
666
 
667
+ # Validate response format
668
+ if isinstance(new_history, list) and len(new_history) > 0:
669
+ return new_history, conv_id, "" # Return history, conv_id and clear message
670
+
671
+ raise ValueError("Invalid response format")
672
 
673
  except Exception as e:
674
  print(f"Error in respond_and_clear: {str(e)}")
675
 
 
 
 
 
 
 
 
 
 
 
676
  # Create error history in the correct format
677
  error_history = history.copy() if history else []
678
  error_history.append({"role": "user", "content": message})
679
+ error_history.append({
680
+ "role": "assistant",
681
+ "content": "⚠️ Произошла ошибка при обработке сообщения. Пожалуйста, попробуйте еще раз."
682
+ })
683
 
 
 
 
 
684
  return error_history, conversation_id, ""
685
 
686
  def update_model_info(model_key):