Spaces:
Sleeping
Sleeping
Chris
commited on
Commit
·
0a9db12
1
Parent(s):
e107ea2
Final 6.8.3
Browse files- src/agents/__pycache__/router.cpython-310.pyc +0 -0
- src/agents/router.py +237 -123
src/agents/__pycache__/router.cpython-310.pyc
CHANGED
|
Binary files a/src/agents/__pycache__/router.cpython-310.pyc and b/src/agents/__pycache__/router.cpython-310.pyc differ
|
|
|
src/agents/router.py
CHANGED
|
@@ -6,7 +6,7 @@ Analyzes questions and routes them to appropriate specialized agents
|
|
| 6 |
|
| 7 |
import re
|
| 8 |
import logging
|
| 9 |
-
from typing import List, Dict, Any
|
| 10 |
from urllib.parse import urlparse
|
| 11 |
|
| 12 |
from agents.state import GAIAAgentState, QuestionType, AgentRole, AgentResult
|
|
@@ -29,18 +29,18 @@ class RouterAgent:
|
|
| 29 |
logger.info(f"Routing question: {state.question[:100]}...")
|
| 30 |
state.add_processing_step("Router: Starting question analysis")
|
| 31 |
|
| 32 |
-
# Step 1:
|
| 33 |
-
|
| 34 |
-
state.question_type =
|
| 35 |
-
state.add_processing_step(f"Router:
|
| 36 |
|
| 37 |
# Step 2: Complexity assessment
|
| 38 |
complexity = self._assess_complexity(state.question)
|
| 39 |
state.complexity_assessment = complexity
|
| 40 |
state.add_processing_step(f"Router: Assessed complexity as {complexity}")
|
| 41 |
|
| 42 |
-
# Step 3: Select appropriate agents
|
| 43 |
-
selected_agents = self.
|
| 44 |
state.selected_agents = selected_agents
|
| 45 |
state.add_processing_step(f"Router: Selected agents: {[a.value for a in selected_agents]}")
|
| 46 |
|
|
@@ -51,129 +51,175 @@ class RouterAgent:
|
|
| 51 |
|
| 52 |
# Step 5: Create routing decision summary
|
| 53 |
state.routing_decision = {
|
| 54 |
-
"
|
|
|
|
| 55 |
"complexity": complexity,
|
| 56 |
"agents": [agent.value for agent in selected_agents],
|
| 57 |
"estimated_cost": estimated_cost,
|
| 58 |
-
"reasoning": self._get_routing_reasoning(
|
| 59 |
}
|
| 60 |
|
| 61 |
# Step 6: Use LLM for complex routing decisions if needed
|
| 62 |
-
if complexity == "complex" or
|
| 63 |
state = self._llm_enhanced_routing(state)
|
| 64 |
|
| 65 |
-
logger.info(f"✅ Routing complete: {
|
| 66 |
return state
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
question_lower = question.lower()
|
|
|
|
| 72 |
|
| 73 |
-
# File processing questions
|
| 74 |
if file_name:
|
| 75 |
file_ext = file_name.lower().split('.')[-1] if '.' in file_name else ""
|
| 76 |
|
| 77 |
if file_ext in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg']:
|
| 78 |
-
|
| 79 |
elif file_ext in ['mp3', 'wav', 'ogg', 'flac', 'm4a']:
|
| 80 |
-
|
| 81 |
elif file_ext in ['xlsx', 'xls', 'csv']:
|
| 82 |
-
|
| 83 |
elif file_ext in ['py', 'js', 'java', 'cpp', 'c']:
|
| 84 |
-
|
| 85 |
else:
|
| 86 |
-
|
| 87 |
|
| 88 |
-
# URL-based classification
|
| 89 |
url_patterns = {
|
| 90 |
QuestionType.WIKIPEDIA: [
|
| 91 |
-
r'wikipedia\.org', r'
|
|
|
|
| 92 |
],
|
| 93 |
QuestionType.YOUTUBE: [
|
| 94 |
-
r'youtube\.com', r'youtu\.be', r'watch\?v=', r'video'
|
|
|
|
| 95 |
]
|
| 96 |
}
|
| 97 |
|
| 98 |
for question_type, patterns in url_patterns.items():
|
| 99 |
if any(re.search(pattern, question_lower) for pattern in patterns):
|
| 100 |
-
|
| 101 |
|
| 102 |
-
#
|
| 103 |
classification_patterns = {
|
| 104 |
QuestionType.MATHEMATICAL: [
|
| 105 |
-
|
| 106 |
-
r'\
|
| 107 |
-
r'\
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
],
|
| 110 |
QuestionType.CODE_EXECUTION: [
|
| 111 |
r'\bcode\b', r'\bprogram\b', r'\bscript\b', r'\bfunction\b', r'\balgorithm\b',
|
| 112 |
-
r'\bexecute\b', r'\brun.*code\b', r'\bpython\b', r'\bjavascript\b'
|
| 113 |
-
|
| 114 |
-
QuestionType.TEXT_MANIPULATION: [
|
| 115 |
-
r'\breverse\b', r'\bencode\b', r'\bdecode\b', r'\btransform\b', r'\bconvert\b',
|
| 116 |
-
r'\buppercase\b', r'\blowercase\b', r'\breplace\b', r'\bextract\b'
|
| 117 |
],
|
| 118 |
QuestionType.REASONING: [
|
|
|
|
| 119 |
r'\bwhy\b', r'\bexplain\b', r'\banalyze\b', r'\breasoning\b', r'\blogic\b',
|
| 120 |
-
r'\brelationship\b', r'\bcompare\b', r'\bcontrast\b', r'\bconclusion\b'
|
|
|
|
|
|
|
|
|
|
| 121 |
],
|
| 122 |
QuestionType.WEB_RESEARCH: [
|
|
|
|
| 123 |
r'\bsearch\b', r'\bfind.*information\b', r'\bresearch\b', r'\blook up\b',
|
| 124 |
-
r'\bwebsite\b', r'\bonline\b', r'\binternet\b',
|
|
|
|
|
|
|
| 125 |
r'\bwhat\s+(?:is|was|are|were)\b', r'\bwhen\s+(?:is|was|did|does)\b',
|
| 126 |
-
r'\bwhere\s+(?:is|was|are|were)\b'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
]
|
| 128 |
}
|
| 129 |
|
| 130 |
-
# Score each category with
|
| 131 |
type_scores = {}
|
| 132 |
for question_type, patterns in classification_patterns.items():
|
| 133 |
score = 0
|
| 134 |
for pattern in patterns:
|
| 135 |
matches = re.findall(pattern, question_lower)
|
| 136 |
score += len(matches)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
if score > 0:
|
| 138 |
type_scores[question_type] = score
|
| 139 |
|
| 140 |
# Special handling for specific question patterns
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
if
|
| 144 |
-
type_scores[QuestionType.
|
| 145 |
-
|
| 146 |
-
#
|
| 147 |
-
if re.search(r'\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
type_scores[QuestionType.WEB_RESEARCH] = type_scores.get(QuestionType.WEB_RESEARCH, 0) + 2
|
|
|
|
| 149 |
|
| 150 |
-
#
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
-
#
|
| 155 |
-
if
|
| 156 |
-
|
| 157 |
|
| 158 |
-
#
|
| 159 |
if type_scores:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
max_score = type_scores[best_type]
|
| 164 |
-
if max_score <= 1:
|
| 165 |
-
# Check if it's a general informational question
|
| 166 |
-
info_patterns = [r'\bwhat\b', r'\bwho\b', r'\bwhen\b', r'\bwhere\b', r'\bhow\b']
|
| 167 |
-
if any(re.search(pattern, question_lower) for pattern in info_patterns):
|
| 168 |
-
return QuestionType.WEB_RESEARCH
|
| 169 |
-
|
| 170 |
-
return best_type
|
| 171 |
|
| 172 |
-
|
| 173 |
-
return QuestionType.WEB_RESEARCH
|
| 174 |
|
| 175 |
def _assess_complexity(self, question: str) -> str:
|
| 176 |
-
"""Assess question complexity"""
|
| 177 |
|
| 178 |
question_lower = question.lower()
|
| 179 |
|
|
@@ -181,63 +227,117 @@ class RouterAgent:
|
|
| 181 |
complex_indicators = [
|
| 182 |
'multi-step', 'multiple', 'several', 'complex', 'detailed',
|
| 183 |
'analyze', 'explain why', 'reasoning', 'relationship',
|
| 184 |
-
'compare and contrast', 'comprehensive', 'thorough'
|
|
|
|
| 185 |
]
|
| 186 |
|
| 187 |
# Simple indicators
|
| 188 |
simple_indicators = [
|
| 189 |
-
'what is', 'who is', 'when', 'where', 'yes or no',
|
| 190 |
-
'true or false', 'simple', 'quick', 'name'
|
| 191 |
]
|
| 192 |
|
| 193 |
-
complex_score = sum(1 for indicator in complex_indicators if indicator
|
| 194 |
-
simple_score = sum(1 for indicator in simple_indicators if indicator
|
| 195 |
|
| 196 |
# Additional complexity factors
|
| 197 |
if len(question) > 200:
|
| 198 |
complex_score += 1
|
| 199 |
if len(question.split()) > 30:
|
| 200 |
complex_score += 1
|
| 201 |
-
if question.count('?') >
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
complex_score += 1
|
| 203 |
|
| 204 |
# Determine complexity
|
| 205 |
-
if complex_score >=
|
| 206 |
return "complex"
|
|
|
|
|
|
|
| 207 |
elif simple_score >= 2 and complex_score == 0:
|
| 208 |
return "simple"
|
| 209 |
else:
|
| 210 |
return "medium"
|
| 211 |
|
| 212 |
-
def
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
agents = []
|
| 216 |
|
| 217 |
-
# Always include synthesizer for final answer compilation
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
#
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
agents.append(AgentRole.REASONING_AGENT)
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
if has_file:
|
| 240 |
-
agents.append(AgentRole.FILE_PROCESSOR)
|
| 241 |
|
| 242 |
# Remove duplicates while preserving order
|
| 243 |
seen = set()
|
|
@@ -260,39 +360,49 @@ class RouterAgent:
|
|
| 260 |
|
| 261 |
base_cost = base_costs.get(complexity, 0.015)
|
| 262 |
|
| 263 |
-
# Additional cost per agent
|
| 264 |
-
agent_cost = len(agents) * 0.
|
| 265 |
|
| 266 |
return base_cost + agent_cost
|
| 267 |
|
| 268 |
-
def _get_routing_reasoning(self,
|
|
|
|
| 269 |
"""Generate human-readable reasoning for routing decision"""
|
| 270 |
|
| 271 |
reasons = []
|
| 272 |
|
| 273 |
-
#
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
# Complexity reasoning
|
| 288 |
if complexity == "complex":
|
| 289 |
-
reasons.append("Complex reasoning required")
|
| 290 |
elif complexity == "simple":
|
| 291 |
reasons.append("Straightforward question")
|
| 292 |
|
| 293 |
-
# Agent reasoning
|
| 294 |
agent_names = [agent.value.replace('_', ' ') for agent in agents]
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
return "; ".join(reasons)
|
| 298 |
|
|
@@ -304,22 +414,26 @@ class RouterAgent:
|
|
| 304 |
|
| 305 |
Question: {state.question}
|
| 306 |
File attached: {state.file_name if state.file_name else "None"}
|
| 307 |
-
|
|
|
|
| 308 |
Current complexity: {state.complexity_assessment}
|
|
|
|
| 309 |
|
| 310 |
-
|
| 311 |
-
1.
|
| 312 |
-
2.
|
| 313 |
-
3.
|
| 314 |
-
4.
|
|
|
|
| 315 |
|
|
|
|
| 316 |
Keep response concise and focused on routing decisions.
|
| 317 |
"""
|
| 318 |
|
| 319 |
try:
|
| 320 |
-
# Use main model (32B) for better routing decisions
|
| 321 |
-
tier = ModelTier.MAIN
|
| 322 |
-
result = self.llm_client.generate(prompt, tier=tier, max_tokens=
|
| 323 |
|
| 324 |
if result.success:
|
| 325 |
state.add_processing_step("Router: Enhanced with LLM analysis")
|
|
|
|
| 6 |
|
| 7 |
import re
|
| 8 |
import logging
|
| 9 |
+
from typing import List, Dict, Any, Tuple
|
| 10 |
from urllib.parse import urlparse
|
| 11 |
|
| 12 |
from agents.state import GAIAAgentState, QuestionType, AgentRole, AgentResult
|
|
|
|
| 29 |
logger.info(f"Routing question: {state.question[:100]}...")
|
| 30 |
state.add_processing_step("Router: Starting question analysis")
|
| 31 |
|
| 32 |
+
# Step 1: Enhanced question classification with multi-type detection
|
| 33 |
+
question_types, primary_type = self._classify_question_types(state.question, state.file_name)
|
| 34 |
+
state.question_type = primary_type
|
| 35 |
+
state.add_processing_step(f"Router: Primary type: {primary_type.value}, All types: {[t.value for t in question_types]}")
|
| 36 |
|
| 37 |
# Step 2: Complexity assessment
|
| 38 |
complexity = self._assess_complexity(state.question)
|
| 39 |
state.complexity_assessment = complexity
|
| 40 |
state.add_processing_step(f"Router: Assessed complexity as {complexity}")
|
| 41 |
|
| 42 |
+
# Step 3: Select appropriate agents with sequencing
|
| 43 |
+
selected_agents = self._select_agents_enhanced(question_types, primary_type, state.file_name is not None, complexity)
|
| 44 |
state.selected_agents = selected_agents
|
| 45 |
state.add_processing_step(f"Router: Selected agents: {[a.value for a in selected_agents]}")
|
| 46 |
|
|
|
|
| 51 |
|
| 52 |
# Step 5: Create routing decision summary
|
| 53 |
state.routing_decision = {
|
| 54 |
+
"primary_type": primary_type.value,
|
| 55 |
+
"all_types": [t.value for t in question_types],
|
| 56 |
"complexity": complexity,
|
| 57 |
"agents": [agent.value for agent in selected_agents],
|
| 58 |
"estimated_cost": estimated_cost,
|
| 59 |
+
"reasoning": self._get_routing_reasoning(primary_type, complexity, selected_agents, question_types)
|
| 60 |
}
|
| 61 |
|
| 62 |
# Step 6: Use LLM for complex routing decisions if needed
|
| 63 |
+
if complexity == "complex" or primary_type == QuestionType.UNKNOWN or len(question_types) > 2:
|
| 64 |
state = self._llm_enhanced_routing(state)
|
| 65 |
|
| 66 |
+
logger.info(f"✅ Routing complete: {primary_type.value} -> {[a.value for a in selected_agents]}")
|
| 67 |
return state
|
| 68 |
|
| 69 |
+
def _classify_question_types(self, question: str, file_name: str = None) -> Tuple[List[QuestionType], QuestionType]:
|
| 70 |
+
"""
|
| 71 |
+
Enhanced classification that can detect multiple question types
|
| 72 |
+
Returns: (all_detected_types, primary_type)
|
| 73 |
+
"""
|
| 74 |
|
| 75 |
question_lower = question.lower()
|
| 76 |
+
detected_types = []
|
| 77 |
|
| 78 |
+
# File processing questions (highest priority when file is present)
|
| 79 |
if file_name:
|
| 80 |
file_ext = file_name.lower().split('.')[-1] if '.' in file_name else ""
|
| 81 |
|
| 82 |
if file_ext in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg']:
|
| 83 |
+
detected_types.append(QuestionType.FILE_PROCESSING)
|
| 84 |
elif file_ext in ['mp3', 'wav', 'ogg', 'flac', 'm4a']:
|
| 85 |
+
detected_types.append(QuestionType.FILE_PROCESSING)
|
| 86 |
elif file_ext in ['xlsx', 'xls', 'csv']:
|
| 87 |
+
detected_types.append(QuestionType.FILE_PROCESSING)
|
| 88 |
elif file_ext in ['py', 'js', 'java', 'cpp', 'c']:
|
| 89 |
+
detected_types.append(QuestionType.CODE_EXECUTION)
|
| 90 |
else:
|
| 91 |
+
detected_types.append(QuestionType.FILE_PROCESSING)
|
| 92 |
|
| 93 |
+
# Enhanced URL-based classification
|
| 94 |
url_patterns = {
|
| 95 |
QuestionType.WIKIPEDIA: [
|
| 96 |
+
r'wikipedia\.org', r'featured article', r'promoted.*wikipedia',
|
| 97 |
+
r'english wikipedia', r'wiki.*article'
|
| 98 |
],
|
| 99 |
QuestionType.YOUTUBE: [
|
| 100 |
+
r'youtube\.com', r'youtu\.be', r'watch\?v=', r'video.*youtube',
|
| 101 |
+
r'https://www\.youtube\.com/watch'
|
| 102 |
]
|
| 103 |
}
|
| 104 |
|
| 105 |
for question_type, patterns in url_patterns.items():
|
| 106 |
if any(re.search(pattern, question_lower) for pattern in patterns):
|
| 107 |
+
detected_types.append(question_type)
|
| 108 |
|
| 109 |
+
# Enhanced content-based classification with better patterns
|
| 110 |
classification_patterns = {
|
| 111 |
QuestionType.MATHEMATICAL: [
|
| 112 |
+
# Counting/quantity questions
|
| 113 |
+
r'\bhow many\b', r'\bhow much\b', r'\bcount\b', r'\bnumber of\b',
|
| 114 |
+
r'\btotal\b', r'\bsum\b', r'\baverage\b', r'\bmean\b',
|
| 115 |
+
# Calculations
|
| 116 |
+
r'\bcalculate\b', r'\bcompute\b', r'\bsolve\b',
|
| 117 |
+
# Mathematical operations
|
| 118 |
+
r'\d+\s*[\+\-\*/]\s*\d+', r'\bsquare root\b', r'\bpercentage\b',
|
| 119 |
+
# Table analysis
|
| 120 |
+
r'\btable\b.*\bdefining\b', r'\bgiven.*table\b', r'\boperation table\b',
|
| 121 |
+
# Specific math terms
|
| 122 |
+
r'\bequation\b', r'\bformula\b', r'\bratio\b', r'\bfactorial\b',
|
| 123 |
+
# Economic/statistical
|
| 124 |
+
r'\binterest\b', r'\bcompound\b', r'\bstatistics\b'
|
| 125 |
+
],
|
| 126 |
+
QuestionType.TEXT_MANIPULATION: [
|
| 127 |
+
# Text operations
|
| 128 |
+
r'\breverse\b', r'\bbackwards\b', r'\bencode\b', r'\bdecode\b',
|
| 129 |
+
r'\btransform\b', r'\bconvert\b', r'\buppercase\b', r'\blowercase\b',
|
| 130 |
+
r'\breplace\b', r'\bextract\b', r'\bopposite\b',
|
| 131 |
+
# Pattern recognition for backwards text
|
| 132 |
+
r'[a-z]+\s+[a-z]+\s+[a-z]+.*\.', # Potential backwards sentence
|
| 133 |
+
# Specific text manipulation clues
|
| 134 |
+
r'\.rewsna\b', r'\bword.*opposite\b'
|
| 135 |
],
|
| 136 |
QuestionType.CODE_EXECUTION: [
|
| 137 |
r'\bcode\b', r'\bprogram\b', r'\bscript\b', r'\bfunction\b', r'\balgorithm\b',
|
| 138 |
+
r'\bexecute\b', r'\brun.*code\b', r'\bpython\b', r'\bjavascript\b',
|
| 139 |
+
r'\battached.*code\b', r'\bfinal.*output\b', r'\bnumeric output\b'
|
|
|
|
|
|
|
|
|
|
| 140 |
],
|
| 141 |
QuestionType.REASONING: [
|
| 142 |
+
# Logical reasoning
|
| 143 |
r'\bwhy\b', r'\bexplain\b', r'\banalyze\b', r'\breasoning\b', r'\blogic\b',
|
| 144 |
+
r'\brelationship\b', r'\bcompare\b', r'\bcontrast\b', r'\bconclusion\b',
|
| 145 |
+
# Complex analysis
|
| 146 |
+
r'\bexamine\b', r'\bidentify\b', r'\bdetermine\b', r'\bassess\b',
|
| 147 |
+
r'\bevaluate\b', r'\binterpret\b'
|
| 148 |
],
|
| 149 |
QuestionType.WEB_RESEARCH: [
|
| 150 |
+
# General research
|
| 151 |
r'\bsearch\b', r'\bfind.*information\b', r'\bresearch\b', r'\blook up\b',
|
| 152 |
+
r'\bwebsite\b', r'\bonline\b', r'\binternet\b',
|
| 153 |
+
# Who/what/when/where questions
|
| 154 |
+
r'\bwho\s+(?:is|was|are|were|did|does)\b',
|
| 155 |
r'\bwhat\s+(?:is|was|are|were)\b', r'\bwhen\s+(?:is|was|did|does)\b',
|
| 156 |
+
r'\bwhere\s+(?:is|was|are|were)\b',
|
| 157 |
+
# Factual queries
|
| 158 |
+
r'\bauthor\b', r'\bpublished\b', r'\bhistory\b', r'\bhistorical\b',
|
| 159 |
+
r'\bcentury\b', r'\byear\b', r'\bbiography\b', r'\bwinner\b',
|
| 160 |
+
# Specific research indicators
|
| 161 |
+
r'\bstudio albums\b', r'\brecipient\b', r'\bcompetition\b', r'\bspecimens\b'
|
| 162 |
]
|
| 163 |
}
|
| 164 |
|
| 165 |
+
# Score each category with enhanced scoring
|
| 166 |
type_scores = {}
|
| 167 |
for question_type, patterns in classification_patterns.items():
|
| 168 |
score = 0
|
| 169 |
for pattern in patterns:
|
| 170 |
matches = re.findall(pattern, question_lower)
|
| 171 |
score += len(matches)
|
| 172 |
+
# Give extra weight to certain patterns
|
| 173 |
+
if question_type == QuestionType.MATHEMATICAL and pattern in [r'\bhow many\b', r'\bhow much\b']:
|
| 174 |
+
score += 2 # Boost counting questions
|
| 175 |
+
elif question_type == QuestionType.TEXT_MANIPULATION and any(special in pattern for special in ['opposite', 'reverse', 'backwards']):
|
| 176 |
+
score += 2 # Reduced from 3 to 2 to avoid over-weighting
|
| 177 |
if score > 0:
|
| 178 |
type_scores[question_type] = score
|
| 179 |
|
| 180 |
# Special handling for specific question patterns
|
| 181 |
|
| 182 |
+
# Detect backwards/scrambled text (strong indicator)
|
| 183 |
+
if re.search(r'\.rewsna|tfel|etirw', question_lower):
|
| 184 |
+
type_scores[QuestionType.TEXT_MANIPULATION] = type_scores.get(QuestionType.TEXT_MANIPULATION, 0) + 3
|
| 185 |
+
|
| 186 |
+
# Detect code execution patterns (strong indicator)
|
| 187 |
+
if re.search(r'\bfinal.*output\b|\bnumeric.*output\b|\battached.*code\b', question_lower):
|
| 188 |
+
type_scores[QuestionType.CODE_EXECUTION] = type_scores.get(QuestionType.CODE_EXECUTION, 0) + 4
|
| 189 |
+
|
| 190 |
+
# Detect mathematical operations with numbers
|
| 191 |
+
if re.search(r'\b\d+.*\b(?:studio albums|between|and)\b.*\d+', question_lower):
|
| 192 |
+
type_scores[QuestionType.MATHEMATICAL] = type_scores.get(QuestionType.MATHEMATICAL, 0) + 3
|
| 193 |
+
|
| 194 |
+
# Detect table/grid operations
|
| 195 |
+
if re.search(r'\btable.*defining.*\*', question_lower) or '|*|' in question:
|
| 196 |
+
type_scores[QuestionType.MATHEMATICAL] = type_scores.get(QuestionType.MATHEMATICAL, 0) + 4
|
| 197 |
+
|
| 198 |
+
# Multi-step questions that need research AND calculation
|
| 199 |
+
if ('how many' in question_lower or 'how much' in question_lower) and \
|
| 200 |
+
any(term in question_lower for term in ['between', 'from', 'during', 'published', 'released']):
|
| 201 |
type_scores[QuestionType.WEB_RESEARCH] = type_scores.get(QuestionType.WEB_RESEARCH, 0) + 2
|
| 202 |
+
type_scores[QuestionType.MATHEMATICAL] = type_scores.get(QuestionType.MATHEMATICAL, 0) + 2
|
| 203 |
|
| 204 |
+
# Add detected types based on scores
|
| 205 |
+
for qtype, score in type_scores.items():
|
| 206 |
+
if score > 0 and qtype not in detected_types:
|
| 207 |
+
detected_types.append(qtype)
|
| 208 |
|
| 209 |
+
# If no types detected, default to web research
|
| 210 |
+
if not detected_types:
|
| 211 |
+
detected_types.append(QuestionType.WEB_RESEARCH)
|
| 212 |
|
| 213 |
+
# Determine primary type (highest scoring)
|
| 214 |
if type_scores:
|
| 215 |
+
primary_type = max(type_scores.keys(), key=lambda t: type_scores[t])
|
| 216 |
+
else:
|
| 217 |
+
primary_type = detected_types[0] if detected_types else QuestionType.WEB_RESEARCH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
return detected_types, primary_type
|
|
|
|
| 220 |
|
| 221 |
def _assess_complexity(self, question: str) -> str:
|
| 222 |
+
"""Assess question complexity with enhanced logic"""
|
| 223 |
|
| 224 |
question_lower = question.lower()
|
| 225 |
|
|
|
|
| 227 |
complex_indicators = [
|
| 228 |
'multi-step', 'multiple', 'several', 'complex', 'detailed',
|
| 229 |
'analyze', 'explain why', 'reasoning', 'relationship',
|
| 230 |
+
'compare and contrast', 'comprehensive', 'thorough',
|
| 231 |
+
'between.*and', 'table.*defining', 'attached.*file'
|
| 232 |
]
|
| 233 |
|
| 234 |
# Simple indicators
|
| 235 |
simple_indicators = [
|
| 236 |
+
'what is', 'who is', 'when did', 'where is', 'yes or no',
|
| 237 |
+
'true or false', 'simple', 'quick', 'name'
|
| 238 |
]
|
| 239 |
|
| 240 |
+
complex_score = sum(1 for indicator in complex_indicators if re.search(indicator, question_lower))
|
| 241 |
+
simple_score = sum(1 for indicator in simple_indicators if re.search(indicator, question_lower))
|
| 242 |
|
| 243 |
# Additional complexity factors
|
| 244 |
if len(question) > 200:
|
| 245 |
complex_score += 1
|
| 246 |
if len(question.split()) > 30:
|
| 247 |
complex_score += 1
|
| 248 |
+
if question.count('?') > 1: # Multiple questions
|
| 249 |
+
complex_score += 1
|
| 250 |
+
if '|' in question and '*' in question: # Tables
|
| 251 |
+
complex_score += 2
|
| 252 |
+
if re.search(r'\d+.*between.*\d+', question_lower): # Date ranges
|
| 253 |
complex_score += 1
|
| 254 |
|
| 255 |
# Determine complexity
|
| 256 |
+
if complex_score >= 3:
|
| 257 |
return "complex"
|
| 258 |
+
elif complex_score >= 1 and simple_score == 0:
|
| 259 |
+
return "medium"
|
| 260 |
elif simple_score >= 2 and complex_score == 0:
|
| 261 |
return "simple"
|
| 262 |
else:
|
| 263 |
return "medium"
|
| 264 |
|
| 265 |
+
def _select_agents_enhanced(self, question_types: List[QuestionType], primary_type: QuestionType,
|
| 266 |
+
has_file: bool, complexity: str) -> List[AgentRole]:
|
| 267 |
+
"""
|
| 268 |
+
Enhanced agent selection that can choose multiple agents for complex workflows
|
| 269 |
+
"""
|
| 270 |
|
| 271 |
agents = []
|
| 272 |
|
| 273 |
+
# Always include synthesizer at the end for final answer compilation
|
| 274 |
+
# (We'll add it at the end to ensure proper ordering)
|
| 275 |
+
|
| 276 |
+
# Multi-agent selection based on detected question types
|
| 277 |
+
agent_priorities = {
|
| 278 |
+
QuestionType.FILE_PROCESSING: [AgentRole.FILE_PROCESSOR],
|
| 279 |
+
QuestionType.CODE_EXECUTION: [AgentRole.CODE_EXECUTOR],
|
| 280 |
+
QuestionType.WIKIPEDIA: [AgentRole.WEB_RESEARCHER],
|
| 281 |
+
QuestionType.YOUTUBE: [AgentRole.WEB_RESEARCHER],
|
| 282 |
+
QuestionType.WEB_RESEARCH: [AgentRole.WEB_RESEARCHER],
|
| 283 |
+
QuestionType.MATHEMATICAL: [AgentRole.REASONING_AGENT],
|
| 284 |
+
QuestionType.TEXT_MANIPULATION: [AgentRole.REASONING_AGENT],
|
| 285 |
+
QuestionType.REASONING: [AgentRole.REASONING_AGENT]
|
| 286 |
+
}
|
| 287 |
|
| 288 |
+
# Add agents based on all detected question types
|
| 289 |
+
for qtype in question_types:
|
| 290 |
+
if qtype in agent_priorities:
|
| 291 |
+
for agent in agent_priorities[qtype]:
|
| 292 |
+
if agent not in agents:
|
| 293 |
+
agents.append(agent)
|
| 294 |
+
|
| 295 |
+
# Special combinations for multi-step questions
|
| 296 |
+
|
| 297 |
+
# For CODE_EXECUTION as primary type, prioritize code executor
|
| 298 |
+
if primary_type == QuestionType.CODE_EXECUTION:
|
| 299 |
+
# Ensure code executor is first, followed by any other needed agents
|
| 300 |
+
ordered_agents = []
|
| 301 |
+
if AgentRole.CODE_EXECUTOR not in ordered_agents:
|
| 302 |
+
ordered_agents.append(AgentRole.CODE_EXECUTOR)
|
| 303 |
+
# Add other agents if needed for multi-type questions
|
| 304 |
+
for agent in agents:
|
| 305 |
+
if agent != AgentRole.CODE_EXECUTOR and agent not in ordered_agents:
|
| 306 |
+
ordered_agents.append(agent)
|
| 307 |
+
agents = ordered_agents
|
| 308 |
+
|
| 309 |
+
# Research + Math combinations (e.g., "How many albums between 2000-2009?")
|
| 310 |
+
elif (QuestionType.WEB_RESEARCH in question_types and QuestionType.MATHEMATICAL in question_types):
|
| 311 |
+
# Ensure proper order: Research first, then math
|
| 312 |
+
ordered_agents = []
|
| 313 |
+
if AgentRole.WEB_RESEARCHER not in ordered_agents:
|
| 314 |
+
ordered_agents.append(AgentRole.WEB_RESEARCHER)
|
| 315 |
+
if AgentRole.REASONING_AGENT not in ordered_agents:
|
| 316 |
+
ordered_agents.append(AgentRole.REASONING_AGENT)
|
| 317 |
+
agents = ordered_agents
|
| 318 |
+
|
| 319 |
+
# File + Analysis combinations
|
| 320 |
+
elif has_file and len(question_types) > 1:
|
| 321 |
+
# File processing should come first
|
| 322 |
+
ordered_agents = []
|
| 323 |
+
if AgentRole.FILE_PROCESSOR not in ordered_agents:
|
| 324 |
+
ordered_agents.append(AgentRole.FILE_PROCESSOR)
|
| 325 |
+
# Then add other agents
|
| 326 |
+
for agent in agents:
|
| 327 |
+
if agent != AgentRole.FILE_PROCESSOR and agent not in ordered_agents:
|
| 328 |
+
ordered_agents.append(agent)
|
| 329 |
+
agents = ordered_agents
|
| 330 |
+
|
| 331 |
+
# For complex questions, add reasoning if not already present
|
| 332 |
+
if complexity == "complex" and AgentRole.REASONING_AGENT not in agents:
|
| 333 |
agents.append(AgentRole.REASONING_AGENT)
|
| 334 |
+
|
| 335 |
+
# Fallback for unknown/unclear questions - use multiple agents
|
| 336 |
+
if primary_type == QuestionType.UNKNOWN or not agents:
|
| 337 |
+
agents = [AgentRole.WEB_RESEARCHER, AgentRole.REASONING_AGENT]
|
| 338 |
+
|
| 339 |
+
# Always add synthesizer at the end
|
| 340 |
+
agents.append(AgentRole.SYNTHESIZER)
|
|
|
|
|
|
|
| 341 |
|
| 342 |
# Remove duplicates while preserving order
|
| 343 |
seen = set()
|
|
|
|
| 360 |
|
| 361 |
base_cost = base_costs.get(complexity, 0.015)
|
| 362 |
|
| 363 |
+
# Additional cost per agent (more agents = more processing)
|
| 364 |
+
agent_cost = len(agents) * 0.008
|
| 365 |
|
| 366 |
return base_cost + agent_cost
|
| 367 |
|
| 368 |
+
def _get_routing_reasoning(self, primary_type: QuestionType, complexity: str,
|
| 369 |
+
agents: List[AgentRole], all_types: List[QuestionType]) -> str:
|
| 370 |
"""Generate human-readable reasoning for routing decision"""
|
| 371 |
|
| 372 |
reasons = []
|
| 373 |
|
| 374 |
+
# Primary type reasoning
|
| 375 |
+
type_descriptions = {
|
| 376 |
+
QuestionType.WIKIPEDIA: "References Wikipedia content",
|
| 377 |
+
QuestionType.YOUTUBE: "Involves YouTube video analysis",
|
| 378 |
+
QuestionType.FILE_PROCESSING: "Requires file processing",
|
| 379 |
+
QuestionType.MATHEMATICAL: "Involves mathematical computation/counting",
|
| 380 |
+
QuestionType.CODE_EXECUTION: "Requires code execution",
|
| 381 |
+
QuestionType.TEXT_MANIPULATION: "Involves text transformation/manipulation",
|
| 382 |
+
QuestionType.REASONING: "Requires logical reasoning/analysis",
|
| 383 |
+
QuestionType.WEB_RESEARCH: "Needs web research for factual information"
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
if primary_type in type_descriptions:
|
| 387 |
+
reasons.append(type_descriptions[primary_type])
|
| 388 |
+
|
| 389 |
+
# Multi-type questions
|
| 390 |
+
if len(all_types) > 1:
|
| 391 |
+
other_types = [t for t in all_types if t != primary_type]
|
| 392 |
+
reasons.append(f"Also involves: {', '.join([t.value for t in other_types])}")
|
| 393 |
|
| 394 |
# Complexity reasoning
|
| 395 |
if complexity == "complex":
|
| 396 |
+
reasons.append("Complex multi-step reasoning required")
|
| 397 |
elif complexity == "simple":
|
| 398 |
reasons.append("Straightforward question")
|
| 399 |
|
| 400 |
+
# Agent workflow reasoning
|
| 401 |
agent_names = [agent.value.replace('_', ' ') for agent in agents]
|
| 402 |
+
if len(agents) > 2: # More than synthesizer + one agent
|
| 403 |
+
reasons.append(f"Multi-agent workflow: {' → '.join(agent_names)}")
|
| 404 |
+
else:
|
| 405 |
+
reasons.append(f"Single-agent workflow: {', '.join(agent_names)}")
|
| 406 |
|
| 407 |
return "; ".join(reasons)
|
| 408 |
|
|
|
|
| 414 |
|
| 415 |
Question: {state.question}
|
| 416 |
File attached: {state.file_name if state.file_name else "None"}
|
| 417 |
+
Detected types: {state.routing_decision.get('all_types', [])}
|
| 418 |
+
Primary classification: {state.question_type.value}
|
| 419 |
Current complexity: {state.complexity_assessment}
|
| 420 |
+
Selected agents: {[a.value for a in state.selected_agents]}
|
| 421 |
|
| 422 |
+
Does this question need:
|
| 423 |
+
1. Web research to find factual information?
|
| 424 |
+
2. Mathematical calculation or counting?
|
| 425 |
+
3. Text manipulation or decoding?
|
| 426 |
+
4. File processing or analysis?
|
| 427 |
+
5. Logical reasoning or analysis?
|
| 428 |
|
| 429 |
+
Should the agent selection be adjusted? If so, provide specific recommendations.
|
| 430 |
Keep response concise and focused on routing decisions.
|
| 431 |
"""
|
| 432 |
|
| 433 |
try:
|
| 434 |
+
# Use main model (32B) for better routing decisions
|
| 435 |
+
tier = ModelTier.MAIN
|
| 436 |
+
result = self.llm_client.generate(prompt, tier=tier, max_tokens=300)
|
| 437 |
|
| 438 |
if result.success:
|
| 439 |
state.add_processing_step("Router: Enhanced with LLM analysis")
|