Raiff1982 commited on
Commit
cca6fc0
·
verified ·
1 Parent(s): 66185a0

Create response_templates.py

Browse files
Files changed (1) hide show
  1. src/components/response_templates.py +136 -0
src/components/response_templates.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Shared response templates and utilities to prevent looping and ensure variety
3
+ across all response generation components
4
+ """
5
+ import hashlib
6
+ from typing import List, Dict, Optional
7
+
8
+ class ResponseTemplates:
9
+ """Central repository for response templates with deduplication"""
10
+
11
+ def __init__(self):
12
+ self.recent_responses = []
13
+ self.recent_response_hashes = set()
14
+ self.max_recent = 20
15
+
16
+ # Error/Fallback responses with variations
17
+ self.error_responses = [
18
+ "I apologize, but I encountered an error. Could you please rephrase your question?",
19
+ "Something went wrong processing that. Let me try again if you ask differently.",
20
+ "I ran into a technical issue. Could you try rephrasing your question?",
21
+ "That caused an error on my end. Could you ask that in another way?"
22
+ ]
23
+
24
+ self.empty_response_fallbacks = [
25
+ "I need to collect my thoughts. Could you provide more context?",
26
+ "Let me think about that differently. Could you rephrase?",
27
+ "I'm not quite sure how to respond to that. Could you add more details?",
28
+ "That's an interesting question. Could you elaborate a bit more?"
29
+ ]
30
+
31
+ self.understanding_responses = [
32
+ "I understand. Let me help with that.",
33
+ "Got it. Here's what I can tell you:",
34
+ "I see what you mean. Let me address that:",
35
+ "I hear you. Here's my perspective:",
36
+ "Understood. Let me explain:"
37
+ ]
38
+
39
+ self.uncertain_responses = [
40
+ "I'm not entirely certain about this, but",
41
+ "Based on what I understand,",
42
+ "From my perspective,",
43
+ "Here's my best assessment:",
44
+ "To the best of my knowledge,"
45
+ ]
46
+
47
+ self.reflection_responses = [
48
+ "That's worth considering. ",
49
+ "Good question. ",
50
+ "Interesting point. ",
51
+ "Let me think about that. ",
52
+ "That's a thoughtful inquiry. "
53
+ ]
54
+
55
+ def get_next_variation(self, template_list: List[str]) -> str:
56
+ """Get next unused variation from template list"""
57
+ if not template_list:
58
+ return ""
59
+
60
+ for template in template_list:
61
+ template_hash = hashlib.md5(template[:80].encode()).hexdigest()
62
+ if template_hash not in self.recent_response_hashes:
63
+ return template
64
+
65
+ # If all used, return first one (start cycling)
66
+ return template_list[0]
67
+
68
+ def track_response(self, response: str) -> None:
69
+ """Track response to prevent immediate repetition"""
70
+ response_hash = hashlib.md5(response[:100].encode()).hexdigest()
71
+ self.recent_response_hashes.add(response_hash)
72
+ self.recent_responses.append(response[:100])
73
+
74
+ # Keep only recent ones
75
+ if len(self.recent_responses) > self.max_recent:
76
+ old_response = self.recent_responses.pop(0)
77
+ # Remove old hash after half the window passes
78
+ if len(self.recent_responses) > self.max_recent // 2:
79
+ old_hash = hashlib.md5(old_response.encode()).hexdigest()
80
+ self.recent_response_hashes.discard(old_hash)
81
+
82
+ def get_error_response(self, context: Optional[str] = None) -> str:
83
+ """Get varied error response"""
84
+ response = self.get_next_variation(self.error_responses)
85
+ self.track_response(response)
86
+ return response
87
+
88
+ def get_empty_response_fallback(self) -> str:
89
+ """Get varied fallback for empty responses"""
90
+ response = self.get_next_variation(self.empty_response_fallbacks)
91
+ self.track_response(response)
92
+ return response
93
+
94
+ def get_understanding_prefix(self) -> str:
95
+ """Get varied understanding prefix"""
96
+ return self.get_next_variation(self.understanding_responses)
97
+
98
+ def get_uncertain_prefix(self) -> str:
99
+ """Get varied uncertain prefix"""
100
+ return self.get_next_variation(self.uncertain_responses)
101
+
102
+ def get_reflection_prefix(self) -> str:
103
+ """Get varied reflection prefix"""
104
+ return self.get_next_variation(self.reflection_responses)
105
+
106
+ def wrap_response_with_prefix(
107
+ self,
108
+ response: str,
109
+ prefix_type: str = "understanding"
110
+ ) -> str:
111
+ """Wrap response with varied prefix"""
112
+ if not response:
113
+ return self.get_empty_response_fallback()
114
+
115
+ if prefix_type == "understanding":
116
+ prefix = self.get_understanding_prefix()
117
+ elif prefix_type == "uncertain":
118
+ prefix = self.get_uncertain_prefix()
119
+ elif prefix_type == "reflection":
120
+ prefix = self.get_reflection_prefix()
121
+ else:
122
+ prefix = self.get_understanding_prefix()
123
+
124
+ wrapped = f"{prefix} {response}".strip()
125
+ self.track_response(wrapped)
126
+ return wrapped
127
+
128
+ # Global instance for shared use
129
+ _response_templates_instance = None
130
+
131
+ def get_response_templates() -> ResponseTemplates:
132
+ """Get singleton instance of ResponseTemplates"""
133
+ global _response_templates_instance
134
+ if _response_templates_instance is None:
135
+ _response_templates_instance = ResponseTemplates()
136
+ return _response_templates_instance