app/core/derive_edges.py aktualisiert
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s
All checks were successful
Deploy mindnet to llm-node / deploy (push) Successful in 3s
This commit is contained in:
parent
6e0e965883
commit
6ea452cc3f
|
|
@ -2,53 +2,39 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Modul: app/core/derive_edges.py
|
Modul: app/core/derive_edges.py
|
||||||
Version: 1.3.0
|
Version: 1.4.0
|
||||||
Datum: 2025-09-30
|
Datum: 2025-10-01
|
||||||
|
|
||||||
Zweck
|
Zweck
|
||||||
-----
|
-----
|
||||||
Robuste Kantenbildung für mindnet (Notes/Chunks):
|
Robuste Kantenbildung für mindnet (Notes/Chunks):
|
||||||
- belongs_to (chunk -> note)
|
- belongs_to (chunk -> note)
|
||||||
- next / prev (chunk-Kette)
|
- next / prev (chunk-Kette)
|
||||||
- references (chunk-scope) aus Chunk.window (Fallback text/content/raw)
|
- references (chunk-scope) aus Chunk.window/text
|
||||||
- optional references (note-scope) dedupliziert
|
- optional references/backlink (note-scope)
|
||||||
- optional backlink (note-scope) als Gegenkante
|
|
||||||
|
|
||||||
Designhinweise
|
Wichtig: Wikilinks werden mit der Parser-Funktion `extract_wikilinks` extrahiert,
|
||||||
--------------
|
damit Varianten wie [[id#anchor]] oder [[id|label]] korrekt auf 'id' reduziert werden.
|
||||||
- Für die Referenz-Extraktion wird bewusst das Feld **window** verwendet (nicht 'text'),
|
|
||||||
damit Links, die an einer Overlap-Grenze liegen, nicht verloren gehen.
|
|
||||||
- IDs werden später deterministisch in app/core/qdrant_points.py aus Payload erzeugt.
|
|
||||||
- 'status' setzen wir nicht hart; ein separater Resolver kann 'unresolved' o.ä. bestimmen.
|
|
||||||
|
|
||||||
Erwartete Chunk-Payload-Felder (pro Element):
|
Erwartete Chunk-Payload-Felder:
|
||||||
{
|
{
|
||||||
"note_id": "...",
|
"note_id": "...",
|
||||||
"chunk_id": "...", # Alias "id" sollte ebenfalls vorhanden sein (abwärtskompatibel)
|
"chunk_id": "...", # Alias "id" ist zulässig
|
||||||
"id": "...",
|
"id": "...",
|
||||||
"chunk_index": int,
|
"chunk_index": int,
|
||||||
"seq": int,
|
"seq": int,
|
||||||
"window": str,
|
"window": str,
|
||||||
"text": str,
|
"text": str,
|
||||||
"path": "rel/path.md",
|
"path": "rel/path.md",
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
API
|
|
||||||
---
|
|
||||||
def build_edges_for_note(
|
|
||||||
note_id: str,
|
|
||||||
chunks: List[dict],
|
|
||||||
note_level_references: List[str] | None = None,
|
|
||||||
include_note_scope_refs: bool = False,
|
|
||||||
) -> List[dict]
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import re
|
from typing import Dict, List, Optional, Iterable
|
||||||
from typing import Dict, List, Optional, Iterable, Tuple
|
|
||||||
|
|
||||||
_WIKILINK_RE = re.compile(r"\[\[([^\]]+)\]\]")
|
# WICHTIG: benutze die Parser-Extraktion für saubere Wikilinks
|
||||||
|
from app.core.parser import extract_wikilinks
|
||||||
|
|
||||||
def _get(d: dict, *keys, default=None):
|
def _get(d: dict, *keys, default=None):
|
||||||
for k in keys:
|
for k in keys:
|
||||||
|
|
@ -66,19 +52,6 @@ def _chunk_text_for_refs(chunk: dict) -> str:
|
||||||
or ""
|
or ""
|
||||||
)
|
)
|
||||||
|
|
||||||
def _extract_wikilinks(text: str) -> List[str]:
|
|
||||||
if not text:
|
|
||||||
return []
|
|
||||||
out: List[str] = []
|
|
||||||
for m in _WIKILINK_RE.finditer(text):
|
|
||||||
label = m.group(1).strip()
|
|
||||||
if not label:
|
|
||||||
continue
|
|
||||||
# Einfach-Normalisierung: Leerraum trimmen; weitere Normalisierung (Slugging)
|
|
||||||
# kann upstream erfolgen (Parser oder Resolver).
|
|
||||||
out.append(label)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def _dedupe(seq: Iterable[str]) -> List[str]:
|
def _dedupe(seq: Iterable[str]) -> List[str]:
|
||||||
seen = set()
|
seen = set()
|
||||||
out: List[str] = []
|
out: List[str] = []
|
||||||
|
|
@ -94,7 +67,7 @@ def _edge(kind: str, scope: str, source_id: str, target_id: str, note_id: str, e
|
||||||
"scope": scope, # "chunk" | "note"
|
"scope": scope, # "chunk" | "note"
|
||||||
"source_id": source_id,
|
"source_id": source_id,
|
||||||
"target_id": target_id,
|
"target_id": target_id,
|
||||||
"note_id": note_id, # Quelle/Träger der Kante (die aktuelle Note)
|
"note_id": note_id, # Träger/Quelle der Kante (aktuelle Note)
|
||||||
}
|
}
|
||||||
if extra:
|
if extra:
|
||||||
pl.update(extra)
|
pl.update(extra)
|
||||||
|
|
@ -113,24 +86,19 @@ def build_edges_for_note(
|
||||||
- next / prev: zwischen aufeinanderfolgenden Chunks
|
- next / prev: zwischen aufeinanderfolgenden Chunks
|
||||||
- references: pro Chunk aus window/text
|
- references: pro Chunk aus window/text
|
||||||
- optional note-scope references/backlinks: dedupliziert über alle Chunk-Funde + note_level_references
|
- optional note-scope references/backlinks: dedupliziert über alle Chunk-Funde + note_level_references
|
||||||
|
|
||||||
Rückgabe: Liste von Edge-Payloads (ohne 'id'; Qdrant-ID wird deterministisch aus Payload erzeugt)
|
|
||||||
"""
|
"""
|
||||||
edges: List[dict] = []
|
edges: List[dict] = []
|
||||||
|
|
||||||
# --- Strukturkanten ---
|
|
||||||
# belongs_to
|
# belongs_to
|
||||||
for ch in chunks:
|
for ch in chunks:
|
||||||
cid = _get(ch, "chunk_id", "id")
|
cid = _get(ch, "chunk_id", "id")
|
||||||
if not cid:
|
if not cid:
|
||||||
# defensiv: überspringen statt Crash
|
|
||||||
continue
|
continue
|
||||||
edges.append(_edge("belongs_to", "chunk", cid, note_id, note_id, {"chunk_id": cid}))
|
edges.append(_edge("belongs_to", "chunk", cid, note_id, note_id, {"chunk_id": cid}))
|
||||||
|
|
||||||
# next/prev
|
# next/prev
|
||||||
for i in range(len(chunks) - 1):
|
for i in range(len(chunks) - 1):
|
||||||
a = chunks[i]
|
a, b = chunks[i], chunks[i + 1]
|
||||||
b = chunks[i + 1]
|
|
||||||
a_id = _get(a, "chunk_id", "id")
|
a_id = _get(a, "chunk_id", "id")
|
||||||
b_id = _get(b, "chunk_id", "id")
|
b_id = _get(b, "chunk_id", "id")
|
||||||
if not a_id or not b_id:
|
if not a_id or not b_id:
|
||||||
|
|
@ -138,32 +106,26 @@ def build_edges_for_note(
|
||||||
edges.append(_edge("next", "chunk", a_id, b_id, note_id, {"chunk_id": a_id}))
|
edges.append(_edge("next", "chunk", a_id, b_id, note_id, {"chunk_id": a_id}))
|
||||||
edges.append(_edge("prev", "chunk", b_id, a_id, note_id, {"chunk_id": b_id}))
|
edges.append(_edge("prev", "chunk", b_id, a_id, note_id, {"chunk_id": b_id}))
|
||||||
|
|
||||||
# --- Referenzkanten (chunk-scope) ---
|
# references (chunk-scope) – Links aus window bevorzugen (Overlap-fest)
|
||||||
refs_all: List[str] = []
|
refs_all: List[str] = []
|
||||||
for ch in chunks:
|
for ch in chunks:
|
||||||
cid = _get(ch, "chunk_id", "id")
|
cid = _get(ch, "chunk_id", "id")
|
||||||
if not cid:
|
if not cid:
|
||||||
continue
|
continue
|
||||||
txt = _chunk_text_for_refs(ch)
|
txt = _chunk_text_for_refs(ch)
|
||||||
refs = _extract_wikilinks(txt)
|
refs = extract_wikilinks(txt) # <— Parser-Logik, kompatibel zu deinem System
|
||||||
if not refs:
|
|
||||||
continue
|
|
||||||
for r in refs:
|
for r in refs:
|
||||||
edges.append(_edge("references", "chunk", cid, r, note_id, {"chunk_id": cid, "ref_text": r}))
|
edges.append(_edge("references", "chunk", cid, r, note_id, {"chunk_id": cid, "ref_text": r}))
|
||||||
refs_all.extend(refs)
|
refs_all.extend(refs)
|
||||||
|
|
||||||
# --- Note-scope (optional) ---
|
# optional: note-scope references/backlinks
|
||||||
if include_note_scope_refs:
|
if include_note_scope_refs:
|
||||||
# Inputs: dedup aller Chunk-Funde + optional vorhandene Note-Level-Refs aus Payload
|
|
||||||
refs_note = refs_all[:]
|
refs_note = refs_all[:]
|
||||||
if note_level_references:
|
if note_level_references:
|
||||||
refs_note.extend([r for r in note_level_references if isinstance(r, str) and r])
|
refs_note.extend([r for r in note_level_references if isinstance(r, str) and r])
|
||||||
refs_note = _dedupe(refs_note)
|
refs_note = _dedupe(refs_note)
|
||||||
|
|
||||||
for r in refs_note:
|
for r in refs_note:
|
||||||
# forward
|
|
||||||
edges.append(_edge("references", "note", note_id, r, note_id))
|
edges.append(_edge("references", "note", note_id, r, note_id))
|
||||||
# backlink (reverse)
|
|
||||||
edges.append(_edge("backlink", "note", r, note_id, note_id))
|
edges.append(_edge("backlink", "note", r, note_id, note_id))
|
||||||
|
|
||||||
return edges
|
return edges
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user