164 lines
5.5 KiB
Python
164 lines
5.5 KiB
Python
"""
|
|
FILE: app/models/dto.py
|
|
DESCRIPTION: Pydantic-Modelle (DTOs) für Request/Response Bodies. Definiert das API-Schema.
|
|
VERSION: 0.6.4 (WP-22 Semantic Graph Routing, Lifecycle & Provenance)
|
|
STATUS: Active
|
|
DEPENDENCIES: pydantic, typing, uuid
|
|
LAST_ANALYSIS: 2025-12-18
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
from pydantic import BaseModel, Field
|
|
from typing import List, Literal, Optional, Dict, Any
|
|
import uuid
|
|
|
|
# WP-22: Definition der gültigen Kanten-Typen gemäß Manual
|
|
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"]
|
|
|
|
|
|
# --- Basis-DTOs ---
|
|
|
|
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
|
|
kind: str
|
|
source: str
|
|
target: str
|
|
weight: float
|
|
direction: Literal["out", "in", "undirected"] = "out"
|
|
# WP-22: Provenance Tracking (Herkunft und Vertrauen)
|
|
provenance: Optional[Literal["explicit", "rule", "smart", "structure"]] = "explicit"
|
|
confidence: float = 1.0
|
|
|
|
|
|
# --- Request Models ---
|
|
|
|
class QueryRequest(BaseModel):
|
|
"""
|
|
Request für /query.
|
|
"""
|
|
mode: Literal["semantic", "edge", "hybrid"] = "hybrid"
|
|
query: Optional[str] = None
|
|
query_vector: Optional[List[float]] = None
|
|
top_k: int = 10
|
|
expand: Dict = {"depth": 1, "edge_types": ["references", "belongs_to", "prev", "next", "depends_on", "related_to"]}
|
|
filters: Optional[Dict] = None
|
|
ret: Dict = {"with_paths": True, "with_notes": True, "with_chunks": True}
|
|
explain: bool = False
|
|
|
|
# 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
|
|
|
|
|
|
class FeedbackRequest(BaseModel):
|
|
"""
|
|
User-Feedback zu einem spezifischen Treffer oder der Gesamtantwort (Basis für WP-08).
|
|
"""
|
|
query_id: str = Field(..., description="ID der ursprünglichen Suche")
|
|
# 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.
|
|
node_id: str = Field(..., description="ID des bewerteten Treffers oder 'generated_answer'")
|
|
# Update: Range auf 1-5 erweitert für differenziertes Tuning
|
|
score: int = Field(..., ge=1, le=5, description="1 (Irrelevant/Falsch) bis 5 (Perfekt)")
|
|
comment: Optional[str] = None
|
|
|
|
|
|
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)")
|
|
# RAG Parameter (Override defaults)
|
|
top_k: int = 5
|
|
explain: bool = False
|
|
|
|
|
|
# --- WP-04b Explanation Models ---
|
|
|
|
class ScoreBreakdown(BaseModel):
|
|
"""Aufschlüsselung der Score-Komponenten nach der WP-22 Formel."""
|
|
semantic_contribution: float
|
|
edge_contribution: float
|
|
centrality_contribution: float
|
|
raw_semantic: float
|
|
raw_edge_bonus: float
|
|
raw_centrality: float
|
|
node_weight: float
|
|
|
|
|
|
class Reason(BaseModel):
|
|
"""Ein semantischer Grund für das Ranking."""
|
|
# WP-22: 'lifecycle' hinzugefügt für Status-Begründungen (Draft vs Stable)
|
|
kind: Literal["semantic", "edge", "type", "centrality", "lifecycle"]
|
|
message: str
|
|
score_impact: Optional[float] = None
|
|
details: Optional[Dict[str, Any]] = None
|
|
|
|
|
|
class Explanation(BaseModel):
|
|
"""Container für alle Erklärungsdaten eines Treffers."""
|
|
breakdown: ScoreBreakdown
|
|
reasons: List[Reason]
|
|
related_edges: Optional[List[EdgeDTO]] = None
|
|
|
|
|
|
# --- Response Models ---
|
|
|
|
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
|
|
payload: Optional[Dict] = None # Added for flexibility & WP-06 meta-data
|
|
explanation: Optional[Explanation] = None
|
|
|
|
|
|
class QueryResponse(BaseModel):
|
|
"""Antwortstruktur für /query."""
|
|
query_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
|
results: List[QueryHit]
|
|
used_mode: str
|
|
latency_ms: int
|
|
|
|
|
|
class GraphResponse(BaseModel):
|
|
"""Antwortstruktur für /graph/{note_id}."""
|
|
center_note_id: str
|
|
nodes: List[NodeDTO]
|
|
edges: List[EdgeDTO]
|
|
stats: Dict[str, int]
|
|
|
|
|
|
class ChatResponse(BaseModel):
|
|
"""
|
|
WP-05/06: Antwortstruktur für /chat.
|
|
"""
|
|
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")
|
|
latency_ms: int
|
|
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)")
|
|
intent_source: Optional[str] = Field("Unknown", description="WP-06: Quelle der Intent-Erkennung (Keyword vs. LLM)") |