@@ -75,8 +75,9 @@ def determine_agent(
7575 return result .output
7676
7777 except Exception as e :
78- # Fallback to simple keyword-based routing
79- return self ._fallback_routing (user_query , csv_loaded )
78+ print (f"Routing failed with error: { e } " )
79+ # Use simple keyword-based routing as fallback
80+ return self ._keyword_based_routing (user_query , csv_loaded )
8081
8182 def handle_conversation_query (self , user_query : str ) -> str :
8283 """Handle conversational queries."""
@@ -131,7 +132,7 @@ def handle_csv_query(
131132
132133 except Exception as e :
133134 # If LLM fails, provide a graceful response without showing errors
134- return WORST_CASE_SCENARIO
135+ return WORST_CASE_SCENARIO
135136
136137 def _execute_csv_analysis (
137138 self , python_code : str , csv_info : Dict [str , Any ], explanation : str
@@ -224,15 +225,15 @@ def install_package(package):
224225 current_code = fixed_code
225226 continue # Try again with fixed code
226227
227- return WORST_CASE_SCENARIO
228+ return WORST_CASE_SCENARIO
228229
229230 except Exception as e :
230- return WORST_CASE_SCENARIO
231+ return WORST_CASE_SCENARIO
231232
232233 except Exception as e :
233- return WORST_CASE_SCENARIO
234+ return WORST_CASE_SCENARIO
234235
235- return WORST_CASE_SCENARIO
236+ return WORST_CASE_SCENARIO
236237
237238 def _fix_python_code (
238239 self , original_code : str , error_message : str , csv_info : Dict [str , Any ]
@@ -375,6 +376,68 @@ def _format_sql_response(self, sql_response) -> str:
375376
376377 return "\n \n " .join (response_parts )
377378
379+ def _keyword_based_routing (self , user_query : str , csv_loaded : bool ) -> RoutingDecision :
380+ """Keyword-based routing when LLM routing fails."""
381+ query_lower = user_query .lower ()
382+
383+ # CSV Agent keywords
384+ csv_keywords = [
385+ "csv" , "analyze" , "chart" , "plot" , "graph" , "average" , "mean" , "sum" ,
386+ "count" , "max" , "min" , "statistics" , "data" , "visualization" , "top" ,
387+ "bottom" , "highest" , "lowest" , "distribution" , "correlation"
388+ ]
389+
390+ # SQL Agent keywords
391+ sql_keywords = [
392+ "select" , "insert" , "update" , "delete" , "sql" , "query" , "table" ,
393+ "database" , "users" , "customers" , "orders" , "products" , "where" ,
394+ "join" , "group by" , "order by" , "from"
395+ ]
396+
397+ # Conversation Agent keywords
398+ conversation_keywords = [
399+ "hello" , "hi" , "hey" , "how are you" , "what can you do" , "help" ,
400+ "thanks" , "thank you" , "goodbye" , "bye" , "good morning" , "good evening"
401+ ]
402+
403+ # Check for CSV analysis (prioritize if CSV is loaded)
404+ if csv_loaded and any (keyword in query_lower for keyword in csv_keywords ):
405+ return RoutingDecision (
406+ agent = "CSV_AGENT" ,
407+ confidence = 0.8 ,
408+ reasoning = "Keyword-based routing detected CSV analysis request"
409+ )
410+
411+ # Check for SQL keywords
412+ if any (keyword in query_lower for keyword in sql_keywords ):
413+ return RoutingDecision (
414+ agent = "SQL_AGENT" ,
415+ confidence = 0.8 ,
416+ reasoning = "Keyword-based routing detected SQL request"
417+ )
418+
419+ # Check for conversation keywords
420+ if any (keyword in query_lower for keyword in conversation_keywords ):
421+ return RoutingDecision (
422+ agent = "CONVERSATION_AGENT" ,
423+ confidence = 0.9 ,
424+ reasoning = "Keyword-based routing detected conversation request"
425+ )
426+
427+ # Default based on context
428+ if csv_loaded :
429+ return RoutingDecision (
430+ agent = "CSV_AGENT" ,
431+ confidence = 0.6 ,
432+ reasoning = "CSV loaded, defaulting to CSV analysis"
433+ )
434+ else :
435+ return RoutingDecision (
436+ agent = "CONVERSATION_AGENT" ,
437+ confidence = 0.5 ,
438+ reasoning = "No clear intent detected, defaulting to conversation"
439+ )
440+
378441 def _fallback_routing (self , user_query : str , csv_loaded : bool ) -> RoutingDecision :
379442 """Fallback routing when LLM routing fails - let LLM decide, not hardcoded keywords."""
380443 # Default to conversation - let the LLM handle all decisions
@@ -386,4 +449,4 @@ def _fallback_routing(self, user_query: str, csv_loaded: bool) -> RoutingDecisio
386449
387450 def _get_fallback_conversation_response (self , user_query : str ) -> str :
388451 """Get fallback conversation response when LLM fails."""
389- return WORST_CASE_SCENARIO
452+ return WORST_CASE_SCENARIO
0 commit comments