Spaces:
Sleeping
Sleeping
| """ | |
| 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 = [] |