Rulga commited on
Commit
4eb711e
·
1 Parent(s): 721bbf2

Refactor respond_and_clear function to simplify history formatting and improve error handling

Browse files
Files changed (1) hide show
  1. app.py +23 -57
app.py CHANGED
@@ -214,74 +214,40 @@ def respond_and_clear(message, history, conversation_id):
214
  print("Debug - Message type:", type(message), "Content:", message)
215
  print("Debug - History type:", type(history), "Content:", history)
216
 
217
- # Create user message in proper format
218
- user_message = {"role": "user", "content": message}
219
-
220
- # Use system message from constants
221
- response_generator = respond(
222
- message=message,
223
- history=history,
224
- conversation_id=conversation_id,
225
- system_message=DEFAULT_SYSTEM_MESSAGE,
226
- max_tokens=max_tokens,
227
- temperature=temperature,
228
- top_p=top_p
229
- )
230
-
231
  try:
 
 
 
 
 
 
 
 
 
 
 
232
  # Get first response from generator
233
  new_history, conv_id = next(response_generator)
234
 
235
- # Print debug information about the received history
236
- print("Debug - New history received:", new_history)
237
-
238
- # Ensure the final output is properly formatted for Gradio
239
  formatted_history = []
240
- for item in new_history:
241
- if isinstance(item, list) and len(item) == 2:
242
- user_msg, assistant_msg = item
243
-
244
- # Ensure user message is properly formatted
245
- if not isinstance(user_msg, dict) or "role" not in user_msg or "content" not in user_msg:
246
- user_msg = {"role": "user", "content": str(user_msg) if not isinstance(user_msg, dict) else user_msg.get("content", "")}
247
-
248
- # Ensure assistant message is properly formatted
249
- if not isinstance(assistant_msg, dict) or "role" not in assistant_msg or "content" not in assistant_msg:
250
- assistant_msg = {"role": "assistant", "content": str(assistant_msg) if not isinstance(assistant_msg, dict) else assistant_msg.get("content", "")}
251
-
252
- formatted_history.append([user_msg, assistant_msg])
253
-
254
- # Print the final formatted history
255
- print("Debug - Formatted history:", formatted_history)
256
 
 
257
  return formatted_history, conv_id, "" # Clear message input
 
258
  except Exception as e:
259
  print(f"Error in respond_and_clear: {str(e)}")
260
- # Create a properly formatted error message
261
- error_history = []
262
- if history:
263
- # Copy existing history (ensuring proper format)
264
- for item in history:
265
- if isinstance(item, list) and len(item) == 2:
266
- user_msg, assistant_msg = item
267
-
268
- # Ensure user message is properly formatted
269
- if not isinstance(user_msg, dict) or "role" not in user_msg or "content" not in user_msg:
270
- user_msg = {"role": "user", "content": str(user_msg) if not isinstance(user_msg, dict) else user_msg.get("content", "")}
271
-
272
- # Ensure assistant message is properly formatted
273
- if not isinstance(assistant_msg, dict) or "role" not in assistant_msg or "content" not in assistant_msg:
274
- assistant_msg = {"role": "assistant", "content": str(assistant_msg) if not isinstance(assistant_msg, dict) else assistant_msg.get("content", "")}
275
-
276
- error_history.append([user_msg, assistant_msg])
277
-
278
- # Add the error message
279
- error_history.append([
280
  {"role": "user", "content": message},
281
  {"role": "assistant", "content": f"An error occurred: {str(e)}"}
282
- ])
283
-
284
- return error_history, conversation_id, ""
285
 
286
  # Create interface
287
  with gr.Blocks() as demo:
 
214
  print("Debug - Message type:", type(message), "Content:", message)
215
  print("Debug - History type:", type(history), "Content:", history)
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  try:
218
+ # Get response generator
219
+ response_generator = respond(
220
+ message=message,
221
+ history=history if history else [],
222
+ conversation_id=conversation_id,
223
+ system_message=DEFAULT_SYSTEM_MESSAGE,
224
+ max_tokens=max_tokens,
225
+ temperature=temperature,
226
+ top_p=top_p
227
+ )
228
+
229
  # Get first response from generator
230
  new_history, conv_id = next(response_generator)
231
 
232
+ # Format history as list of message pairs
 
 
 
233
  formatted_history = []
234
+ for i in range(0, len(new_history), 2):
235
+ if i + 1 < len(new_history):
236
+ formatted_history.append([
237
+ new_history[i],
238
+ new_history[i + 1]
239
+ ])
 
 
 
 
 
 
 
 
 
 
240
 
241
+ print("Debug - Final formatted history:", formatted_history)
242
  return formatted_history, conv_id, "" # Clear message input
243
+
244
  except Exception as e:
245
  print(f"Error in respond_and_clear: {str(e)}")
246
+ error_pair = [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  {"role": "user", "content": message},
248
  {"role": "assistant", "content": f"An error occurred: {str(e)}"}
249
+ ]
250
+ return [error_pair], conversation_id, ""
 
251
 
252
  # Create interface
253
  with gr.Blocks() as demo: