-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: feedback enrichment #1571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
44ec814
feat: feedback enrichment preparation
lxobr 78fca9f
feat: extract feedback interactions
lxobr 97eb893
feat: generate improved answers temp
lxobr 1e1fac3
feat: allow structured output in the cot retriever
lxobr ce41882
feat: generate improved answers
lxobr 834cf8b
feat: create_enrichments.py
lxobr 8e580bd
fix: create enrichments
lxobr 590c3ad
feat: use datapoints only
lxobr cccf523
Merge branch 'dev' into feature/cog-3187-feedback-enrichment
lxobr 70c0a98
chore: use cot retriever only
lxobr 46b19ad
Merge branch 'dev' into feature/cog-3187-feedback-enrichment
hajdul88 f4d038b
chore: pre-align cot retriever with dev
lxobr 46e6d87
Merge branch 'dev' into feature/cog-3187-feedback-enrichment-merge-test
lxobr 66a8242
chore: restore the feedback enrichment cot retriever functionality
lxobr ecae650
refactor: unify structured and str completion
lxobr aba5f9b
test: add e2e feedback enrichment test
lxobr 2d61885
chore: minor improvements
lxobr b09e4b7
chore: adhere to memify input convention
lxobr f49b171
fix: emphasize negative feedback language
lxobr 23e66a6
chore: expand logging
lxobr 7a08e13
chore: further expand logging
lxobr 6dea23b
fix: update kuzu get_filtered_graph_data
lxobr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: create_enrichments.py
- Loading branch information
commit 834cf8b11307f38a09b42660db493bdf2ddaa14c
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict, List, Optional | ||
| from uuid import NAMESPACE_OID, uuid5 | ||
|
|
||
| from cognee.infrastructure.llm import LLMGateway | ||
| from cognee.infrastructure.llm.prompts.read_query_prompt import read_query_prompt | ||
| from cognee.shared.logging_utils import get_logger | ||
| from cognee.modules.engine.models import NodeSet | ||
|
|
||
| from .models import FeedbackEnrichment | ||
|
|
||
|
|
||
| logger = get_logger("create_enrichments") | ||
|
|
||
|
|
||
| def _validate_improved_answers(improved_answers: List[Dict]) -> bool: | ||
| """Validate that all items contain required fields for enrichment creation.""" | ||
| required_fields = [ | ||
| "question", | ||
| "answer", # This is the original answer field from feedback_interaction | ||
| "improved_answer", | ||
| "new_context", | ||
| "feedback_id", | ||
| "interaction_id", | ||
| ] | ||
| return all( | ||
| all(item.get(field) is not None for field in required_fields) for item in improved_answers | ||
| ) | ||
|
|
||
|
|
||
| def _validate_uuid_fields(improved_answers: List[Dict]) -> bool: | ||
| """Validate that feedback_id and interaction_id are valid UUID objects.""" | ||
| try: | ||
| for item in improved_answers: | ||
| feedback_id = item.get("feedback_id") | ||
| interaction_id = item.get("interaction_id") | ||
| if not isinstance(feedback_id, type(feedback_id)) or not isinstance( | ||
| interaction_id, type(interaction_id) | ||
| ): | ||
| return False | ||
| return True | ||
| except Exception: | ||
| return False | ||
|
|
||
|
|
||
| async def _generate_enrichment_report( | ||
| question: str, improved_answer: str, new_context: str, report_prompt_location: str | ||
| ) -> str: | ||
| """Generate educational report using feedback report prompt.""" | ||
| try: | ||
| prompt_template = read_query_prompt(report_prompt_location) | ||
| rendered_prompt = prompt_template.format( | ||
| question=question, | ||
| improved_answer=improved_answer, | ||
| new_context=new_context, | ||
| ) | ||
| return await LLMGateway.acreate_structured_output( | ||
| text_input=rendered_prompt, | ||
| system_prompt="You are a helpful assistant that creates educational content.", | ||
| response_model=str, | ||
| ) | ||
| except Exception as exc: | ||
| logger.warning("Failed to generate enrichment report", error=str(exc), question=question) | ||
| return f"Educational content for: {question} - {improved_answer}" | ||
|
|
||
|
|
||
| async def _create_enrichment_datapoint( | ||
| improved_answer_item: Dict, | ||
| report_text: str, | ||
| ) -> Optional[FeedbackEnrichment]: | ||
| """Create a single FeedbackEnrichment DataPoint with proper ID and nodeset assignment.""" | ||
| try: | ||
| question = improved_answer_item["question"] | ||
| improved_answer = improved_answer_item["improved_answer"] | ||
|
|
||
| # Create nodeset following UserQAFeedback pattern | ||
| nodeset = NodeSet( | ||
| id=uuid5(NAMESPACE_OID, name="FeedbackEnrichment"), name="FeedbackEnrichment" | ||
| ) | ||
|
|
||
| enrichment = FeedbackEnrichment( | ||
| id=str(uuid5(NAMESPACE_OID, f"{question}_{improved_answer}")), | ||
| text=report_text, | ||
| question=question, | ||
| original_answer=improved_answer_item["answer"], # Use "answer" field | ||
| improved_answer=improved_answer, | ||
| feedback_id=improved_answer_item["feedback_id"], | ||
| interaction_id=improved_answer_item["interaction_id"], | ||
| belongs_to_set=nodeset, | ||
| ) | ||
|
|
||
| return enrichment | ||
| except Exception as exc: | ||
| logger.error( | ||
| "Failed to create enrichment datapoint", | ||
| error=str(exc), | ||
| question=improved_answer_item.get("question"), | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| async def create_enrichments( | ||
| improved_answers: List[Dict], | ||
| report_prompt_location: str = "feedback_report_prompt.txt", | ||
| ) -> List[FeedbackEnrichment]: | ||
| """Create FeedbackEnrichment DataPoint instances from improved answers.""" | ||
| if not improved_answers: | ||
| logger.info("No improved answers provided; returning empty list") | ||
| return [] | ||
|
|
||
| if not _validate_improved_answers(improved_answers): | ||
| logger.error("Input validation failed; missing required fields") | ||
| return [] | ||
|
|
||
| if not _validate_uuid_fields(improved_answers): | ||
| logger.error("UUID validation failed; invalid feedback_id or interaction_id") | ||
| return [] | ||
|
|
||
| logger.info("Creating enrichments", count=len(improved_answers)) | ||
|
|
||
| enrichments: List[FeedbackEnrichment] = [] | ||
|
|
||
| for improved_answer_item in improved_answers: | ||
| question = improved_answer_item["question"] | ||
| improved_answer = improved_answer_item["improved_answer"] | ||
| new_context = improved_answer_item["new_context"] | ||
|
|
||
| report_text = await _generate_enrichment_report( | ||
| question, improved_answer, new_context, report_prompt_location | ||
| ) | ||
|
|
||
| enrichment = await _create_enrichment_datapoint(improved_answer_item, report_text) | ||
|
|
||
| if enrichment: | ||
| enrichments.append(enrichment) | ||
| else: | ||
| logger.warning( | ||
| "Failed to create enrichment", | ||
| question=question, | ||
| interaction_id=improved_answer_item.get("interaction_id"), | ||
| ) | ||
|
|
||
| logger.info("Created enrichments", successful=len(enrichments)) | ||
| return enrichments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.