Spaces:
Sleeping
Sleeping
Ajey95
commited on
Commit
·
830ce1c
1
Parent(s):
862d762
Fix: chat_history addition
Browse files- agents/agent_helpers.py +1 -1
- agents/router_agent.py +59 -33
- app.py +25 -27
agents/agent_helpers.py
CHANGED
@@ -7,7 +7,7 @@ def format_history_for_prompt(chat_history: list = None) -> str:
|
|
7 |
|
8 |
history_for_prompt = ""
|
9 |
for turn in chat_history:
|
10 |
-
#
|
11 |
if turn.get('parts') and isinstance(turn.get('parts'), list):
|
12 |
role = "User" if turn['role'] == 'user' else "AI"
|
13 |
history_for_prompt += f"{role}: {turn['parts'][0]}\n"
|
|
|
7 |
|
8 |
history_for_prompt = ""
|
9 |
for turn in chat_history:
|
10 |
+
# Ensure 'parts' is a list and not empty before accessing
|
11 |
if turn.get('parts') and isinstance(turn.get('parts'), list):
|
12 |
role = "User" if turn['role'] == 'user' else "AI"
|
13 |
history_for_prompt += f"{role}: {turn['parts'][0]}\n"
|
agents/router_agent.py
CHANGED
@@ -294,46 +294,72 @@ class RouterAgent:
|
|
294 |
self.quiz_agent = QuizAgent(gemini_model)
|
295 |
self.viva_agent = VivaAgent(gemini_model)
|
296 |
self.default_agent = self.academic_agent
|
297 |
-
|
298 |
def route_query(self, query: str, file_context: str = "", viva_state: dict = None, chat_history: list = None):
|
|
|
|
|
|
|
299 |
"""
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
|
312 |
-
|
313 |
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
|
337 |
-
|
338 |
-
|
339 |
-
|
|
|
294 |
self.quiz_agent = QuizAgent(gemini_model)
|
295 |
self.viva_agent = VivaAgent(gemini_model)
|
296 |
self.default_agent = self.academic_agent
|
297 |
+
|
298 |
def route_query(self, query: str, file_context: str = "", viva_state: dict = None, chat_history: list = None):
|
299 |
+
"""
|
300 |
+
Determines the user's intent and routes the query with all necessary
|
301 |
+
context to the correct specialist agent.
|
302 |
"""
|
303 |
+
query_lower = query.lower()
|
304 |
+
|
305 |
+
# --- Intent Detection Logic ---
|
306 |
+
if viva_state and viva_state.get('active'):
|
307 |
+
return self.viva_agent.process_query(query, file_context, viva_state)
|
308 |
+
if any(cmd in query_lower for cmd in ["viva", "interview", "start viva"]):
|
309 |
+
return self.viva_agent.process_query(query, file_context, viva_state)
|
310 |
+
|
311 |
+
if any(cmd in query_lower for cmd in ["mnemonic", "memory aid", "remember"]):
|
312 |
+
return self.mnemonic_agent.process_query(query, file_context, chat_history)
|
313 |
+
|
314 |
+
if any(cmd in query_lower for cmd in ["quiz", "test me", "flashcard"]):
|
315 |
+
return self.quiz_agent.process_query(query, file_context, chat_history)
|
316 |
+
|
317 |
+
if any(cmd in query_lower for cmd in ["drug", "medicine", "medication", "side effect", "dosage"]):
|
318 |
+
return self.drug_info_agent.process_query(query, file_context, chat_history)
|
319 |
+
|
320 |
+
# Default to Academic Agent for general queries, passing all context
|
321 |
+
return self.academic_agent.process_query(query, file_context, chat_history)
|
322 |
+
|
323 |
+
|
324 |
+
# def route_query(self, query: str, file_context: str = "", viva_state: dict = None, chat_history: list = None):
|
325 |
+
# """
|
326 |
+
# Determines the user's intent and routes the query to the correct agent.
|
327 |
|
328 |
+
# Args:
|
329 |
+
# query (str): The user's input query.
|
330 |
+
# file_context (str): Text content from any uploaded files.
|
331 |
+
# viva_state (dict): The current state of the viva session.
|
332 |
|
333 |
+
# Returns:
|
334 |
+
# dict: The response dictionary from the selected agent.
|
335 |
+
# """
|
336 |
+
# query_lower = query.lower()
|
337 |
|
338 |
+
# # --- Intent Detection Logic ---
|
339 |
|
340 |
+
# # 1. Viva Agent: High priority to catch session-based commands
|
341 |
+
# # If a viva session is active, or user wants to start/end one.
|
342 |
+
# if viva_state and viva_state.get('active'):
|
343 |
+
# # The VivaAgent itself handles all logic when a session is active
|
344 |
+
# return self.viva_agent.process_query(query, file_context, viva_state)
|
345 |
+
# if any(cmd in query_lower for cmd in ["viva", "interview"]):
|
346 |
+
# return self.viva_agent.process_query(query, file_context, viva_state)
|
347 |
|
348 |
+
# # 2. Mnemonic Agent
|
349 |
+
# if any(cmd in query_lower for cmd in ["mnemonic", "memory aid", "remember"]):
|
350 |
+
# return self.mnemonic_agent.process_query(query, file_context,chat_history)
|
351 |
|
352 |
+
# # 3. Quiz Agent
|
353 |
+
# if any(cmd in query_lower for cmd in ["quiz", "test me", "flashcard"]):
|
354 |
+
# return self.quiz_agent.process_query(query, file_context,chat_history)
|
355 |
|
356 |
+
# # 4. Drug Info Agent
|
357 |
+
# # Uses keywords and also checks for common drug endings like 'ol', 'in', 'am'
|
358 |
+
# if any(cmd in query_lower for cmd in ["drug", "medicine", "medication", "side effect", "dosage", "interaction"]):
|
359 |
+
# return self.drug_info_agent.process_query(query, file_context,chat_history)
|
360 |
+
# if re.search(r'\b(paracetamol|ibuprofen|metformin|aspirin|amoxicillin)\b', query_lower):
|
361 |
+
# return self.drug_info_agent.process_query(query, file_context)
|
362 |
|
363 |
+
# # 5. Default to Academic Agent
|
364 |
+
# # If no other intent is detected, it's likely a general academic question.
|
365 |
+
# return self.academic_agent.process_query(query, file_context,chat_history)
|
app.py
CHANGED
@@ -443,36 +443,34 @@ class MyPharmaAI:
|
|
443 |
self.quotes = load_quotes()
|
444 |
self.file_processor = FileProcessor()
|
445 |
|
446 |
-
def process_query(self, query, user_name="Student", viva_state=None, uploaded_files=None,chat_history=None):
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
}
|
470 |
|
471 |
def get_file_context(self, uploaded_files_session):
|
472 |
"""Extracts text from the most recent files to use as context."""
|
473 |
context = ""
|
474 |
-
# Limit
|
475 |
-
for file_info in uploaded_files_session[-3:]:
|
476 |
file_path = file_info.get('path')
|
477 |
if file_path and os.path.exists(file_path):
|
478 |
try:
|
@@ -523,7 +521,7 @@ def chat():
|
|
523 |
uploaded_files = session.get('uploaded_files', None)
|
524 |
|
525 |
# Process the query through the main orchestrator
|
526 |
-
result = pharma_ai.process_query(query, viva_state=viva_state, uploaded_files=uploaded_files,
|
527 |
# If the query was successful, update the history
|
528 |
if result.get('success'):
|
529 |
# Add the user's query and the AI's message to the history
|
|
|
443 |
self.quotes = load_quotes()
|
444 |
self.file_processor = FileProcessor()
|
445 |
|
446 |
+
def process_query(self, query, user_name="Student", viva_state=None, uploaded_files=None, chat_history=None):
|
447 |
+
"""Routes a user's query to the appropriate agent, handling context."""
|
448 |
+
try:
|
449 |
+
# This block correctly gets the file content from the session data
|
450 |
+
file_context = ""
|
451 |
+
if uploaded_files:
|
452 |
+
file_context = self.get_file_context(uploaded_files)
|
453 |
+
|
454 |
+
# This passes the file content and chat history to the router
|
455 |
+
response_data = self.router.route_query(query, file_context, viva_state, chat_history)
|
456 |
+
|
457 |
+
return {
|
458 |
+
'success': True,
|
459 |
+
**response_data
|
460 |
+
}
|
461 |
+
except Exception as e:
|
462 |
+
print(f"Error in MyPharmaAI.process_query: {e}")
|
463 |
+
return {
|
464 |
+
'success': False,
|
465 |
+
'message': f"Sorry, a critical error occurred: {str(e)}",
|
466 |
+
'agent_used': 'error'
|
467 |
+
}
|
468 |
+
|
|
|
469 |
|
470 |
def get_file_context(self, uploaded_files_session):
|
471 |
"""Extracts text from the most recent files to use as context."""
|
472 |
context = ""
|
473 |
+
for file_info in uploaded_files_session[-3:]: # Limit to last 3 files
|
|
|
474 |
file_path = file_info.get('path')
|
475 |
if file_path and os.path.exists(file_path):
|
476 |
try:
|
|
|
521 |
uploaded_files = session.get('uploaded_files', None)
|
522 |
|
523 |
# Process the query through the main orchestrator
|
524 |
+
result = pharma_ai.process_query(query, viva_state=viva_state, uploaded_files=uploaded_files,chathistory=chat_history)
|
525 |
# If the query was successful, update the history
|
526 |
if result.get('success'):
|
527 |
# Add the user's query and the AI's message to the history
|