mindnet/app/models/dto.py

164 lines
5.5 KiB
Python
Raw Normal View History

2025-10-07 11:03:16 +02:00
"""
2025-12-15 15:40:39 +01:00
FILE: app/models/dto.py
DESCRIPTION: Pydantic-Modelle (DTOs) für Request/Response Bodies. Definiert das API-Schema.
2025-12-18 14:30:24 +01:00
VERSION: 0.6.4 (WP-22 Semantic Graph Routing, Lifecycle & Provenance)
2025-12-15 15:40:39 +01:00
STATUS: Active
DEPENDENCIES: pydantic, typing, uuid
2025-12-18 14:30:24 +01:00
LAST_ANALYSIS: 2025-12-18
2025-10-07 11:03:16 +02:00
"""
from __future__ import annotations
2025-12-07 16:32:02 +01:00
from pydantic import BaseModel, Field
from typing import List, Literal, Optional, Dict, Any
2025-12-07 19:29:48 +01:00
import uuid
2025-10-07 11:03:16 +02:00
2025-12-18 14:30:24 +01:00
# WP-22: Definition der gültigen Kanten-Typen gemäß Manual
2025-12-18 12:40:34 +01:00
EdgeKind = Literal["references", "references_at", "backlink", "next", "prev", "belongs_to", "depends_on", "related_to", "similar_to", "caused_by", "derived_from", "based_on", "solves", "blocks", "uses", "guides"]
2025-10-07 11:03:16 +02:00
2025-12-07 19:29:48 +01:00
# --- Basis-DTOs ---
2025-10-07 11:03:16 +02:00
class NodeDTO(BaseModel):
"""Darstellung eines Knotens (Note oder Chunk) im API-Graph."""
id: str
type: Literal["note", "chunk"]
title: Optional[str] = None
note_id: Optional[str] = None
tags: Optional[List[str]] = None
in_degree: Optional[int] = None
out_degree: Optional[int] = None
score: Optional[float] = None
section_title: Optional[str] = None
section_path: Optional[str] = None
path: Optional[str] = None
class EdgeDTO(BaseModel):
"""Darstellung einer Kante im API-Graph."""
id: str
2025-12-08 10:32:57 +01:00
kind: str
2025-10-07 11:03:16 +02:00
source: str
target: str
weight: float
direction: Literal["out", "in", "undirected"] = "out"
2025-12-18 14:30:24 +01:00
# WP-22: Provenance Tracking (Herkunft und Vertrauen)
provenance: Optional[Literal["explicit", "rule", "smart", "structure"]] = "explicit"
confidence: float = 1.0
2025-10-07 11:03:16 +02:00
2025-12-07 19:29:48 +01:00
# --- Request Models ---
2025-10-07 11:03:16 +02:00
class QueryRequest(BaseModel):
"""
2025-12-08 10:32:57 +01:00
Request für /query.
2025-10-07 11:03:16 +02:00
"""
mode: Literal["semantic", "edge", "hybrid"] = "hybrid"
query: Optional[str] = None
query_vector: Optional[List[float]] = None
top_k: int = 10
2025-12-07 16:32:02 +01:00
expand: Dict = {"depth": 1, "edge_types": ["references", "belongs_to", "prev", "next", "depends_on", "related_to"]}
2025-10-07 11:03:16 +02:00
filters: Optional[Dict] = None
ret: Dict = {"with_paths": True, "with_notes": True, "with_chunks": True}
2025-12-07 16:32:02 +01:00
explain: bool = False
2025-12-18 12:40:34 +01:00
# WP-22: Semantic Graph Routing
# Erlaubt dem Router, Kantengewichte dynamisch zu überschreiben.
# Format: {"caused_by": 3.0, "related_to": 0.5}
boost_edges: Optional[Dict[str, float]] = None
2025-12-07 16:32:02 +01:00
2025-12-07 19:29:48 +01:00
class FeedbackRequest(BaseModel):
"""
2025-12-18 14:30:24 +01:00
User-Feedback zu einem spezifischen Treffer oder der Gesamtantwort (Basis für WP-08).
2025-12-07 19:29:48 +01:00
"""
query_id: str = Field(..., description="ID der ursprünglichen Suche")
2025-12-18 13:56:45 +01:00
# node_id ist optional: Wenn leer oder "generated_answer", gilt es für die Antwort.
# Wenn eine echte Chunk-ID, gilt es für die Quelle.
2025-12-09 22:27:56 +01:00
node_id: str = Field(..., description="ID des bewerteten Treffers oder 'generated_answer'")
2025-12-18 13:56:45 +01:00
# Update: Range auf 1-5 erweitert für differenziertes Tuning
2025-12-09 22:27:56 +01:00
score: int = Field(..., ge=1, le=5, description="1 (Irrelevant/Falsch) bis 5 (Perfekt)")
2025-12-07 19:29:48 +01:00
comment: Optional[str] = None
2025-12-08 10:32:57 +01:00
class ChatRequest(BaseModel):
"""
WP-05: Request für /chat.
"""
message: str = Field(..., description="Die Nachricht des Users")
conversation_id: Optional[str] = Field(None, description="Optional: ID für Chat-Verlauf (noch nicht implementiert)")
2025-12-18 13:56:45 +01:00
# RAG Parameter (Override defaults)
2025-12-08 10:32:57 +01:00
top_k: int = 5
explain: bool = False
2025-12-07 16:32:02 +01:00
# --- WP-04b Explanation Models ---
class ScoreBreakdown(BaseModel):
2025-12-18 14:30:24 +01:00
"""Aufschlüsselung der Score-Komponenten nach der WP-22 Formel."""
2025-12-08 10:32:57 +01:00
semantic_contribution: float
edge_contribution: float
centrality_contribution: float
2025-12-07 16:32:02 +01:00
raw_semantic: float
raw_edge_bonus: float
raw_centrality: float
2025-12-08 10:32:57 +01:00
node_weight: float
2025-12-07 16:32:02 +01:00
class Reason(BaseModel):
2025-12-07 19:29:48 +01:00
"""Ein semantischer Grund für das Ranking."""
2025-12-18 12:40:34 +01:00
# WP-22: 'lifecycle' hinzugefügt für Status-Begründungen (Draft vs Stable)
kind: Literal["semantic", "edge", "type", "centrality", "lifecycle"]
2025-12-07 16:32:02 +01:00
message: str
score_impact: Optional[float] = None
details: Optional[Dict[str, Any]] = None
class Explanation(BaseModel):
2025-12-07 19:29:48 +01:00
"""Container für alle Erklärungsdaten eines Treffers."""
2025-12-07 16:32:02 +01:00
breakdown: ScoreBreakdown
reasons: List[Reason]
related_edges: Optional[List[EdgeDTO]] = None
2025-12-07 19:29:48 +01:00
# --- Response Models ---
2025-10-07 11:03:16 +02:00
class QueryHit(BaseModel):
"""Einzelnes Trefferobjekt für /query."""
node_id: str
note_id: Optional[str]
semantic_score: float
edge_bonus: float
centrality_bonus: float
total_score: float
paths: Optional[List[List[Dict]]] = None
source: Optional[Dict] = None
2025-12-09 18:11:25 +01:00
payload: Optional[Dict] = None # Added for flexibility & WP-06 meta-data
2025-12-07 16:32:02 +01:00
explanation: Optional[Explanation] = None
2025-10-07 11:03:16 +02:00
class QueryResponse(BaseModel):
2025-12-08 10:32:57 +01:00
"""Antwortstruktur für /query."""
2025-12-07 19:29:48 +01:00
query_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
2025-10-07 11:03:16 +02:00
results: List[QueryHit]
used_mode: str
latency_ms: int
class GraphResponse(BaseModel):
2025-12-07 19:29:48 +01:00
"""Antwortstruktur für /graph/{note_id}."""
2025-10-07 11:03:16 +02:00
center_note_id: str
nodes: List[NodeDTO]
edges: List[EdgeDTO]
2025-12-08 10:32:57 +01:00
stats: Dict[str, int]
class ChatResponse(BaseModel):
"""
2025-12-08 18:48:24 +01:00
WP-05/06: Antwortstruktur für /chat.
2025-12-08 10:32:57 +01:00
"""
query_id: str = Field(..., description="Traceability ID (dieselbe wie für Search)")
answer: str = Field(..., description="Generierte Antwort vom LLM")
sources: List[QueryHit] = Field(..., description="Die für die Antwort genutzten Quellen")
2025-12-08 18:48:24 +01:00
latency_ms: int
2025-12-09 18:02:21 +01:00
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)")
2025-12-18 12:40:34 +01:00
intent_source: Optional[str] = Field("Unknown", description="WP-06: Quelle der Intent-Erkennung (Keyword vs. LLM)")