-
Notifications
You must be signed in to change notification settings - Fork 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: generate improved answers temp
- Loading branch information
commit 97eb89386ececf8acaa5cf26161ba7892dc4595e
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,132 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Dict, List, Optional, Tuple | ||
| from pydantic import BaseModel | ||
|
|
||
| from cognee.infrastructure.llm import LLMGateway | ||
| from cognee.infrastructure.llm.prompts.read_query_prompt import read_query_prompt | ||
| from cognee.modules.graph.utils import resolve_edges_to_text | ||
| from cognee.shared.logging_utils import get_logger | ||
|
|
||
| from .utils import create_retriever | ||
|
|
||
|
|
||
| class ImprovedAnswerResponse(BaseModel): | ||
| """Response model for improved answer generation containing answer and explanation.""" | ||
|
|
||
| answer: str | ||
| explanation: str | ||
|
|
||
|
|
||
| logger = get_logger("generate_improved_answers") | ||
|
|
||
|
|
||
| def _validate_input_data(feedback_interactions: List[Dict]) -> bool: | ||
| """Validate that input contains required fields for all items.""" | ||
| required_fields = [ | ||
| "question", | ||
| "answer", | ||
| "context", | ||
| "feedback_text", | ||
| "feedback_id", | ||
| "interaction_id", | ||
| ] | ||
| return all( | ||
| all(item.get(field) is not None for field in required_fields) | ||
| for item in feedback_interactions | ||
| ) | ||
|
|
||
|
|
||
| def _render_reaction_prompt( | ||
| question: str, context: str, wrong_answer: str, negative_feedback: str | ||
| ) -> str: | ||
| """Render the feedback reaction prompt with provided variables.""" | ||
| prompt_template = read_query_prompt("feedback_reaction_prompt.txt") | ||
| return prompt_template.format( | ||
| question=question, | ||
| context=context, | ||
| wrong_answer=wrong_answer, | ||
| negative_feedback=negative_feedback, | ||
| ) | ||
|
|
||
|
|
||
| async def _generate_improved_answer_for_single_interaction( | ||
| feedback_interaction: Dict, retriever, reaction_prompt_location: str | ||
| ) -> Optional[Dict]: | ||
| """Generate improved answer for a single feedback-interaction pair using structured retriever completion.""" | ||
| try: | ||
| question_text = feedback_interaction["question"] | ||
| original_answer_text = feedback_interaction["answer"] | ||
| context_text = feedback_interaction["context"] | ||
| feedback_text = feedback_interaction["feedback_text"] | ||
|
|
||
| query_text = _render_reaction_prompt( | ||
| question_text, context_text, original_answer_text, feedback_text | ||
| ) | ||
|
|
||
| retrieved_context = await retriever.get_context(query_text) | ||
| completion, new_context_text = await retriever.get_structured_completion( | ||
| query=query_text, context=retrieved_context, response_model=ImprovedAnswerResponse | ||
| ) | ||
|
|
||
| if completion: | ||
| return { | ||
| **feedback_interaction, | ||
| "improved_answer": completion.answer, | ||
| "new_context": new_context_text, | ||
| "explanation": completion.explanation, | ||
| } | ||
| else: | ||
| logger.warning( | ||
| "Failed to get structured completion from retriever", question=question_text | ||
| ) | ||
| return None | ||
|
|
||
| except Exception as exc: # noqa: BLE001 | ||
| logger.error( | ||
| "Failed to generate improved answer", | ||
| error=str(exc), | ||
| question=feedback_interaction.get("question"), | ||
| ) | ||
| return None | ||
|
|
||
|
|
||
| async def generate_improved_answers( | ||
| feedback_interactions: List[Dict], | ||
| retriever_name: str = "graph_completion_cot", | ||
| top_k: int = 20, | ||
| reaction_prompt_location: str = "feedback_reaction_prompt.txt", | ||
| ) -> List[Dict]: | ||
| """Generate improved answers using configurable retriever and LLM.""" | ||
| if not feedback_interactions: | ||
| logger.info("No feedback interactions provided; returning empty list") | ||
| return [] | ||
|
|
||
| if not _validate_input_data(feedback_interactions): | ||
| logger.error("Input data validation failed; missing required fields") | ||
| return [] | ||
|
|
||
| retriever = create_retriever( | ||
| retriever_name=retriever_name, | ||
| top_k=top_k, | ||
| user_prompt_path="graph_context_for_question.txt", | ||
| system_prompt_path="answer_simple_question.txt", | ||
| ) | ||
|
|
||
| improved_answers: List[Dict] = [] | ||
| successful_count = 0 | ||
| failed_count = 0 | ||
|
|
||
| for feedback_interaction in feedback_interactions: | ||
| result = await _generate_improved_answer_for_single_interaction( | ||
| feedback_interaction, retriever, reaction_prompt_location | ||
| ) | ||
|
|
||
| if result: | ||
| improved_answers.append(result) | ||
| successful_count += 1 | ||
| else: | ||
| failed_count += 1 | ||
|
|
||
| logger.info("Generated improved answers", successful=successful_count, failed=failed_count) | ||
| return improved_answers | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reaction_prompt_location ignored and prompt None not handled
_read_query_prompt may return None; formatting will crash. Also the function hardcodes the filename ignoring the parameter passed to the caller.
📝 Committable suggestion
🤖 Prompt for AI Agents