File size: 16,357 Bytes
b30f068 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | """
Tool Matcher
Routes user questions to appropriate tools based on keyword matching
"""
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
from typing import Dict, List, Tuple
import re
from rapidfuzz import fuzz
class ToolMatcher:
"""
Matches user queries to appropriate tools using hybrid approach
Uses keyword-based matching (fast path) and LLM classification (smart path):
- weather: weather, temperature, forecast, etc.
- soil: soil, agriculture, farming, pH, etc.
- rag/documentation: how, what, explain, documentation, etc.
"""
def __init__(
self,
use_llm_fallback: bool = True,
confidence_threshold: float = 0.85,
llm_threshold: float = 0.6
):
"""
Initialize tool matcher with hybrid routing
Args:
use_llm_fallback: Enable LLM fallback for low confidence queries
confidence_threshold: High confidence threshold (use fast path if above)
llm_threshold: Low confidence threshold (use LLM if below)
"""
# Configuration
self.use_llm_fallback = use_llm_fallback
self.confidence_threshold = confidence_threshold
self.llm_threshold = llm_threshold
# Lazy load LLM classifier (only if needed)
self._llm_classifier = None
# Define tool patterns and keywords
self.tool_patterns = {
"weather": {
"keywords": [
"weather", "temperature", "forecast", "climate", "rain",
"sunny", "cloudy", "wind", "humid", "hot", "cold",
"degrees", "celsius", "fahrenheit", "precipitation",
"humidity", "pressure"
],
"description": "Get weather information for any location",
"priority": 1
},
"soil": {
"keywords": [
"soil data", "soil composition", "usda soil", "soil properties",
"soil type", "soil texture", "soil ph", "soil nutrient"
],
"description": "Get soil properties and agricultural data",
"priority": 1
},
"cdms_label": {
"keywords": [
"label", "pesticide label", "herbicide label", "insecticide label",
"fungicide label", "cdms label", "product label", "epa label",
"roundup", "sevin", "2,4-d", "atrazine", "glyphosate", "carbaryl",
"find label", "get label", "show label", "download label",
"chemical label", "safety data sheet", "sds", "pesticide",
"herbicide", "insecticide", "fungicide", "application rate",
"safety precautions", "mixing instructions", "re-entry interval"
],
"description": "Search CDMS database for pesticide labels (first priority)",
"priority": 3 # Highest priority for pesticide/label queries
},
"agriculture_web": {
"keywords": [
"how to", "best practices", "pest control", "control pests",
"fertilizer", "fertilization", "organic matter", "crop management",
"aphids", "tomato", "corn", "wheat", "soybean",
"nitrogen", "phosphorus", "potassium", "growing", "planting",
"improve soil", "increase yield", "disease control"
],
"description": "Search web for agriculture information",
"priority": 1
},
"rag": {
"keywords": [
"what is", "explain", "tell me about", "information about",
"documentation", "guide", "tutorial", "pesticide", "herbicide",
"insecticide", "label", "safety"
],
"description": "Search CDMS database for pesticide labels (RAG)",
"priority": 0 # Lowest priority, fallback option - redirects to CDMS
}
}
def _get_llm_classifier(self):
"""Lazy load LLM classifier"""
if self._llm_classifier is None:
try:
from src.tools.llm_intent_classifier import LLMIntentClassifier
self._llm_classifier = LLMIntentClassifier()
except Exception as e:
print(f"⚠️ Could not initialize LLM classifier: {e}")
return None
return self._llm_classifier
def _fuzzy_match(self, keywords: List[str], full_query: str, conversation_context: list = None) -> Dict:
"""
Fast path: Fuzzy keyword matching (existing logic)
Returns:
Dict with tool_name, confidence, matched_keywords, etc.
"""
scores = {}
query_lower = full_query.lower()
# Check if this is a follow-up question
previous_tool = None
if conversation_context:
# Look for tool indicators in previous messages
for msg in reversed(conversation_context):
content = msg.get("content", "").lower()
# Check for tool-specific keywords in previous messages
if any(kw in content for kw in ["roundup", "sevin", "pesticide", "herbicide",
"label", "application rate", "safety", "mixing"]):
previous_tool = "cdms_label"
break
elif any(kw in content for kw in ["soil", "soil data", "soil properties"]):
previous_tool = "soil"
break
elif any(kw in content for kw in ["weather", "temperature", "forecast"]):
previous_tool = "weather"
break
elif any(kw in content for kw in ["aphid", "pest", "crop", "fertilizer", "organic"]):
previous_tool = "agriculture_web"
break
# First, check if this is a pesticide/label question (highest priority)
pesticide_keywords = [
"pesticide", "herbicide", "insecticide", "fungicide", "label",
"roundup", "sevin", "2,4-d", "atrazine", "glyphosate", "carbaryl",
"application rate", "safety precautions", "mixing instructions",
"re-entry interval", "chemical", "sds", "safety data sheet"
]
is_pesticide_query = any(kw in query_lower for kw in pesticide_keywords)
# If this is a follow-up and query is vague, boost previous tool
is_vague_followup = (
previous_tool and
(len(full_query.split()) <= 5 or
any(phrase in query_lower for phrase in ["what about", "how about", "tell me", "it", "this"]))
)
# Score each tool based on keyword overlap
for tool_id, tool_info in self.tool_patterns.items():
score = 0
matched_keywords = []
tool_keywords = tool_info["keywords"]
for keyword in keywords:
keyword_lower = keyword.lower()
for tool_keyword in tool_keywords:
# Use fuzzy matching
similarity = fuzz.ratio(keyword_lower, tool_keyword)
if similarity > 80: # 80% similarity threshold
score += similarity
matched_keywords.append(tool_keyword)
# Also check full query against tool keywords
for tool_keyword in tool_keywords:
if tool_keyword in query_lower:
score += 50 # Bonus for exact matches in query
if tool_keyword not in matched_keywords:
matched_keywords.append(tool_keyword)
# Special boost for CDMS if it's a pesticide query
if tool_id == "cdms_label" and is_pesticide_query:
score += 200 # Large boost for pesticide queries
# Boost previous tool for follow-up questions
if is_vague_followup and tool_id == previous_tool:
score += 150 # Large boost for follow-up to previous tool
# Add priority bonus
priority = tool_info.get("priority", 0)
priority_bonus = priority * 10
scores[tool_id] = {
"score": score + priority_bonus,
"raw_score": score,
"priority": priority,
"matched_keywords": list(set(matched_keywords))
}
# Get best matching tool
if not scores or max(s["raw_score"] for s in scores.values()) == 0:
# If it's a pesticide query, default to CDMS; otherwise agriculture_web
if is_pesticide_query:
best_tool = "cdms_label"
confidence = 0.5
else:
best_tool = "agriculture_web"
confidence = 0.3
else:
# Sort by score (includes priority), then by priority if tied
best_tool = max(scores, key=lambda x: (scores[x]["score"], scores[x]["priority"]))
max_score = scores[best_tool]["raw_score"]
# Normalize confidence to 0-1
confidence = min(max_score / 300, 1.0)
# Boost confidence if it's a clear match
if max_score > 100:
confidence = min(confidence + 0.2, 1.0)
return {
"tool_name": best_tool,
"tool_display_name": self.tool_patterns[best_tool]["description"],
"confidence": confidence,
"matched_keywords": scores[best_tool]["matched_keywords"],
"all_scores": scores,
"raw_score": scores[best_tool]["raw_score"]
}
def match_tool(
self,
keywords: List[str],
full_query: str,
conversation_context: list = None
) -> Dict:
"""
Match extracted keywords to the best tool using hybrid approach
Flow:
1. Try fast path (fuzzy matching)
2. If confidence > threshold: return immediately
3. If confidence < llm_threshold: use LLM
4. If in between: combine both results
Args:
keywords: List of extracted keywords from query
full_query: Original user question
conversation_context: Optional list of previous messages for context
Returns:
Dict with tool_name, confidence, matched_keywords, method, etc.
"""
# Step 1: Fast path (existing fuzzy matching logic)
fast_result = self._fuzzy_match(keywords, full_query, conversation_context)
fast_confidence = fast_result.get("confidence", 0.0)
# Step 2: High confidence - return fast path result immediately
if fast_confidence >= self.confidence_threshold:
return {
**fast_result,
"method": "fast_path",
"llm_used": False
}
# Step 3: Low confidence - use LLM
if fast_confidence < self.llm_threshold and self.use_llm_fallback:
llm_classifier = self._get_llm_classifier()
if llm_classifier:
llm_result = llm_classifier.classify_intent(
full_query,
conversation_context
)
return {
"tool_name": llm_result["tool_name"],
"tool_display_name": self.tool_patterns[llm_result["tool_name"]]["description"],
"confidence": llm_result["confidence"],
"matched_keywords": [],
"method": llm_result.get("method", "llm_path"),
"llm_used": True,
"llm_reasoning": llm_result.get("reasoning", ""),
"fast_path_confidence": fast_confidence
}
else:
# LLM not available, return fast path
return {
**fast_result,
"method": "fast_path_fallback",
"llm_used": False
}
# Step 4: Medium confidence - hybrid approach
# Combine fast path and LLM results
if self.use_llm_fallback:
llm_classifier = self._get_llm_classifier()
if llm_classifier:
llm_result = llm_classifier.classify_intent(
full_query,
conversation_context
)
# Weighted combination
fast_weight = fast_confidence
llm_weight = llm_result.get("confidence", 0.5)
# If both agree, boost confidence
if fast_result["tool_name"] == llm_result["tool_name"]:
combined_confidence = min(1.0, (fast_weight + llm_weight) / 2 + 0.1)
selected_tool = fast_result["tool_name"]
else:
# Disagreement - trust LLM more for ambiguous cases
if llm_weight > fast_weight + 0.2:
selected_tool = llm_result["tool_name"]
combined_confidence = llm_weight
else:
selected_tool = fast_result["tool_name"]
combined_confidence = fast_weight
return {
"tool_name": selected_tool,
"tool_display_name": self.tool_patterns[selected_tool]["description"],
"confidence": combined_confidence,
"matched_keywords": fast_result.get("matched_keywords", []),
"method": "hybrid",
"llm_used": True,
"fast_path_result": {
"tool": fast_result["tool_name"],
"confidence": fast_confidence
},
"llm_result": {
"tool": llm_result["tool_name"],
"confidence": llm_weight,
"reasoning": llm_result.get("reasoning", "")
}
}
# Fallback: return fast path even if low confidence
return {
**fast_result,
"method": "fast_path_fallback",
"llm_used": False
}
def execute_tool(self, tool_name: str, question: str, keywords: List[str]) -> Dict:
"""
Execute the selected tool (delegates to ToolExecutor)
This is kept for backward compatibility, but ToolExecutor
should be used directly in the UI
"""
from src.tools.tool_executor import ToolExecutor
executor = ToolExecutor()
return executor.execute(tool_name, question)
# Test function
if __name__ == "__main__":
print("Testing Tool Matcher...")
print("-" * 70)
from src.utils.parameter_extractor import extract_keywords_from_query
matcher = ToolMatcher()
test_queries = [
"What's the weather in London?",
"Show me soil data for Iowa",
"Find me the Roundup pesticide label",
"How to control aphids on tomato plants?",
"Get the Sevin insecticide label",
"Best practices for corn fertilization",
"What's the temperature in Tokyo?",
"Tell me about soil pH in California",
"Show me 2,4-D herbicide label",
"How to improve soil organic matter?",
"Explain the API documentation"
]
for query in test_queries:
keywords = extract_keywords_from_query(query)
match = matcher.match_tool(keywords, query)
print(f"\n📝 Query: {query}")
print(f" Keywords: {keywords}")
print(f" → Tool: {match['tool_name']} ({match['confidence']:.0%})")
print(f" Matched: {match['matched_keywords'][:3]}")
|