File size: 5,373 Bytes
cca6fc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared response templates and utilities to prevent looping and ensure variety
across all response generation components
"""
import hashlib
from typing import List, Dict, Optional

class ResponseTemplates:
    """Central repository for response templates with deduplication"""
    
    def __init__(self):
        self.recent_responses = []
        self.recent_response_hashes = set()
        self.max_recent = 20
        
        # Error/Fallback responses with variations
        self.error_responses = [
            "I apologize, but I encountered an error. Could you please rephrase your question?",
            "Something went wrong processing that. Let me try again if you ask differently.",
            "I ran into a technical issue. Could you try rephrasing your question?",
            "That caused an error on my end. Could you ask that in another way?"
        ]
        
        self.empty_response_fallbacks = [
            "I need to collect my thoughts. Could you provide more context?",
            "Let me think about that differently. Could you rephrase?",
            "I'm not quite sure how to respond to that. Could you add more details?",
            "That's an interesting question. Could you elaborate a bit more?"
        ]
        
        self.understanding_responses = [
            "I understand. Let me help with that.",
            "Got it. Here's what I can tell you:",
            "I see what you mean. Let me address that:",
            "I hear you. Here's my perspective:",
            "Understood. Let me explain:"
        ]
        
        self.uncertain_responses = [
            "I'm not entirely certain about this, but",
            "Based on what I understand,",
            "From my perspective,",
            "Here's my best assessment:",
            "To the best of my knowledge,"
        ]
        
        self.reflection_responses = [
            "That's worth considering. ",
            "Good question. ",
            "Interesting point. ",
            "Let me think about that. ",
            "That's a thoughtful inquiry. "
        ]
        
    def get_next_variation(self, template_list: List[str]) -> str:
        """Get next unused variation from template list"""
        if not template_list:
            return ""
        
        for template in template_list:
            template_hash = hashlib.md5(template[:80].encode()).hexdigest()
            if template_hash not in self.recent_response_hashes:
                return template
        
        # If all used, return first one (start cycling)
        return template_list[0]
    
    def track_response(self, response: str) -> None:
        """Track response to prevent immediate repetition"""
        response_hash = hashlib.md5(response[:100].encode()).hexdigest()
        self.recent_response_hashes.add(response_hash)
        self.recent_responses.append(response[:100])
        
        # Keep only recent ones
        if len(self.recent_responses) > self.max_recent:
            old_response = self.recent_responses.pop(0)
            # Remove old hash after half the window passes
            if len(self.recent_responses) > self.max_recent // 2:
                old_hash = hashlib.md5(old_response.encode()).hexdigest()
                self.recent_response_hashes.discard(old_hash)
    
    def get_error_response(self, context: Optional[str] = None) -> str:
        """Get varied error response"""
        response = self.get_next_variation(self.error_responses)
        self.track_response(response)
        return response
    
    def get_empty_response_fallback(self) -> str:
        """Get varied fallback for empty responses"""
        response = self.get_next_variation(self.empty_response_fallbacks)
        self.track_response(response)
        return response
    
    def get_understanding_prefix(self) -> str:
        """Get varied understanding prefix"""
        return self.get_next_variation(self.understanding_responses)
    
    def get_uncertain_prefix(self) -> str:
        """Get varied uncertain prefix"""
        return self.get_next_variation(self.uncertain_responses)
    
    def get_reflection_prefix(self) -> str:
        """Get varied reflection prefix"""
        return self.get_next_variation(self.reflection_responses)
    
    def wrap_response_with_prefix(
        self, 
        response: str, 
        prefix_type: str = "understanding"
    ) -> str:
        """Wrap response with varied prefix"""
        if not response:
            return self.get_empty_response_fallback()
        
        if prefix_type == "understanding":
            prefix = self.get_understanding_prefix()
        elif prefix_type == "uncertain":
            prefix = self.get_uncertain_prefix()
        elif prefix_type == "reflection":
            prefix = self.get_reflection_prefix()
        else:
            prefix = self.get_understanding_prefix()
        
        wrapped = f"{prefix} {response}".strip()
        self.track_response(wrapped)
        return wrapped

# Global instance for shared use
_response_templates_instance = None

def get_response_templates() -> ResponseTemplates:
    """Get singleton instance of ResponseTemplates"""
    global _response_templates_instance
    if _response_templates_instance is None:
        _response_templates_instance = ResponseTemplates()
    return _response_templates_instance