Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

111 lines
4.0 KiB
Python

"""Session turn analysis from user messages via LLM."""
from cognee.infrastructure.llm.LLMGateway import LLMGateway
from cognee.infrastructure.llm.prompts import read_query_prompt
from cognee.infrastructure.session.feedback_models import SessionTurnAnalysis
from cognee.shared.logging_utils import get_logger
logger = get_logger("feedback_detection")
def _render_served_context(served_context) -> str:
"""Render served session-context entries as an ``id: content`` block.
Accepts a pre-rendered string (returned as-is) or a list of dicts/objects exposing
``id``/``entry_id`` and ``content`` fields. Never raises: malformed items are skipped.
"""
if served_context is None:
return ""
if isinstance(served_context, str):
return served_context.strip()
lines = []
try:
for item in served_context:
if isinstance(item, dict):
entry_id = item.get("id") or item.get("entry_id")
content = item.get("content")
else:
entry_id = getattr(item, "id", None) or getattr(item, "entry_id", None)
content = getattr(item, "content", None)
if entry_id is None or content is None:
continue
lines.append(f"{str(entry_id).strip()}: {str(content).strip()}")
except Exception:
return ""
return "\n".join(lines)
def _append_optional_section(text_input: str, title: str, content: str | None) -> str:
if not content or not str(content).strip():
return text_input
return text_input + f"\n\n{title}:\n" + str(content).strip()
async def analyze_turn_for_session_context(
user_message: str,
*,
previous_question: str | None = None,
previous_answer: str | None = None,
served_context: list | str | None = None,
) -> SessionTurnAnalysis:
"""
Analyze a user message for answer routing and session-context updates.
When ``served_context`` is provided (a pre-rendered string or a list of session-context
entries served to the previous answer), it is appended to the LLM input so the single
turn-analysis call can additionally produce ``served_context_ratings`` and
``candidate_context_updates``. This adds no extra LLM call.
Returns a SessionTurnAnalysis. On LLM failure or timeout, returns an empty analysis so
the main completion flow is never blocked.
"""
if not (user_message and str(user_message).strip()):
return SessionTurnAnalysis()
try:
system_prompt = read_query_prompt("feedback_detection_system.txt")
if not system_prompt:
logger.warning("Feedback detection: system prompt not found, skipping")
return SessionTurnAnalysis()
text_input = "CURRENT USER MESSAGE:\n" + user_message.strip()
text_input = _append_optional_section(
text_input,
"PREVIOUS QUESTION",
previous_question,
)
text_input = _append_optional_section(
text_input,
"PREVIOUS ANSWER",
previous_answer,
)
rendered_context = _render_served_context(served_context)
if rendered_context:
text_input = (
text_input
+ "\n\nSESSION CONTEXT ENTRIES SERVED TO THE PREVIOUS ANSWER (id: content):\n"
+ rendered_context
)
result = await LLMGateway.acreate_structured_output(
text_input=text_input,
system_prompt=system_prompt,
response_model=SessionTurnAnalysis,
)
return result
except Exception as e:
logger.warning(
"Session turn analysis failed, proceeding with empty analysis: %s",
e,
exc_info=False,
)
return SessionTurnAnalysis()
async def detect_feedback(
user_message: str, served_context: list | str | None = None
) -> SessionTurnAnalysis:
"""Compatibility wrapper for older call sites."""
return await analyze_turn_for_session_context(user_message, served_context=served_context)