From 7e103a748819b2b2fb0856da79aad1a4310b74a0 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Thu, 31 Jul 2025 09:39:59 +0200 Subject: [PATCH] feat: adds userfeedback sentiment calculation to feedback search type --- cognee/modules/retrieval/user_qa_feedback.py | 12 ++++++++++-- cognee/modules/retrieval/utils/models.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cognee/modules/retrieval/user_qa_feedback.py b/cognee/modules/retrieval/user_qa_feedback.py index 5897d80ab..e5fcdf61f 100644 --- a/cognee/modules/retrieval/user_qa_feedback.py +++ b/cognee/modules/retrieval/user_qa_feedback.py @@ -2,10 +2,12 @@ from typing import Any, Optional from uuid import NAMESPACE_OID, uuid5, UUID from cognee.infrastructure.databases.graph import get_graph_engine +from cognee.infrastructure.llm.get_llm_client import get_llm_client from cognee.modules.engine.models import NodeSet from cognee.shared.logging_utils import get_logger from cognee.modules.retrieval.base_feedback import BaseFeedback from cognee.modules.retrieval.utils.models import CogneeUserFeedback +from cognee.modules.retrieval.utils.models import UserFeedbackEvaluation from cognee.tasks.storage import add_data_points logger = get_logger("CompletionRetriever") @@ -25,10 +27,15 @@ class UserQAFeedback(BaseFeedback): self.last_k = last_k async def add_feedback(self, feedback_text: str) -> Any: - graph_engine = await get_graph_engine() + llm_client = get_llm_client() + feedback_sentiment = await llm_client.acreate_structured_output( + feedback_text, + "You are a sentiment analysis assistant. For each piece of user feedback you receive, return exactly one of: Positive, Negative, or Neutral classification", + UserFeedbackEvaluation, + ) + graph_engine = await get_graph_engine() last_interaction_ids = await graph_engine.get_last_user_interaction_ids(limit=self.last_k) - print() nodeset_name = "UserQAFeedbacks" feedbacks_node_set = NodeSet(id=uuid5(NAMESPACE_OID, name=nodeset_name), name=nodeset_name) @@ -37,6 +44,7 @@ class UserQAFeedback(BaseFeedback): cognee_user_feedback = CogneeUserFeedback( id=feedback_id, feedback=feedback_text, + sentiment=feedback_sentiment.evaluation.value, belongs_to_set=feedbacks_node_set, ) diff --git a/cognee/modules/retrieval/utils/models.py b/cognee/modules/retrieval/utils/models.py index 42c49b799..9053939ea 100644 --- a/cognee/modules/retrieval/utils/models.py +++ b/cognee/modules/retrieval/utils/models.py @@ -1,6 +1,8 @@ from typing import Optional from cognee.infrastructure.engine.models.DataPoint import DataPoint from cognee.modules.engine.models.node_set import NodeSet +from enum import Enum +from pydantic import BaseModel, ValidationError class CogneeUserInteraction(DataPoint): @@ -16,4 +18,15 @@ class CogneeUserFeedback(DataPoint): """User - Cognee Feedback""" feedback: str + sentiment: str belongs_to_set: Optional[NodeSet] = None + + +class UserFeedbackSentiment(str, Enum): + positive = "positive" + negative = "negative" + neutral = "neutral" + + +class UserFeedbackEvaluation(BaseModel): + evaluation: UserFeedbackSentiment