cstr commited on
Commit
bb79bb4
·
verified ·
1 Parent(s): 187163f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -536,12 +536,12 @@ def prepare_message_with_media(text, images=None, documents=None):
536
  def format_to_message_dict(history):
537
  """Convert history to proper message format"""
538
  messages = []
539
- for message in history:
540
- if isinstance(message, dict) and "role" in message and "content" in message:
541
- # Already in the correct format
542
- messages.append(message)
543
- elif len(message) == 2: # Handle old format [user_msg, ai_msg]
544
- human, ai = message
545
  if human:
546
  messages.append({"role": "user", "content": human})
547
  if ai:
@@ -1131,12 +1131,11 @@ def extract_ai_response(result, provider):
1131
  # STREAMING HANDLERS
1132
  # ==========================================================
1133
 
1134
- def openrouter_streaming_handler(response, chatbot, message_idx, message):
1135
  try:
1136
- # First add the user message if needed
1137
- if len(chatbot) == message_idx:
1138
- chatbot.append([message, ""])
1139
-
1140
  for line in response.iter_lines():
1141
  if not line:
1142
  continue
@@ -1155,16 +1154,14 @@ def openrouter_streaming_handler(response, chatbot, message_idx, message):
1155
  delta = chunk["choices"][0].get("delta", {})
1156
  if "content" in delta and delta["content"]:
1157
  # Update the current response
1158
- chatbot[-1][1] += delta["content"]
1159
- yield chatbot
1160
  except json.JSONDecodeError:
1161
  logger.error(f"Failed to parse JSON from chunk: {data}")
1162
  except Exception as e:
1163
  logger.error(f"Error in streaming handler: {str(e)}")
1164
  # Add error message to the current response
1165
- if len(chatbot) > message_idx:
1166
- chatbot[-1][1] += f"\n\nError during streaming: {str(e)}"
1167
- yield chatbot
1168
 
1169
  def openai_streaming_handler(response, chatbot, message_idx, message):
1170
  try:
 
536
  def format_to_message_dict(history):
537
  """Convert history to proper message format"""
538
  messages = []
539
+ for item in history:
540
+ if isinstance(item, dict) and "role" in item and "content" in item:
541
+ messages.append(item)
542
+ elif isinstance(item, list) and len(item) == 2:
543
+ # Convert from old format [user_msg, ai_msg]
544
+ human, ai = item
545
  if human:
546
  messages.append({"role": "user", "content": human})
547
  if ai:
 
1131
  # STREAMING HANDLERS
1132
  # ==========================================================
1133
 
1134
+ def openrouter_streaming_handler(response, history, message):
1135
  try:
1136
+ updated_history = history + [{"role": "user", "content": message}]
1137
+ assistant_response = ""
1138
+
 
1139
  for line in response.iter_lines():
1140
  if not line:
1141
  continue
 
1154
  delta = chunk["choices"][0].get("delta", {})
1155
  if "content" in delta and delta["content"]:
1156
  # Update the current response
1157
+ assistant_response += delta["content"]
1158
+ yield updated_history + [{"role": "assistant", "content": assistant_response}]
1159
  except json.JSONDecodeError:
1160
  logger.error(f"Failed to parse JSON from chunk: {data}")
1161
  except Exception as e:
1162
  logger.error(f"Error in streaming handler: {str(e)}")
1163
  # Add error message to the current response
1164
+ yield updated_history + [{"role": "assistant", "content": f"Error during streaming: {str(e)}"}]
 
 
1165
 
1166
  def openai_streaming_handler(response, chatbot, message_idx, message):
1167
  try: