Spaces:
Runtime error
Runtime error
Commit ·
f209836
1
Parent(s): fe5fd85
Improve query intelligence: Add adaptive system prompts for policy overviews, impact analysis, and procedural questions
Browse files- hybrid_llm_service.py +86 -16
hybrid_llm_service.py
CHANGED
|
@@ -90,29 +90,101 @@ class HybridLLMService:
|
|
| 90 |
|
| 91 |
def analyze_task_complexity(self, message: str) -> TaskComplexity:
|
| 92 |
"""Analyze if a task requires complex reasoning or simple response"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
complex_keywords = [
|
| 94 |
-
'
|
| 95 |
-
'
|
| 96 |
-
'
|
| 97 |
]
|
| 98 |
|
|
|
|
| 99 |
simple_keywords = [
|
| 100 |
-
'what is', 'who is', 'when', 'where
|
| 101 |
-
'
|
| 102 |
]
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
complex_score = sum(1 for keyword in complex_keywords if keyword in message_lower)
|
| 108 |
simple_score = sum(1 for keyword in simple_keywords if keyword in message_lower)
|
| 109 |
|
| 110 |
-
# If message is very long
|
| 111 |
-
if len(message) >
|
| 112 |
return TaskComplexity.COMPLEX
|
| 113 |
|
| 114 |
return TaskComplexity.SIMPLE
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
def determine_task_complexity(self, message: str, context: str = "") -> TaskComplexity:
|
| 117 |
"""Determine task complexity - alias for analyze_task_complexity for compatibility"""
|
| 118 |
return self.analyze_task_complexity(message)
|
|
@@ -200,9 +272,8 @@ At least one of these API keys is required for the Voice Bot to function properl
|
|
| 200 |
|
| 201 |
async def _get_groq_response(self, message: str, context: str = "") -> str:
|
| 202 |
"""Get response from Groq LLM"""
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
Provide clear, concise, and helpful responses."""
|
| 206 |
|
| 207 |
if context:
|
| 208 |
system_prompt += f"\n\nRelevant context from documents:\n{context}"
|
|
@@ -217,9 +288,8 @@ At least one of these API keys is required for the Voice Bot to function properl
|
|
| 217 |
|
| 218 |
async def _get_gemini_response(self, message: str, context: str = "") -> str:
|
| 219 |
"""Get response from Gemini LLM"""
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
Provide detailed, analytical responses when needed."""
|
| 223 |
|
| 224 |
if context:
|
| 225 |
system_prompt += f"\n\nRelevant context from documents:\n{context}"
|
|
|
|
| 90 |
|
| 91 |
def analyze_task_complexity(self, message: str) -> TaskComplexity:
|
| 92 |
"""Analyze if a task requires complex reasoning or simple response"""
|
| 93 |
+
message_lower = message.lower()
|
| 94 |
+
|
| 95 |
+
# Impact analysis and scenario keywords (always complex)
|
| 96 |
+
impact_keywords = [
|
| 97 |
+
'impact', 'effect', 'scenario', 'chart', 'graph', 'visualization',
|
| 98 |
+
'analyze', 'compare', 'evaluate', 'breakdown', 'simulation',
|
| 99 |
+
'projection', 'forecast', 'calculation', 'calculate'
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
# Policy overview keywords (complex for comprehensive responses)
|
| 103 |
+
overview_keywords = [
|
| 104 |
+
'policies', 'schemes', 'types of', 'categories', 'overview',
|
| 105 |
+
'comprehensive', 'detailed', 'all about', 'everything about',
|
| 106 |
+
'complete guide', 'full information'
|
| 107 |
+
]
|
| 108 |
+
|
| 109 |
+
# Complex analysis keywords
|
| 110 |
complex_keywords = [
|
| 111 |
+
'detailed analysis', 'multi-step', 'in-depth', 'elaborate',
|
| 112 |
+
'comprehensive analysis', 'step by step', 'procedure',
|
| 113 |
+
'process', 'workflow', 'implementation'
|
| 114 |
]
|
| 115 |
|
| 116 |
+
# Simple definition keywords
|
| 117 |
simple_keywords = [
|
| 118 |
+
'what is', 'who is', 'when is', 'where is', 'define',
|
| 119 |
+
'meaning', 'definition', 'brief', 'quick', 'simple'
|
| 120 |
]
|
| 121 |
|
| 122 |
+
# Check for impact/scenario analysis (highest priority)
|
| 123 |
+
if any(keyword in message_lower for keyword in impact_keywords):
|
| 124 |
+
return TaskComplexity.COMPLEX
|
| 125 |
+
|
| 126 |
+
# Check for policy overview questions (need comprehensive responses)
|
| 127 |
+
if any(keyword in message_lower for keyword in overview_keywords):
|
| 128 |
+
return TaskComplexity.COMPLEX
|
| 129 |
+
|
| 130 |
+
# Check for other complex requests
|
| 131 |
complex_score = sum(1 for keyword in complex_keywords if keyword in message_lower)
|
| 132 |
simple_score = sum(1 for keyword in simple_keywords if keyword in message_lower)
|
| 133 |
|
| 134 |
+
# If message is very long or has complex keywords, use complex
|
| 135 |
+
if len(message) > 150 or complex_score > simple_score:
|
| 136 |
return TaskComplexity.COMPLEX
|
| 137 |
|
| 138 |
return TaskComplexity.SIMPLE
|
| 139 |
|
| 140 |
+
def _create_adaptive_system_prompt(self, message: str) -> str:
|
| 141 |
+
"""Create adaptive system prompt based on query type"""
|
| 142 |
+
message_lower = message.lower()
|
| 143 |
+
|
| 144 |
+
# Base prompt
|
| 145 |
+
base_prompt = """You are a helpful AI assistant specializing in government policies and procedures. You have access to thousands of government documents and can provide accurate information based on them."""
|
| 146 |
+
|
| 147 |
+
# Policy overview questions
|
| 148 |
+
if any(keyword in message_lower for keyword in ['policies', 'schemes', 'types of', 'categories', 'overview', 'all about']):
|
| 149 |
+
return f"""{base_prompt}
|
| 150 |
+
|
| 151 |
+
For policy overview questions:
|
| 152 |
+
- Provide a comprehensive overview of different policies/schemes available
|
| 153 |
+
- Organize information into clear categories (e.g., Old Pension Scheme vs New Pension Scheme)
|
| 154 |
+
- Include eligibility criteria, key features, and benefits for each
|
| 155 |
+
- Structure the response with clear headings and bullet points
|
| 156 |
+
- Focus on giving the user a complete understanding of their options
|
| 157 |
+
- Don't just focus on calculations - explain the policy framework"""
|
| 158 |
+
|
| 159 |
+
# Impact analysis and scenario questions
|
| 160 |
+
elif any(keyword in message_lower for keyword in ['impact', 'effect', 'scenario', 'chart', 'analyze', 'calculate']):
|
| 161 |
+
return f"""{base_prompt}
|
| 162 |
+
|
| 163 |
+
For impact analysis and scenario questions:
|
| 164 |
+
- Provide detailed calculations and projections
|
| 165 |
+
- Include charts, graphs, and visualizations when relevant
|
| 166 |
+
- Show step-by-step analysis
|
| 167 |
+
- Compare different scenarios or options
|
| 168 |
+
- Provide numerical examples with specific amounts
|
| 169 |
+
- Include year-wise breakdowns when applicable"""
|
| 170 |
+
|
| 171 |
+
# Procedural questions
|
| 172 |
+
elif any(keyword in message_lower for keyword in ['how to', 'process', 'procedure', 'steps', 'application']):
|
| 173 |
+
return f"""{base_prompt}
|
| 174 |
+
|
| 175 |
+
For procedural questions:
|
| 176 |
+
- Provide clear step-by-step instructions
|
| 177 |
+
- Include required documents and forms
|
| 178 |
+
- Mention timelines and deadlines
|
| 179 |
+
- Provide contact information for relevant offices
|
| 180 |
+
- Include tips and common pitfalls to avoid"""
|
| 181 |
+
|
| 182 |
+
# General questions
|
| 183 |
+
else:
|
| 184 |
+
return f"""{base_prompt}
|
| 185 |
+
|
| 186 |
+
Provide clear, accurate, and helpful responses based on the available government documents. Structure your response appropriately for the type of question asked."""
|
| 187 |
+
|
| 188 |
def determine_task_complexity(self, message: str, context: str = "") -> TaskComplexity:
|
| 189 |
"""Determine task complexity - alias for analyze_task_complexity for compatibility"""
|
| 190 |
return self.analyze_task_complexity(message)
|
|
|
|
| 272 |
|
| 273 |
async def _get_groq_response(self, message: str, context: str = "") -> str:
|
| 274 |
"""Get response from Groq LLM"""
|
| 275 |
+
# Create context-aware system prompt based on query type
|
| 276 |
+
system_prompt = self._create_adaptive_system_prompt(message)
|
|
|
|
| 277 |
|
| 278 |
if context:
|
| 279 |
system_prompt += f"\n\nRelevant context from documents:\n{context}"
|
|
|
|
| 288 |
|
| 289 |
async def _get_gemini_response(self, message: str, context: str = "") -> str:
|
| 290 |
"""Get response from Gemini LLM"""
|
| 291 |
+
# Create context-aware system prompt based on query type
|
| 292 |
+
system_prompt = self._create_adaptive_system_prompt(message)
|
|
|
|
| 293 |
|
| 294 |
if context:
|
| 295 |
system_prompt += f"\n\nRelevant context from documents:\n{context}"
|