Dateien nach "app/core" hochladen
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s

This commit is contained in:
Lars 2025-11-16 21:20:10 +01:00
parent a97f757e34
commit 22d08afe2d
2 changed files with 191 additions and 145 deletions

View File

@ -1,12 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
app/core/chunk_payload.py (Mindnet V2 robust v2) app/core/chunk_payload.py (Mindnet V2 types.yaml authoritative)
- neighbors_prev / neighbors_next sind Listen ([], [id]).
Änderungen ggü. v1: - retriever_weight / chunk_profile kommen aus types.yaml (Frontmatter wird ignoriert).
- neighbors_prev / neighbors_next werden als **Array** persistiert ([], [id]). - Fallbacks: defaults.* in types.yaml; sonst 1.0 / "default".
- retriever_weight / chunk_profile werden je Chunk aufgelöst (Frontmatter > types.yaml > Defaults).
- Lädt config/types.yaml selbst, wenn types_cfg nicht übergeben wurde.
""" """
from __future__ import annotations from __future__ import annotations
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
@ -16,21 +14,7 @@ def _env(n: str, d: Optional[str]=None) -> str:
v = os.getenv(n) v = os.getenv(n)
return v if v is not None else (d or "") return v if v is not None else (d or "")
def _deep_get(root: Any, path: str) -> Any: def _load_types() -> dict:
cur = root
for key in path.split("."):
if not isinstance(cur, dict) or key not in cur:
return None
cur = cur[key]
return cur
def _as_float(x: Any):
try:
return float(x)
except Exception:
return None
def _load_types_local() -> dict:
p = _env("MINDNET_TYPES_FILE", "./config/types.yaml") p = _env("MINDNET_TYPES_FILE", "./config/types.yaml")
try: try:
with open(p, "r", encoding="utf-8") as f: with open(p, "r", encoding="utf-8") as f:
@ -38,38 +22,46 @@ def _load_types_local() -> dict:
except Exception: except Exception:
return {} return {}
def _effective_chunk_profile(note_type: str, fm: Dict[str, Any], reg: dict) -> Optional[str]: def _get_types_map(reg: dict) -> dict:
if isinstance(fm.get("chunk_profile"), str): if isinstance(reg, dict) and isinstance(reg.get("types"), dict):
return fm.get("chunk_profile") return reg["types"]
types = reg.get("types") if isinstance(reg.get("types"), dict) else reg return reg if isinstance(reg, dict) else {}
if isinstance(types, dict):
v = types.get(note_type, {})
if isinstance(v, dict):
cp = v.get("chunk_profile")
if isinstance(cp, str):
return cp
return None
def _effective_retriever_weight(note_type: str, fm: Dict[str, Any], reg: dict) -> float: def _get_defaults(reg: dict) -> dict:
if fm.get("retriever_weight") is not None: if isinstance(reg, dict) and isinstance(reg.get("defaults"), dict):
v = _as_float(fm.get("retriever_weight")) return reg["defaults"]
if v is not None: if isinstance(reg, dict) and isinstance(reg.get("global"), dict):
return float(v) return reg["global"]
types = reg.get("types") if isinstance(reg.get("types"), dict) else reg return {}
candidates = [
f"{note_type}.retriever_weight", def _as_float(x: Any):
f"{note_type}.retriever.weight", try:
f"{note_type}.retrieval.weight", return float(x)
"defaults.retriever_weight", except Exception:
"defaults.retriever.weight", return None
"global.retriever_weight",
"global.retriever.weight", def _resolve_chunk_profile(note_type: str, reg: dict) -> str:
] types = _get_types_map(reg)
for path in candidates: if isinstance(types, dict):
val = _deep_get(types, path) if "." in path else (types.get(path) if isinstance(types, dict) else None) t = types.get(note_type, {})
if val is None and isinstance(reg, dict): if isinstance(t, dict) and isinstance(t.get("chunk_profile"), str):
val = _deep_get(reg, f"types.{path}") return t["chunk_profile"]
v = _as_float(val) defs = _get_defaults(reg)
if isinstance(defs, dict) and isinstance(defs.get("chunk_profile"), str):
return defs["chunk_profile"]
return "default"
def _resolve_retriever_weight(note_type: str, reg: dict) -> float:
types = _get_types_map(reg)
if isinstance(types, dict):
t = types.get(note_type, {})
if isinstance(t, dict) and (t.get("retriever_weight") is not None):
v = _as_float(t.get("retriever_weight"))
if v is not None:
return float(v)
defs = _get_defaults(reg)
if isinstance(defs, dict) and (defs.get("retriever_weight") is not None):
v = _as_float(defs.get("retriever_weight"))
if v is not None: if v is not None:
return float(v) return float(v)
return 1.0 return 1.0
@ -90,10 +82,11 @@ def make_chunk_payloads(note: Dict[str, Any],
file_path: Optional[str] = None) -> List[Dict[str, Any]]: file_path: Optional[str] = None) -> List[Dict[str, Any]]:
fm = (note or {}).get("frontmatter", {}) or {} fm = (note or {}).get("frontmatter", {}) or {}
note_type = fm.get("type") or note.get("type") or "concept" note_type = fm.get("type") or note.get("type") or "concept"
reg = types_cfg if isinstance(types_cfg, dict) else _load_types_local() reg = types_cfg if isinstance(types_cfg, dict) else _load_types()
cp = _effective_chunk_profile(note_type, fm, reg) # types.yaml authoritative
rw = _effective_retriever_weight(note_type, fm, reg) cp = _resolve_chunk_profile(note_type, reg)
rw = _resolve_retriever_weight(note_type, reg)
tags = fm.get("tags") or [] tags = fm.get("tags") or []
if isinstance(tags, str): if isinstance(tags, str):
@ -125,11 +118,9 @@ def make_chunk_payloads(note: Dict[str, Any],
"path": note_path, "path": note_path,
"source_path": file_path or note_path, "source_path": file_path or note_path,
"retriever_weight": float(rw), "retriever_weight": float(rw),
"chunk_profile": cp,
} }
if cp is not None: # Aufräumen von Alt-Feldern
pl["chunk_profile"] = cp
# Aufräumen
for alias in ("chunk_num", "Chunk_Number"): for alias in ("chunk_num", "Chunk_Number"):
pl.pop(alias, None) pl.pop(alias, None)

View File

@ -1,104 +1,159 @@
# note_payload.py #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
app/core/note_payload.py (Mindnet V2 types.yaml authoritative)
- retriever_weight und chunk_profile werden aus config/types.yaml gelesen.
- Reihenfolge: types.<type> > defaults.* > Fallbacks (1.0 / "default").
- Frontmatter-Overrides für diese beiden Felder werden bewusst IGNORIERT.
- edge_defaults (falls in types.yaml definiert) werden in die Note-Payload gespiegelt.
- MINDNET_TYPES_FILE kann absoluten Pfad liefern, sonst ./config/types.yaml.
"""
from __future__ import annotations from __future__ import annotations
from typing import Any, Dict, Optional, Tuple from typing import Any, Dict, Optional, List
import os, json, pathlib, yaml import os, yaml, hashlib, datetime
def _as_dict(note: Any) -> Dict[str, Any]: # ----------------------- helpers -----------------------
if isinstance(note, dict):
return note
d: Dict[str, Any] = {}
for attr in ("id","note_id","title","path","frontmatter","meta","metadata","type","created","modified","date","tags"):
if hasattr(note, attr):
d[attr] = getattr(note, attr)
# Normalisiere Frontmatter
fm = d.get("frontmatter") or d.get("meta") or d.get("metadata") or {}
d["frontmatter"] = fm if isinstance(fm, dict) else {}
return d
def _pick_args(*args, **kwargs) -> Tuple[Optional[str], Optional[Dict[str,Any]]]: def _env(n: str, d: Optional[str]=None) -> str:
path = kwargs.get("path") v = os.getenv(n)
types_cfg = kwargs.get("types_config") return v if v is not None else (d or "")
# legacy positional: (path, types_config) oder (types_config, ...)
for a in args:
if path is None and isinstance(a, (str, pathlib.Path)):
path = str(a)
if types_cfg is None and isinstance(a, dict):
types_cfg = a
return path, types_cfg
def _load_types_config(explicit: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: def _load_types() -> dict:
if isinstance(explicit, dict): p = _env("MINDNET_TYPES_FILE", "./config/types.yaml")
return explicit try:
for rel in ("config/config.yaml", "config/types.yaml"): with open(p, "r", encoding="utf-8") as f:
p = pathlib.Path(rel) return yaml.safe_load(f) or {}
if p.exists(): except Exception:
with p.open("r", encoding="utf-8") as f: return {}
data = yaml.safe_load(f) or {}
if isinstance(data, dict) and "types" in data and isinstance(data["types"], dict): def _get_types_map(reg: dict) -> dict:
return data["types"] if isinstance(reg, dict) and isinstance(reg.get("types"), dict):
return data if isinstance(data, dict) else {} return reg["types"]
return reg if isinstance(reg, dict) else {}
def _get_defaults(reg: dict) -> dict:
if isinstance(reg, dict) and isinstance(reg.get("defaults"), dict):
return reg["defaults"]
# alias "global" erlaubt
if isinstance(reg, dict) and isinstance(reg.get("global"), dict):
return reg["global"]
return {} return {}
def _coalesce(*vals): def _resolve_chunk_profile(note_type: str, reg: dict) -> str:
for v in vals: types = _get_types_map(reg)
if isinstance(types, dict):
t = types.get(note_type, {})
if isinstance(t, dict) and isinstance(t.get("chunk_profile"), str):
return t["chunk_profile"]
defs = _get_defaults(reg)
if isinstance(defs, dict) and isinstance(defs.get("chunk_profile"), str):
return defs["chunk_profile"]
return "default"
def _as_float(x: Any) -> Optional[float]:
try:
return float(x)
except Exception:
return None
def _resolve_retriever_weight(note_type: str, reg: dict) -> float:
types = _get_types_map(reg)
if isinstance(types, dict):
t = types.get(note_type, {})
if isinstance(t, dict) and (t.get("retriever_weight") is not None):
v = _as_float(t.get("retriever_weight"))
if v is not None:
return float(v)
defs = _get_defaults(reg)
if isinstance(defs, dict) and (defs.get("retriever_weight") is not None):
v = _as_float(defs.get("retriever_weight"))
if v is not None: if v is not None:
return v return float(v)
return None return 1.0
def _env_float(name: str, default: float) -> float: # ------------------- public API -------------------
try:
return float(os.environ.get(name, default))
except Exception:
return default
def _ensure_list(x) -> list: def make_note_payload(parsed_note: Any,
if x is None: return [] *,
if isinstance(x, list): return [str(i) for i in x] vault_root: str,
if isinstance(x, (set, tuple)): return [str(i) for i in x] hash_mode: str = "body",
return [str(x)] hash_normalize: str = "canonical",
hash_source: str = "parsed",
file_path: Optional[str] = None) -> Dict[str, Any]:
"""
Erwartet ein Parsed-Objekt mit Attributen:
- frontmatter (dict)
- body (str)
- path (optional)
Liefert Note-Payload mit deterministischer note_id und Typ-Werten aus types.yaml.
"""
fm = (getattr(parsed_note, "frontmatter", None) or {}) if parsed_note else {}
title = fm.get("title") or ""
note_type = fm.get("type") or "concept"
note_id = fm.get("id") or _stable_id_from_path_or_title(file_path or "", title)
tags = fm.get("tags") or []
if isinstance(tags, str):
tags = [tags]
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]: # types.yaml authoritative:
n = _as_dict(note) reg = _load_types()
path_arg, types_cfg_explicit = _pick_args(*args, **kwargs) chunk_profile = _resolve_chunk_profile(note_type, reg)
types_cfg = _load_types_config(types_cfg_explicit) retriever_weight = _resolve_retriever_weight(note_type, reg)
# edge_defaults (falls vorhanden)
edge_defaults = None
types = _get_types_map(reg)
if isinstance(types, dict):
t = types.get(note_type, {})
if isinstance(t, dict) and isinstance(t.get("edge_defaults"), list):
edge_defaults = t["edge_defaults"]
fm = n.get("frontmatter") or {} updated = _ts_to_int(fm.get("updated"))
note_type = str(fm.get("type") or n.get("type") or "note")
cfg_for_type = types_cfg.get(note_type, {}) if isinstance(types_cfg, dict) else {}
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0) payload: Dict[str, Any] = {
retriever_weight = _coalesce(fm.get("retriever_weight"), cfg_for_type.get("retriever_weight"), default_rw)
try:
retriever_weight = float(retriever_weight)
except Exception:
retriever_weight = default_rw
chunk_profile = _coalesce(fm.get("chunk_profile"), cfg_for_type.get("chunk_profile"), os.environ.get("MINDNET_DEFAULT_CHUNK_PROFILE","medium"))
chunk_profile = chunk_profile if isinstance(chunk_profile, str) else "medium"
edge_defaults = _ensure_list(_coalesce(fm.get("edge_defaults"), cfg_for_type.get("edge_defaults"), []))
note_id = n.get("note_id") or n.get("id") or fm.get("id")
title = n.get("title") or fm.get("title") or ""
path = n.get("path") or path_arg
if isinstance(path, pathlib.Path):
path = str(path)
payload = {
"note_id": note_id, "note_id": note_id,
"title": title,
"type": note_type, "type": note_type,
"path": path or "", # immer vorhanden "title": title,
"retriever_weight": retriever_weight, "tags": tags,
"updated": updated if updated is not None else 0,
"path": file_path or "",
"chunk_profile": chunk_profile, "chunk_profile": chunk_profile,
"edge_defaults": edge_defaults, "retriever_weight": float(retriever_weight),
} }
if edge_defaults is not None:
payload["edge_defaults"] = edge_defaults
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
if tags: payload["tags"] = _ensure_list(tags)
for k in ("created","modified","date"):
v = fm.get(k) or n.get(k)
if v: payload[k] = str(v)
json.loads(json.dumps(payload, ensure_ascii=False))
return payload return payload
# ------------------- internal utilities -------------------
def _stable_id_from_path_or_title(path: str, title: str) -> str:
base = path or title or ""
if not base:
base = "note"
h = hashlib.sha1(base.encode("utf-8")).hexdigest()[:6]
# title-sourced; in V2 typischerweise durch Frontmatter id ersetzt
return f"auto-{h}"
def _ts_to_int(val: Any) -> Optional[int]:
# akzeptiert YYYY-MM-DD oder epoch int; None bei Fehler
if val is None:
return None
if isinstance(val, int):
return val
if isinstance(val, float):
return int(val)
if isinstance(val, str):
val = val.strip()
# YYYY-MM-DD
try:
dt = datetime.date.fromisoformat(val)
return int(datetime.datetime(dt.year, dt.month, dt.day).timestamp())
except Exception:
pass
# epoch string
try:
return int(val)
except Exception:
return None
return None