neue version

This commit is contained in:
Lars committed 2025-12-18 14:00:08 +01:00
1 parent 2c3ee8efd6
commit 136c3bb43f
6 files changed
+138 -94

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ DESCRIPTION: Haupt-Ingestion-Logik.
FIX: Korrekte Priorisierung von Frontmatter für chunk_profile und retriever_weight.
Lade Chunk-Config basierend auf dem effektiven Profil, nicht nur dem Notiz-Typ.
WP-22: Integration von Content Lifecycle (Status) und Edge Registry.
VERSION: 2.8.1 (WP-22 Lifecycle & Registry)
VERSION: 2.8.5 (WP-22 Lifecycle & Registry)
STATUS: Active
DEPENDENCIES: app.core.parser, app.core.note_payload, app.core.chunker, app.core.derive_edges, app.core.qdrant*, app.services.embeddings_client, app.services.edge_registry
EXTERNAL_CONFIG: config/types.yaml
+52 -51
View File
@@ -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.
VERSION: 0.6.1 (WP-22 Dynamic Scoring)
VERSION: 0.6.5 (WP-22 Scoring Formula)
STATUS: Active
DEPENDENCIES: app.config, app.models.dto, app.core.qdrant*, app.services.embeddings_client, app.core.graph_adapter
LAST_ANALYSIS: 2025-12-18
@@ -101,45 +101,48 @@ def _semantic_hits(
# --- WP-22 Helper: Lifecycle Multipliers (Teil A) ---
def _get_status_multiplier(payload: Dict[str, Any]) -> float:
"""
WP-22: stable (1.2), active (1.0), draft (0.5).
WP-22: stable (1.2), active/default (1.0), draft (0.5).
"""
status = str(payload.get("status", "active")).lower()
if status == "stable": return 1.2
if status == "active": return 1.0
if status == "draft": return 0.5
return 1.0
# --- WP-22: Dynamic Scoring Formula (Teil C) ---
def _compute_total_score(
semantic_score: float,
payload: Dict[str, Any],
edge_bonus: float = 0.0,
cent_bonus: float = 0.0,
edge_bonus_raw: float = 0.0,
cent_bonus_raw: float = 0.0,
dynamic_edge_boosts: Dict[str, float] = None
) -> Tuple[float, float, float]:
"""
Berechnet total_score nach WP-22 Scoring Formel.
WP-22 Mathematische Logik:
Score = BaseScore * (1 + ConfigWeight + DynamicBoost)
Hierbei gilt:
- BaseScore: semantic_similarity * status_multiplier
- ConfigWeight: retriever_weight (Type Boost)
- DynamicBoost: (edge_weight * edge_bonus) + (centrality_weight * centrality_bonus)
"""
raw_weight = payload.get("retriever_weight", 1.0)
try:
weight = float(raw_weight)
except (TypeError, ValueError):
weight = 1.0
if weight < 0.0:
weight = 0.0
sem_w, edge_w, cent_w = _get_scoring_weights()
# 1. Base Score (Semantik * Lifecycle)
status_mult = _get_status_multiplier(payload)
# Dynamic Edge Boosting (Teil C)
# Globaler Bonus falls Kanten-spezifische Boosts aktiv sind (z.B. WHY Frage)
# Die kanten-spezifische Gewichtung passiert bereits im Subgraph in hybrid_retrieve.
final_edge_score = edge_w * edge_bonus
if dynamic_edge_boosts and edge_bonus > 0:
# Globaler Verstärker für Graph-Signale bei spezifischen Intents
final_edge_score *= 1.5
total = (sem_w * float(semantic_score) * weight * status_mult) + final_edge_score + (cent_w * cent_bonus)
return float(total), float(edge_bonus), float(cent_bonus)
base_score = float(semantic_score) * status_mult
# 2. Config Weight (Static Type Boost)
config_weight = float(payload.get("retriever_weight", 1.0)) - 1.0 # 1.0 ist neutral
# 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
if dynamic_edge_boosts and (edge_bonus_raw > 0 or cent_bonus_raw > 0):
dynamic_boost *= 1.5
total = base_score * (1.0 + config_weight + dynamic_boost)
return float(total), float(edge_bonus_raw), float(cent_bonus_raw)
# --- WP-04b Explanation Logic ---
@@ -153,22 +156,21 @@ def _build_explanation(
node_key: Optional[str]
) -> Explanation:
"""Erstellt ein Explanation-Objekt (WP-04b)."""
sem_w, _edge_w, _cent_w = _get_scoring_weights()
# Scoring weights erneut laden für Reason-Details
_, edge_w_cfg, cent_w_cfg = _get_scoring_weights()
try:
type_weight = float(payload.get("retriever_weight", 1.0))
except (TypeError, ValueError):
type_weight = 1.0
type_weight = float(payload.get("retriever_weight", 1.0))
status_mult = _get_status_multiplier(payload)
note_type = payload.get("type", "unknown")
# Breakdown für Explanation (Muss die Scoring Formel spiegeln)
config_w_impact = type_weight - 1.0
dynamic_b_impact = (edge_w_cfg * edge_bonus) + (cent_w_cfg * cent_bonus)
base_val = semantic_score * status_mult
breakdown = ScoreBreakdown(
semantic_contribution=(sem_w * semantic_score * type_weight * status_mult),
edge_contribution=(edge_w_cfg * edge_bonus),
centrality_contribution=(cent_w_cfg * cent_bonus),
semantic_contribution=base_val,
edge_contribution=base_val * dynamic_b_impact,
centrality_contribution=0.0, # In dynamic_b_impact enthalten
raw_semantic=semantic_score,
raw_edge_bonus=edge_bonus,
raw_centrality=cent_bonus,
@@ -185,9 +187,8 @@ def _build_explanation(
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=(sem_w * semantic_score * (type_weight - 1.0))))
reasons.append(Reason(kind="type", message=f"{msg} aufgrund des Typs '{note_type}'.", score_impact=base_val * config_w_impact))
# WP-22: Status Grund hinzufügen
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))
@@ -219,13 +220,13 @@ def _build_explanation(
reasons.append(Reason(kind="edge", message=f"{dir_txt} '{tgt_txt}' via '{top_edge.kind}'", score_impact=impact, details={"kind": top_edge.kind}))
if cent_bonus > 0.01:
reasons.append(Reason(kind="centrality", message="Knoten liegt zentral im Kontext.", score_impact=breakdown.centrality_contribution))
reasons.append(Reason(kind="centrality", message="Knoten liegt zentral im Kontext.", score_impact=cent_w_cfg * cent_bonus))
return Explanation(breakdown=breakdown, reasons=reasons, related_edges=edges_dto if edges_dto else None)
def _extract_expand_options(req: QueryRequest) -> Tuple[int, List[str] | None]:
"""Extrahiert depth und edge_types für die Graph-Expansion."""
"""Extrahiert depth und edge_types für die Expansion."""
expand = getattr(req, "expand", None)
if not expand:
return 0, None
@@ -258,7 +259,7 @@ def _build_hits_from_semantic(
explain: bool = False,
dynamic_edge_boosts: Dict[str, float] = None
) -> QueryResponse:
"""Baut strukturierte QueryHits basierend auf den berechneten Scores."""
"""Baut strukturierte QueryHits basierend auf Hybrid-Scoring."""
t0 = time.time()
enriched: List[Tuple[str, float, Dict[str, Any], float, float, float]] = []
@@ -277,27 +278,27 @@ def _build_hits_from_semantic(
except Exception:
cent_bonus = 0.0
total, edge_bonus, cent_bonus = _compute_total_score(
total, eb, cb = _compute_total_score(
semantic_score,
payload,
edge_bonus=edge_bonus,
cent_bonus=cent_bonus,
edge_bonus_raw=edge_bonus,
cent_bonus_raw=cent_bonus,
dynamic_edge_boosts=dynamic_edge_boosts
)
enriched.append((pid, float(semantic_score), payload, total, edge_bonus, cent_bonus))
enriched.append((pid, float(semantic_score), payload, total, eb, cb))
enriched_sorted = sorted(enriched, key=lambda h: h[3], reverse=True)
limited = enriched_sorted[: max(1, top_k)]
results: List[QueryHit] = []
for pid, semantic_score, payload, total, edge_bonus, cent_bonus in limited:
for pid, semantic_score, payload, total, eb, cb in limited:
explanation_obj = None
if explain:
explanation_obj = _build_explanation(
semantic_score=float(semantic_score),
payload=payload,
edge_bonus=edge_bonus,
cent_bonus=cent_bonus,
edge_bonus=eb,
cent_bonus=cb,
subgraph=subgraph,
node_key=payload.get("chunk_id") or payload.get("note_id")
)
@@ -308,8 +309,8 @@ def _build_hits_from_semantic(
node_id=str(pid),
note_id=payload.get("note_id", "unknown"),
semantic_score=float(semantic_score),
edge_bonus=edge_bonus,
centrality_bonus=cent_bonus,
edge_bonus=eb,
centrality_bonus=cb,
total_score=total,
paths=None,
source={
@@ -348,7 +349,7 @@ def hybrid_retrieve(req: QueryRequest) -> QueryResponse:
depth, edge_types = _extract_expand_options(req)
# WP-22: Dynamic Boosts aus dem Request (vom Router) (Teil C)
# WP-22: Dynamic Boosts aus dem Request (vom Router)
boost_edges = getattr(req, "boost_edges", {})
subgraph: ga.Subgraph | None = None