File size: 14,022 Bytes
5065491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55067b7
 
 
 
 
 
 
 
 
5065491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfccdc1
5065491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfccdc1
5065491
 
dfccdc1
 
5065491
 
 
 
 
dfccdc1
 
 
 
 
 
 
 
 
 
 
 
 
5065491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Conversational Voice Bot Service
Makes the bot more interactive by asking for clarification when context is unclear
"""

import logging
from typing import Dict, Any, List, Optional
import re

logger = logging.getLogger("voicebot")

class ConversationalService:
    def __init__(self):
        self.conversation_history = {}
        
    def analyze_query_clarity(self, query: str, search_results: List[Dict[str, Any]]) -> Dict[str, Any]:
        """
        Analyze if the query is clear enough or needs clarification
        Returns clarification questions if needed
        """
        query_lower = query.lower().strip()
        
        # Remove filler words
        filler_words = ['um', 'uh', 'like', 'you know', 'actually', 'basically']
        clean_query = query_lower
        for filler in filler_words:
            clean_query = clean_query.replace(filler, '')
        
        clarity_analysis = {
            'is_clear': True,
            'confidence': 1.0,
            'clarification_needed': False,
            'clarification_questions': [],
            'suggested_responses': [],
            'query_type': 'specific'
        }
        
        # Check for vague queries
        vague_patterns = [
            r'\b(what|how|tell me|explain)\s+(about|regarding)?\s*$',
            r'^(help|info|information)$',
            r'^(what|how)$',
            r'\b(anything|something|stuff)\b',
            r'^(yes|no|okay|ok)$',
            r'\b(this|that|it)\b.*\?$'
        ]
        
        is_vague = any(re.search(pattern, clean_query) for pattern in vague_patterns)
        
        # Check for ambiguous pension queries
        if 'pension' in query_lower:
            pension_ambiguity = self._check_pension_ambiguity(query_lower)
            if pension_ambiguity['needs_clarification']:
                clarity_analysis.update(pension_ambiguity)
                return clarity_analysis
            else:
                # Pension query is clear - don't ask for clarification
                clarity_analysis.update({
                    'is_clear': True,
                    'confidence': 0.9,
                    'clarification_needed': False,
                    'query_type': 'pension_clear'
                })
                return clarity_analysis
        
        # Check for ambiguous procurement queries
        elif any(word in query_lower for word in ['tender', 'procurement', 'bid']):
            procurement_ambiguity = self._check_procurement_ambiguity(query_lower)
            if procurement_ambiguity['needs_clarification']:
                clarity_analysis.update(procurement_ambiguity)
                return clarity_analysis
        
        # Check for too generic queries
        elif is_vague or len(clean_query.split()) <= 2:
            clarity_analysis.update({
                'is_clear': False,
                'confidence': 0.3,
                'clarification_needed': True,
                'query_type': 'vague',
                'clarification_questions': [
                    "I'd be happy to help! Could you be more specific about what you're looking for?",
                    "Are you asking about:",
                    "• Pension rules and benefits?",
                    "• Leave policies and applications?", 
                    "• Procurement and tender processes?",
                    "• Salary and allowances?",
                    "• Or something else?"
                ],
                'suggested_responses': [
                    "Please let me know which topic interests you most, and I'll provide detailed information."
                ]
            })
            return clarity_analysis
        
        # Check if search results are relevant
        if search_results:
            relevance_score = self._calculate_relevance_score(query_lower, search_results)
            if relevance_score < 0.1:  # Low relevance - made much more permissive
                clarity_analysis.update({
                    'is_clear': False,
                    'confidence': relevance_score,
                    'clarification_needed': True,
                    'query_type': 'low_relevance',
                    'clarification_questions': [
                        f"I found some information, but I want to make sure I understand your question correctly.",
                        f"When you asked '{query}', did you mean:",
                        "• Something specific about government policies?",
                        "• A particular process or procedure?",
                        "• Information for a specific situation?",
                        "",
                        "Could you provide a bit more context about what you're trying to accomplish?"
                    ]
                })
        
        return clarity_analysis
    
    def _check_pension_ambiguity(self, query: str) -> Dict[str, Any]:
        """Check if pension query needs clarification"""
        
        # Specific pension queries that are clear
        clear_pension_terms = [
            'pension', 'retirement', 'gratuity', 'superannuation',
            'pension calculation', 'pension formula', 'pension eligibility', 
            'pension application', 'family pension', 'commutation', 
            'pension documents', 'pension process', 'pension rules',
            'pension increment', 'pension impact', 'old age', 'benefits'
        ]
        
        if any(term in query for term in clear_pension_terms):
            return {'needs_clarification': False}
        
        # Disable overly aggressive clarification for pension queries
        # Users asking about "pension rules" should get direct answers
        # generic_pension_patterns = [
        #     r'^pension\??$',
        #     r'^what.*pension\??$',
        #     r'^tell me.*pension\??$',
        #     r'^pension.*rules?\??$',
        #     r'^how.*pension\??$'
        # ]
        
        # Always provide direct answers for pension-related queries
        # if any(re.search(pattern, query) for pattern in generic_pension_patterns):
        #     return clarification logic - DISABLED to provide direct answers
        
        return {'needs_clarification': False}
    
    def _check_procurement_ambiguity(self, query: str) -> Dict[str, Any]:
        """Check if procurement query needs clarification"""
        
        clear_procurement_terms = [
            'tender process', 'bid submission', 'gem portal', 'msme benefits',
            'vendor registration', 'procurement threshold', 'tender documents'
        ]
        
        if any(term in query for term in clear_procurement_terms):
            return {'needs_clarification': False}
        
        generic_procurement_patterns = [
            r'^tender\??$',
            r'^procurement\??$', 
            r'^bid\??$',
            r'^what.*tender\??$',
            r'^how.*procurement\??$'
        ]
        
        if any(re.search(pattern, query) for pattern in generic_procurement_patterns):
            return {
                'needs_clarification': True,
                'is_clear': False,
                'confidence': 0.4,
                'clarification_needed': True,
                'query_type': 'procurement_generic',
                'clarification_questions': [
                    "I can help with procurement and tendering! To provide the right information, could you clarify:",
                    "",
                    "🏢 **What specifically about procurement?**",
                    "• How to participate in tenders?",
                    "• Procurement rules and thresholds?",
                    "• GeM portal registration?",
                    "• MSME benefits in procurement?",
                    "• Vendor empanelment process?",
                    "• Bid preparation and submission?",
                    "",
                    "Are you a vendor looking to participate, or an officer managing procurement?"
                ],
                'suggested_responses': [
                    "Please specify your role and what aspect of procurement you need help with."
                ]
            }
        
        return {'needs_clarification': False}
    
    def _calculate_relevance_score(self, query: str, search_results: List[Dict[str, Any]]) -> float:
        """Calculate how relevant search results are to the query"""
        if not search_results:
            return 0.0
        
        query_words = set(query.lower().split())
        query_words = {word for word in query_words if len(word) > 2}  # Remove short words
        
        total_relevance = 0.0
        for result in search_results:
            content = result.get('content', '').lower()
            filename = result.get('filename', '').lower()
            
            # Count query word matches in content
            content_matches = sum(1 for word in query_words if word in content)
            filename_matches = sum(1 for word in query_words if word in filename)
            
            # Calculate relevance score for this result
            max_possible_matches = len(query_words)
            if max_possible_matches > 0:
                relevance = (content_matches + filename_matches * 2) / (max_possible_matches * 2)
                total_relevance += relevance
        
        # Average relevance across all results
        return total_relevance / len(search_results) if search_results else 0.0
    
    def generate_conversational_response(self, 
                                       query: str, 
                                       search_results: List[Dict[str, Any]],
                                       session_id: str = None) -> Dict[str, Any]:
        """
        Generate a conversational response that asks for clarification when needed
        """
        clarity = self.analyze_query_clarity(query, search_results)
        
        if clarity['clarification_needed']:
            # Generate clarification response
            clarification_text = "\n".join(clarity['clarification_questions'])
            
            response = {
                'needs_clarification': True,
                'response': clarification_text,
                'type': 'clarification_request',
                'suggested_follow_ups': clarity.get('suggested_responses', []),
                'query_type': clarity.get('query_type', 'unclear'),
                'confidence': clarity.get('confidence', 0.5)
            }
            
            # Store conversation context for follow-up
            if session_id:
                self.conversation_history[session_id] = {
                    'last_query': query,
                    'awaiting_clarification': True,
                    'clarification_type': clarity.get('query_type', 'unclear'),
                    'timestamp': __import__('time').time()
                }
            
            return response
        
        else:
            # Query is clear, proceed with normal response
            return {
                'needs_clarification': False,
                'response': None,  # Will be filled by normal RAG processing
                'type': 'information_request',
                'confidence': clarity.get('confidence', 1.0)
            }
    
    def handle_follow_up(self, query: str, session_id: str) -> Dict[str, Any]:
        """Handle follow-up queries after clarification request"""
        if session_id not in self.conversation_history:
            return {'is_follow_up': False}
        
        context = self.conversation_history[session_id]
        
        if not context.get('awaiting_clarification', False):
            return {'is_follow_up': False}
        
        # Check if this is a clarification response
        query_lower = query.lower().strip()
        
        # Clear clarification state
        context['awaiting_clarification'] = False
        
        # Enhanced query based on clarification
        clarification_type = context.get('clarification_type', '')
        original_query = context.get('last_query', '')
        
        enhanced_query = f"{original_query} {query}"
        
        return {
            'is_follow_up': True,
            'enhanced_query': enhanced_query,
            'context_type': clarification_type,
            'original_query': original_query
        }
    
    def generate_friendly_greeting(self) -> str:
        """Generate a friendly greeting that encourages conversation"""
        greetings = [
            "Hello! I'm here to help you with government policies and procedures. What would you like to know about?",
            "Hi there! I can assist you with information about pensions, procurement, leave policies, and more. What's on your mind?",
            "Welcome! I'm your government assistant. Feel free to ask me about any policies, rules, or procedures you need help with.",
            "Hello! I'm ready to help you navigate government policies and processes. What information are you looking for today?"
        ]
        
        import random
        return random.choice(greetings)
    
    def generate_helpful_response(self, response_text: str, sources: List[Dict[str, Any]]) -> str:
        """Make responses more helpful and conversational"""
        
        # Add conversational elements
        if response_text:
            # Add follow-up encouragement
            follow_up_phrases = [
                "\n\nIs there anything specific about this topic you'd like me to explain further?",
                "\n\nDo you have any follow-up questions about this information?",
                "\n\nWould you like me to provide more details on any particular aspect?",
                "\n\nIs there a specific situation you're dealing with that I can help you navigate?"
            ]
            
            import random
            follow_up = random.choice(follow_up_phrases)
            response_text += follow_up
        
        return response_text

# Global instance
conversational_service = ConversationalService()