NitinBot001 commited on
Commit
c4be6e7
·
verified ·
1 Parent(s): e15c8a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -1
app.py CHANGED
@@ -289,6 +289,117 @@ Rules:
289
  "total_messages": len(self.manager.get_history(chat_id, user_id))
290
  }
291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  except Exception as e:
293
  logger.error(f"Error processing query for chat {chat_id}, user {user_id}: {e}")
294
  return {
@@ -299,7 +410,7 @@ Rules:
299
  "assistant_message_id": None,
300
  "total_messages": 0
301
  }.tool_calls
302
- ]
303
  })
304
 
305
  # Execute all tool calls
 
289
  "total_messages": len(self.manager.get_history(chat_id, user_id))
290
  }
291
 
292
+ except Exception as e:
293
+ logger.error(f"Error processing query for chat {chat_id}, user {user_id}: {e}")
294
+ return {
295
+ "error": f"I apologize, but I encountered an error: {str(e)}. Please try again or rephrase your question.",
296
+ "chat_id": chat_id or self.manager.generate_chat_id(user_id) if user_id else None,
297
+ "is_new_chat": True,
298
+ "user_message_id": None,
299
+ "assistant_message_id": None,
300
+ "total_messages": 0
301
+ }
302
+
303
+ def get_chat_info(self, chat_id: str, user_id: str) -> Dict[str, Any]:
304
+ """
305
+ Get information about a specific chat for a specific user
306
+
307
+ Args:
308
+ chat_id: The chat ID to get information for
309
+ user_id: The user ID for authentication
310
+
311
+ Returns:
312
+ Dictionary with chat information
313
+ """
314
+ return self.manager.get_chat_info(chat_id, user_id)
315
+
316
+ def get_all_chats(self, user_id: str) -> List[Dict[str, Any]]:
317
+ """
318
+ Get all chat sessions for a specific user
319
+
320
+ Args:
321
+ user_id: The user ID to get sessions for
322
+
323
+ Returns:
324
+ List of chat session information
325
+ """
326
+ return self.manager.get_all_chat_sessions(user_id)
327
+
328
+ def clear_history(self, chat_id: str, user_id: str) -> bool:
329
+ """
330
+ Clear conversation history for a specific chat and user
331
+
332
+ Args:
333
+ chat_id: The ID of the chat to clear
334
+ user_id: The user ID for authentication
335
+
336
+ Returns:
337
+ True if deletion was successful, False otherwise
338
+ """
339
+ logger.info(f"Clearing history for chat: {chat_id}, user: {user_id}")
340
+ return self.manager.delete_history(chat_id, user_id)
341
+
342
+ def get_messages(self, chat_id: str, user_id: str) -> List[Dict[str, Any]]:
343
+ """
344
+ Get all messages for a specific chat and user
345
+
346
+ Args:
347
+ chat_id: The chat ID to get messages for
348
+ user_id: The user ID for authentication
349
+
350
+ Returns:
351
+ List of messages with their IDs
352
+ """
353
+ return self.manager.get_history(chat_id, user_id).tool_calls
354
+ ]
355
+ })
356
+
357
+ # Execute all tool calls
358
+ for tool_call in message.tool_calls:
359
+ function_name = tool_call.function.name
360
+ function_args = json.loads(tool_call.function.arguments)
361
+
362
+ logger.info(f"Calling function: {function_name} with args: {function_args}")
363
+
364
+ # Call the function
365
+ function_result = self.call_function(function_name, function_args)
366
+
367
+ # Add function result to messages
368
+ messages.append({
369
+ "role": "tool",
370
+ "tool_call_id": tool_call.id,
371
+ "content": json.dumps(function_result)
372
+ })
373
+
374
+ # Add final system prompt for generating response
375
+ messages.append({
376
+ "role": "system",
377
+ "content": self.final_system
378
+ })
379
+
380
+ # Get final response
381
+ final_response = self.client.chat.completions.create(
382
+ model=self.config.model_name,
383
+ messages=messages,
384
+ temperature=self.config.temperature
385
+ )
386
+ response_content = final_response.choices[0].message.content
387
+ else:
388
+ response_content = message.content
389
+
390
+ # Add messages to conversation history with unique IDs
391
+ user_msg = self.manager.add_message(chat_id, user_id, "user", user_message, image_url)
392
+ assistant_msg = self.manager.add_message(chat_id, user_id, "assistant", response_content)
393
+
394
+ return {
395
+ "response": response_content,
396
+ "chat_id": chat_id,
397
+ "is_new_chat": is_new_chat,
398
+ "user_message_id": user_msg.get("message_id"),
399
+ "assistant_message_id": assistant_msg.get("message_id"),
400
+ "total_messages": len(self.manager.get_history(chat_id, user_id))
401
+ }
402
+
403
  except Exception as e:
404
  logger.error(f"Error processing query for chat {chat_id}, user {user_id}: {e}")
405
  return {
 
410
  "assistant_message_id": None,
411
  "total_messages": 0
412
  }.tool_calls
413
+ ]
414
  })
415
 
416
  # Execute all tool calls