Spaces:
Running
Running
cleanup
Browse files- src/router.py +0 -137
src/router.py
CHANGED
|
@@ -1,140 +1,3 @@
|
|
| 1 |
-
# """
|
| 2 |
-
# Stage 1: Query Router - Intelligent Server Selection
|
| 3 |
-
# """
|
| 4 |
-
|
| 5 |
-
# import json
|
| 6 |
-
# from typing import Dict, Any
|
| 7 |
-
# from openai import OpenAI
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# class QueryRouter:
|
| 11 |
-
# """Stage 1: Routes queries to appropriate MCP servers"""
|
| 12 |
-
|
| 13 |
-
# def __init__(self, client: OpenAI, registry: Dict[str, Any]):
|
| 14 |
-
# self.client = client
|
| 15 |
-
# self.registry = registry
|
| 16 |
-
|
| 17 |
-
# def route(self, query: str, location: Dict[str, Any]) -> Dict[str, Any]:
|
| 18 |
-
# """
|
| 19 |
-
# Analyze query and determine which MCP servers are needed
|
| 20 |
-
|
| 21 |
-
# Returns:
|
| 22 |
-
# {
|
| 23 |
-
# "intent": str,
|
| 24 |
-
# "required_servers": List[str],
|
| 25 |
-
# "reasoning": str
|
| 26 |
-
# }
|
| 27 |
-
# """
|
| 28 |
-
# # Create registry summary
|
| 29 |
-
# registry_text = "Available MCP Servers:\n"
|
| 30 |
-
# for server_id, info in self.registry.items():
|
| 31 |
-
# registry_text += f"\n{server_id}:\n"
|
| 32 |
-
# registry_text += f" Description: {info['description']}\n"
|
| 33 |
-
# registry_text += f" Use for: {', '.join(info['use_for'][:5])}\n"
|
| 34 |
-
|
| 35 |
-
# system_prompt = f"""You are a query router for Farmer.chat agricultural system.
|
| 36 |
-
|
| 37 |
-
# Your task: Analyze the farmer's query and select which MCP servers are needed.
|
| 38 |
-
|
| 39 |
-
# {registry_text}
|
| 40 |
-
|
| 41 |
-
# Location: {location['name']} ({location['lat']}°N, {location['lon']}°E)
|
| 42 |
-
|
| 43 |
-
# CRITICAL RULES:
|
| 44 |
-
# 1. Select ALL servers that provide data relevant to answering the query completely
|
| 45 |
-
# 2. Consider IMPLICIT needs - look for context clues in the query
|
| 46 |
-
# 3. Keywords that trigger elevation: "elevation", "slope", "terrain", "my land", "my field", "drainage", "waterlogged", "frost risk", "wind exposure"
|
| 47 |
-
# 4. For crop decisions: ALWAYS include soil_properties + water + weather (comprehensive assessment)
|
| 48 |
-
# 5. For weather risk questions (wind, frost, flood): Include weather + elevation (terrain affects risk)
|
| 49 |
-
# 6. For pest questions with weather context: Include pests + weather
|
| 50 |
-
# 7. Be generous - better to have extra data than miss critical information
|
| 51 |
-
# 8. When farmer mentions location characteristics (height, slope, elevation), ALWAYS include elevation
|
| 52 |
-
|
| 53 |
-
# FEW-SHOT EXAMPLES:
|
| 54 |
-
|
| 55 |
-
# Example 1:
|
| 56 |
-
# Query: "Are strong winds expected at my land elevation?"
|
| 57 |
-
# Required: ["weather", "elevation"]
|
| 58 |
-
# Reasoning: Wind forecast from weather, but elevation affects wind exposure and risk. Farmer explicitly mentions elevation.
|
| 59 |
-
|
| 60 |
-
# Example 2:
|
| 61 |
-
# Query: "Should I plant rice today?"
|
| 62 |
-
# Required: ["weather", "soil_properties", "water"]
|
| 63 |
-
# Reasoning: Planting decisions need weather conditions, soil suitability, and water availability for comprehensive assessment.
|
| 64 |
-
|
| 65 |
-
# Example 3:
|
| 66 |
-
# Query: "Is there risk of frost tonight?"
|
| 67 |
-
# Required: ["weather", "elevation"]
|
| 68 |
-
# Reasoning: Frost risk depends on temperature from weather AND elevation (cold air sinks to lower areas).
|
| 69 |
-
|
| 70 |
-
# Example 4:
|
| 71 |
-
# Query: "What's my soil composition?"
|
| 72 |
-
# Required: ["soil_properties"]
|
| 73 |
-
# Reasoning: Direct soil query, only soil data needed. No implicit needs.
|
| 74 |
-
|
| 75 |
-
# Example 5:
|
| 76 |
-
# Query: "Can I grow tomatoes here?"
|
| 77 |
-
# Required: ["soil_properties", "water", "weather"]
|
| 78 |
-
# Reasoning: Crop suitability requires soil type, water availability, and climate conditions.
|
| 79 |
-
|
| 80 |
-
# Example 6:
|
| 81 |
-
# Query: "My field gets waterlogged after rain"
|
| 82 |
-
# Required: ["elevation", "soil_properties", "weather"]
|
| 83 |
-
# Reasoning: Waterlogging relates to drainage (elevation/slope), soil permeability, and rainfall patterns.
|
| 84 |
-
|
| 85 |
-
# Example 7:
|
| 86 |
-
# Query: "Should I spray pesticides now?"
|
| 87 |
-
# Required: ["pests", "weather"]
|
| 88 |
-
# Reasoning: Need to know pest presence AND weather conditions for optimal application timing.
|
| 89 |
-
|
| 90 |
-
# Example 8:
|
| 91 |
-
# Query: "How's the weather?"
|
| 92 |
-
# Required: ["weather"]
|
| 93 |
-
# Reasoning: Direct weather query, no implicit needs.
|
| 94 |
-
|
| 95 |
-
# Example 9:
|
| 96 |
-
# Query: "Give me complete farm status"
|
| 97 |
-
# Required: ["weather", "soil_properties", "water", "elevation", "pests"]
|
| 98 |
-
# Reasoning: Comprehensive assessment requires all available data sources.
|
| 99 |
-
|
| 100 |
-
# Example 10:
|
| 101 |
-
# Query: "Will it be too windy on my elevated farm?"
|
| 102 |
-
# Required: ["weather", "elevation"]
|
| 103 |
-
# Reasoning: Wind from weather, elevation affects exposure. "Elevated" is explicit context clue.
|
| 104 |
-
|
| 105 |
-
# Response format (JSON only):
|
| 106 |
-
# {{
|
| 107 |
-
# "intent": "brief description of farmer's need",
|
| 108 |
-
# "required_servers": ["server_id1", "server_id2"],
|
| 109 |
-
# "reasoning": "why these servers"
|
| 110 |
-
# }}
|
| 111 |
-
# """
|
| 112 |
-
|
| 113 |
-
# try:
|
| 114 |
-
# response = self.client.chat.completions.create(
|
| 115 |
-
# model="gpt-4o",
|
| 116 |
-
# messages=[
|
| 117 |
-
# {"role": "system", "content": system_prompt},
|
| 118 |
-
# {"role": "user", "content": query}
|
| 119 |
-
# ],
|
| 120 |
-
# temperature=0.3
|
| 121 |
-
# )
|
| 122 |
-
|
| 123 |
-
# result_text = response.choices[0].message.content.strip()
|
| 124 |
-
# result_text = result_text.replace("```json", "").replace("```", "").strip()
|
| 125 |
-
|
| 126 |
-
# routing_decision = json.loads(result_text)
|
| 127 |
-
# return routing_decision
|
| 128 |
-
|
| 129 |
-
# except Exception as e:
|
| 130 |
-
# print(f"❌ Routing error: {e}")
|
| 131 |
-
# # Fallback - include common servers
|
| 132 |
-
# return {
|
| 133 |
-
# "intent": "general_inquiry",
|
| 134 |
-
# "required_servers": ["weather", "soil_properties", "water"],
|
| 135 |
-
# "reasoning": "Fallback routing due to error"
|
| 136 |
-
# }
|
| 137 |
-
|
| 138 |
"""
|
| 139 |
Query Router - Stage 1
|
| 140 |
Simple router that always queries all MCP servers for alert generation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Query Router - Stage 1
|
| 3 |
Simple router that always queries all MCP servers for alert generation.
|