chunker mit LLM initial

This commit is contained in:
Lars committed 2025-12-11 23:21:24 +01:00
1 parent 714763f92f
commit 13ab2b7d68
3 files changed
+197 -34

No files matched your search

+86 -29
View File
@@ -8,15 +8,17 @@ from pathlib import Path
from markdown_it import MarkdownIt
from markdown_it.token import Token
# NEUE IMPORTS
# Import des Semantic Analyzer Services
from app.services.semantic_analyzer import get_semantic_analyzer
import asyncio # Für den asynchronen Aufruf des Chunkers
# ==========================================
# 1. CONFIGURATION LOADER (Updated for config/ dir)
# 1. CONFIGURATION LOADER (Ehemals chunk_config.py)
# ==========================================
# Pfad-Logik:
# Wir gehen 3 Ebenen hoch: app/core/chunker.py -> app/core -> app -> root
# Pfad-Logik: app/core/chunker.py -> app/core -> app -> root/config/types.yaml
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# KORREKTUR: types.yaml liegt im Unterordner "config"
CONFIG_PATH = BASE_DIR / "config" / "types.yaml"
# Fallback Values
@@ -38,7 +40,6 @@ def _load_yaml_config() -> Dict[str, Any]:
if not CONFIG_PATH.exists():
# Debugging-Hilfe: Zeigt an, wo gesucht wurde
print(f"WARNUNG: types.yaml nicht gefunden unter: {CONFIG_PATH}")
print(f" (Basis-Verzeichnis war: {BASE_DIR})")
return {}
try:
@@ -51,32 +52,24 @@ def _load_yaml_config() -> Dict[str, Any]:
return {}
def get_chunk_config(note_type: str) -> Dict[str, Any]:
"""
Löst Typ -> Profil -> Konfiguration auf.
"""
"""Löst Typ -> Profil -> Konfiguration auf."""
full_config = _load_yaml_config()
# 1. Profile holen
profiles = full_config.get("chunking_profiles", {})
# 2. Typ-Definition holen
type_def = full_config.get("types", {}).get(note_type.lower(), {})
# 3. Profil-Namen ermitteln (Fallback auf defaults)
profile_name = type_def.get("chunking_profile")
if not profile_name:
profile_name = full_config.get("defaults", {}).get("chunking_profile", "sliding_standard")
# 4. Config bauen
config = profiles.get(profile_name, DEFAULT_PROFILE).copy()
# Sicherstellen, dass Overlap ein Tuple ist
if "overlap" in config and isinstance(config["overlap"], list):
config["overlap"] = tuple(config["overlap"])
return config
# Legacy Support für alten Code
# Legacy Support
def get_sizes(note_type: str):
cfg = get_chunk_config(note_type)
return {
@@ -86,7 +79,7 @@ def get_sizes(note_type: str):
}
# ==========================================
# 2. CHUNKING LOGIC & PARSER
# 2. DATA CLASSES & HELPERS
# ==========================================
# --- Hilfen ---
@@ -94,7 +87,6 @@ _SENT_SPLIT = re.compile(r'(?<=[.!?])\s+(?=[A-ZÄÖÜ0-9„(])')
_WS = re.compile(r'\s+')
def estimate_tokens(text: str) -> int:
# 1 Token ≈ 4 chars
t = len(text.strip())
return max(1, math.ceil(t / 4))
@@ -117,8 +109,8 @@ class Chunk:
id: str
note_id: str
index: int
text: str # Reintext für Anzeige
window: str # Text + Context für Embeddings
text: str # Reintext für Anzeige (JETZT INKL. INJIZIERTER LINKS)
window: str # Text + Context für Embeddings (WIE 'text' BEI LLM-CHUNK)
token_count: int
section_title: Optional[str]
section_path: str
@@ -193,7 +185,9 @@ def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
i += 1
return blocks, h1_title
# --- Strategien ---
# ==========================================
# 3. STRATEGIES (SYNCHRON)
# ==========================================
def _strategy_sliding_window(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, context_prefix: str = "") -> List[Chunk]:
target = config.get("target", 400)
@@ -266,6 +260,8 @@ def _strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id
for path in ordered:
s_blocks = sections[path]
if not s_blocks: continue
breadcrumbs = path.strip("/").replace("/", " > ")
context_header = f"# {doc_title}\n## {breadcrumbs}"
full_text = "\n\n".join([b.text for b in s_blocks])
@@ -279,6 +275,7 @@ def _strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id
neighbors_prev=None, neighbors_next=None, char_start=0, char_end=0
))
else:
# Fallback auf Sliding Window mit Context Injection
sub = _strategy_sliding_window(s_blocks, config, note_id, context_prefix=context_header)
base = len(chunks)
for i, sc in enumerate(sub):
@@ -287,19 +284,79 @@ def _strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id
chunks.append(sc)
return chunks
# --- Main Entry Point ---
# ==========================================
# 4. STRATEGY (ASYNCHRON)
# ==========================================
def assemble_chunks(note_id: str, md_text: str, note_type: str) -> List[Chunk]:
async def _strategy_semantic_llm(md_text: str, config: Dict[str, Any], note_id: str, note_type: str) -> List[Chunk]:
"""
NEUE STRATEGIE: Delegiert die Zerlegung und Kanten-Extraktion an ein LLM.
"""
analyzer = get_semantic_analyzer()
# Text-Splitting wird hier vom LLM übernommen
semantic_chunks = await analyzer.analyze_and_chunk(md_text, note_type)
chunks: List[Chunk] = []
for i, sc in enumerate(semantic_chunks):
# 1. Edge Injection für derive_edges.py
# Wir formatieren die LLM-generierten Kanten in die Inline-Syntax,
# damit die bestehende derive_edges.py (Regex) sie findet.
injection_block = "\n"
for edge_str in sc.suggested_edges:
kind, target = edge_str.split(":", 1)
# Nutzt die Syntax: [[rel:kind | Target]]
injection_block += f"[[rel:{kind} | {target}]] "
full_text = sc.content + injection_block
# 2. Chunk Objekt bauen
chunks.append(Chunk(
id=f"{note_id}#sem{i:02d}",
note_id=note_id,
index=i,
text=full_text.strip(), # Enthält die Links (für derive_edges)
window=full_text.strip(), # Auch das Embedding "sieht" die Links (gut für Retrieval)
token_count=estimate_tokens(full_text),
section_title="Semantic Section",
section_path="/LLM",
neighbors_prev=None, neighbors_next=None,
char_start=0, char_end=0
))
return chunks
# ==========================================
# 5. MAIN ENTRY POINT (ASYNC)
# ==========================================
async def assemble_chunks(note_id: str, md_text: str, note_type: str) -> List[Chunk]:
"""
Hauptfunktion. Analysiert Config und wählt Strategie. MUSS ASYNC SEIN.
"""
config = get_chunk_config(note_type)
strategy = config.get("strategy", "sliding_window")
blocks, doc_title = parse_blocks(md_text)
if strategy == "by_heading":
chunks = _strategy_by_heading(blocks, config, note_id, doc_title)
else:
chunks = _strategy_sliding_window(blocks, config, note_id)
# Die beiden bestehenden Strategien rufen wir über einen Sync-Wrapper auf,
# damit assemble_chunks ASYNC bleiben kann.
if strategy == "semantic_llm":
chunks = await _strategy_semantic_llm(md_text, config, note_id, note_type)
elif strategy == "by_heading":
blocks, doc_title = parse_blocks(md_text)
# Blockiert nur kurz für die sync-Rechenarbeit
chunks = await asyncio.to_thread(_strategy_by_heading, blocks, config, note_id, doc_title)
else: # sliding_window (Default)
blocks, doc_title = parse_blocks(md_text)
# Blockiert nur kurz für die sync-Rechenarbeit
chunks = await asyncio.to_thread(_strategy_sliding_window, blocks, config, note_id)
# Post-Process: Neighbors setzen
for i, ch in enumerate(chunks):
ch.neighbors_prev = chunks[i-1].id if i > 0 else None
ch.neighbors_next = chunks[i+1].id if i < len(chunks)-1 else None
return chunks