bug fix
This commit is contained in:
parent
e47241740d
commit
c61d9c8236
|
|
@ -2,7 +2,7 @@
|
|||
FILE: app/core/retriever.py
|
||||
DESCRIPTION: Implementiert die Hybrid-Suche (Vektor + Graph-Expansion) und das Scoring-Modell (Explainability).
|
||||
WP-22 Update: Dynamic Edge Boosting, Lifecycle Scoring & Provenance Awareness.
|
||||
VERSION: 0.6.6 (WP-22 Scoring & Provenance)
|
||||
VERSION: 0.6.7 (WP-22 Scoring & Provenance Fix)
|
||||
STATUS: Active
|
||||
DEPENDENCIES: app.config, app.models.dto, app.core.qdrant*, app.services.embeddings_client, app.core.graph_adapter
|
||||
LAST_ANALYSIS: 2025-12-18
|
||||
|
|
@ -11,6 +11,7 @@ from __future__ import annotations
|
|||
|
||||
import os
|
||||
import time
|
||||
import logging
|
||||
from functools import lru_cache
|
||||
from typing import Any, Dict, List, Tuple, Iterable, Optional
|
||||
|
||||
|
|
@ -34,6 +35,7 @@ try:
|
|||
except Exception: # pragma: no cover
|
||||
yaml = None # type: ignore[assignment]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@lru_cache
|
||||
def _get_scoring_weights() -> Tuple[float, float, float]:
|
||||
|
|
@ -136,13 +138,21 @@ def _compute_total_score(
|
|||
|
||||
# 3. Dynamic Boost (Graph-Signale)
|
||||
_sem_w, edge_w_cfg, cent_w_cfg = _get_scoring_weights()
|
||||
dynamic_boost = (edge_w_cfg * edge_bonus_raw) + (cent_w_cfg * cent_bonus_raw)
|
||||
|
||||
# Falls Intent-Boosts vorliegen, verstärken wir den Dynamic Boost global
|
||||
if dynamic_edge_boosts and (edge_bonus_raw > 0 or cent_bonus_raw > 0):
|
||||
dynamic_boost *= 1.5
|
||||
# Multiplikator für Intent-Boosting (Teil C)
|
||||
graph_boost_factor = 1.5 if dynamic_edge_boosts and (edge_bonus_raw > 0 or cent_bonus_raw > 0) else 1.0
|
||||
|
||||
edge_impact = (edge_w_cfg * edge_bonus_raw) * graph_boost_factor
|
||||
cent_impact = (cent_w_cfg * cent_bonus_raw) * graph_boost_factor
|
||||
|
||||
dynamic_boost = edge_impact + cent_impact
|
||||
|
||||
total = base_score * (1.0 + config_weight + dynamic_boost)
|
||||
|
||||
# Debug Logging für Berechnungs-Validierung
|
||||
if logger.isEnabledFor(logging.DEBUG):
|
||||
logger.debug(f"Scoring Node {payload.get('note_id')}: Base={base_score:.3f}, ConfigW={config_weight:.3f}, GraphB={dynamic_boost:.3f} -> Total={total:.3f}")
|
||||
|
||||
return float(total), float(edge_bonus_raw), float(cent_bonus_raw)
|
||||
|
||||
|
||||
|
|
@ -154,7 +164,7 @@ def _build_explanation(
|
|||
edge_bonus: float,
|
||||
cent_bonus: float,
|
||||
subgraph: Optional[ga.Subgraph],
|
||||
node_key: Optional[str]
|
||||
target_note_id: Optional[str]
|
||||
) -> Explanation:
|
||||
"""Erstellt ein Explanation-Objekt mit Provenance-Details."""
|
||||
_, edge_w_cfg, cent_w_cfg = _get_scoring_weights()
|
||||
|
|
@ -163,15 +173,15 @@ def _build_explanation(
|
|||
status_mult = _get_status_multiplier(payload)
|
||||
note_type = payload.get("type", "unknown")
|
||||
|
||||
# Breakdown für Explanation
|
||||
config_w_impact = type_weight - 1.0
|
||||
dynamic_b_impact = (edge_w_cfg * edge_bonus) + (cent_w_cfg * cent_bonus)
|
||||
# Breakdown für Explanation (Reflektiert die WP-22 Formel exakt)
|
||||
base_val = semantic_score * status_mult
|
||||
config_w_impact = type_weight - 1.0
|
||||
|
||||
# Zentrale Berechnung der Kontributionen für den Breakdown
|
||||
breakdown = ScoreBreakdown(
|
||||
semantic_contribution=base_val,
|
||||
edge_contribution=base_val * dynamic_b_impact,
|
||||
centrality_contribution=0.0,
|
||||
edge_contribution=base_val * (edge_w_cfg * edge_bonus),
|
||||
centrality_contribution=base_val * (cent_w_cfg * cent_bonus),
|
||||
raw_semantic=semantic_score,
|
||||
raw_edge_bonus=edge_bonus,
|
||||
raw_centrality=cent_bonus,
|
||||
|
|
@ -181,46 +191,62 @@ def _build_explanation(
|
|||
reasons: List[Reason] = []
|
||||
edges_dto: List[EdgeDTO] = []
|
||||
|
||||
# 1. Semantische Gründe
|
||||
if semantic_score > 0.85:
|
||||
reasons.append(Reason(kind="semantic", message="Sehr hohe textuelle Übereinstimmung.", score_impact=breakdown.semantic_contribution))
|
||||
elif semantic_score > 0.70:
|
||||
reasons.append(Reason(kind="semantic", message="Gute textuelle Übereinstimmung.", score_impact=breakdown.semantic_contribution))
|
||||
|
||||
# 2. Typ-Gründe
|
||||
if type_weight != 1.0:
|
||||
msg = "Bevorzugt" if type_weight > 1.0 else "Leicht abgewertet"
|
||||
reasons.append(Reason(kind="type", message=f"{msg} aufgrund des Typs '{note_type}'.", score_impact=base_val * config_w_impact))
|
||||
|
||||
# WP-22: Lifecycle Grund hinzufügen
|
||||
# 3. Lifecycle-Gründe
|
||||
if status_mult != 1.0:
|
||||
msg = "Status-Bonus" if status_mult > 1.0 else "Status-Malus"
|
||||
reasons.append(Reason(kind="lifecycle", message=f"{msg} ({payload.get('status', 'unknown')}).", score_impact=0.0))
|
||||
reasons.append(Reason(kind="lifecycle", message=f"{msg} (Notiz ist '{payload.get('status', 'unknown')}').", score_impact=0.0))
|
||||
|
||||
if subgraph and node_key and edge_bonus > 0:
|
||||
# WP-22: Detaillierte Provenance-Gründe (Basis für WP-08)
|
||||
incoming_raw = subgraph.get_incoming_edges(node_key) or []
|
||||
for edge in incoming_raw:
|
||||
src = edge.get("source", "Unknown")
|
||||
# 4. Graph-Gründe (Edges) - FIX: Beachtet eingehende UND ausgehende Kanten
|
||||
if subgraph and target_note_id and edge_bonus > 0:
|
||||
# Sammle alle relevanten Kanten (Incoming + Outgoing)
|
||||
edges_raw = []
|
||||
if hasattr(subgraph, "get_incoming_edges"):
|
||||
edges_raw.extend(subgraph.get_incoming_edges(target_note_id) or [])
|
||||
if hasattr(subgraph, "get_outgoing_edges"):
|
||||
edges_raw.extend(subgraph.get_outgoing_edges(target_note_id) or [])
|
||||
|
||||
for edge in edges_raw:
|
||||
src = edge.get("source", target_note_id)
|
||||
tgt = edge.get("target", target_note_id)
|
||||
k = edge.get("kind", "edge")
|
||||
prov = edge.get("provenance", "rule")
|
||||
conf = float(edge.get("confidence", 1.0))
|
||||
|
||||
# Richtung bestimmen
|
||||
direction = "in" if tgt == target_note_id else "out"
|
||||
peer_id = src if direction == "in" else tgt
|
||||
|
||||
edges_dto.append(EdgeDTO(
|
||||
id=f"{src}->{node_key}:{k}", kind=k, source=src, target=node_key,
|
||||
weight=conf, direction="in", provenance=prov, confidence=conf
|
||||
id=f"{src}->{tgt}:{k}", kind=k, source=src, target=tgt,
|
||||
weight=conf, direction=direction, provenance=prov, confidence=conf
|
||||
))
|
||||
|
||||
# Die 3 stärksten Kanten als Begründung auflisten
|
||||
all_edges = sorted(edges_dto, key=lambda e: e.confidence, reverse=True)
|
||||
for top_edge in all_edges[:3]:
|
||||
prov_txt = "Bestätigt durch" if top_edge.provenance == "explicit" else "Vermutet durch"
|
||||
for top_e in all_edges[:3]:
|
||||
prov_txt = "Bestätigte" if top_e.provenance == "explicit" else "Vermutete (KI)"
|
||||
dir_txt = "Referenz von" if top_e.direction == "in" else "Verweis auf"
|
||||
reasons.append(Reason(
|
||||
kind="edge",
|
||||
message=f"{prov_txt} Kante '{top_edge.kind}' von '{top_edge.source}'.",
|
||||
score_impact=edge_w_cfg * top_edge.confidence,
|
||||
details={"provenance": top_edge.provenance}
|
||||
message=f"{prov_txt} Kante '{top_e.kind}': {dir_txt} '{top_e.peer_id if hasattr(top_e, 'peer_id') else (top_e.source if top_e.direction=='in' else top_e.target)}'.",
|
||||
score_impact=edge_w_cfg * top_e.confidence,
|
||||
details={"provenance": top_e.provenance, "kind": top_e.kind}
|
||||
))
|
||||
|
||||
# 5. Zentralitäts-Gründe
|
||||
if cent_bonus > 0.01:
|
||||
reasons.append(Reason(kind="centrality", message="Knoten liegt zentral im Kontext.", score_impact=cent_w_cfg * cent_bonus))
|
||||
reasons.append(Reason(kind="centrality", message="Knoten liegt zentral im aktuellen Kontext-Graphen.", score_impact=breakdown.centrality_contribution))
|
||||
|
||||
return Explanation(breakdown=breakdown, reasons=reasons, related_edges=edges_dto if edges_dto else None)
|
||||
|
||||
|
|
@ -266,16 +292,17 @@ def _build_hits_from_semantic(
|
|||
for pid, semantic_score, payload in hits:
|
||||
edge_bonus = 0.0
|
||||
cent_bonus = 0.0
|
||||
node_key = payload.get("chunk_id") or payload.get("note_id")
|
||||
# WICHTIG für WP-22: Graph-Abfragen IMMER über die Note-ID, nicht Chunk-ID
|
||||
target_note_id = payload.get("note_id")
|
||||
|
||||
if subgraph is not None and node_key:
|
||||
if subgraph is not None and target_note_id:
|
||||
try:
|
||||
# WP-22: edge_bonus nutzt intern bereits die confidence-gewichteten Pfade
|
||||
edge_bonus = float(subgraph.edge_bonus(node_key))
|
||||
# edge_bonus nutzt intern bereits die confidence-gewichteten Pfade
|
||||
edge_bonus = float(subgraph.edge_bonus(target_note_id))
|
||||
except Exception:
|
||||
edge_bonus = 0.0
|
||||
try:
|
||||
cent_bonus = float(subgraph.centrality_bonus(node_key))
|
||||
cent_bonus = float(subgraph.centrality_bonus(target_note_id))
|
||||
except Exception:
|
||||
cent_bonus = 0.0
|
||||
|
||||
|
|
@ -288,6 +315,7 @@ def _build_hits_from_semantic(
|
|||
)
|
||||
enriched.append((pid, float(semantic_score), payload, total, eb, cb))
|
||||
|
||||
# Sortierung nach finalem Score
|
||||
enriched_sorted = sorted(enriched, key=lambda h: h[3], reverse=True)
|
||||
limited = enriched_sorted[: max(1, top_k)]
|
||||
|
||||
|
|
@ -301,7 +329,7 @@ def _build_hits_from_semantic(
|
|||
edge_bonus=eb,
|
||||
cent_bonus=cb,
|
||||
subgraph=subgraph,
|
||||
node_key=payload.get("chunk_id") or payload.get("note_id")
|
||||
target_note_id=payload.get("note_id")
|
||||
)
|
||||
|
||||
text_content = payload.get("page_content") or payload.get("text") or payload.get("content")
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user