File size: 3,848 Bytes
57219b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d6b8af
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
"""
This module manages feedback collection and application for improving AI responses.
"""

from typing import Dict, Any
from ..utils.database import Database

class ImprovedFeedbackManager:
    def __init__(self, database: Database):
        self.database = database
        self.feedback_history = []
        self.adjustment_strategies = {
            "positive": self._apply_positive_feedback,
            "negative": self._apply_negative_feedback,
            "neutral": self._apply_neutral_feedback
        }

    def collect_feedback(self, user_id: int, response_id: int, feedback_type: str, feedback_text: str = "") -> Dict[str, Any]:
        """
        Collect user feedback for a specific response.
        
        Args:
            user_id: The ID of the user providing feedback
            response_id: The ID of the response being rated
            feedback_type: The type of feedback (positive/negative/neutral)
            feedback_text: Optional detailed feedback text
        
        Returns:
            A dictionary containing the processed feedback data
        """
        feedback_data = {
            "user_id": user_id,
            "response_id": response_id,
            "feedback_type": feedback_type,
            "feedback_text": feedback_text,
            "processed": False
        }
        
        self.feedback_history.append(feedback_data)
        return feedback_data

    def adjust_response_based_on_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
        """
        Adjust a response based on previous feedback.
        
        Args:
            response: The current response to adjust
            feedback: The feedback data to use for adjustment
        
        Returns:
            The adjusted response
        """
        feedback_type = feedback.get("feedback_type", "neutral")
        
        if feedback_type in self.adjustment_strategies:
            return self.adjustment_strategies[feedback_type](response, feedback)
        
        return response

    def _apply_positive_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
        """
        Apply adjustments for positive feedback.
        """
        # For positive feedback, maintain similar response patterns
        return response

    def _apply_negative_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
        """
        Apply adjustments for negative feedback.
        """
        # For negative feedback, try to modify the response
        # This is a simple example - in practice, you'd want more sophisticated adjustments
        return f"I understand the previous response wasn't optimal. Let me try again: {response}"

    def _apply_neutral_feedback(self, response: str, feedback: Dict[str, Any]) -> str:
        """
        Apply adjustments for neutral feedback.
        """
        # For neutral feedback, make minor refinements
        return response.strip()

    def get_feedback_stats(self) -> Dict[str, Any]:
        """
        Get statistics about collected feedback.
        
        Returns:
            A dictionary containing feedback statistics
        """
        total = len(self.feedback_history)
        positive = sum(1 for f in self.feedback_history if f["feedback_type"] == "positive")
        negative = sum(1 for f in self.feedback_history if f["feedback_type"] == "negative")
        neutral = total - positive - negative
        
        return {
            "total": total,
            "positive": positive,
            "negative": negative,
            "neutral": neutral,
            "positive_ratio": positive/total if total > 0 else 0,
            "negative_ratio": negative/total if total > 0 else 0
        }

    def clear_feedback_history(self):
        """
        Clear the feedback history.
        """
        self.feedback_history = []