Raiff1982 commited on
Commit
57219b4
·
verified ·
1 Parent(s): d62538e

Update src/components/feedback_manager.py

Browse files
Files changed (1) hide show
  1. src/components/feedback_manager.py +106 -106
src/components/feedback_manager.py CHANGED
@@ -1,107 +1,107 @@
1
- """
2
- This module manages feedback collection and application for improving AI responses.
3
- """
4
-
5
- from typing import Dict, Any
6
- from utils.database import Database
7
-
8
- class ImprovedFeedbackManager:
9
- def __init__(self, database: Database):
10
- self.database = database
11
- self.feedback_history = []
12
- self.adjustment_strategies = {
13
- "positive": self._apply_positive_feedback,
14
- "negative": self._apply_negative_feedback,
15
- "neutral": self._apply_neutral_feedback
16
- }
17
-
18
- def collect_feedback(self, user_id: int, response_id: int, feedback_type: str, feedback_text: str = "") -> Dict[str, Any]:
19
- """
20
- Collect user feedback for a specific response.
21
-
22
- Args:
23
- user_id: The ID of the user providing feedback
24
- response_id: The ID of the response being rated
25
- feedback_type: The type of feedback (positive/negative/neutral)
26
- feedback_text: Optional detailed feedback text
27
-
28
- Returns:
29
- A dictionary containing the processed feedback data
30
- """
31
- feedback_data = {
32
- "user_id": user_id,
33
- "response_id": response_id,
34
- "feedback_type": feedback_type,
35
- "feedback_text": feedback_text,
36
- "processed": False
37
- }
38
-
39
- self.feedback_history.append(feedback_data)
40
- return feedback_data
41
-
42
- def adjust_response_based_on_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
43
- """
44
- Adjust a response based on previous feedback.
45
-
46
- Args:
47
- response: The current response to adjust
48
- feedback: The feedback data to use for adjustment
49
-
50
- Returns:
51
- The adjusted response
52
- """
53
- feedback_type = feedback.get("feedback_type", "neutral")
54
-
55
- if feedback_type in self.adjustment_strategies:
56
- return self.adjustment_strategies[feedback_type](response, feedback)
57
-
58
- return response
59
-
60
- def _apply_positive_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
61
- """
62
- Apply adjustments for positive feedback.
63
- """
64
- # For positive feedback, maintain similar response patterns
65
- return response
66
-
67
- def _apply_negative_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
68
- """
69
- Apply adjustments for negative feedback.
70
- """
71
- # For negative feedback, try to modify the response
72
- # This is a simple example - in practice, you'd want more sophisticated adjustments
73
- return f"I understand the previous response wasn't optimal. Let me try again: {response}"
74
-
75
- def _apply_neutral_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
76
- """
77
- Apply adjustments for neutral feedback.
78
- """
79
- # For neutral feedback, make minor refinements
80
- return response.strip()
81
-
82
- def get_feedback_stats(self) -> Dict[str, Any]:
83
- """
84
- Get statistics about collected feedback.
85
-
86
- Returns:
87
- A dictionary containing feedback statistics
88
- """
89
- total = len(self.feedback_history)
90
- positive = sum(1 for f in self.feedback_history if f["feedback_type"] == "positive")
91
- negative = sum(1 for f in self.feedback_history if f["feedback_type"] == "negative")
92
- neutral = total - positive - negative
93
-
94
- return {
95
- "total": total,
96
- "positive": positive,
97
- "negative": negative,
98
- "neutral": neutral,
99
- "positive_ratio": positive/total if total > 0 else 0,
100
- "negative_ratio": negative/total if total > 0 else 0
101
- }
102
-
103
- def clear_feedback_history(self):
104
- """
105
- Clear the feedback history.
106
- """
107
  self.feedback_history = []
 
1
+ """
2
+ This module manages feedback collection and application for improving AI responses.
3
+ """
4
+
5
+ from typing import Dict, Any
6
+ from ..utils.database import Database
7
+
8
+ class ImprovedFeedbackManager:
9
+ def __init__(self, database: Database):
10
+ self.database = database
11
+ self.feedback_history = []
12
+ self.adjustment_strategies = {
13
+ "positive": self._apply_positive_feedback,
14
+ "negative": self._apply_negative_feedback,
15
+ "neutral": self._apply_neutral_feedback
16
+ }
17
+
18
+ def collect_feedback(self, user_id: int, response_id: int, feedback_type: str, feedback_text: str = "") -> Dict[str, Any]:
19
+ """
20
+ Collect user feedback for a specific response.
21
+
22
+ Args:
23
+ user_id: The ID of the user providing feedback
24
+ response_id: The ID of the response being rated
25
+ feedback_type: The type of feedback (positive/negative/neutral)
26
+ feedback_text: Optional detailed feedback text
27
+
28
+ Returns:
29
+ A dictionary containing the processed feedback data
30
+ """
31
+ feedback_data = {
32
+ "user_id": user_id,
33
+ "response_id": response_id,
34
+ "feedback_type": feedback_type,
35
+ "feedback_text": feedback_text,
36
+ "processed": False
37
+ }
38
+
39
+ self.feedback_history.append(feedback_data)
40
+ return feedback_data
41
+
42
+ def adjust_response_based_on_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
43
+ """
44
+ Adjust a response based on previous feedback.
45
+
46
+ Args:
47
+ response: The current response to adjust
48
+ feedback: The feedback data to use for adjustment
49
+
50
+ Returns:
51
+ The adjusted response
52
+ """
53
+ feedback_type = feedback.get("feedback_type", "neutral")
54
+
55
+ if feedback_type in self.adjustment_strategies:
56
+ return self.adjustment_strategies[feedback_type](response, feedback)
57
+
58
+ return response
59
+
60
+ def _apply_positive_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
61
+ """
62
+ Apply adjustments for positive feedback.
63
+ """
64
+ # For positive feedback, maintain similar response patterns
65
+ return response
66
+
67
+ def _apply_negative_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
68
+ """
69
+ Apply adjustments for negative feedback.
70
+ """
71
+ # For negative feedback, try to modify the response
72
+ # This is a simple example - in practice, you'd want more sophisticated adjustments
73
+ return f"I understand the previous response wasn't optimal. Let me try again: {response}"
74
+
75
+ def _apply_neutral_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
76
+ """
77
+ Apply adjustments for neutral feedback.
78
+ """
79
+ # For neutral feedback, make minor refinements
80
+ return response.strip()
81
+
82
+ def get_feedback_stats(self) -> Dict[str, Any]:
83
+ """
84
+ Get statistics about collected feedback.
85
+
86
+ Returns:
87
+ A dictionary containing feedback statistics
88
+ """
89
+ total = len(self.feedback_history)
90
+ positive = sum(1 for f in self.feedback_history if f["feedback_type"] == "positive")
91
+ negative = sum(1 for f in self.feedback_history if f["feedback_type"] == "negative")
92
+ neutral = total - positive - negative
93
+
94
+ return {
95
+ "total": total,
96
+ "positive": positive,
97
+ "negative": negative,
98
+ "neutral": neutral,
99
+ "positive_ratio": positive/total if total > 0 else 0,
100
+ "negative_ratio": negative/total if total > 0 else 0
101
+ }
102
+
103
+ def clear_feedback_history(self):
104
+ """
105
+ Clear the feedback history.
106
+ """
107
  self.feedback_history = []