mindnet/scripts/import_markdown.py
Lars 0a116ea3f4
Some checks failed
Deploy mindnet to llm-node / deploy (push) Failing after 1s
scripts/import_markdown.py aktualisiert
2025-09-08 18:36:59 +02:00

298 lines
12 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
import_markdown.py — Markdown → Qdrant (Notes, Chunks, Edges) mit Change-Detection & optionalem Purge
Version: 3.0 (2025-09-08)
Änderungen ggü. bisherigen Varianten:
- Stabiler Qdrant-Delete über Filter-API (kompatibel mit aktuellen Clients).
- Option --purge-before-upsert: löscht NUR Chunks/Edges der geänderten Note(n) vor Upsert.
- Change-Detection ausschließlich über Note-Body-Hash (Frontmatter/Dateidatum ignoriert).
- Robuste Edge-Erzeugung:
- Chunk-Level: belongs_to, prev/next, references (+backlink)
- Fallback: Note-Level references, falls Chunk-Payloads keine references liefern
- Korrekte Qdrant-Scroll/Query-Nutzung (Rückgabesignaturen neuer Clients).
- Saubere Dry-Run/Apply-Ausgaben je Note als JSON.
Aufruf:
python3 -m scripts.import_markdown --vault ./vault
python3 -m scripts.import_markdown --vault ./vault --apply
python3 -m scripts.import_markdown --vault ./vault --apply --purge-before-upsert
python3 -m scripts.import_markdown --vault ./vault --note-id 20250821-architektur-ki-trainerassistent-761cfe --apply
Parameter:
--vault PATH Pflicht. Root des (Obsidian-)Vaults.
--apply Wirkt schreibend in Qdrant; ohne Flag nur Dry-Run.
--purge-before-upsert Vor dem Upsert (nur) für geänderte Notes: zugehörige Chunks & Edges löschen.
--note-id ID Optional: Nur eine Note mit genau dieser ID verarbeiten.
--embed-note Optional: zusätzlich Volltext-Vektor für die Note erzeugen.
--force-replace Ignoriert Change-Detection (erzwingt Upsert + optionalen Purge).
Umgebungsvariablen (optional):
QDRANT_URL z.B. http://127.0.0.1:6333 (falls QDRANT_HOST/PORT nicht gesetzt)
QDRANT_API_KEY API Key (falls erforderlich)
QDRANT_HOST/QDRANT_PORT Alternative zu QDRANT_URL
COLLECTION_PREFIX Default "mindnet"
VECTOR_DIM Default 384
Voraussetzungen:
- app/core:
parser.read_markdown, parser.normalize_frontmatter, parser.validate_required_frontmatter
note_payload.make_note_payload (liefert u.a. hash_fulltext)
validate_note.validate_note_payload
chunker.assemble_chunks
chunk_payload.make_chunk_payloads (idealerweise inkl. 'references' je Chunk; wenn nicht → Fallback)
embed.embed_texts, embed.embed_one
qdrant.QdrantConfig, qdrant.get_client, qdrant.ensure_collections
qdrant_points.points_for_note / points_for_chunks / points_for_edges / upsert_batch
edges.deriv_edges_for_note (nutzt Chunk-Payloads; wir ergänzen Fallback)
"""
from __future__ import annotations
import argparse
import os
import sys
import json
import glob
from typing import List, Dict, Tuple, Optional
from dotenv import load_dotenv
from qdrant_client.http import models as rest
from app.core.parser import read_markdown, normalize_frontmatter, validate_required_frontmatter
from app.core.note_payload import make_note_payload
from app.core.validate_note import validate_note_payload
from app.core.chunker import assemble_chunks
from app.core.chunk_payload import make_chunk_payloads
from app.core.embed import embed_texts, embed_one
from app.core.qdrant import QdrantConfig, get_client, ensure_collections
from app.core.qdrant_points import (
points_for_chunks,
points_for_note,
points_for_edges,
upsert_batch,
)
from app.core.edges import deriv_edges_for_note
# -----------------------------
# Utils
# -----------------------------
def iter_md(root: str) -> List[str]:
files = [p for p in glob.glob(os.path.join(root, "**", "*.md"), recursive=True)]
out = []
for p in files:
pn = p.replace("\\", "/")
if any(ex in pn for ex in ["/.obsidian/", "/_backup_frontmatter/", "/_imported/"]):
continue
out.append(p)
return sorted(out)
def collections(prefix: str) -> Tuple[str, str, str]:
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
def fetch_existing_note_hash(client, prefix: str, note_id: str) -> Optional[str]:
notes_col, _, _ = collections(prefix)
f = rest.Filter(
must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))]
)
# neuere Clients: scroll gibt (points, next_page_offset) zurück
points, _ = client.scroll(
collection_name=notes_col,
scroll_filter=f,
with_payload=True,
with_vectors=False,
limit=1,
)
if not points:
return None
pl = points[0].payload or {}
return pl.get("hash_fulltext")
def purge_note_artifacts(client, prefix: str, note_id: str) -> None:
"""
Löscht NUR Chunks & Edges der angegebenen Note (vor Upsert).
- Chunks: payload.note_id == note_id
- Edges : payload.source_id startswith note_id#c ODER payload.target_id startswith note_id#c
plus note-scope edges, falls vorhanden (source_id == note_id oder target_id == note_id)
"""
_, chunks_col, edges_col = collections(prefix)
# Chunks der Note löschen
f_chunks = rest.Filter(
must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))]
)
client.delete(collection_name=chunks_col, points_selector=f_chunks, wait=True)
# Edges der Note löschen (Chunk- und Note-Scope)
should = [
rest.FieldCondition(key="source_id", match=rest.MatchText(text=f"{note_id}#")),
rest.FieldCondition(key="target_id", match=rest.MatchText(text=f"{note_id}#")),
rest.FieldCondition(key="source_id", match=rest.MatchValue(value=note_id)),
rest.FieldCondition(key="target_id", match=rest.MatchValue(value=note_id)),
]
f_edges = rest.Filter(should=should)
client.delete(collection_name=edges_col, points_selector=f_edges, wait=True)
def has_chunk_level_refs(chunk_payloads: List[Dict]) -> bool:
return any(isinstance(ch.get("references"), list) and ch["references"] for ch in chunk_payloads)
def fallback_note_level_edges(note_meta: Dict, chunk_payloads: List[Dict]) -> List[Dict]:
"""
Falls chunk_payloads keine 'references' enthalten, erzeugen wir
- belongs_to + prev/next aus chunk_payloads
- references/backlink auf Note-Scope aus note_meta['references'] (falls vorhanden)
"""
edges: List[Dict] = []
# belongs_to + prev/next (Chunk-Scope)
for ch in chunk_payloads:
src = ch["id"]
edges.append({"src_id": src, "dst_id": note_meta["id"], "edge_type": "belongs_to", "scope": "chunk"})
nb = ch.get("neighbors") or {}
prev_id = nb.get("prev")
next_id = nb.get("next")
if prev_id:
edges.append({"src_id": src, "dst_id": prev_id, "edge_type": "next", "scope": "chunk"})
edges.append({"src_id": prev_id, "dst_id": src, "edge_type": "prev", "scope": "chunk"})
if next_id:
edges.append({"src_id": src, "dst_id": next_id, "edge_type": "next", "scope": "chunk"})
edges.append({"src_id": next_id, "dst_id": src, "edge_type": "prev", "scope": "chunk"})
# Note-Scope references/backlink
for tid in (note_meta.get("references") or []):
edges.append({"src_id": note_meta["id"], "dst_id": tid, "edge_type": "references", "scope": "note"})
edges.append({"src_id": tid, "dst_id": note_meta["id"], "edge_type": "backlink", "scope": "note"})
# Dedupe
uniq = {}
for e in edges:
k = (e["src_id"], e["edge_type"], e["dst_id"], e.get("scope", "note"))
uniq[k] = e
return list(uniq.values())
# -----------------------------
# Main
# -----------------------------
def main():
load_dotenv()
ap = argparse.ArgumentParser()
ap.add_argument("--vault", required=True, help="Pfad zum Obsidian-Vault (Root-Ordner)")
ap.add_argument("--apply", action="store_true", help="Schreibt in Qdrant; ohne Flag nur Dry-Run")
ap.add_argument("--purge-before-upsert", action="store_true",
help="Vor Upsert Chunks & Edges der GEÄNDERTEN Note löschen")
ap.add_argument("--note-id", help="Nur eine bestimmte Note-ID verarbeiten")
ap.add_argument("--embed-note", action="store_true", help="Optional: Note-Volltext einbetten")
ap.add_argument("--force-replace", action="store_true",
help="Änderungserkennung ignorieren und immer upserten (+ optional Purge)")
args = ap.parse_args()
# Qdrant-Config
cfg = QdrantConfig.from_env()
client = get_client(cfg)
ensure_collections(client, cfg.prefix, cfg.dim)
notes_col, chunks_col, edges_col = collections(cfg.prefix)
root = os.path.abspath(args.vault)
files = iter_md(root)
if not files:
print("Keine Markdown-Dateien gefunden.", file=sys.stderr)
sys.exit(2)
processed = 0
for path in files:
parsed = read_markdown(path)
fm = normalize_frontmatter(parsed.frontmatter)
# Pflichtfelder?
try:
validate_required_frontmatter(fm)
except Exception:
# Überspringen, aber nicht abbrechen
continue
if args.note_id and fm.get("id") != args.note_id:
continue
processed += 1
# Note-Payload & Validierung
note_pl = make_note_payload(parsed, vault_root=root)
validate_note_payload(note_pl)
note_id = note_pl["note_id"]
# Change-Detection (Body-Hash)
new_hash = note_pl.get("hash_fulltext")
old_hash = None
if not args.force_replace:
old_hash = fetch_existing_note_hash(client, cfg.prefix, note_id)
changed = args.force_replace or (old_hash != new_hash)
# Chunks + Embeddings
chunks = assemble_chunks(fm["id"], parsed.body, fm.get("type", "concept"))
chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks)
vecs = embed_texts([c.text for c in chunks])
# Optional: Note-Vektor
note_vec = embed_one(parsed.body) if args.embed_note else None
# Edges
# 1) Versuch: volle Kanten aus Chunk-Payloads
edges = deriv_edges_for_note(fm, chunk_pls)
# 2) Fallback, falls keine Chunk-References vorhanden sind
if not edges or (not has_chunk_level_refs(chunk_pls) and (fm.get("references") or note_pl.get("references"))):
# Ergänze fm['references'] falls noch nicht im fm vorhanden
if "references" not in fm and "references" in note_pl:
fm["references"] = note_pl["references"]
edges = fallback_note_level_edges(fm, chunk_pls)
# Zusammenfassung für Log
summary = {
"note_id": note_id,
"title": fm.get("title"),
"chunks": len(chunk_pls),
"edges": len(edges),
"changed": changed,
"decision": ("apply" if args.apply and changed else
"apply-skip-unchanged" if args.apply and not changed else
"dry-run"),
"path": note_pl["path"],
}
print(json.dumps(summary, ensure_ascii=False))
# Apply?
if not args.apply:
continue
# Optionaler Purge NUR für geänderte Notes
if changed and args.purge_before_upsert:
purge_note_artifacts(client, cfg.prefix, note_id)
# Upserts
# Notes
notes_name, note_pts = points_for_note(cfg.prefix, note_pl, note_vec, cfg.dim)
upsert_batch(client, notes_name, note_pts)
# Chunks
chunks_name, chunk_pts = points_for_chunks(cfg.prefix, chunk_pls, vecs)
upsert_batch(client, chunks_name, chunk_pts)
# Edges
edges_name, edge_pts = points_for_edges(cfg.prefix, edges)
upsert_batch(client, edges_name, edge_pts)
print(f"Done. Processed notes: {processed}")
if __name__ == "__main__":
main()