Dateien nach "app/core" hochladen
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 2s
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 2s
This commit is contained in:
parent
2a1c62aeed
commit
9e8b433c95
|
|
@ -3,14 +3,17 @@
|
||||||
"""
|
"""
|
||||||
app/core/chunk_payload.py — Mindnet V2 (compat)
|
app/core/chunk_payload.py — Mindnet V2 (compat)
|
||||||
|
|
||||||
Ziele:
|
Ziele (ohne Bruch zur lauffähigen v1-Basis):
|
||||||
- Bewahrt bestehendes Verhalten (index, chunk_profile, retriever_weight, etc.)
|
- Akzeptiert `file_path=` (Alias zu path_arg)
|
||||||
- Denormalisiert optional `tags` aus der Note‑FM auf Chunks
|
- Verarbeitet Chunks sowohl als `dict` **als auch** als Objekt (z. B. Dataclass `Chunk`)
|
||||||
- Fügt Aliase für die Chunk‑Nummer hinzu: `ord` (v2‑Schema), `chunk_num`, `Chunk_Nummer`
|
- Schreibt v1-kompatible Felder:
|
||||||
- **Kompatibilität:** akzeptiert sowohl `path_arg` (positional) als auch `file_path` (keyword)
|
* `id` (Alias von `chunk_id` – **wichtig** für app/core/edges.py v1)
|
||||||
|
* `neighbors: {prev, next}` wird **berechnet** (Sequenz), falls nicht vorhanden
|
||||||
|
- Denormalisiert optional `tags` der Note auf Chunks
|
||||||
|
- Fügt Nummern-Aliase hinzu: `ord`, `chunk_num`, `Chunk_Nummer`
|
||||||
|
|
||||||
Hinweis:
|
Wichtig:
|
||||||
- `edge_defaults` sind Note‑Regeln (Typ) und werden nicht pro Chunk gespiegelt.
|
- `edge_defaults` gehören zur *Note* (Typ-Regeln), nicht pro Chunk. Werden hier **nicht** gespiegelt.
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
@ -22,12 +25,18 @@ from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from app.core.chunker import assemble_chunks
|
from app.core.chunker import assemble_chunks
|
||||||
|
|
||||||
|
# ---------- Helpers ----------
|
||||||
|
|
||||||
def _as_dict(obj):
|
def _as_dict(obj):
|
||||||
if isinstance(obj, dict): return obj
|
if isinstance(obj, dict):
|
||||||
try:
|
return obj
|
||||||
return dict(obj) # type: ignore
|
# Objekt → (teilweise) Dict-Ansicht via Attribute
|
||||||
except Exception:
|
d = {}
|
||||||
return {"value": obj}
|
for k in ("index","ord","chunk_index","text","window","id","chunk_id","neighbors","note_id","type","title"):
|
||||||
|
if hasattr(obj, k):
|
||||||
|
d[k] = getattr(obj, k)
|
||||||
|
# Fallback: bestehe nicht auf Vollständigkeit
|
||||||
|
return d
|
||||||
|
|
||||||
def _coalesce(*vals):
|
def _coalesce(*vals):
|
||||||
for v in vals:
|
for v in vals:
|
||||||
|
|
@ -47,21 +56,21 @@ def _ensure_list(x) -> list:
|
||||||
if isinstance(x, (set, tuple)): return [str(i) for i in x]
|
if isinstance(x, (set, tuple)): return [str(i) for i in x]
|
||||||
return [str(x)]
|
return [str(x)]
|
||||||
|
|
||||||
def _load_types_config(types_cfg_explicit: Optional[dict] = None) -> dict:
|
|
||||||
"""Types-Registry *optional* einspeisen (bereits geparst), sonst lazy-laden vermeiden."""
|
|
||||||
return types_cfg_explicit or {}
|
|
||||||
|
|
||||||
def _text_from_note(note: Dict[str, Any]) -> str:
|
def _text_from_note(note: Dict[str, Any]) -> str:
|
||||||
# Erwartete Inputs (siehe parser.py / import_markdown.py):
|
|
||||||
# note["body"] oder note["text"]; Fallback leerer String
|
|
||||||
return note.get("body") or note.get("text") or ""
|
return note.get("body") or note.get("text") or ""
|
||||||
|
|
||||||
def _iter_chunks(note: Dict[str, Any], chunk_profile: str, fulltext: str) -> List[Dict[str, Any]]:
|
def _iter_chunks(note: Dict[str, Any], chunk_profile: str, fulltext: str) -> List[Dict[str, Any]]:
|
||||||
"""Nutze bestehenden assemble_chunks(note_id, body, type)."""
|
"""Nutze bestehenden assemble_chunks(note_id, body, type). Rückgabe kann Objektliste sein."""
|
||||||
note_id = note.get("id") or (note.get("frontmatter") or {}).get("id")
|
note_id = note.get("id") or (note.get("frontmatter") or {}).get("id")
|
||||||
ntype = (note.get("frontmatter") or {}).get("type") or note.get("type") or "note"
|
ntype = (note.get("frontmatter") or {}).get("type") or note.get("type") or "note"
|
||||||
# assemble_chunks liefert Liste von Dicts mit mindestens {"index","text"} (v1)
|
raw_list = assemble_chunks(note_id, fulltext, ntype)
|
||||||
return assemble_chunks(note_id, fulltext, ntype)
|
# Normalisiere auf Dicts (unter Bewahrung vorhandener Keys)
|
||||||
|
out: List[Dict[str, Any]] = []
|
||||||
|
for c in raw_list:
|
||||||
|
out.append(_as_dict(c) if not isinstance(c, dict) else c)
|
||||||
|
return out
|
||||||
|
|
||||||
|
# ---------- Main ----------
|
||||||
|
|
||||||
def make_chunk_payloads(
|
def make_chunk_payloads(
|
||||||
note: Any,
|
note: Any,
|
||||||
|
|
@ -73,23 +82,16 @@ def make_chunk_payloads(
|
||||||
types_cfg: Optional[dict] = None,
|
types_cfg: Optional[dict] = None,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Erzeugt Chunk-Payloads. Erwartet:
|
Erzeugt Chunk-Payloads im v1-kompatiblen Format (plus V2-Aliase).
|
||||||
- `note`: Normalisierte Note-Struktur (inkl. frontmatter)
|
|
||||||
- `path_arg` oder `file_path`: Pfad der Note
|
|
||||||
- `chunks_from_chunker`: optional: Ergebnis von assemble_chunks (sonst wird intern erzeugt)
|
|
||||||
|
|
||||||
Rückgabe: Liste aus Payload-Dicts, jedes mit mind.:
|
|
||||||
- note_id, chunk_id, index, ord (Alias), title, type, path, text, retriever_weight, chunk_profile
|
|
||||||
- optional: tags (aus Note-FM), chunk_num, Chunk_Nummer (Aliases von index/ord)
|
|
||||||
"""
|
"""
|
||||||
n = _as_dict(note)
|
# ---- Note-Kontext ----
|
||||||
|
n = note if isinstance(note, dict) else {"frontmatter": {}}
|
||||||
fm = n.get("frontmatter") or {}
|
fm = n.get("frontmatter") or {}
|
||||||
note_type = str(fm.get("type") or n.get("type") or "note")
|
note_type = str(fm.get("type") or n.get("type") or "note")
|
||||||
types_cfg = _load_types_config(types_cfg)
|
types_cfg = types_cfg or {}
|
||||||
|
|
||||||
cfg_for_type = types_cfg.get(note_type, {}) if isinstance(types_cfg, dict) else {}
|
cfg_for_type = types_cfg.get(note_type, {}) if isinstance(types_cfg, dict) else {}
|
||||||
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
|
|
||||||
|
|
||||||
|
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
|
||||||
retriever_weight = _coalesce(fm.get("retriever_weight"), cfg_for_type.get("retriever_weight"), default_rw)
|
retriever_weight = _coalesce(fm.get("retriever_weight"), cfg_for_type.get("retriever_weight"), default_rw)
|
||||||
try:
|
try:
|
||||||
retriever_weight = float(retriever_weight)
|
retriever_weight = float(retriever_weight)
|
||||||
|
|
@ -101,52 +103,80 @@ def make_chunk_payloads(
|
||||||
|
|
||||||
note_id = n.get("note_id") or n.get("id") or fm.get("id")
|
note_id = n.get("note_id") or n.get("id") or fm.get("id")
|
||||||
title = n.get("title") or fm.get("title") or ""
|
title = n.get("title") or fm.get("title") or ""
|
||||||
# Pfad-Auflösung: Priorität file_path > note['path'] > path_arg
|
|
||||||
|
# Pfadauflösung: file_path > note['path'] > path_arg
|
||||||
path = file_path or n.get("path") or path_arg
|
path = file_path or n.get("path") or path_arg
|
||||||
if isinstance(path, pathlib.Path):
|
if isinstance(path, pathlib.Path):
|
||||||
path = str(path)
|
path = str(path)
|
||||||
path = path or "" # garantiert vorhanden
|
path = path or ""
|
||||||
|
|
||||||
# Denormalisierte Tags (optional): auf Chunks spiegeln, wenn vorhanden
|
# Tags denormalisieren (optional)
|
||||||
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
||||||
tags_list = _ensure_list(tags) if tags else []
|
tags_list = _ensure_list(tags) if tags else []
|
||||||
|
|
||||||
# Quelltext
|
# ---- Chunks besorgen ----
|
||||||
fulltext = note_text if isinstance(note_text, str) else _text_from_note(n)
|
fulltext = note_text if isinstance(note_text, str) else _text_from_note(n)
|
||||||
|
raw_chunks = chunks_from_chunker if isinstance(chunks_from_chunker, list) else _iter_chunks(n, chunk_profile, fulltext)
|
||||||
# Chunks besorgen
|
|
||||||
chunks = chunks_from_chunker if isinstance(chunks_from_chunker, list) else _iter_chunks(n, chunk_profile, fulltext)
|
|
||||||
|
|
||||||
payloads: List[Dict[str, Any]] = []
|
payloads: List[Dict[str, Any]] = []
|
||||||
for c in chunks:
|
for c in raw_chunks:
|
||||||
idx = c.get("index", len(payloads))
|
cdict = c if isinstance(c, dict) else _as_dict(c)
|
||||||
text = c.get("text") if isinstance(c, dict) else (str(c) if c is not None else "")
|
# Index/Basisdaten robust ermitteln
|
||||||
text = text if isinstance(text, str) else str(text or "")
|
idx = _coalesce(cdict.get("index"), cdict.get("ord"), cdict.get("chunk_index"), len(payloads))
|
||||||
|
try:
|
||||||
|
idx = int(idx)
|
||||||
|
except Exception:
|
||||||
|
idx = len(payloads)
|
||||||
|
|
||||||
# deterministische ID (unter Beibehaltung deines bisherigen Schemas)
|
text = _coalesce(cdict.get("window"), cdict.get("text"), "")
|
||||||
|
if not isinstance(text, str):
|
||||||
|
text = str(text or "")
|
||||||
|
|
||||||
|
# deterministische ID (kompatibel & stabil)
|
||||||
key = f"{note_id}|{idx}"
|
key = f"{note_id}|{idx}"
|
||||||
h = hashlib.sha1(key.encode("utf-8")).hexdigest()[:12] if note_id else hashlib.sha1(f"{path}|{idx}".encode("utf-8")).hexdigest()[:12]
|
h = hashlib.sha1(key.encode("utf-8")).hexdigest()[:12] if note_id else hashlib.sha1(f"{path}|{idx}".encode("utf-8")).hexdigest()[:12]
|
||||||
chunk_id = f"{note_id}-{idx:03d}-{h}" if note_id else f"{h}"
|
chunk_id = cdict.get("chunk_id") or cdict.get("id") or (f"{note_id}-{idx:03d}-{h}" if note_id else h)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"note_id": note_id,
|
# v1 Kernfelder (+Erweiterungen)
|
||||||
"chunk_id": chunk_id,
|
"id": chunk_id, # <— WICHTIG: v1 edges.py erwartet 'id'
|
||||||
|
"chunk_id": chunk_id, # v2-Alias
|
||||||
"index": idx,
|
"index": idx,
|
||||||
"ord": idx, # Alias für v2‑Schema
|
"ord": idx, # v2-Alias
|
||||||
"chunk_num": idx, # neutraler Alias
|
"chunk_num": idx,
|
||||||
"Chunk_Nummer": idx, # deutschsprachiger Alias
|
"Chunk_Nummer": idx,
|
||||||
"title": title,
|
"note_id": note_id,
|
||||||
"type": note_type,
|
"type": note_type,
|
||||||
"path": path, # garantiert vorhanden
|
"title": title,
|
||||||
"text": text, # nie leer
|
"path": path,
|
||||||
|
"text": text,
|
||||||
|
"window": text, # falls der Chunker bereits ein Fenster liefert, bleibt es identisch
|
||||||
"retriever_weight": retriever_weight,
|
"retriever_weight": retriever_weight,
|
||||||
"chunk_profile": chunk_profile,
|
"chunk_profile": chunk_profile,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Bestehende neighbors vom Chunk übernehmen (falls vorhanden)
|
||||||
|
nb = cdict.get("neighbors")
|
||||||
|
if isinstance(nb, dict):
|
||||||
|
prev_id = nb.get("prev"); next_id = nb.get("next")
|
||||||
|
payload["neighbors"] = {"prev": prev_id, "next": next_id}
|
||||||
|
# Tags spiegeln
|
||||||
if tags_list:
|
if tags_list:
|
||||||
payload["tags"] = tags_list
|
payload["tags"] = tags_list
|
||||||
|
|
||||||
# JSON‑Roundtrip als einfache Validierung
|
# JSON-Roundtrip als Validierung
|
||||||
json.loads(json.dumps(payload, ensure_ascii=False))
|
json.loads(json.dumps(payload, ensure_ascii=False))
|
||||||
payloads.append(payload)
|
payloads.append(payload)
|
||||||
|
|
||||||
|
# Nachgelagert: neighbors berechnen, falls fehlend
|
||||||
|
for i, p in enumerate(payloads):
|
||||||
|
nb = p.get("neighbors") or {}
|
||||||
|
prev_id = nb.get("prev")
|
||||||
|
next_id = nb.get("next")
|
||||||
|
if prev_id is None and i > 0:
|
||||||
|
prev_id = payloads[i-1]["id"]
|
||||||
|
if next_id is None and i+1 < len(payloads):
|
||||||
|
next_id = payloads[i+1]["id"]
|
||||||
|
p["neighbors"] = {"prev": prev_id, "next": next_id}
|
||||||
|
|
||||||
return payloads
|
return payloads
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user