Spaces:
Runtime error
Runtime error
Ajey95
commited on
Commit
·
fc88e67
1
Parent(s):
bb036c7
Fix: chat_history addition
Browse files- agents/router_agent.py +25 -338
agents/router_agent.py
CHANGED
|
@@ -1,280 +1,6 @@
|
|
| 1 |
-
# """
|
| 2 |
-
# Router Agent - The Coordinator
|
| 3 |
-
# Classifies user queries and routes them to appropriate specialist agents
|
| 4 |
-
# Now with Gemini API integration!
|
| 5 |
-
# """
|
| 6 |
-
|
| 7 |
-
# import re
|
| 8 |
-
# from .academic_agent import AcademicAgent
|
| 9 |
-
# from .drug_info_agent import DrugInfoAgent
|
| 10 |
-
# from .quiz_agent import QuizAgent
|
| 11 |
-
# from .mnemonic_agent import MnemonicAgent
|
| 12 |
-
# from .viva_agent import VivaAgent
|
| 13 |
-
|
| 14 |
-
# class RouterAgent:
|
| 15 |
-
# def __init__(self, gemini_model=None):
|
| 16 |
-
# # Store Gemini model
|
| 17 |
-
# self.model = gemini_model
|
| 18 |
-
|
| 19 |
-
# # Initialize specialist agents with Gemini model
|
| 20 |
-
# self.academic_agent = AcademicAgent(gemini_model)
|
| 21 |
-
# self.drug_info_agent = DrugInfoAgent(gemini_model)
|
| 22 |
-
# self.quiz_agent = QuizAgent(gemini_model)
|
| 23 |
-
# self.mnemonic_agent = MnemonicAgent(gemini_model)
|
| 24 |
-
# self.viva_agent = VivaAgent(gemini_model)
|
| 25 |
-
|
| 26 |
-
# # Define keywords for each agent type (Free-tier friendly classification)
|
| 27 |
-
# self.agent_keywords = {
|
| 28 |
-
# 'drug_info': [
|
| 29 |
-
# 'drug', 'medicine', 'medication', 'side effects', 'dosage',
|
| 30 |
-
# 'contraindication', 'interaction', 'pharmacology', 'therapeutic',
|
| 31 |
-
# 'adverse', 'mechanism', 'action', 'indication', 'prescription'
|
| 32 |
-
# ],
|
| 33 |
-
# 'quiz_generation': [
|
| 34 |
-
# 'quiz', 'test', 'questions', 'mcq', 'multiple choice',
|
| 35 |
-
# 'flashcard', 'practice', 'exam', 'assessment', 'evaluate'
|
| 36 |
-
# ],
|
| 37 |
-
# 'mnemonic_creation': [
|
| 38 |
-
# 'mnemonic', 'remember', 'memory', 'trick', 'acronym',
|
| 39 |
-
# 'rhyme', 'shortcut', 'memorize', 'recall', 'aide'
|
| 40 |
-
# ],
|
| 41 |
-
# 'viva_practice': [
|
| 42 |
-
# 'viva', 'oral', 'interview', 'practice session', 'mock',
|
| 43 |
-
# 'question answer', 'preparation', 'rehearse'
|
| 44 |
-
# ]
|
| 45 |
-
# }
|
| 46 |
-
|
| 47 |
-
# def classify_query_with_ai(self, query):
|
| 48 |
-
# """Use Gemini AI to classify queries more accurately"""
|
| 49 |
-
# if not self.model:
|
| 50 |
-
# return self.classify_query(query) # Fallback to keyword matching
|
| 51 |
-
|
| 52 |
-
# try:
|
| 53 |
-
# classification_prompt = f"""
|
| 54 |
-
# You are a query classifier for a pharmacy education AI assistant.
|
| 55 |
-
# Classify this user query into ONE of these categories:
|
| 56 |
-
|
| 57 |
-
# 1. academic_query - General academic questions about pharmacy, chemistry, biology, mechanisms
|
| 58 |
-
# 2. drug_info - Specific questions about drugs, medicines, side effects, dosages, interactions
|
| 59 |
-
# 3. quiz_generation - Requests to create quizzes, tests, MCQs, practice questions
|
| 60 |
-
# 4. mnemonic_creation - Requests for memory aids, mnemonics, acronyms, memory tricks
|
| 61 |
-
# 5. viva_practice - Requests for mock interviews, viva practice, oral exam preparation
|
| 62 |
-
|
| 63 |
-
# User Query: "{query}"
|
| 64 |
-
|
| 65 |
-
# Respond with ONLY the category name (e.g., "academic_query")
|
| 66 |
-
# """
|
| 67 |
-
|
| 68 |
-
# response = self.model.generate_content(classification_prompt)
|
| 69 |
-
# classification = response.text.strip().lower()
|
| 70 |
-
|
| 71 |
-
# # Validate the classification
|
| 72 |
-
# valid_types = ['academic_query', 'drug_info', 'quiz_generation', 'mnemonic_creation', 'viva_practice']
|
| 73 |
-
# if classification in valid_types:
|
| 74 |
-
# return classification
|
| 75 |
-
# else:
|
| 76 |
-
# return 'academic_query' # Default fallback
|
| 77 |
-
|
| 78 |
-
# except Exception as e:
|
| 79 |
-
# print(f"AI classification failed: {e}")
|
| 80 |
-
# return self.classify_query(query) # Fallback to keyword matching
|
| 81 |
-
# """
|
| 82 |
-
# Classify the user query into one of the agent categories
|
| 83 |
-
# Uses keyword matching for free-tier efficiency
|
| 84 |
-
# """
|
| 85 |
-
# query_lower = query.lower()
|
| 86 |
-
|
| 87 |
-
# # Count keyword matches for each agent type
|
| 88 |
-
# scores = {}
|
| 89 |
-
|
| 90 |
-
# for agent_type, keywords in self.agent_keywords.items():
|
| 91 |
-
# score = sum(1 for keyword in keywords if keyword in query_lower)
|
| 92 |
-
# scores[agent_type] = score
|
| 93 |
-
|
| 94 |
-
# # Special pattern matching for better accuracy
|
| 95 |
-
# if re.search(r'\b(what|explain|definition|mechanism|process|how does)\b', query_lower):
|
| 96 |
-
# scores['drug_info'] += 1 if any(drug_word in query_lower for drug_word in ['drug', 'medicine', 'pharmacology']) else 0
|
| 97 |
-
# scores.setdefault('academic_query', 0)
|
| 98 |
-
# scores['academic_query'] += 1
|
| 99 |
-
|
| 100 |
-
# if re.search(r'\b(create|make|generate|give me)\s+(quiz|questions|mcq)\b', query_lower):
|
| 101 |
-
# scores['quiz_generation'] += 2
|
| 102 |
-
|
| 103 |
-
# if re.search(r'\b(help.*remember|memory.*trick|mnemonic.*for)\b', query_lower):
|
| 104 |
-
# scores['mnemonic_creation'] += 2
|
| 105 |
-
|
| 106 |
-
# if re.search(r'\b(practice.*viva|mock.*interview|oral.*exam)\b', query_lower):
|
| 107 |
-
# scores['viva_practice'] += 2
|
| 108 |
-
|
| 109 |
-
# # Find the highest scoring agent type
|
| 110 |
-
# if max(scores.values()) == 0:
|
| 111 |
-
# return 'academic_query' # Default to academic for general questions
|
| 112 |
-
|
| 113 |
-
# return max(scores, key=scores.get)
|
| 114 |
-
|
| 115 |
-
# def route_query(self, query):
|
| 116 |
-
# """
|
| 117 |
-
# Route the query to the appropriate specialist agent
|
| 118 |
-
# """
|
| 119 |
-
# agent_type = self.classify_query(query)
|
| 120 |
-
|
| 121 |
-
# try:
|
| 122 |
-
# if agent_type == 'drug_info':
|
| 123 |
-
# response = self.drug_info_agent.process_query(query)
|
| 124 |
-
# elif agent_type == 'quiz_generation':
|
| 125 |
-
# response = self.quiz_agent.process_query(query)
|
| 126 |
-
# elif agent_type == 'mnemonic_creation':
|
| 127 |
-
# response = self.mnemonic_agent.process_query(query)
|
| 128 |
-
# elif agent_type == 'viva_practice':
|
| 129 |
-
# response = self.viva_agent.process_query(query)
|
| 130 |
-
# else: # academic_query or default
|
| 131 |
-
# response = self.academic_agent.process_query(query)
|
| 132 |
-
# agent_type = 'academic_query'
|
| 133 |
-
|
| 134 |
-
# # Add metadata to response
|
| 135 |
-
# if isinstance(response, dict):
|
| 136 |
-
# response['agent_type'] = agent_type
|
| 137 |
-
# return response
|
| 138 |
-
# else:
|
| 139 |
-
# return {
|
| 140 |
-
# 'message': response,
|
| 141 |
-
# 'agent_type': agent_type,
|
| 142 |
-
# 'success': True
|
| 143 |
-
# }
|
| 144 |
-
|
| 145 |
-
# except Exception as e:
|
| 146 |
-
# return {
|
| 147 |
-
# 'message': f"Router Error: {str(e)}",
|
| 148 |
-
# 'agent_type': 'error',
|
| 149 |
-
# 'success': False
|
| 150 |
-
# }
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
# # """
|
| 158 |
-
# # Router Agent - The Coordinator
|
| 159 |
-
# # Classifies user queries and routes them to appropriate specialist agents
|
| 160 |
-
# # """
|
| 161 |
-
|
| 162 |
-
# # import re
|
| 163 |
-
# # from .academic_agent import AcademicAgent
|
| 164 |
-
# # from .drug_info_agent import DrugInfoAgent
|
| 165 |
-
# # from .quiz_agent import QuizAgent
|
| 166 |
-
# # from .mnemonic_agent import MnemonicAgent
|
| 167 |
-
# # from .viva_agent import VivaAgent
|
| 168 |
-
|
| 169 |
-
# # class RouterAgent:
|
| 170 |
-
# # def __init__(self):
|
| 171 |
-
# # # Initialize specialist agents
|
| 172 |
-
# # self.academic_agent = AcademicAgent()
|
| 173 |
-
# # self.drug_info_agent = DrugInfoAgent()
|
| 174 |
-
# # self.quiz_agent = QuizAgent()
|
| 175 |
-
# # self.mnemonic_agent = MnemonicAgent()
|
| 176 |
-
# # self.viva_agent = VivaAgent()
|
| 177 |
-
|
| 178 |
-
# # # Define keywords for each agent type (Free-tier friendly classification)
|
| 179 |
-
# # self.agent_keywords = {
|
| 180 |
-
# # 'drug_info': [
|
| 181 |
-
# # 'drug', 'medicine', 'medication', 'side effects', 'dosage',
|
| 182 |
-
# # 'contraindication', 'interaction', 'pharmacology', 'therapeutic',
|
| 183 |
-
# # 'adverse', 'mechanism', 'action', 'indication', 'prescription'
|
| 184 |
-
# # ],
|
| 185 |
-
# # 'quiz_generation': [
|
| 186 |
-
# # 'quiz', 'test', 'questions', 'mcq', 'multiple choice',
|
| 187 |
-
# # 'flashcard', 'practice', 'exam', 'assessment', 'evaluate'
|
| 188 |
-
# # ],
|
| 189 |
-
# # 'mnemonic_creation': [
|
| 190 |
-
# # 'mnemonic', 'remember', 'memory', 'trick', 'acronym',
|
| 191 |
-
# # 'rhyme', 'shortcut', 'memorize', 'recall', 'aide'
|
| 192 |
-
# # ],
|
| 193 |
-
# # 'viva_practice': [
|
| 194 |
-
# # 'viva', 'oral', 'interview', 'practice session', 'mock',
|
| 195 |
-
# # 'question answer', 'preparation', 'rehearse'
|
| 196 |
-
# # ]
|
| 197 |
-
# # }
|
| 198 |
-
|
| 199 |
-
# # def classify_query(self, query):
|
| 200 |
-
# # """
|
| 201 |
-
# # Classify the user query into one of the agent categories
|
| 202 |
-
# # Uses keyword matching for free-tier efficiency
|
| 203 |
-
# # """
|
| 204 |
-
# # query_lower = query.lower()
|
| 205 |
-
|
| 206 |
-
# # # Count keyword matches for each agent type
|
| 207 |
-
# # scores = {}
|
| 208 |
-
|
| 209 |
-
# # for agent_type, keywords in self.agent_keywords.items():
|
| 210 |
-
# # score = sum(1 for keyword in keywords if keyword in query_lower)
|
| 211 |
-
# # scores[agent_type] = score
|
| 212 |
-
|
| 213 |
-
# # # Special pattern matching for better accuracy
|
| 214 |
-
# # if re.search(r'\b(what|explain|definition|mechanism|process|how does)\b', query_lower):
|
| 215 |
-
# # scores['drug_info'] += 1 if any(drug_word in query_lower for drug_word in ['drug', 'medicine', 'pharmacology']) else 0
|
| 216 |
-
# # scores.setdefault('academic_query', 0)
|
| 217 |
-
# # scores['academic_query'] += 1
|
| 218 |
-
|
| 219 |
-
# # if re.search(r'\b(create|make|generate|give me)\s+(quiz|questions|mcq)\b', query_lower):
|
| 220 |
-
# # scores['quiz_generation'] += 2
|
| 221 |
-
|
| 222 |
-
# # if re.search(r'\b(help.*remember|memory.*trick|mnemonic.*for)\b', query_lower):
|
| 223 |
-
# # scores['mnemonic_creation'] += 2
|
| 224 |
-
|
| 225 |
-
# # if re.search(r'\b(practice.*viva|mock.*interview|oral.*exam)\b', query_lower):
|
| 226 |
-
# # scores['viva_practice'] += 2
|
| 227 |
-
|
| 228 |
-
# # # Find the highest scoring agent type
|
| 229 |
-
# # if max(scores.values()) == 0:
|
| 230 |
-
# # return 'academic_query' # Default to academic for general questions
|
| 231 |
-
|
| 232 |
-
# # return max(scores, key=scores.get)
|
| 233 |
-
|
| 234 |
-
# # def route_query(self, query):
|
| 235 |
-
# # """
|
| 236 |
-
# # Route the query to the appropriate specialist agent
|
| 237 |
-
# # """
|
| 238 |
-
# # agent_type = self.classify_query(query)
|
| 239 |
-
|
| 240 |
-
# # try:
|
| 241 |
-
# # if agent_type == 'drug_info':
|
| 242 |
-
# # response = self.drug_info_agent.process_query(query)
|
| 243 |
-
# # elif agent_type == 'quiz_generation':
|
| 244 |
-
# # response = self.quiz_agent.process_query(query)
|
| 245 |
-
# # elif agent_type == 'mnemonic_creation':
|
| 246 |
-
# # response = self.mnemonic_agent.process_query(query)
|
| 247 |
-
# # elif agent_type == 'viva_practice':
|
| 248 |
-
# # response = self.viva_agent.process_query(query)
|
| 249 |
-
# # else: # academic_query or default
|
| 250 |
-
# # response = self.academic_agent.process_query(query)
|
| 251 |
-
# # agent_type = 'academic_query'
|
| 252 |
-
|
| 253 |
-
# # # Add metadata to response
|
| 254 |
-
# # if isinstance(response, dict):
|
| 255 |
-
# # response['agent_type'] = agent_type
|
| 256 |
-
# # return response
|
| 257 |
-
# # else:
|
| 258 |
-
# # return {
|
| 259 |
-
# # 'message': response,
|
| 260 |
-
# # 'agent_type': agent_type,
|
| 261 |
-
# # 'success': True
|
| 262 |
-
# # }
|
| 263 |
-
|
| 264 |
-
# # except Exception as e:
|
| 265 |
-
# # return {
|
| 266 |
-
# # 'message': f"Router Error: {str(e)}",
|
| 267 |
-
# # 'agent_type': 'error',
|
| 268 |
-
# # 'success': False
|
| 269 |
-
# # }
|
| 270 |
-
|
| 271 |
# agents/router_agent.py
|
| 272 |
-
"""
|
| 273 |
-
Router Agent - Directs queries to the appropriate specialist agent.
|
| 274 |
-
"""
|
| 275 |
-
import re
|
| 276 |
|
| 277 |
-
|
| 278 |
from .academic_agent import AcademicAgent
|
| 279 |
from .drug_info_agent import DrugInfoAgent
|
| 280 |
from .mnemonic_agent import MnemonicAgent
|
|
@@ -284,82 +10,43 @@ from .viva_agent import VivaAgent
|
|
| 284 |
class RouterAgent:
|
| 285 |
def __init__(self, gemini_model=None):
|
| 286 |
"""
|
| 287 |
-
Initializes the router and all specialist agents
|
| 288 |
"""
|
| 289 |
-
self.model = gemini_model
|
| 290 |
-
# Instantiate all agents
|
| 291 |
self.academic_agent = AcademicAgent(gemini_model)
|
| 292 |
self.drug_info_agent = DrugInfoAgent(gemini_model)
|
| 293 |
self.mnemonic_agent = MnemonicAgent(gemini_model)
|
| 294 |
self.quiz_agent = QuizAgent(gemini_model)
|
| 295 |
self.viva_agent = VivaAgent(gemini_model)
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
"""
|
| 300 |
Determines the user's intent and routes the query with all necessary
|
| 301 |
context to the correct specialist agent.
|
| 302 |
"""
|
| 303 |
-
|
| 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 |
-
|
| 315 |
-
return self.quiz_agent.process_query(query, file_context, chat_history)
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
|
|
|
| 322 |
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
|
| 328 |
-
|
| 329 |
-
|
| 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 |
-
|
| 364 |
-
|
| 365 |
-
# return self.academic_agent.process_query(query, file_context,chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# agents/router_agent.py
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import re
|
| 4 |
from .academic_agent import AcademicAgent
|
| 5 |
from .drug_info_agent import DrugInfoAgent
|
| 6 |
from .mnemonic_agent import MnemonicAgent
|
|
|
|
| 10 |
class RouterAgent:
|
| 11 |
def __init__(self, gemini_model=None):
|
| 12 |
"""
|
| 13 |
+
Initializes the router and all specialist agents.
|
| 14 |
"""
|
|
|
|
|
|
|
| 15 |
self.academic_agent = AcademicAgent(gemini_model)
|
| 16 |
self.drug_info_agent = DrugInfoAgent(gemini_model)
|
| 17 |
self.mnemonic_agent = MnemonicAgent(gemini_model)
|
| 18 |
self.quiz_agent = QuizAgent(gemini_model)
|
| 19 |
self.viva_agent = VivaAgent(gemini_model)
|
| 20 |
+
|
| 21 |
+
def route_query(self, query: str, file_context: str, viva_state: dict, chat_history: list):
|
| 22 |
+
"""
|
|
|
|
| 23 |
Determines the user's intent and routes the query with all necessary
|
| 24 |
context to the correct specialist agent.
|
| 25 |
"""
|
| 26 |
+
query_lower = query.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# --- Intent Detection Logic ---
|
|
|
|
| 29 |
|
| 30 |
+
# 1. Viva Agent (High priority for session commands)
|
| 31 |
+
if viva_state and viva_state.get('active'):
|
| 32 |
+
return self.viva_agent.process_query(query, file_context, viva_state)
|
| 33 |
+
if any(cmd in query_lower for cmd in ["viva", "interview", "start viva"]):
|
| 34 |
+
return self.viva_agent.process_query(query, file_context, viva_state)
|
| 35 |
|
| 36 |
+
# 2. Mnemonic Agent
|
| 37 |
+
if any(cmd in query_lower for cmd in ["mnemonic", "memory aid", "remember"]):
|
| 38 |
+
return self.mnemonic_agent.process_query(query, file_context, chat_history)
|
| 39 |
|
| 40 |
+
# 3. Quiz Agent
|
| 41 |
+
if any(cmd in query_lower for cmd in ["quiz", "test me", "flashcard"]):
|
| 42 |
+
return self.quiz_agent.process_query(query, file_context, chat_history)
|
| 43 |
|
| 44 |
+
# 4. Drug Info Agent
|
| 45 |
+
drug_keywords = ["drug", "medicine", "medication", "side effect", "dosage"]
|
| 46 |
+
specific_drugs = r'\b(paracetamol|ibuprofen|metformin|aspirin|amoxicillin)\b'
|
| 47 |
|
| 48 |
+
if any(cmd in query_lower for cmd in drug_keywords) or re.search(specific_drugs, query_lower):
|
| 49 |
+
return self.drug_info_agent.process_query(query, file_context, chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# 5. Default to Academic Agent for general queries
|
| 52 |
+
return self.academic_agent.process_query(query, file_context, chat_history)
|
|
|