Compare commits
211
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
347ca50e71 | ||
|
|
6a2ab522fc | ||
|
|
666719ca4c | ||
|
|
6cdad8b046 | ||
|
|
eae30775e3 | ||
|
|
10a3524b59 | ||
|
|
6c15b51e7a | ||
|
|
26fa7732e9 | ||
|
|
5016ed6996 | ||
|
|
c4af7606da | ||
|
|
ded97fc58c | ||
|
|
9d5b52a3c1 | ||
|
|
7f5cdeb4cb | ||
|
|
8f9f83aa79 | ||
|
|
7a225ba2b6 | ||
|
|
0d61a9e191 | ||
|
|
55d1a7e290 | ||
|
|
4537e65428 | ||
|
|
43327c1f6d | ||
|
|
39a6998123 | ||
|
|
273c4c6919 | ||
|
|
2ed4488cf6 | ||
|
|
36490425c5 | ||
|
|
b8cb8bb89b | ||
|
|
6d268d9dfb | ||
|
|
df5f9b3fe4 | ||
|
|
5e67cd470c | ||
|
|
0b2a1f1a63 | ||
|
|
d0012355b9 | ||
|
|
1056078e6a | ||
|
|
c42a76b3d7 | ||
|
|
ec9b3c68af | ||
|
|
f9118a36f8 | ||
|
|
e52eed40ca | ||
|
|
43641441ef | ||
|
|
c613d81846 | ||
|
|
de5db09b51 | ||
|
|
7cb8fd6602 | ||
|
|
6047e94964 | ||
|
|
78fbc9b31b | ||
|
|
742792770c | ||
|
|
b19f91c3ee | ||
|
|
9b0d8c18cb | ||
|
|
f2a2f4d2df | ||
|
|
ea0fd951f2 | ||
|
|
c8c828c8a8 | ||
|
|
716a063849 | ||
|
|
3dc81ade0f | ||
|
|
1df89205ac | ||
|
|
2445f7cb2b | ||
|
|
47fdcf8eed | ||
|
|
3e27c72b80 | ||
|
|
2d87f9d816 | ||
|
|
d7d6155203 | ||
|
|
f8506c0bb2 | ||
|
|
c91910ee9f | ||
|
|
ee91583614 | ||
|
|
3a17b646e1 | ||
|
|
727de50290 | ||
|
|
a780104b3c | ||
|
|
f51e1cb2c4 | ||
|
|
20fb1e92e2 | ||
|
|
1d66ca0649 | ||
|
|
55b64c331a | ||
|
|
4d43cc526e | ||
|
|
6131b315d7 | ||
|
|
dfff46e45c | ||
|
|
003a270548 | ||
|
|
39fd15b565 | ||
|
|
be2bed9927 | ||
|
|
2da98e8e37 | ||
|
|
a852975811 | ||
|
|
8fd7ef804d | ||
|
|
b0f4309a29 | ||
|
|
c33b1c644a | ||
|
|
7cc823e2f4 | ||
|
|
7e00344b84 | ||
|
|
ec89d83916 | ||
|
|
57656bbaaf | ||
|
|
7953acf3ee | ||
|
|
3f528f2184 | ||
|
|
29e334625e | ||
|
|
114cea80de | ||
|
|
981b0cba1f | ||
|
|
e2c40666d1 | ||
|
|
c9ae58725c | ||
|
|
4318395c83 | ||
|
|
00264a9653 | ||
|
|
7e4ea670b1 | ||
|
|
008a470f02 | ||
|
|
7ed82ad82e | ||
|
|
72cf71fa87 | ||
|
|
9cb08777fa | ||
|
|
2c18f8b3de | ||
|
|
d5d6987ce2 | ||
|
|
61a319a049 | ||
|
|
a392dc2786 | ||
|
|
5e2a074019 | ||
|
|
9b3fd7723e | ||
|
|
4802eba27b | ||
|
|
745352ff3f | ||
|
|
13f0a0c9bc | ||
|
|
4a404d74de | ||
|
|
8ed4efaadc | ||
|
|
d17c966301 | ||
|
|
548c503e7c | ||
|
|
277444ec0a | ||
|
|
62a00d1ac3 | ||
|
|
8505538b34 | ||
|
|
a9d0874fe9 | ||
|
|
1563ebbdf9 | ||
|
|
38fac89f73 | ||
|
|
7026fc4fed | ||
|
|
d41da670fc | ||
|
|
5541ceb13d | ||
|
|
ac26cc4940 | ||
|
|
9b906bbabf | ||
|
|
9a98093e70 | ||
|
|
de05784428 | ||
|
|
f62983b08f | ||
|
|
d0eae8e43c | ||
|
|
3d2f3d12d9 | ||
|
|
0d2469f8fa | ||
|
|
124849c580 | ||
|
|
ea38743a2a | ||
|
|
5ab01c5150 | ||
|
|
ed3f3e5588 | ||
|
|
bb6959a090 | ||
|
|
d49d509451 | ||
|
|
008167268f | ||
|
|
67d7154328 | ||
|
|
4de9a4f649 | ||
|
|
d35bdc64b9 | ||
|
|
3a768be488 | ||
|
|
1965c984f4 | ||
|
|
e3e1700de5 | ||
|
|
f05d766b64 | ||
|
|
58e414041a | ||
|
|
cd5056d4c9 | ||
|
|
39fb821481 | ||
|
|
beb87a8c43 | ||
|
|
4327fc939c | ||
|
|
ef1046c6f5 | ||
|
|
ef8cf719f2 | ||
|
|
6aa6b32a6c | ||
|
|
33dff04d47 | ||
|
|
65d697b7be | ||
|
|
06fc42ed37 | ||
|
|
3c5c567077 | ||
|
|
8f65e550c8 | ||
|
|
6b83879741 | ||
|
|
be265e9cc0 | ||
|
|
680c36ab59 | ||
|
|
96b4f65cd1 | ||
|
|
b1a897e51c | ||
|
|
e5a34efee9 | ||
|
|
f9ac4e4dbf | ||
|
|
1b40e29f40 | ||
|
|
7eba1fb487 | ||
|
|
838083b909 | ||
|
|
8f5eb36b5f | ||
|
|
b7d1bcce3d | ||
|
|
03d3173ca6 | ||
|
|
38a61d7b50 | ||
|
|
0a429e1f7b | ||
|
|
857ba953e3 | ||
|
|
e180018c99 | ||
|
|
ac9956bf00 | ||
|
|
62b5a8bf65 | ||
|
|
303efefcb7 | ||
|
|
feeb7c2d92 | ||
|
|
ea9a54421a | ||
|
|
fdf99b2bb0 | ||
|
|
c7cd641f89 | ||
|
|
18b90c8df3 | ||
|
|
8d3bc1c2e2 | ||
|
|
079d988034 | ||
|
|
aa9d388337 | ||
|
|
92bd3d9a47 | ||
|
|
53058d1504 | ||
|
|
3fe8463a03 | ||
|
|
5c4ce5d727 | ||
|
|
459193e7b1 | ||
|
|
98f21323fb | ||
|
|
515248d438 | ||
|
|
b0c69ad3e0 | ||
|
|
c5f29ab4ae | ||
|
|
876ee898d8 | ||
|
|
e93bab6ea7 | ||
|
|
5225090490 | ||
|
|
e9532e8878 | ||
|
|
23b1cb2966 | ||
|
|
fa909e2e7d | ||
|
|
7fa9ce81bd | ||
|
|
8490911958 | ||
|
|
19d899b277 | ||
|
|
37ec8b614e | ||
|
|
e045371969 | ||
|
|
cd5383432e | ||
|
|
8b8baa27b3 | ||
|
|
386fa3ef0c | ||
|
|
19c96fd00f | ||
|
|
ecb35fb869 | ||
|
|
21cda0072a | ||
|
|
e3858e8bc3 | ||
|
|
f08a331bc6 | ||
|
|
cfcaa926cd | ||
|
|
8ade34af0a | ||
|
|
a6d37c92d2 | ||
|
|
1b7b8091a3 | ||
|
|
94e5ebf577 |
No files matched your search
@@ -0,0 +1,23 @@
|
|||||||
|
# Allowlist: Nur was hier ausdrücklich freigegeben ist, gelangt in den Build-Kontext.
|
||||||
|
# Damit landen weder .env-Dateien, Vault-Inhalte, Logs noch lokale venvs im Image.
|
||||||
|
**
|
||||||
|
|
||||||
|
!app/
|
||||||
|
!config/
|
||||||
|
!scripts/
|
||||||
|
!tests/
|
||||||
|
!deploy/
|
||||||
|
# Nur für die statischen Prüfungen in der Test-Stage (nicht im Laufzeit-Image):
|
||||||
|
!.dockerignore
|
||||||
|
!.gitea/workflows/
|
||||||
|
|
||||||
|
# Innerhalb der freigegebenen Ordner wieder ausschließen
|
||||||
|
**/__pycache__/
|
||||||
|
**/*.pyc
|
||||||
|
**/.pytest_cache/
|
||||||
|
config/*.env
|
||||||
|
config/*.local.*
|
||||||
|
config/*_old*
|
||||||
|
config/* - Kopie*
|
||||||
|
deploy/env/*.env
|
||||||
|
tests/temp_*/
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# Shell-Skripte laufen im Container bzw. auf dem Linux-Runner.
|
||||||
|
*.sh text eol=lf
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# Mindnet DEV als Container: jeder Push auf develop baut und startet den Dev-Stack
|
||||||
|
# auf dem Runner-Host (Muster wie Kansho). Gleiche Konfiguration wie die systemd-
|
||||||
|
# Dev-Instanz (8002/8502), parallel auf 8012/8512. systemd und Prod bleiben unberührt.
|
||||||
|
# Ablauf und Abbruchbedingungen: deploy/deploy.sh. Prod-Gegenstück (noch inaktiv):
|
||||||
|
# deploy/gitea-workflows/deploy-prod.yml.
|
||||||
|
|
||||||
|
name: Deploy mindnet DEV (Container)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [develop]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: deploy-mindnet-dev
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
# Runner auf dem Zielhost (heute llm-node). Bei einem Umzug auf einen anderen
|
||||||
|
# Mini-PC nur das Label und die beiden Pfade anpassen.
|
||||||
|
runs-on: linux_host
|
||||||
|
env:
|
||||||
|
# Unveränderte Konfiguration der systemd-Dev-Instanz.
|
||||||
|
MINDNET_APP_ENV_FILE: /home/llmadmin/mindnet_dev/.env
|
||||||
|
# Nur Container-Angaben (Vorlage: deploy/env/dev.env.example).
|
||||||
|
MINDNET_ENV_FILE: /home/llmadmin/docker/mindnet-dev/container.env
|
||||||
|
steps:
|
||||||
|
# Ohne actions/checkout: kein Abruf von github.com (DNS/Internet auf dem Host
|
||||||
|
# nicht nötig). Holt genau den Commit des Pushes vom eigenen Gitea, mit Job-Token.
|
||||||
|
- name: Checkout
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
git init -q .
|
||||||
|
git -c http.extraHeader="Authorization: token ${{ github.token }}" \
|
||||||
|
fetch -q --depth 1 "${{ github.server_url }}/${{ github.repository }}.git" "${{ github.sha }}"
|
||||||
|
git checkout -q --force FETCH_HEAD
|
||||||
|
git clean -q -ffdx
|
||||||
|
git log -1 --format='Checkout %H %s'
|
||||||
|
|
||||||
|
- name: Container-Datei beim ersten Lauf anlegen
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
install -d -m 0750 "$(dirname "$MINDNET_ENV_FILE")"
|
||||||
|
if [ ! -f "$MINDNET_ENV_FILE" ]; then
|
||||||
|
install -m 0600 deploy/env/dev.env.example "$MINDNET_ENV_FILE"
|
||||||
|
echo "Container-Datei aus Vorlage angelegt: $MINDNET_ENV_FILE"
|
||||||
|
fi
|
||||||
|
docker version --format 'Docker {{.Server.Version}}'
|
||||||
|
docker compose version
|
||||||
|
|
||||||
|
- name: Test, Build, Preflight, Start, Smoke
|
||||||
|
run: bash deploy/deploy.sh dev
|
||||||
@@ -0,0 +1,237 @@
|
|||||||
|
# Analyse: Zugriffe auf config/types.yaml
|
||||||
|
|
||||||
|
## Zusammenfassung
|
||||||
|
|
||||||
|
Diese Analyse prüft, welche Scripte auf `config/types.yaml` zugreifen und ob sie auf Elemente zugreifen, die in der aktuellen `types.yaml` nicht mehr vorhanden sind.
|
||||||
|
|
||||||
|
**Datum:** 2025-01-XX
|
||||||
|
**Version types.yaml:** 2.7.0
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ❌ KRITISCHE PROBLEME
|
||||||
|
|
||||||
|
### 1. `edge_defaults` fehlt in types.yaml, wird aber im Code verwendet
|
||||||
|
|
||||||
|
**Status:** ⚠️ **PROBLEM** - Code sucht nach `edge_defaults` in types.yaml, aber dieses Feld existiert nicht mehr.
|
||||||
|
|
||||||
|
**Betroffene Dateien:**
|
||||||
|
|
||||||
|
#### a) `app/core/graph/graph_utils.py` (Zeilen 101-112)
|
||||||
|
```python
|
||||||
|
def get_edge_defaults_for(note_type: Optional[str], reg: dict) -> List[str]:
|
||||||
|
"""Ermittelt Standard-Kanten für einen Typ."""
|
||||||
|
types_map = reg.get("types", reg) if isinstance(reg, dict) else {}
|
||||||
|
if note_type and isinstance(types_map, dict):
|
||||||
|
t = types_map.get(note_type)
|
||||||
|
if isinstance(t, dict) and isinstance(t.get("edge_defaults"), list): # ❌ Sucht nach edge_defaults
|
||||||
|
return [str(x) for x in t["edge_defaults"] if isinstance(x, str)]
|
||||||
|
for key in ("defaults", "default", "global"):
|
||||||
|
v = reg.get(key)
|
||||||
|
if isinstance(v, dict) and isinstance(v.get("edge_defaults"), list): # ❌ Sucht nach edge_defaults
|
||||||
|
return [str(x) for x in v["edge_defaults"] if isinstance(x, str)]
|
||||||
|
return []
|
||||||
|
```
|
||||||
|
**Problem:** Funktion gibt immer `[]` zurück, da `edge_defaults` nicht in types.yaml existiert.
|
||||||
|
|
||||||
|
#### b) `app/core/graph/graph_derive_edges.py` (Zeile 64)
|
||||||
|
```python
|
||||||
|
defaults = get_edge_defaults_for(note_type, reg) # ❌ Wird verwendet, liefert aber []
|
||||||
|
```
|
||||||
|
**Problem:** Keine automatischen Default-Kanten werden mehr erzeugt.
|
||||||
|
|
||||||
|
#### c) `app/services/discovery.py` (Zeile 212)
|
||||||
|
```python
|
||||||
|
defaults = type_def.get("edge_defaults") # ❌ Sucht nach edge_defaults
|
||||||
|
return defaults[0] if defaults else "related_to"
|
||||||
|
```
|
||||||
|
**Problem:** Fallback funktioniert, aber nutzt nicht die neue dynamische Lösung.
|
||||||
|
|
||||||
|
#### d) `tests/check_types_registry_edges.py` (Zeile 170)
|
||||||
|
```python
|
||||||
|
eddefs = (tdef or {}).get("edge_defaults") or [] # ❌ Sucht nach edge_defaults
|
||||||
|
```
|
||||||
|
**Problem:** Test findet keine `edge_defaults` mehr und gibt Warnung aus.
|
||||||
|
|
||||||
|
**✅ Lösung bereits implementiert:**
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (WP-24c, Zeilen 124-134) nutzt bereits die neue dynamische Lösung über `edge_registry.get_topology_info()`.
|
||||||
|
|
||||||
|
**Empfehlung:**
|
||||||
|
- `get_edge_defaults_for()` in `graph_utils.py` sollte auf die EdgeRegistry umgestellt werden.
|
||||||
|
- `discovery.py` sollte ebenfalls die EdgeRegistry nutzen.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. Inkonsistenz: `chunk_profile` vs `chunking_profile`
|
||||||
|
|
||||||
|
**Status:** ⚠️ **WARNUNG** - Meistens abgefangen durch Fallback-Logik.
|
||||||
|
|
||||||
|
**Problem:**
|
||||||
|
- In `types.yaml` heißt es: `chunking_profile` ✅
|
||||||
|
- `app/core/type_registry.py` (Zeile 88) sucht nach: `chunk_profile` ❌
|
||||||
|
|
||||||
|
```python
|
||||||
|
def effective_chunk_profile(note_type: Optional[str], reg: Dict[str, Any]) -> Optional[str]:
|
||||||
|
cfg = get_type_config(note_type, reg)
|
||||||
|
prof = cfg.get("chunk_profile") # ❌ Sucht nach "chunk_profile", aber types.yaml hat "chunking_profile"
|
||||||
|
if isinstance(prof, str) and prof.strip():
|
||||||
|
return prof.strip().lower()
|
||||||
|
return None
|
||||||
|
```
|
||||||
|
|
||||||
|
**Betroffene Dateien:**
|
||||||
|
- `app/core/type_registry.py` (Zeile 88) - verwendet `chunk_profile` statt `chunking_profile`
|
||||||
|
|
||||||
|
**✅ Gut gehandhabt:**
|
||||||
|
- `app/core/ingestion/ingestion_chunk_payload.py` (Zeile 33) - hat Fallback: `t_cfg.get(key) or t_cfg.get(key.replace("ing", ""))`
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (Zeile 120) - prüft beide Varianten
|
||||||
|
|
||||||
|
**Empfehlung:**
|
||||||
|
- `type_registry.py` sollte auch `chunking_profile` prüfen (oder beide Varianten).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ KORREKT VERWENDETE ELEMENTE
|
||||||
|
|
||||||
|
### 1. `chunking_profiles` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/chunking/chunking_utils.py` (Zeile 33) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 2. `defaults` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/ingestion/ingestion_chunk_payload.py` (Zeile 36) ✅
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (Zeile 104) ✅
|
||||||
|
- `app/core/chunking/chunking_utils.py` (Zeile 35) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 3. `ingestion_settings` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (Zeile 105) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 4. `llm_settings` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/registry.py` (Zeile 37) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 5. `types` (Hauptstruktur) ✅
|
||||||
|
- **Verwendet in:** Viele Dateien
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 6. `types[].chunking_profile` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/chunking/chunking_utils.py` (Zeile 35) ✅
|
||||||
|
- `app/core/ingestion/ingestion_chunk_payload.py` (Zeile 67) ✅
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (Zeile 120) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 7. `types[].retriever_weight` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/core/ingestion/ingestion_chunk_payload.py` (Zeile 71) ✅
|
||||||
|
- `app/core/ingestion/ingestion_note_payload.py` (Zeile 111) ✅
|
||||||
|
- `app/core/retrieval/retriever_scoring.py` (Zeile 87) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 8. `types[].detection_keywords` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/routers/chat.py` (Zeilen 104, 150) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
### 9. `types[].schema` ✅
|
||||||
|
- **Verwendet in:**
|
||||||
|
- `app/routers/chat.py` (vermutlich) ✅
|
||||||
|
- **Status:** Korrekt vorhanden in types.yaml
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📋 ZUSAMMENFASSUNG DER ZUGRIFFE
|
||||||
|
|
||||||
|
### Dateien, die auf types.yaml zugreifen:
|
||||||
|
|
||||||
|
1. **app/core/type_registry.py** ⚠️
|
||||||
|
- Verwendet: `types`, `chunk_profile` (sollte `chunking_profile` sein)
|
||||||
|
- Problem: Sucht nach `chunk_profile` statt `chunking_profile`
|
||||||
|
|
||||||
|
2. **app/core/registry.py** ✅
|
||||||
|
- Verwendet: `llm_settings.cleanup_patterns`
|
||||||
|
- Status: OK
|
||||||
|
|
||||||
|
3. **app/core/ingestion/ingestion_chunk_payload.py** ✅
|
||||||
|
- Verwendet: `types`, `defaults`, `chunking_profile`, `retriever_weight`
|
||||||
|
- Status: OK (hat Fallback für chunk_profile/chunking_profile)
|
||||||
|
|
||||||
|
4. **app/core/ingestion/ingestion_note_payload.py** ✅
|
||||||
|
- Verwendet: `types`, `defaults`, `ingestion_settings`, `chunking_profile`, `retriever_weight`
|
||||||
|
- Status: OK (nutzt neue EdgeRegistry für edge_defaults)
|
||||||
|
|
||||||
|
5. **app/core/chunking/chunking_utils.py** ✅
|
||||||
|
- Verwendet: `chunking_profiles`, `types`, `defaults.chunking_profile`
|
||||||
|
- Status: OK
|
||||||
|
|
||||||
|
6. **app/core/retrieval/retriever_scoring.py** ✅
|
||||||
|
- Verwendet: `retriever_weight` (aus Payload, kommt ursprünglich aus types.yaml)
|
||||||
|
- Status: OK
|
||||||
|
|
||||||
|
7. **app/core/graph/graph_utils.py** ❌
|
||||||
|
- Verwendet: `types[].edge_defaults` (existiert nicht mehr!)
|
||||||
|
- Problem: Sucht nach `edge_defaults` in types.yaml
|
||||||
|
|
||||||
|
8. **app/core/graph/graph_derive_edges.py** ❌
|
||||||
|
- Verwendet: `get_edge_defaults_for()` → sucht nach `edge_defaults`
|
||||||
|
- Problem: Keine Default-Kanten mehr
|
||||||
|
|
||||||
|
9. **app/services/discovery.py** ⚠️
|
||||||
|
- Verwendet: `types[].edge_defaults` (existiert nicht mehr!)
|
||||||
|
- Problem: Fallback funktioniert, aber nutzt nicht neue Lösung
|
||||||
|
|
||||||
|
10. **app/routers/chat.py** ✅
|
||||||
|
- Verwendet: `types[].detection_keywords`
|
||||||
|
- Status: OK
|
||||||
|
|
||||||
|
11. **tests/test_type_registry.py** ⚠️
|
||||||
|
- Verwendet: `types[].chunk_profile`, `types[].edge_defaults`
|
||||||
|
- Problem: Test verwendet alte Struktur
|
||||||
|
|
||||||
|
12. **tests/check_types_registry_edges.py** ❌
|
||||||
|
- Verwendet: `types[].edge_defaults` (existiert nicht mehr!)
|
||||||
|
- Problem: Test findet keine edge_defaults
|
||||||
|
|
||||||
|
13. **scripts/payload_dryrun.py** ✅
|
||||||
|
- Verwendet: Indirekt über `make_note_payload()` und `make_chunk_payloads()`
|
||||||
|
- Status: OK
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🔧 EMPFOHLENE FIXES
|
||||||
|
|
||||||
|
### Priorität 1 (Kritisch):
|
||||||
|
|
||||||
|
1. **`app/core/graph/graph_utils.py` - `get_edge_defaults_for()`**
|
||||||
|
- Sollte auf `edge_registry.get_topology_info()` umgestellt werden
|
||||||
|
- Oder: Rückwärtskompatibilität beibehalten, aber EdgeRegistry als primäre Quelle nutzen
|
||||||
|
|
||||||
|
2. **`app/core/graph/graph_derive_edges.py`**
|
||||||
|
- Nutzt `get_edge_defaults_for()`, sollte nach Fix von graph_utils.py funktionieren
|
||||||
|
|
||||||
|
3. **`app/services/discovery.py`**
|
||||||
|
- Sollte EdgeRegistry für `edge_defaults` nutzen
|
||||||
|
|
||||||
|
### Priorität 2 (Warnung):
|
||||||
|
|
||||||
|
4. **`app/core/type_registry.py` - `effective_chunk_profile()`**
|
||||||
|
- Sollte auch `chunking_profile` prüfen (nicht nur `chunk_profile`)
|
||||||
|
|
||||||
|
5. **`tests/test_type_registry.py`**
|
||||||
|
- Test sollte aktualisiert werden, um `chunking_profile` statt `chunk_profile` zu verwenden
|
||||||
|
|
||||||
|
6. **`tests/check_types_registry_edges.py`**
|
||||||
|
- Test sollte auf EdgeRegistry umgestellt werden oder als deprecated markiert werden
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📝 HINWEISE
|
||||||
|
|
||||||
|
- **WP-24c** hat bereits eine Lösung für `edge_defaults` implementiert: Dynamische Abfrage über `edge_registry.get_topology_info()`
|
||||||
|
- Die alte Lösung (statische `edge_defaults` in types.yaml) wurde durch die dynamische Lösung ersetzt
|
||||||
|
- Code-Stellen, die noch die alte Lösung verwenden, sollten migriert werden
|
||||||
+33
-2
@@ -15,13 +15,44 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
# WP-20: Lade Umgebungsvariablen aus der .env Datei
|
# WP-20: Lade Umgebungsvariablen aus der .env Datei
|
||||||
# override=True garantiert, dass Änderungen in der .env immer Vorrang haben.
|
# override=True garantiert, dass Änderungen in der .env immer Vorrang haben.
|
||||||
load_dotenv(override=True)
|
# WP-24c v4.5.10: Expliziter Pfad für .env-Datei, um Probleme mit Arbeitsverzeichnis zu vermeiden
|
||||||
|
# Suche .env im Projekt-Root (3 Ebenen über app/config.py: app/config.py -> app/ -> root/)
|
||||||
|
_project_root = Path(__file__).parent.parent.parent
|
||||||
|
_env_file = _project_root / ".env"
|
||||||
|
_env_loaded = False
|
||||||
|
|
||||||
|
# Versuche zuerst expliziten Pfad
|
||||||
|
if _env_file.exists():
|
||||||
|
_env_loaded = load_dotenv(_env_file, override=True)
|
||||||
|
if _env_loaded:
|
||||||
|
# Optional: Logging (nur wenn logging bereits initialisiert ist)
|
||||||
|
try:
|
||||||
|
import logging
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
_logger.debug(f"✅ .env geladen von: {_env_file}")
|
||||||
|
except:
|
||||||
|
pass # Logging noch nicht initialisiert
|
||||||
|
|
||||||
|
# Fallback: Automatische Suche (für Dev/Test oder wenn .env an anderer Stelle liegt)
|
||||||
|
if not _env_loaded:
|
||||||
|
_env_loaded = load_dotenv(override=True)
|
||||||
|
if _env_loaded:
|
||||||
|
try:
|
||||||
|
import logging
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
_logger.debug(f"✅ .env geladen via automatische Suche (cwd: {Path.cwd()})")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
# --- Qdrant Datenbank ---
|
# --- Qdrant Datenbank ---
|
||||||
QDRANT_URL: str = os.getenv("QDRANT_URL", "http://127.0.0.1:6333")
|
QDRANT_URL: str = os.getenv("QDRANT_URL", "http://127.0.0.1:6333")
|
||||||
QDRANT_API_KEY: str | None = os.getenv("QDRANT_API_KEY")
|
QDRANT_API_KEY: str | None = os.getenv("QDRANT_API_KEY")
|
||||||
COLLECTION_PREFIX: str = os.getenv("MINDNET_PREFIX", "mindnet_dev")
|
# WP-24c v4.5.10: Harmonisierung - Unterstützt beide Umgebungsvariablen für Abwärtskompatibilität
|
||||||
|
# COLLECTION_PREFIX hat Priorität, MINDNET_PREFIX als Fallback
|
||||||
|
# WP-24c v4.5.10-FIX: Default auf "mindnet" (Prod) statt "mindnet_dev" (Dev)
|
||||||
|
# Dev muss explizit COLLECTION_PREFIX=mindnet_dev in .env setzen
|
||||||
|
COLLECTION_PREFIX: str = os.getenv("COLLECTION_PREFIX") or os.getenv("MINDNET_PREFIX") or "mindnet"
|
||||||
|
|
||||||
# WP-22: Vektor-Dimension für das Embedding-Modell (nomic)
|
# WP-22: Vektor-Dimension für das Embedding-Modell (nomic)
|
||||||
VECTOR_SIZE: int = int(os.getenv("VECTOR_DIM", "768"))
|
VECTOR_SIZE: int = int(os.getenv("VECTOR_DIM", "768"))
|
||||||
|
|||||||
@@ -1,176 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/chunk_payload.py
|
|
||||||
DESCRIPTION: Baut das JSON-Objekt für 'mindnet_chunks'.
|
|
||||||
FEATURES:
|
|
||||||
- Inkludiert Nachbarschafts-IDs (prev/next) und Titel.
|
|
||||||
- FIX 3: Robuste Erkennung des Inputs (Frontmatter-Dict vs. Note-Objekt), damit Overrides ankommen.
|
|
||||||
VERSION: 2.3.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: yaml, os
|
|
||||||
EXTERNAL_CONFIG: config/types.yaml
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
from typing import Any, Dict, List, Optional
|
|
||||||
import os, yaml
|
|
||||||
|
|
||||||
def _env(n: str, d: Optional[str]=None) -> str:
|
|
||||||
v = os.getenv(n)
|
|
||||||
return v if v is not None else (d or "")
|
|
||||||
|
|
||||||
def _load_types() -> dict:
|
|
||||||
p = _env("MINDNET_TYPES_FILE", "./config/types.yaml")
|
|
||||||
try:
|
|
||||||
with open(p, "r", encoding="utf-8") as f:
|
|
||||||
return yaml.safe_load(f) or {}
|
|
||||||
except Exception:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def _get_types_map(reg: dict) -> dict:
|
|
||||||
if isinstance(reg, dict) and isinstance(reg.get("types"), dict):
|
|
||||||
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"]
|
|
||||||
if isinstance(reg, dict) and isinstance(reg.get("global"), dict):
|
|
||||||
return reg["global"]
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def _as_float(x: Any):
|
|
||||||
try: return float(x)
|
|
||||||
except Exception: return None
|
|
||||||
|
|
||||||
def _resolve_chunk_profile_from_config(note_type: str, reg: dict) -> Optional[str]:
|
|
||||||
# 1. Type Level
|
|
||||||
types = _get_types_map(reg)
|
|
||||||
if isinstance(types, dict):
|
|
||||||
t = types.get(note_type, {})
|
|
||||||
if isinstance(t, dict):
|
|
||||||
cp = t.get("chunking_profile") or t.get("chunk_profile")
|
|
||||||
if isinstance(cp, str) and cp: return cp
|
|
||||||
# 2. Defaults Level
|
|
||||||
defs = _get_defaults(reg)
|
|
||||||
if isinstance(defs, dict):
|
|
||||||
cp = defs.get("chunking_profile") or defs.get("chunk_profile")
|
|
||||||
if isinstance(cp, str) and cp: return cp
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _resolve_retriever_weight_from_config(note_type: str, reg: dict) -> float:
|
|
||||||
"""
|
|
||||||
Liest Weight nur aus Config (Type > Default).
|
|
||||||
Wird aufgerufen, wenn im Frontmatter nichts steht.
|
|
||||||
"""
|
|
||||||
# 1. Type Level
|
|
||||||
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)
|
|
||||||
|
|
||||||
# 2. Defaults Level
|
|
||||||
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: return float(v)
|
|
||||||
|
|
||||||
return 1.0
|
|
||||||
|
|
||||||
def _as_list(x):
|
|
||||||
if x is None: return []
|
|
||||||
if isinstance(x, list): return x
|
|
||||||
return [x]
|
|
||||||
|
|
||||||
def make_chunk_payloads(note: Dict[str, Any],
|
|
||||||
note_path: str,
|
|
||||||
chunks_from_chunker: List[Any],
|
|
||||||
*,
|
|
||||||
note_text: str = "",
|
|
||||||
types_cfg: Optional[dict] = None,
|
|
||||||
file_path: Optional[str] = None) -> List[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
Erstellt die Payloads für die Chunks.
|
|
||||||
|
|
||||||
Argument 'note' kann sein:
|
|
||||||
A) Ein komplexes Objekt/Dict mit Key "frontmatter" (Legacy / Tests)
|
|
||||||
B) Direkt das Frontmatter-Dictionary (Call aus ingestion.py)
|
|
||||||
"""
|
|
||||||
|
|
||||||
# --- FIX 3: Intelligente Erkennung der Input-Daten ---
|
|
||||||
# Wir prüfen: Ist 'note' ein Container MIT 'frontmatter', oder IST es das 'frontmatter'?
|
|
||||||
if isinstance(note, dict) and "frontmatter" in note and isinstance(note["frontmatter"], dict):
|
|
||||||
# Fall A: Container (wir müssen auspacken)
|
|
||||||
fm = note["frontmatter"]
|
|
||||||
else:
|
|
||||||
# Fall B: Direktes Dict (so ruft ingestion.py es auf!)
|
|
||||||
fm = note or {}
|
|
||||||
|
|
||||||
note_type = fm.get("type") or note.get("type") or "concept"
|
|
||||||
|
|
||||||
# Title Extraction (Fallback Chain)
|
|
||||||
title = fm.get("title") or note.get("title") or fm.get("id") or "Untitled"
|
|
||||||
|
|
||||||
reg = types_cfg if isinstance(types_cfg, dict) else _load_types()
|
|
||||||
|
|
||||||
# --- Profil-Ermittlung ---
|
|
||||||
# Da wir 'fm' jetzt korrekt haben, funktionieren diese lookups:
|
|
||||||
cp = fm.get("chunking_profile") or fm.get("chunk_profile")
|
|
||||||
|
|
||||||
if not cp:
|
|
||||||
cp = _resolve_chunk_profile_from_config(note_type, reg)
|
|
||||||
if not cp:
|
|
||||||
cp = "sliding_standard"
|
|
||||||
|
|
||||||
# --- Retriever Weight Ermittlung ---
|
|
||||||
rw = fm.get("retriever_weight")
|
|
||||||
|
|
||||||
if rw is None:
|
|
||||||
rw = _resolve_retriever_weight_from_config(note_type, reg)
|
|
||||||
|
|
||||||
try:
|
|
||||||
rw = float(rw)
|
|
||||||
except Exception:
|
|
||||||
rw = 1.0
|
|
||||||
|
|
||||||
tags = fm.get("tags") or []
|
|
||||||
if isinstance(tags, str):
|
|
||||||
tags = [tags]
|
|
||||||
|
|
||||||
out: List[Dict[str, Any]] = []
|
|
||||||
for idx, ch in enumerate(chunks_from_chunker):
|
|
||||||
# Attribute extrahieren
|
|
||||||
cid = getattr(ch, "id", None) or (ch.get("id") if isinstance(ch, dict) else None)
|
|
||||||
nid = getattr(ch, "note_id", None) or (ch.get("note_id") if isinstance(ch, dict) else fm.get("id"))
|
|
||||||
index = getattr(ch, "index", None) or (ch.get("index") if isinstance(ch, dict) else idx)
|
|
||||||
text = getattr(ch, "text", None) or (ch.get("text") if isinstance(ch, dict) else "")
|
|
||||||
window = getattr(ch, "window", None) or (ch.get("window") if isinstance(ch, dict) else text)
|
|
||||||
prev_id = getattr(ch, "neighbors_prev", None) or (ch.get("neighbors_prev") if isinstance(ch, dict) else None)
|
|
||||||
next_id = getattr(ch, "neighbors_next", None) or (ch.get("neighbors_next") if isinstance(ch, dict) else None)
|
|
||||||
|
|
||||||
pl: Dict[str, Any] = {
|
|
||||||
"note_id": nid,
|
|
||||||
"chunk_id": cid,
|
|
||||||
"title": title,
|
|
||||||
"index": int(index),
|
|
||||||
"ord": int(index) + 1,
|
|
||||||
"type": note_type,
|
|
||||||
"tags": tags,
|
|
||||||
"text": text,
|
|
||||||
"window": window,
|
|
||||||
"neighbors_prev": _as_list(prev_id),
|
|
||||||
"neighbors_next": _as_list(next_id),
|
|
||||||
"section": getattr(ch, "section", None) or (ch.get("section") if isinstance(ch, dict) else ""),
|
|
||||||
"path": note_path,
|
|
||||||
"source_path": file_path or note_path,
|
|
||||||
"retriever_weight": float(rw),
|
|
||||||
"chunk_profile": cp, # Jetzt endlich mit dem Override-Wert!
|
|
||||||
}
|
|
||||||
|
|
||||||
# Cleanup
|
|
||||||
for alias in ("chunk_num", "Chunk_Number"):
|
|
||||||
pl.pop(alias, None)
|
|
||||||
|
|
||||||
out.append(pl)
|
|
||||||
|
|
||||||
return out
|
|
||||||
@@ -1,447 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/chunker.py
|
|
||||||
DESCRIPTION: Zerlegt Texte in Chunks (Sliding Window oder nach Headings).
|
|
||||||
WP-15b: Implementiert Edge-Inheritance und Candidate-Pool Vorbereitung.
|
|
||||||
Zentralisiert die Kanten-Vorbereitung für die spätere binäre Validierung.
|
|
||||||
Bietet volle Unterstützung für Hybrid-Chunking (Strict/Soft/Safety-Net).
|
|
||||||
VERSION: 3.2.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: re, math, yaml, pathlib, asyncio, logging
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import List, Dict, Optional, Tuple, Any, Set
|
|
||||||
import re
|
|
||||||
import math
|
|
||||||
import yaml
|
|
||||||
from pathlib import Path
|
|
||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
|
|
||||||
# Services
|
|
||||||
# In WP-15b wird die KI-Validierung in die ingestion.py verlagert.
|
|
||||||
# Wir behalten den Import für Abwärtskompatibilität, falls Legacy-Skripte ihn benötigen.
|
|
||||||
try:
|
|
||||||
from app.services.semantic_analyzer import get_semantic_analyzer
|
|
||||||
except ImportError:
|
|
||||||
def get_semantic_analyzer(): return None
|
|
||||||
|
|
||||||
# Core Imports
|
|
||||||
try:
|
|
||||||
from app.core.derive_edges import build_edges_for_note
|
|
||||||
except ImportError:
|
|
||||||
# Fallback für Standalone-Betrieb oder Tests
|
|
||||||
def build_edges_for_note(note_id, chunks, note_level_references=None, include_note_scope_refs=False): return []
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# ==========================================
|
|
||||||
# 1. HELPER & CONFIG
|
|
||||||
# ==========================================
|
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
||||||
CONFIG_PATH = BASE_DIR / "config" / "types.yaml"
|
|
||||||
# Fallback Default, falls types.yaml fehlt
|
|
||||||
DEFAULT_PROFILE = {"strategy": "sliding_window", "target": 400, "max": 600, "overlap": (50, 80)}
|
|
||||||
_CONFIG_CACHE = None
|
|
||||||
|
|
||||||
def _load_yaml_config() -> Dict[str, Any]:
|
|
||||||
global _CONFIG_CACHE
|
|
||||||
if _CONFIG_CACHE is not None: return _CONFIG_CACHE
|
|
||||||
if not CONFIG_PATH.exists(): return {}
|
|
||||||
try:
|
|
||||||
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
|
||||||
data = yaml.safe_load(f)
|
|
||||||
_CONFIG_CACHE = data
|
|
||||||
return data
|
|
||||||
except Exception: return {}
|
|
||||||
|
|
||||||
def get_chunk_config(note_type: str) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Lädt die Chunking-Strategie basierend auf dem Note-Type aus types.yaml.
|
|
||||||
Sichert die Kompatibilität zu WP-15 Profilen.
|
|
||||||
"""
|
|
||||||
full_config = _load_yaml_config()
|
|
||||||
profiles = full_config.get("chunking_profiles", {})
|
|
||||||
type_def = full_config.get("types", {}).get(note_type.lower(), {})
|
|
||||||
|
|
||||||
# Welches Profil nutzt dieser Typ? (z.B. 'sliding_smart_edges')
|
|
||||||
profile_name = type_def.get("chunking_profile")
|
|
||||||
|
|
||||||
if not profile_name:
|
|
||||||
profile_name = full_config.get("defaults", {}).get("chunking_profile", "sliding_standard")
|
|
||||||
|
|
||||||
config = profiles.get(profile_name, DEFAULT_PROFILE).copy()
|
|
||||||
|
|
||||||
# Tupel-Konvertierung für Overlap (YAML liest oft Listen)
|
|
||||||
if "overlap" in config and isinstance(config["overlap"], list):
|
|
||||||
config["overlap"] = tuple(config["overlap"])
|
|
||||||
|
|
||||||
return config
|
|
||||||
|
|
||||||
def extract_frontmatter_from_text(md_text: str) -> Tuple[Dict[str, Any], str]:
|
|
||||||
"""Trennt YAML-Frontmatter vom eigentlichen Text."""
|
|
||||||
fm_match = re.match(r'^\s*---\s*\n(.*?)\n---', md_text, re.DOTALL)
|
|
||||||
if not fm_match: return {}, md_text
|
|
||||||
try:
|
|
||||||
frontmatter = yaml.safe_load(fm_match.group(1))
|
|
||||||
if not isinstance(frontmatter, dict): frontmatter = {}
|
|
||||||
except yaml.YAMLError:
|
|
||||||
frontmatter = {}
|
|
||||||
text_without_fm = re.sub(r'^\s*---\s*\n(.*?)\n---', '', md_text, flags=re.DOTALL)
|
|
||||||
return frontmatter, text_without_fm.strip()
|
|
||||||
|
|
||||||
# ==========================================
|
|
||||||
# 2. DATA CLASSES & TEXT TOOLS
|
|
||||||
# ==========================================
|
|
||||||
|
|
||||||
_SENT_SPLIT = re.compile(r'(?<=[.!?])\s+(?=[A-ZÄÖÜ0-9„(])')
|
|
||||||
_WS = re.compile(r'\s+')
|
|
||||||
|
|
||||||
def estimate_tokens(text: str) -> int:
|
|
||||||
"""Grobe Schätzung der Token-Anzahl (4 Zeichen pro Token)."""
|
|
||||||
return max(1, math.ceil(len(text.strip()) / 4))
|
|
||||||
|
|
||||||
def split_sentences(text: str) -> list[str]:
|
|
||||||
"""Teilt Text in Sätze auf unter Berücksichtigung von Interpunktion."""
|
|
||||||
text = _WS.sub(' ', text.strip())
|
|
||||||
if not text: return []
|
|
||||||
parts = _SENT_SPLIT.split(text)
|
|
||||||
return [p.strip() for p in parts if p.strip()]
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class RawBlock:
|
|
||||||
kind: str
|
|
||||||
text: str
|
|
||||||
level: Optional[int]
|
|
||||||
section_path: str
|
|
||||||
section_title: Optional[str]
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Chunk:
|
|
||||||
id: str
|
|
||||||
note_id: str
|
|
||||||
index: int
|
|
||||||
text: str
|
|
||||||
window: str
|
|
||||||
token_count: int
|
|
||||||
section_title: Optional[str]
|
|
||||||
section_path: str
|
|
||||||
neighbors_prev: Optional[str]
|
|
||||||
neighbors_next: Optional[str]
|
|
||||||
# WP-15b: Liste von Kandidaten für die semantische Validierung
|
|
||||||
candidate_pool: List[Dict[str, Any]] = field(default_factory=list)
|
|
||||||
suggested_edges: Optional[List[str]] = None
|
|
||||||
|
|
||||||
# ==========================================
|
|
||||||
# 3. PARSING & STRATEGIES
|
|
||||||
# ==========================================
|
|
||||||
|
|
||||||
def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
|
||||||
"""
|
|
||||||
Zerlegt Text in logische Blöcke (Absätze, Header).
|
|
||||||
Wichtig für die Strategie 'by_heading' und die Edge-Inheritance.
|
|
||||||
"""
|
|
||||||
blocks = []
|
|
||||||
h1_title = "Dokument"
|
|
||||||
section_path = "/"
|
|
||||||
current_h2 = None
|
|
||||||
|
|
||||||
fm, text_without_fm = extract_frontmatter_from_text(md_text)
|
|
||||||
|
|
||||||
h1_match = re.search(r'^#\s+(.*)', text_without_fm, re.MULTILINE)
|
|
||||||
if h1_match:
|
|
||||||
h1_title = h1_match.group(1).strip()
|
|
||||||
|
|
||||||
lines = text_without_fm.split('\n')
|
|
||||||
buffer = []
|
|
||||||
|
|
||||||
for line in lines:
|
|
||||||
stripped = line.strip()
|
|
||||||
if stripped.startswith('# '):
|
|
||||||
continue
|
|
||||||
elif stripped.startswith('## '):
|
|
||||||
if buffer:
|
|
||||||
content = "\n".join(buffer).strip()
|
|
||||||
if content:
|
|
||||||
blocks.append(RawBlock("paragraph", content, None, section_path, current_h2))
|
|
||||||
buffer = []
|
|
||||||
current_h2 = stripped[3:].strip()
|
|
||||||
section_path = f"/{current_h2}"
|
|
||||||
blocks.append(RawBlock("heading", stripped, 2, section_path, current_h2))
|
|
||||||
elif not stripped:
|
|
||||||
if buffer:
|
|
||||||
content = "\n".join(buffer).strip()
|
|
||||||
if content:
|
|
||||||
blocks.append(RawBlock("paragraph", content, None, section_path, current_h2))
|
|
||||||
buffer = []
|
|
||||||
else:
|
|
||||||
buffer.append(line)
|
|
||||||
|
|
||||||
if buffer:
|
|
||||||
content = "\n".join(buffer).strip()
|
|
||||||
if content:
|
|
||||||
blocks.append(RawBlock("paragraph", content, None, section_path, current_h2))
|
|
||||||
|
|
||||||
return blocks, h1_title
|
|
||||||
|
|
||||||
def _strategy_sliding_window(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, doc_title: str = "", context_prefix: str = "") -> List[Chunk]:
|
|
||||||
"""
|
|
||||||
Standard-Strategie aus WP-15.
|
|
||||||
Fasst Blöcke zusammen und schneidet bei 'target' Tokens.
|
|
||||||
"""
|
|
||||||
target = config.get("target", 400)
|
|
||||||
max_tokens = config.get("max", 600)
|
|
||||||
overlap_val = config.get("overlap", (50, 80))
|
|
||||||
overlap = sum(overlap_val) // 2 if isinstance(overlap_val, tuple) else overlap_val
|
|
||||||
chunks = []
|
|
||||||
buf = []
|
|
||||||
|
|
||||||
def _create_chunk(txt, win, sec, path):
|
|
||||||
idx = len(chunks)
|
|
||||||
chunks.append(Chunk(
|
|
||||||
id=f"{note_id}#c{idx:02d}", note_id=note_id, index=idx,
|
|
||||||
text=txt, window=win, token_count=estimate_tokens(txt),
|
|
||||||
section_title=sec, section_path=path, neighbors_prev=None, neighbors_next=None,
|
|
||||||
candidate_pool=[]
|
|
||||||
))
|
|
||||||
|
|
||||||
def flush_buffer():
|
|
||||||
nonlocal buf
|
|
||||||
if not buf: return
|
|
||||||
|
|
||||||
text_body = "\n\n".join([b.text for b in buf])
|
|
||||||
sec_title = buf[-1].section_title if buf else None
|
|
||||||
sec_path = buf[-1].section_path if buf else "/"
|
|
||||||
win_body = f"{context_prefix}\n{text_body}".strip() if context_prefix else text_body
|
|
||||||
|
|
||||||
if estimate_tokens(text_body) <= max_tokens:
|
|
||||||
_create_chunk(text_body, win_body, sec_title, sec_path)
|
|
||||||
else:
|
|
||||||
sentences = split_sentences(text_body)
|
|
||||||
current_chunk_sents = []
|
|
||||||
current_len = 0
|
|
||||||
|
|
||||||
for sent in sentences:
|
|
||||||
sent_len = estimate_tokens(sent)
|
|
||||||
if current_len + sent_len > target and current_chunk_sents:
|
|
||||||
c_txt = " ".join(current_chunk_sents)
|
|
||||||
c_win = f"{context_prefix}\n{c_txt}".strip() if context_prefix else c_txt
|
|
||||||
_create_chunk(c_txt, c_win, sec_title, sec_path)
|
|
||||||
|
|
||||||
overlap_sents = []
|
|
||||||
ov_len = 0
|
|
||||||
for s in reversed(current_chunk_sents):
|
|
||||||
if ov_len + estimate_tokens(s) < overlap:
|
|
||||||
overlap_sents.insert(0, s)
|
|
||||||
ov_len += estimate_tokens(s)
|
|
||||||
else: break
|
|
||||||
|
|
||||||
current_chunk_sents = list(overlap_sents)
|
|
||||||
current_chunk_sents.append(sent)
|
|
||||||
current_len = ov_len + sent_len
|
|
||||||
else:
|
|
||||||
current_chunk_sents.append(sent)
|
|
||||||
current_len += sent_len
|
|
||||||
|
|
||||||
if current_chunk_sents:
|
|
||||||
c_txt = " ".join(current_chunk_sents)
|
|
||||||
c_win = f"{context_prefix}\n{c_txt}".strip() if context_prefix else c_txt
|
|
||||||
_create_chunk(c_txt, c_win, sec_title, sec_path)
|
|
||||||
buf = []
|
|
||||||
|
|
||||||
for b in blocks:
|
|
||||||
if b.kind == "heading": continue
|
|
||||||
current_buf_text = "\n\n".join([x.text for x in buf])
|
|
||||||
if estimate_tokens(current_buf_text) + estimate_tokens(b.text) >= target:
|
|
||||||
flush_buffer()
|
|
||||||
buf.append(b)
|
|
||||||
if estimate_tokens(b.text) >= target:
|
|
||||||
flush_buffer()
|
|
||||||
|
|
||||||
flush_buffer()
|
|
||||||
return chunks
|
|
||||||
|
|
||||||
def _strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, doc_title: str = "") -> List[Chunk]:
|
|
||||||
"""
|
|
||||||
Hybrid-Strategie v2.9 (Strict/Soft/Safety-Net).
|
|
||||||
"""
|
|
||||||
strict = config.get("strict_heading_split", False)
|
|
||||||
target = config.get("target", 400)
|
|
||||||
max_tokens = config.get("max", 600)
|
|
||||||
split_level = config.get("split_level", 2)
|
|
||||||
|
|
||||||
chunks = []
|
|
||||||
current_buf = []
|
|
||||||
current_tokens = 0
|
|
||||||
|
|
||||||
def _flush(sec_title, sec_path):
|
|
||||||
nonlocal current_buf, current_tokens
|
|
||||||
if not current_buf: return
|
|
||||||
txt = "\n\n".join(current_buf)
|
|
||||||
win = f"# {doc_title}\n## {sec_title}\n{txt}".strip() if sec_title else txt
|
|
||||||
idx = len(chunks)
|
|
||||||
chunks.append(Chunk(
|
|
||||||
id=f"{note_id}#c{idx:02d}", note_id=note_id, index=idx,
|
|
||||||
text=txt, window=win, token_count=estimate_tokens(txt),
|
|
||||||
section_title=sec_title, section_path=sec_path,
|
|
||||||
neighbors_prev=None, neighbors_next=None,
|
|
||||||
candidate_pool=[]
|
|
||||||
))
|
|
||||||
current_buf = []
|
|
||||||
current_tokens = 0
|
|
||||||
|
|
||||||
for b in blocks:
|
|
||||||
if b.kind == "heading":
|
|
||||||
# Hierarchie-Check: Split bei Überschriften oberhalb des Split-Levels
|
|
||||||
if b.level < split_level:
|
|
||||||
_flush(b.section_title, b.section_path)
|
|
||||||
elif b.level == split_level:
|
|
||||||
if strict or current_tokens >= target:
|
|
||||||
_flush(b.section_title, b.section_path)
|
|
||||||
continue
|
|
||||||
|
|
||||||
block_tokens = estimate_tokens(b.text)
|
|
||||||
if current_tokens + block_tokens > max_tokens and current_buf:
|
|
||||||
_flush(b.section_title, b.section_path)
|
|
||||||
|
|
||||||
current_buf.append(b.text)
|
|
||||||
current_tokens += block_tokens
|
|
||||||
|
|
||||||
if current_buf:
|
|
||||||
last = blocks[-1] if blocks else None
|
|
||||||
_flush(last.section_title if last else None, last.section_path if last else "/")
|
|
||||||
|
|
||||||
return chunks
|
|
||||||
|
|
||||||
# ==========================================
|
|
||||||
# 4. ROBUST EDGE PARSING & PROPAGATION
|
|
||||||
# ==========================================
|
|
||||||
|
|
||||||
def _parse_edges_robust(text: str) -> Set[str]:
|
|
||||||
"""
|
|
||||||
Findet Kanten im Text (Wikilinks, Inlines, Callouts).
|
|
||||||
Fix V3: Support für mehrzeilige Callouts.
|
|
||||||
"""
|
|
||||||
found_edges = set()
|
|
||||||
|
|
||||||
# A. Inline [[rel:type|target]]
|
|
||||||
inlines = re.findall(r'\[\[rel:([^\|\]]+)\|?([^\]]*)\]\]', text)
|
|
||||||
for kind, target in inlines:
|
|
||||||
k = kind.strip().lower()
|
|
||||||
t = target.strip()
|
|
||||||
if k and t: found_edges.add(f"{k}:{t}")
|
|
||||||
|
|
||||||
# B. Multiline Callouts Parsing (WP-15 Fix)
|
|
||||||
lines = text.split('\n')
|
|
||||||
current_edge_type = None
|
|
||||||
for line in lines:
|
|
||||||
stripped = line.strip()
|
|
||||||
callout_match = re.match(r'>\s*\[!edge\]\s*([^:\s]+)', stripped)
|
|
||||||
if callout_match:
|
|
||||||
current_edge_type = callout_match.group(1).strip().lower()
|
|
||||||
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
|
||||||
for l in links:
|
|
||||||
if "rel:" not in l: found_edges.add(f"{current_edge_type}:{l}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
if current_edge_type and stripped.startswith('>'):
|
|
||||||
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
|
||||||
for l in links:
|
|
||||||
if "rel:" not in l: found_edges.add(f"{current_edge_type}:{l}")
|
|
||||||
elif not stripped.startswith('>'):
|
|
||||||
current_edge_type = None
|
|
||||||
|
|
||||||
return found_edges
|
|
||||||
|
|
||||||
def _propagate_section_edges(chunks: List[Chunk], blocks: List[RawBlock]) -> List[Chunk]:
|
|
||||||
"""
|
|
||||||
WP-15b: Implementiert Edge-Inheritance.
|
|
||||||
Kanten aus Überschriften werden an untergeordnete Chunks vererbt.
|
|
||||||
"""
|
|
||||||
section_inheritance: Dict[str, Set[str]] = {}
|
|
||||||
|
|
||||||
# 1. Sammeln aus den Heading-Blöcken
|
|
||||||
for b in blocks:
|
|
||||||
if b.kind == "heading":
|
|
||||||
edges = _parse_edges_robust(b.text)
|
|
||||||
if edges:
|
|
||||||
if b.section_path not in section_inheritance:
|
|
||||||
section_inheritance[b.section_path] = set()
|
|
||||||
section_inheritance[b.section_path].update(edges)
|
|
||||||
|
|
||||||
# 2. Injektion in den Candidate-Pool
|
|
||||||
for ch in chunks:
|
|
||||||
inherited = section_inheritance.get(ch.section_path, set())
|
|
||||||
for e_str in inherited:
|
|
||||||
kind, target = e_str.split(':', 1)
|
|
||||||
ch.candidate_pool.append({"kind": kind, "to": target, "provenance": "inherited"})
|
|
||||||
|
|
||||||
return chunks
|
|
||||||
|
|
||||||
# ==========================================
|
|
||||||
# 5. ORCHESTRATION (WP-15b)
|
|
||||||
# ==========================================
|
|
||||||
|
|
||||||
async def assemble_chunks(note_id: str, md_text: str, note_type: str, config: Optional[Dict] = None) -> List[Chunk]:
|
|
||||||
"""
|
|
||||||
Hauptfunktion zur Chunk-Generierung.
|
|
||||||
Baut den Candidate-Pool für die semantische Validierung auf.
|
|
||||||
"""
|
|
||||||
if config is None:
|
|
||||||
config = get_chunk_config(note_type)
|
|
||||||
|
|
||||||
fm, body_text = extract_frontmatter_from_text(md_text)
|
|
||||||
primary_strategy = config.get("strategy", "sliding_window")
|
|
||||||
|
|
||||||
# 1. Parsing & Splitting
|
|
||||||
blocks, doc_title = parse_blocks(md_text)
|
|
||||||
|
|
||||||
if primary_strategy == "by_heading":
|
|
||||||
chunks = await asyncio.to_thread(_strategy_by_heading, blocks, config, note_id, doc_title)
|
|
||||||
else:
|
|
||||||
chunks = await asyncio.to_thread(_strategy_sliding_window, blocks, config, note_id, doc_title)
|
|
||||||
|
|
||||||
if not chunks: return []
|
|
||||||
|
|
||||||
# 2. WP-15b: Candidate Pool Vorbereitung
|
|
||||||
|
|
||||||
# A. Edge Inheritance (Sektions-Propagation)
|
|
||||||
chunks = _propagate_section_edges(chunks, blocks)
|
|
||||||
|
|
||||||
# B. Explicit Edges (Direkt im Chunk-Text enthalten)
|
|
||||||
for ch in chunks:
|
|
||||||
explicit = _parse_edges_robust(ch.text)
|
|
||||||
for e_str in explicit:
|
|
||||||
kind, target = e_str.split(':', 1)
|
|
||||||
ch.candidate_pool.append({"kind": kind, "to": target, "provenance": "explicit"})
|
|
||||||
|
|
||||||
# C. Global "Unassigned Pool" Detection (Safety Net)
|
|
||||||
# Sucht nach einer Sektion "Unzugeordnete Kanten" im Body
|
|
||||||
unassigned_pool = set()
|
|
||||||
pool_match = re.search(r'###?\s*(?:Unzugeordnete Kanten|Edge Pool|Candidates)\s*\n(.*?)(?:\n#|$)', body_text, re.DOTALL | re.IGNORECASE)
|
|
||||||
if pool_match:
|
|
||||||
unassigned_pool = _parse_edges_robust(pool_match.group(1))
|
|
||||||
for ch in chunks:
|
|
||||||
for e_str in unassigned_pool:
|
|
||||||
kind, target = e_str.split(':', 1)
|
|
||||||
ch.candidate_pool.append({"kind": kind, "to": target, "provenance": "global_pool"})
|
|
||||||
|
|
||||||
# D. De-Duplikation des Pools
|
|
||||||
for ch in chunks:
|
|
||||||
seen = set()
|
|
||||||
unique_pool = []
|
|
||||||
for cand in ch.candidate_pool:
|
|
||||||
key = (cand["kind"], cand["to"])
|
|
||||||
if key not in seen:
|
|
||||||
seen.add(key)
|
|
||||||
unique_pool.append(cand)
|
|
||||||
ch.candidate_pool = unique_pool
|
|
||||||
|
|
||||||
# 3. Nachbarschafts-Verkettung (Struktur-Kanten)
|
|
||||||
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
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/__init__.py
|
||||||
|
DESCRIPTION: Package-Einstiegspunkt für Chunking. Exportiert assemble_chunks.
|
||||||
|
VERSION: 3.3.0
|
||||||
|
"""
|
||||||
|
from .chunking_processor import assemble_chunks
|
||||||
|
from .chunking_utils import get_chunk_config, extract_frontmatter_from_text
|
||||||
|
from .chunking_models import Chunk
|
||||||
|
|
||||||
|
__all__ = ["assemble_chunks", "get_chunk_config", "extract_frontmatter_from_text", "Chunk"]
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_models.py
|
||||||
|
DESCRIPTION: Datenklassen für das Chunking-System.
|
||||||
|
"""
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import List, Dict, Optional, Any
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class RawBlock:
|
||||||
|
"""Repräsentiert einen logischen Block aus dem Markdown-Parsing."""
|
||||||
|
kind: str
|
||||||
|
text: str
|
||||||
|
level: Optional[int]
|
||||||
|
section_path: str
|
||||||
|
section_title: Optional[str]
|
||||||
|
exclude_from_chunking: bool = False # WP-24c v4.2.0: Flag für Edge-Zonen, die nicht gechunkt werden sollen
|
||||||
|
is_meta_content: bool = False # WP-24c v4.2.6: Flag für Meta-Content (Callouts), der später entfernt wird
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Chunk:
|
||||||
|
"""Das finale Chunk-Objekt für Embedding und Graph-Speicherung."""
|
||||||
|
id: str
|
||||||
|
note_id: str
|
||||||
|
index: int
|
||||||
|
text: str
|
||||||
|
window: str
|
||||||
|
token_count: int
|
||||||
|
section_title: Optional[str]
|
||||||
|
section_path: str
|
||||||
|
neighbors_prev: Optional[str]
|
||||||
|
neighbors_next: Optional[str]
|
||||||
|
candidate_pool: List[Dict[str, Any]] = field(default_factory=list)
|
||||||
|
suggested_edges: Optional[List[str]] = None
|
||||||
@@ -0,0 +1,251 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_parser.py
|
||||||
|
DESCRIPTION: Zerlegt Markdown in logische Einheiten (RawBlocks).
|
||||||
|
Hält alle Überschriftenebenen (H1-H6) im Stream.
|
||||||
|
Stellt die Funktion parse_edges_robust zur Verfügung.
|
||||||
|
WP-24c v4.2.0: Identifiziert Edge-Zonen und markiert sie für Chunking-Ausschluss.
|
||||||
|
WP-24c v4.2.5: Callout-Exclusion - Callouts werden als separate RawBlocks identifiziert und ausgeschlossen.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
from typing import List, Tuple, Set, Dict, Any, Optional
|
||||||
|
from .chunking_models import RawBlock
|
||||||
|
from .chunking_utils import extract_frontmatter_from_text
|
||||||
|
|
||||||
|
_WS = re.compile(r'\s+')
|
||||||
|
_SENT_SPLIT = re.compile(r'(?<=[.!?])\s+(?=[A-ZÄÖÜ0-9„(])')
|
||||||
|
|
||||||
|
def split_sentences(text: str) -> list[str]:
|
||||||
|
"""Teilt Text in Sätze auf unter Berücksichtigung deutscher Interpunktion."""
|
||||||
|
text = _WS.sub(' ', text.strip())
|
||||||
|
if not text: return []
|
||||||
|
# Splittet bei Punkt, Ausrufezeichen oder Fragezeichen, gefolgt von Leerzeichen und Großbuchstabe
|
||||||
|
return [p.strip() for p in _SENT_SPLIT.split(text) if p.strip()]
|
||||||
|
|
||||||
|
def parse_blocks(md_text: str) -> Tuple[List[RawBlock], str]:
|
||||||
|
"""
|
||||||
|
Zerlegt Text in logische Einheiten (RawBlocks), inklusive H1-H6.
|
||||||
|
WP-24c v4.2.0: Identifiziert Edge-Zonen (LLM-Validierung & Note-Scope) und markiert sie für Chunking-Ausschluss.
|
||||||
|
WP-24c v4.2.6: Callouts werden mit is_meta_content=True markiert (werden gechunkt, aber später entfernt).
|
||||||
|
"""
|
||||||
|
blocks = []
|
||||||
|
h1_title = "Dokument"
|
||||||
|
section_path = "/"
|
||||||
|
current_section_title = None
|
||||||
|
|
||||||
|
# Frontmatter entfernen
|
||||||
|
fm, text_without_fm = extract_frontmatter_from_text(md_text)
|
||||||
|
|
||||||
|
# WP-24c v4.2.0: Konfigurierbare Header-Namen und -Ebenen
|
||||||
|
llm_validation_headers = os.getenv(
|
||||||
|
"MINDNET_LLM_VALIDATION_HEADERS",
|
||||||
|
"Unzugeordnete Kanten,Edge Pool,Candidates"
|
||||||
|
)
|
||||||
|
llm_validation_header_list = [h.strip() for h in llm_validation_headers.split(",") if h.strip()]
|
||||||
|
if not llm_validation_header_list:
|
||||||
|
llm_validation_header_list = ["Unzugeordnete Kanten", "Edge Pool", "Candidates"]
|
||||||
|
|
||||||
|
note_scope_headers = os.getenv(
|
||||||
|
"MINDNET_NOTE_SCOPE_ZONE_HEADERS",
|
||||||
|
"Smart Edges,Relationen,Global Links,Note-Level Relations,Globale Verbindungen"
|
||||||
|
)
|
||||||
|
note_scope_header_list = [h.strip() for h in note_scope_headers.split(",") if h.strip()]
|
||||||
|
if not note_scope_header_list:
|
||||||
|
note_scope_header_list = ["Smart Edges", "Relationen", "Global Links", "Note-Level Relations", "Globale Verbindungen"]
|
||||||
|
|
||||||
|
# Header-Ebenen konfigurierbar (Default: LLM=3, Note-Scope=2)
|
||||||
|
llm_validation_level = int(os.getenv("MINDNET_LLM_VALIDATION_HEADER_LEVEL", "3"))
|
||||||
|
note_scope_level = int(os.getenv("MINDNET_NOTE_SCOPE_HEADER_LEVEL", "2"))
|
||||||
|
|
||||||
|
# Status-Tracking für Edge-Zonen
|
||||||
|
in_exclusion_zone = False
|
||||||
|
exclusion_zone_type = None # "llm_validation" oder "note_scope"
|
||||||
|
|
||||||
|
# H1 für Note-Titel extrahieren (Metadaten-Zweck)
|
||||||
|
h1_match = re.search(r'^#\s+(.*)', text_without_fm, re.MULTILINE)
|
||||||
|
if h1_match:
|
||||||
|
h1_title = h1_match.group(1).strip()
|
||||||
|
|
||||||
|
lines = text_without_fm.split('\n')
|
||||||
|
buffer = []
|
||||||
|
|
||||||
|
# WP-24c v4.2.5: Callout-Erkennung (auch verschachtelt: >>)
|
||||||
|
# Regex für Callouts: >\s*[!edge] oder >\s*[!abstract] (auch mit mehreren >)
|
||||||
|
callout_pattern = re.compile(r'^\s*>{1,}\s*\[!(edge|abstract)\]', re.IGNORECASE)
|
||||||
|
|
||||||
|
# WP-24c v4.2.5: Markiere verarbeitete Zeilen, um sie zu überspringen
|
||||||
|
processed_indices = set()
|
||||||
|
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
if i in processed_indices:
|
||||||
|
continue
|
||||||
|
|
||||||
|
stripped = line.strip()
|
||||||
|
|
||||||
|
# WP-24c v4.2.5: Callout-Erkennung (VOR Heading-Erkennung)
|
||||||
|
# Prüfe, ob diese Zeile ein Callout startet
|
||||||
|
callout_match = callout_pattern.match(line)
|
||||||
|
if callout_match:
|
||||||
|
# Vorherigen Text-Block abschließen
|
||||||
|
if buffer:
|
||||||
|
content = "\n".join(buffer).strip()
|
||||||
|
if content:
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"paragraph", content, None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
buffer = []
|
||||||
|
|
||||||
|
# Sammle alle Zeilen des Callout-Blocks
|
||||||
|
callout_lines = [line]
|
||||||
|
leading_gt_count = len(line) - len(line.lstrip('>'))
|
||||||
|
processed_indices.add(i)
|
||||||
|
|
||||||
|
# Sammle alle Zeilen, die zum Callout gehören (gleiche oder höhere Einrückung)
|
||||||
|
j = i + 1
|
||||||
|
while j < len(lines):
|
||||||
|
next_line = lines[j]
|
||||||
|
if not next_line.strip().startswith('>'):
|
||||||
|
break
|
||||||
|
next_leading_gt = len(next_line) - len(next_line.lstrip('>'))
|
||||||
|
if next_leading_gt < leading_gt_count:
|
||||||
|
break
|
||||||
|
callout_lines.append(next_line)
|
||||||
|
processed_indices.add(j)
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
# WP-24c v4.2.6: Erstelle Callout-Block mit is_meta_content = True
|
||||||
|
# Callouts werden gechunkt (für Chunk-Attribution), aber später entfernt (Clean-Context)
|
||||||
|
callout_content = "\n".join(callout_lines)
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"callout", callout_content, None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone, # Nur Edge-Zonen werden ausgeschlossen
|
||||||
|
is_meta_content=True # WP-24c v4.2.6: Markierung für spätere Entfernung
|
||||||
|
))
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Heading-Erkennung (H1 bis H6)
|
||||||
|
heading_match = re.match(r'^(#{1,6})\s+(.*)', stripped)
|
||||||
|
if heading_match:
|
||||||
|
# Vorherigen Text-Block abschließen
|
||||||
|
if buffer:
|
||||||
|
content = "\n".join(buffer).strip()
|
||||||
|
if content:
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"paragraph", content, None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
buffer = []
|
||||||
|
|
||||||
|
level = len(heading_match.group(1))
|
||||||
|
title = heading_match.group(2).strip()
|
||||||
|
|
||||||
|
# WP-24c v4.2.0: Prüfe, ob dieser Header eine Edge-Zone startet
|
||||||
|
is_llm_validation_zone = (
|
||||||
|
level == llm_validation_level and
|
||||||
|
any(title.lower() == h.lower() for h in llm_validation_header_list)
|
||||||
|
)
|
||||||
|
is_note_scope_zone = (
|
||||||
|
level == note_scope_level and
|
||||||
|
any(title.lower() == h.lower() for h in note_scope_header_list)
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_llm_validation_zone:
|
||||||
|
in_exclusion_zone = True
|
||||||
|
exclusion_zone_type = "llm_validation"
|
||||||
|
elif is_note_scope_zone:
|
||||||
|
in_exclusion_zone = True
|
||||||
|
exclusion_zone_type = "note_scope"
|
||||||
|
elif in_exclusion_zone:
|
||||||
|
# Neuer Header gefunden, der keine Edge-Zone ist -> Zone beendet
|
||||||
|
in_exclusion_zone = False
|
||||||
|
exclusion_zone_type = None
|
||||||
|
|
||||||
|
# Pfad- und Titel-Update für die Metadaten der folgenden Blöcke
|
||||||
|
if level == 1:
|
||||||
|
current_section_title = title; section_path = "/"
|
||||||
|
elif level == 2:
|
||||||
|
current_section_title = title; section_path = f"/{current_section_title}"
|
||||||
|
|
||||||
|
# Die Überschrift selbst als regulären Block hinzufügen (auch markiert, wenn in Zone)
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"heading", stripped, level, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Trenner (---) oder Leerzeilen beenden Blöcke, außer innerhalb von Callouts
|
||||||
|
if (not stripped or stripped == "---") and not line.startswith('>'):
|
||||||
|
if buffer:
|
||||||
|
content = "\n".join(buffer).strip()
|
||||||
|
if content:
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"paragraph", content, None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
buffer = []
|
||||||
|
if stripped == "---":
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"separator", "---", None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
buffer.append(line)
|
||||||
|
|
||||||
|
if buffer:
|
||||||
|
content = "\n".join(buffer).strip()
|
||||||
|
if content:
|
||||||
|
blocks.append(RawBlock(
|
||||||
|
"paragraph", content, None, section_path, current_section_title,
|
||||||
|
exclude_from_chunking=in_exclusion_zone
|
||||||
|
))
|
||||||
|
|
||||||
|
return blocks, h1_title
|
||||||
|
|
||||||
|
def parse_edges_robust(text: str) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Extrahiert Kanten-Kandidaten aus Wikilinks und Callouts.
|
||||||
|
WP-24c v4.2.7: Gibt Liste von Dicts zurück mit is_callout Flag für Chunk-Attribution.
|
||||||
|
WP-24c v4.2.9 Fix A: current_edge_type bleibt über Leerzeilen hinweg erhalten,
|
||||||
|
damit alle Links in einem Callout-Block korrekt verarbeitet werden.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict] mit keys: "edge" (str: "kind:target"), "is_callout" (bool)
|
||||||
|
"""
|
||||||
|
found_edges: List[Dict[str, any]] = []
|
||||||
|
# 1. Wikilinks [[rel:kind|target]]
|
||||||
|
inlines = re.findall(r'\[\[rel:([^\|\]]+)\|?([^\]]*)\]\]', text)
|
||||||
|
for kind, target in inlines:
|
||||||
|
k = kind.strip().lower()
|
||||||
|
t = target.strip()
|
||||||
|
if k and t:
|
||||||
|
found_edges.append({"edge": f"{k}:{t}", "is_callout": False})
|
||||||
|
|
||||||
|
# 2. Callout Edges > [!edge] kind
|
||||||
|
lines = text.split('\n')
|
||||||
|
current_edge_type = None
|
||||||
|
for line in lines:
|
||||||
|
stripped = line.strip()
|
||||||
|
callout_match = re.match(r'>+\s*\[!edge\]\s*([^:\s]+)', stripped)
|
||||||
|
if callout_match:
|
||||||
|
current_edge_type = callout_match.group(1).strip().lower()
|
||||||
|
# Links in der gleichen Zeile des Callouts
|
||||||
|
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
||||||
|
for l in links:
|
||||||
|
if "rel:" not in l:
|
||||||
|
found_edges.append({"edge": f"{current_edge_type}:{l}", "is_callout": True})
|
||||||
|
continue
|
||||||
|
# Links in Folgezeilen des Callouts
|
||||||
|
# WP-24c v4.2.9 Fix A: current_edge_type bleibt über Leerzeilen hinweg erhalten
|
||||||
|
# innerhalb eines Callout-Blocks, damit alle Links korrekt verarbeitet werden
|
||||||
|
if current_edge_type and stripped.startswith('>'):
|
||||||
|
# Fortsetzung des Callout-Blocks: Links extrahieren
|
||||||
|
links = re.findall(r'\[\[([^\]]+)\]\]', stripped)
|
||||||
|
for l in links:
|
||||||
|
if "rel:" not in l:
|
||||||
|
found_edges.append({"edge": f"{current_edge_type}:{l}", "is_callout": True})
|
||||||
|
elif current_edge_type and not stripped.startswith('>') and stripped:
|
||||||
|
# Nicht-Callout-Zeile mit Inhalt: Callout-Block beendet
|
||||||
|
current_edge_type = None
|
||||||
|
# Leerzeilen werden ignoriert - current_edge_type bleibt erhalten
|
||||||
|
return found_edges
|
||||||
@@ -0,0 +1,204 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_processor.py
|
||||||
|
DESCRIPTION: Der zentrale Orchestrator für das Chunking-System.
|
||||||
|
AUDIT v3.3.4: Wiederherstellung der "Gold-Standard" Qualität.
|
||||||
|
- Fix: Synchronisierung der Parameter (context_prefix) für alle Strategien.
|
||||||
|
- Integriert physikalische Kanten-Injektion (Propagierung).
|
||||||
|
- Stellt H1-Kontext-Fenster sicher.
|
||||||
|
- Baut den Candidate-Pool für die WP-15b Ingestion auf.
|
||||||
|
WP-24c v4.2.0: Konfigurierbare Header-Namen für LLM-Validierung.
|
||||||
|
WP-24c v4.2.5: Wiederherstellung der Chunking-Präzision
|
||||||
|
- Frontmatter-Override für chunking_profile
|
||||||
|
- Callout-Exclusion aus Chunks
|
||||||
|
- Strict-Mode ohne Carry-Over
|
||||||
|
WP-24c v4.2.6: Finale Härtung - "Semantic First, Clean Second"
|
||||||
|
- Callouts werden gechunkt (Chunk-Attribution), aber später entfernt (Clean-Context)
|
||||||
|
- remove_callouts_from_text erst nach propagate_section_edges und Candidate Pool
|
||||||
|
WP-24c v4.2.7: Wiederherstellung der Chunk-Attribution
|
||||||
|
- Callout-Kanten erhalten explicit:callout Provenance im candidate_pool
|
||||||
|
- graph_derive_edges.py erkennt diese und verhindert Note-Scope Duplikate
|
||||||
|
"""
|
||||||
|
import asyncio
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
from typing import List, Dict, Optional
|
||||||
|
from .chunking_models import Chunk
|
||||||
|
from .chunking_utils import get_chunk_config, extract_frontmatter_from_text
|
||||||
|
from .chunking_parser import parse_blocks, parse_edges_robust
|
||||||
|
from .chunking_strategies import strategy_sliding_window, strategy_by_heading
|
||||||
|
from .chunking_propagation import propagate_section_edges
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
async def assemble_chunks(note_id: str, md_text: str, note_type: str, config: Optional[Dict] = None) -> List[Chunk]:
|
||||||
|
"""
|
||||||
|
Hauptfunktion zur Zerlegung einer Note.
|
||||||
|
Verbindet Strategien mit physikalischer Kontext-Anreicherung.
|
||||||
|
WP-24c v4.2.5: Frontmatter-Override für chunking_profile wird berücksichtigt.
|
||||||
|
"""
|
||||||
|
# 1. WP-24c v4.2.5: Frontmatter VOR Konfiguration extrahieren (für Override)
|
||||||
|
fm, body_text = extract_frontmatter_from_text(md_text)
|
||||||
|
|
||||||
|
# 2. Konfiguration mit Frontmatter-Override
|
||||||
|
if config is None:
|
||||||
|
config = get_chunk_config(note_type, frontmatter=fm)
|
||||||
|
|
||||||
|
blocks, doc_title = parse_blocks(md_text)
|
||||||
|
|
||||||
|
# WP-24c v4.2.6: Filtere NUR Edge-Zonen (LLM-Validierung & Note-Scope)
|
||||||
|
# Callouts (is_meta_content=True) müssen durch, damit Chunk-Attribution erhalten bleibt
|
||||||
|
blocks_for_chunking = [b for b in blocks if not getattr(b, 'exclude_from_chunking', False)]
|
||||||
|
|
||||||
|
# Vorbereitung des H1-Präfix für die Embedding-Fenster (Breadcrumbs)
|
||||||
|
h1_prefix = f"# {doc_title}" if doc_title else ""
|
||||||
|
|
||||||
|
# 2. Anwendung der Splitting-Strategie
|
||||||
|
# Alle Strategien nutzen nun einheitlich context_prefix für die Window-Bildung.
|
||||||
|
# WP-24c v4.2.6: Callouts sind in blocks_for_chunking enthalten (für Chunk-Attribution)
|
||||||
|
if config.get("strategy") == "by_heading":
|
||||||
|
chunks = await asyncio.to_thread(
|
||||||
|
strategy_by_heading, blocks_for_chunking, config, note_id, context_prefix=h1_prefix
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
chunks = await asyncio.to_thread(
|
||||||
|
strategy_sliding_window, blocks_for_chunking, config, note_id, context_prefix=h1_prefix
|
||||||
|
)
|
||||||
|
|
||||||
|
if not chunks:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# 3. Physikalische Kontext-Anreicherung (Der Qualitäts-Fix)
|
||||||
|
# WP-24c v4.2.6: Arbeite auf Original-Text inkl. Callouts (für korrekte Chunk-Attribution)
|
||||||
|
# Schreibt Kanten aus Callouts/Inlines hart in den Text für Qdrant.
|
||||||
|
chunks = propagate_section_edges(chunks)
|
||||||
|
|
||||||
|
# 5. WP-15b: Candidate Pool Aufbau (Metadaten für IngestionService)
|
||||||
|
# WP-24c v4.2.7: Markiere Callout-Kanten explizit für Chunk-Attribution
|
||||||
|
# Zuerst die explizit im Text vorhandenen Kanten sammeln.
|
||||||
|
# WP-24c v4.4.0-DEBUG: Schnittstelle 1 - Extraktion
|
||||||
|
for idx, ch in enumerate(chunks):
|
||||||
|
# Wir extrahieren aus dem bereits (durch Propagation) angereicherten Text.
|
||||||
|
# ch.candidate_pool wird im Modell-Konstruktor als leere Liste initialisiert.
|
||||||
|
for edge_info in parse_edges_robust(ch.text):
|
||||||
|
edge_str = edge_info["edge"]
|
||||||
|
is_callout = edge_info.get("is_callout", False)
|
||||||
|
parts = edge_str.split(':', 1)
|
||||||
|
if len(parts) == 2:
|
||||||
|
k, t = parts
|
||||||
|
# WP-24c v4.2.7: Callout-Kanten erhalten explicit:callout Provenance
|
||||||
|
# WP-24c v4.4.1: Harmonisierung - Provenance muss exakt "explicit:callout" sein
|
||||||
|
provenance = "explicit:callout" if is_callout else "explicit"
|
||||||
|
# WP-24c v4.4.1: Verwende "to" für Kompatibilität (wird auch in graph_derive_edges.py erwartet)
|
||||||
|
# Zusätzlich "target_id" für maximale Kompatibilität mit ingestion_processor Validierung
|
||||||
|
pool_entry = {"kind": k, "to": t, "provenance": provenance}
|
||||||
|
if is_callout:
|
||||||
|
# WP-24c v4.4.1: Für Callouts auch "target_id" hinzufügen für Validierung
|
||||||
|
pool_entry["target_id"] = t
|
||||||
|
ch.candidate_pool.append(pool_entry)
|
||||||
|
|
||||||
|
# WP-24c v4.4.0-DEBUG: Schnittstelle 1 - Logging
|
||||||
|
if is_callout:
|
||||||
|
logger.debug(f"DEBUG-TRACER [Extraction]: Chunk Index: {idx}, Chunk ID: {ch.id}, Kind: {k}, Target: {t}, Provenance: {provenance}, Is_Callout: {is_callout}, Raw_Edge_Str: {edge_str}")
|
||||||
|
|
||||||
|
# 6. Global Pool (Unzugeordnete Kanten - kann mitten im Dokument oder am Ende stehen)
|
||||||
|
# WP-24c v4.2.0: Konfigurierbare Header-Namen und -Ebene via .env
|
||||||
|
# Sucht nach ALLEN Edge-Pool Blöcken im Original-Markdown (nicht nur am Ende).
|
||||||
|
llm_validation_headers = os.getenv(
|
||||||
|
"MINDNET_LLM_VALIDATION_HEADERS",
|
||||||
|
"Unzugeordnete Kanten,Edge Pool,Candidates"
|
||||||
|
)
|
||||||
|
header_list = [h.strip() for h in llm_validation_headers.split(",") if h.strip()]
|
||||||
|
# Fallback auf Defaults, falls leer
|
||||||
|
if not header_list:
|
||||||
|
header_list = ["Unzugeordnete Kanten", "Edge Pool", "Candidates"]
|
||||||
|
|
||||||
|
# Header-Ebene konfigurierbar (Default: 3 für ###)
|
||||||
|
llm_validation_level = int(os.getenv("MINDNET_LLM_VALIDATION_HEADER_LEVEL", "3"))
|
||||||
|
header_level_pattern = "#" * llm_validation_level
|
||||||
|
|
||||||
|
# Regex-Pattern mit konfigurierbaren Headern und Ebene
|
||||||
|
# WP-24c v4.2.0: finditer statt search, um ALLE Zonen zu finden (auch mitten im Dokument)
|
||||||
|
# Zone endet bei einem neuen Header (jeder Ebene) oder am Dokument-Ende
|
||||||
|
header_pattern = "|".join(re.escape(h) for h in header_list)
|
||||||
|
zone_pattern = rf'^{re.escape(header_level_pattern)}\s*(?:{header_pattern})\s*\n(.*?)(?=\n#|$)'
|
||||||
|
|
||||||
|
for pool_match in re.finditer(zone_pattern, body_text, re.DOTALL | re.IGNORECASE | re.MULTILINE):
|
||||||
|
global_edges = parse_edges_robust(pool_match.group(1))
|
||||||
|
for edge_info in global_edges:
|
||||||
|
edge_str = edge_info["edge"]
|
||||||
|
parts = edge_str.split(':', 1)
|
||||||
|
if len(parts) == 2:
|
||||||
|
k, t = parts
|
||||||
|
# Diese Kanten werden als "global_pool" markiert für die spätere KI-Prüfung.
|
||||||
|
for ch in chunks:
|
||||||
|
ch.candidate_pool.append({"kind": k, "to": t, "provenance": "global_pool"})
|
||||||
|
|
||||||
|
# 7. De-Duplikation des Pools & Linking
|
||||||
|
for ch in chunks:
|
||||||
|
seen = set()
|
||||||
|
unique = []
|
||||||
|
for c in ch.candidate_pool:
|
||||||
|
# Eindeutigkeit über Typ, Ziel und Herkunft (Provenance)
|
||||||
|
key = (c["kind"], c["to"], c["provenance"])
|
||||||
|
if key not in seen:
|
||||||
|
seen.add(key)
|
||||||
|
unique.append(c)
|
||||||
|
ch.candidate_pool = unique
|
||||||
|
|
||||||
|
# 8. WP-24c v4.2.6: Clean-Context - Entferne Callout-Syntax aus Chunk-Text
|
||||||
|
# WICHTIG: Dies geschieht NACH propagate_section_edges und Candidate Pool Aufbau,
|
||||||
|
# damit Chunk-Attribution erhalten bleibt und Kanten korrekt extrahiert werden.
|
||||||
|
# Hinweis: Callouts können mehrzeilig sein (auch verschachtelt: >>)
|
||||||
|
def remove_callouts_from_text(text: str) -> str:
|
||||||
|
"""Entfernt alle Callout-Zeilen (> [!edge] oder > [!abstract]) aus dem Text."""
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
|
||||||
|
lines = text.split('\n')
|
||||||
|
cleaned_lines = []
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
# NEU (v4.2.8):
|
||||||
|
# WP-24c v4.2.8: Callout-Pattern für Edge und Abstract
|
||||||
|
callout_start_pattern = re.compile(r'^>\s*\[!(edge|abstract)[^\]]*\]', re.IGNORECASE)
|
||||||
|
|
||||||
|
while i < len(lines):
|
||||||
|
line = lines[i]
|
||||||
|
callout_match = callout_start_pattern.match(line)
|
||||||
|
|
||||||
|
if callout_match:
|
||||||
|
# Callout gefunden: Überspringe alle Zeilen des Callout-Blocks
|
||||||
|
leading_gt_count = len(line) - len(line.lstrip('>'))
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Überspringe alle Zeilen, die zum Callout gehören
|
||||||
|
while i < len(lines):
|
||||||
|
next_line = lines[i]
|
||||||
|
if not next_line.strip().startswith('>'):
|
||||||
|
break
|
||||||
|
next_leading_gt = len(next_line) - len(next_line.lstrip('>'))
|
||||||
|
if next_leading_gt < leading_gt_count:
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
# Normale Zeile: Behalte
|
||||||
|
cleaned_lines.append(line)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Normalisiere Leerzeilen (max. 2 aufeinanderfolgende)
|
||||||
|
result = '\n'.join(cleaned_lines)
|
||||||
|
result = re.sub(r'\n\s*\n\s*\n+', '\n\n', result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
for ch in chunks:
|
||||||
|
ch.text = remove_callouts_from_text(ch.text)
|
||||||
|
if ch.window:
|
||||||
|
ch.window = remove_callouts_from_text(ch.window)
|
||||||
|
|
||||||
|
# Verknüpfung der Nachbarschaften für Graph-Traversierung
|
||||||
|
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
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_propagation.py
|
||||||
|
DESCRIPTION: Injiziert Sektions-Kanten physisch in den Text (Embedding-Enrichment).
|
||||||
|
Fix v3.3.6: Nutzt robustes Parsing zur Erkennung vorhandener Kanten,
|
||||||
|
um Dopplungen direkt hinter [!edge] Callouts format-agnostisch zu verhindern.
|
||||||
|
"""
|
||||||
|
from typing import List, Dict, Set
|
||||||
|
from .chunking_models import Chunk
|
||||||
|
from .chunking_parser import parse_edges_robust
|
||||||
|
|
||||||
|
def propagate_section_edges(chunks: List[Chunk]) -> List[Chunk]:
|
||||||
|
"""
|
||||||
|
Sammelt Kanten pro Sektion und schreibt sie hart in den Text und das Window.
|
||||||
|
Verhindert Dopplungen, wenn Kanten bereits via [!edge] Callout vorhanden sind.
|
||||||
|
"""
|
||||||
|
# 1. Sammeln: Alle expliziten Kanten pro Sektions-Pfad aggregieren
|
||||||
|
section_map: Dict[str, Set[str]] = {} # path -> set(kind:target)
|
||||||
|
|
||||||
|
for ch in chunks:
|
||||||
|
# Root-Level "/" ignorieren (zu global), Fokus auf spezifische Kapitel
|
||||||
|
if not ch.section_path or ch.section_path == "/":
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Nutzt den robusten Parser aus dem Package
|
||||||
|
# WP-24c v4.2.7: parse_edges_robust gibt jetzt Liste von Dicts zurück
|
||||||
|
edge_infos = parse_edges_robust(ch.text)
|
||||||
|
if edge_infos:
|
||||||
|
if ch.section_path not in section_map:
|
||||||
|
section_map[ch.section_path] = set()
|
||||||
|
for edge_info in edge_infos:
|
||||||
|
section_map[ch.section_path].add(edge_info["edge"])
|
||||||
|
|
||||||
|
# 2. Injizieren: Kanten in jeden Chunk der Sektion zurückschreiben (Broadcasting)
|
||||||
|
for ch in chunks:
|
||||||
|
if ch.section_path in section_map:
|
||||||
|
edges_to_add = section_map[ch.section_path]
|
||||||
|
if not edges_to_add:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Vorhandene Kanten (Typ:Ziel) in DIESEM Chunk ermitteln,
|
||||||
|
# um Dopplungen (z.B. durch Callouts) zu vermeiden.
|
||||||
|
# WP-24c v4.2.7: parse_edges_robust gibt jetzt Liste von Dicts zurück
|
||||||
|
existing_edge_infos = parse_edges_robust(ch.text)
|
||||||
|
existing_edges = {ei["edge"] for ei in existing_edge_infos}
|
||||||
|
|
||||||
|
injections = []
|
||||||
|
# Sortierung für deterministische Ergebnisse
|
||||||
|
for e_str in sorted(list(edges_to_add)):
|
||||||
|
# Wenn die Kante (Typ + Ziel) bereits vorhanden ist (egal welches Format),
|
||||||
|
# überspringen wir die Injektion für diesen Chunk.
|
||||||
|
if e_str in existing_edges:
|
||||||
|
continue
|
||||||
|
|
||||||
|
kind, target = e_str.split(':', 1)
|
||||||
|
injections.append(f"[[rel:{kind}|{target}]]")
|
||||||
|
|
||||||
|
if injections:
|
||||||
|
# Physische Anreicherung
|
||||||
|
# Triple-Newline für saubere Trennung im Embedding-Fenster
|
||||||
|
block = "\n\n\n" + " ".join(injections)
|
||||||
|
ch.text += block
|
||||||
|
|
||||||
|
# Auch ins Window schreiben, da Qdrant hier sucht!
|
||||||
|
if ch.window:
|
||||||
|
ch.window += block
|
||||||
|
else:
|
||||||
|
ch.window = ch.text
|
||||||
|
|
||||||
|
return chunks
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_strategies.py
|
||||||
|
DESCRIPTION: Strategien für atomares Sektions-Chunking v3.9.9.
|
||||||
|
Implementiert das 'Pack-and-Carry-Over' Verfahren nach Regel 1-3.
|
||||||
|
- Keine redundante Kanten-Injektion.
|
||||||
|
- Strikte Einhaltung von Sektionsgrenzen via Look-Ahead.
|
||||||
|
- Fix: Synchronisierung der Parameter mit dem Orchestrator (context_prefix).
|
||||||
|
WP-24c v4.2.5: Strict-Mode ohne Carry-Over - Bei strict_heading_split wird nach jeder Sektion geflasht.
|
||||||
|
"""
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
from .chunking_models import RawBlock, Chunk
|
||||||
|
from .chunking_utils import estimate_tokens
|
||||||
|
from .chunking_parser import split_sentences
|
||||||
|
|
||||||
|
def _create_win(context_prefix: str, sec_title: Optional[str], text: str) -> str:
|
||||||
|
"""Baut den Breadcrumb-Kontext für das Embedding-Fenster."""
|
||||||
|
parts = [context_prefix] if context_prefix else []
|
||||||
|
# Verhindert Dopplung, falls der Context-Prefix (H1) bereits den Sektionsnamen enthält
|
||||||
|
if sec_title and f"# {sec_title}" != context_prefix and sec_title not in (context_prefix or ""):
|
||||||
|
parts.append(sec_title)
|
||||||
|
prefix = " > ".join(parts)
|
||||||
|
return f"{prefix}\n{text}".strip() if prefix else text
|
||||||
|
|
||||||
|
def strategy_by_heading(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, context_prefix: str = "") -> List[Chunk]:
|
||||||
|
"""
|
||||||
|
Universelle Heading-Strategie mit Carry-Over Logik.
|
||||||
|
Synchronisiert auf context_prefix für Kompatibilität mit dem Orchestrator.
|
||||||
|
"""
|
||||||
|
smart_edge = config.get("enable_smart_edge_allocation", True)
|
||||||
|
strict = config.get("strict_heading_split", False)
|
||||||
|
target = config.get("target", 400)
|
||||||
|
max_tokens = config.get("max", 600)
|
||||||
|
split_level = config.get("split_level", 2)
|
||||||
|
overlap_cfg = config.get("overlap", (50, 80))
|
||||||
|
overlap = sum(overlap_cfg) // 2 if isinstance(overlap_cfg, (list, tuple)) else overlap_cfg
|
||||||
|
|
||||||
|
chunks: List[Chunk] = []
|
||||||
|
|
||||||
|
def _emit(txt, title, path):
|
||||||
|
"""Schreibt den finalen Chunk ohne Text-Modifikationen."""
|
||||||
|
idx = len(chunks)
|
||||||
|
win = _create_win(context_prefix, title, txt)
|
||||||
|
chunks.append(Chunk(
|
||||||
|
id=f"{note_id}#c{idx:02d}", note_id=note_id, index=idx,
|
||||||
|
text=txt, window=win, token_count=estimate_tokens(txt),
|
||||||
|
section_title=title, section_path=path, neighbors_prev=None, neighbors_next=None
|
||||||
|
))
|
||||||
|
|
||||||
|
# --- SCHRITT 1: Gruppierung in atomare Sektions-Einheiten ---
|
||||||
|
sections: List[Dict[str, Any]] = []
|
||||||
|
curr_blocks = []
|
||||||
|
for b in blocks:
|
||||||
|
if b.kind == "heading" and b.level <= split_level:
|
||||||
|
if curr_blocks:
|
||||||
|
sections.append({
|
||||||
|
"text": "\n\n".join([x.text for x in curr_blocks]),
|
||||||
|
"meta": curr_blocks[0],
|
||||||
|
"is_empty": len(curr_blocks) == 1 and curr_blocks[0].kind == "heading"
|
||||||
|
})
|
||||||
|
curr_blocks = [b]
|
||||||
|
else:
|
||||||
|
curr_blocks.append(b)
|
||||||
|
if curr_blocks:
|
||||||
|
sections.append({
|
||||||
|
"text": "\n\n".join([x.text for x in curr_blocks]),
|
||||||
|
"meta": curr_blocks[0],
|
||||||
|
"is_empty": len(curr_blocks) == 1 and curr_blocks[0].kind == "heading"
|
||||||
|
})
|
||||||
|
|
||||||
|
# --- SCHRITT 2: Verarbeitung der Queue ---
|
||||||
|
queue = list(sections)
|
||||||
|
current_chunk_text = ""
|
||||||
|
current_meta = {"title": None, "path": "/"}
|
||||||
|
|
||||||
|
# Bestimmung des Modus: Hard-Split wenn smart_edge=False ODER strict=True
|
||||||
|
is_hard_split_mode = (not smart_edge) or (strict)
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
item = queue.pop(0)
|
||||||
|
item_text = item["text"]
|
||||||
|
|
||||||
|
# Initialisierung für neuen Chunk
|
||||||
|
if not current_chunk_text:
|
||||||
|
current_meta["title"] = item["meta"].section_title
|
||||||
|
current_meta["path"] = item["meta"].section_path
|
||||||
|
|
||||||
|
# FALL A: HARD SPLIT MODUS (WP-24c v4.2.5: Strict-Mode ohne Carry-Over)
|
||||||
|
if is_hard_split_mode:
|
||||||
|
# WP-24c v4.2.5: Bei strict_heading_split: true wird nach JEDER Sektion geflasht
|
||||||
|
# Kein Carry-Over erlaubt, auch nicht für leere Überschriften
|
||||||
|
if current_chunk_text:
|
||||||
|
# Flashe vorherigen Chunk
|
||||||
|
_emit(current_chunk_text, current_meta["title"], current_meta["path"])
|
||||||
|
current_chunk_text = ""
|
||||||
|
|
||||||
|
# Neue Sektion: Initialisiere Meta
|
||||||
|
current_meta["title"] = item["meta"].section_title
|
||||||
|
current_meta["path"] = item["meta"].section_path
|
||||||
|
|
||||||
|
# WP-24c v4.2.5: Auch leere Sektionen werden als separater Chunk erstellt
|
||||||
|
# (nur Überschrift, kein Inhalt)
|
||||||
|
if item.get("is_empty", False):
|
||||||
|
# Leere Sektion: Nur Überschrift als Chunk
|
||||||
|
_emit(item_text, current_meta["title"], current_meta["path"])
|
||||||
|
else:
|
||||||
|
# Normale Sektion: Prüfe auf Token-Limit
|
||||||
|
if estimate_tokens(item_text) > max_tokens:
|
||||||
|
# Sektion zu groß: Smart Zerlegung (aber trotzdem in separaten Chunks)
|
||||||
|
sents = split_sentences(item_text)
|
||||||
|
header_prefix = item["meta"].text if item["meta"].kind == "heading" else ""
|
||||||
|
|
||||||
|
take_sents = []; take_len = 0
|
||||||
|
while sents:
|
||||||
|
s = sents.pop(0); slen = estimate_tokens(s)
|
||||||
|
if take_len + slen > target and take_sents:
|
||||||
|
_emit(" ".join(take_sents), current_meta["title"], current_meta["path"])
|
||||||
|
take_sents = [s]; take_len = slen
|
||||||
|
else:
|
||||||
|
take_sents.append(s); take_len += slen
|
||||||
|
|
||||||
|
if take_sents:
|
||||||
|
_emit(" ".join(take_sents), current_meta["title"], current_meta["path"])
|
||||||
|
else:
|
||||||
|
# Sektion passt: Direkt als Chunk
|
||||||
|
_emit(item_text, current_meta["title"], current_meta["path"])
|
||||||
|
|
||||||
|
current_chunk_text = ""
|
||||||
|
continue
|
||||||
|
|
||||||
|
# FALL B: SMART MODE (Regel 1-3)
|
||||||
|
combined_text = (current_chunk_text + "\n\n" + item_text).strip() if current_chunk_text else item_text
|
||||||
|
combined_est = estimate_tokens(combined_text)
|
||||||
|
|
||||||
|
if combined_est <= max_tokens:
|
||||||
|
# Regel 1 & 2: Passt rein laut Schätzung -> Aufnehmen
|
||||||
|
current_chunk_text = combined_text
|
||||||
|
else:
|
||||||
|
if current_chunk_text:
|
||||||
|
# Regel 2: Flashen an Sektionsgrenze, Item zurücklegen
|
||||||
|
_emit(current_chunk_text, current_meta["title"], current_meta["path"])
|
||||||
|
current_chunk_text = ""
|
||||||
|
queue.insert(0, item)
|
||||||
|
else:
|
||||||
|
# Regel 3: Einzelne Sektion zu groß -> Smart Zerlegung
|
||||||
|
sents = split_sentences(item_text)
|
||||||
|
header_prefix = item["meta"].text if item["meta"].kind == "heading" else ""
|
||||||
|
|
||||||
|
take_sents = []; take_len = 0
|
||||||
|
while sents:
|
||||||
|
s = sents.pop(0); slen = estimate_tokens(s)
|
||||||
|
if take_len + slen > target and take_sents:
|
||||||
|
sents.insert(0, s); break
|
||||||
|
take_sents.append(s); take_len += slen
|
||||||
|
|
||||||
|
_emit(" ".join(take_sents), current_meta["title"], current_meta["path"])
|
||||||
|
|
||||||
|
if sents:
|
||||||
|
remainder = " ".join(sents)
|
||||||
|
# Kontext-Erhalt: Überschrift für den Rest wiederholen
|
||||||
|
if header_prefix and not remainder.startswith(header_prefix):
|
||||||
|
remainder = header_prefix + "\n\n" + remainder
|
||||||
|
# Carry-Over: Rest wird vorne in die Queue geschoben
|
||||||
|
queue.insert(0, {"text": remainder, "meta": item["meta"], "is_split": True})
|
||||||
|
|
||||||
|
if current_chunk_text:
|
||||||
|
_emit(current_chunk_text, current_meta["title"], current_meta["path"])
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
def strategy_sliding_window(blocks: List[RawBlock], config: Dict[str, Any], note_id: str, context_prefix: str = "") -> List[Chunk]:
|
||||||
|
"""Standard-Sliding-Window für flache Texte ohne Sektionsfokus."""
|
||||||
|
target = config.get("target", 400); max_tokens = config.get("max", 600)
|
||||||
|
chunks: List[Chunk] = []; buf: List[RawBlock] = []
|
||||||
|
|
||||||
|
for b in blocks:
|
||||||
|
b_tokens = estimate_tokens(b.text)
|
||||||
|
curr_tokens = sum(estimate_tokens(x.text) for x in buf) if buf else 0
|
||||||
|
if curr_tokens + b_tokens > max_tokens and buf:
|
||||||
|
txt = "\n\n".join([x.text for x in buf]); idx = len(chunks)
|
||||||
|
win = _create_win(context_prefix, buf[0].section_title, txt)
|
||||||
|
chunks.append(Chunk(id=f"{note_id}#c{idx:02d}", note_id=note_id, index=idx, text=txt, window=win, token_count=curr_tokens, section_title=buf[0].section_title, section_path=buf[0].section_path, neighbors_prev=None, neighbors_next=None))
|
||||||
|
buf = []
|
||||||
|
buf.append(b)
|
||||||
|
|
||||||
|
if buf:
|
||||||
|
txt = "\n\n".join([x.text for x in buf]); idx = len(chunks)
|
||||||
|
win = _create_win(context_prefix, buf[0].section_title, txt)
|
||||||
|
chunks.append(Chunk(id=f"{note_id}#c{idx:02d}", note_id=note_id, index=idx, text=txt, window=win, token_count=estimate_tokens(txt), section_title=buf[0].section_title, section_path=buf[0].section_path, neighbors_prev=None, neighbors_next=None))
|
||||||
|
|
||||||
|
return chunks
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/chunking/chunking_utils.py
|
||||||
|
DESCRIPTION: Hilfswerkzeuge für Token-Schätzung und YAML-Konfiguration.
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
import yaml
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Dict, Any, Tuple, Optional
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
|
||||||
|
CONFIG_PATH = BASE_DIR / "config" / "types.yaml"
|
||||||
|
DEFAULT_PROFILE = {"strategy": "sliding_window", "target": 400, "max": 600, "overlap": (50, 80)}
|
||||||
|
|
||||||
|
_CONFIG_CACHE = None
|
||||||
|
|
||||||
|
def load_yaml_config() -> Dict[str, Any]:
|
||||||
|
global _CONFIG_CACHE
|
||||||
|
if _CONFIG_CACHE is not None: return _CONFIG_CACHE
|
||||||
|
if not CONFIG_PATH.exists(): return {}
|
||||||
|
try:
|
||||||
|
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f)
|
||||||
|
_CONFIG_CACHE = data
|
||||||
|
return data
|
||||||
|
except Exception: return {}
|
||||||
|
|
||||||
|
def get_chunk_config(note_type: str, frontmatter: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Lädt die Chunking-Strategie basierend auf dem Note-Type.
|
||||||
|
WP-24c v4.2.5: Frontmatter-Override für chunking_profile hat höchste Priorität.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
note_type: Der Typ der Note (z.B. "decision", "experience")
|
||||||
|
frontmatter: Optionales Frontmatter-Dict mit chunking_profile Override
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict mit Chunking-Konfiguration
|
||||||
|
"""
|
||||||
|
full_config = load_yaml_config()
|
||||||
|
profiles = full_config.get("chunking_profiles", {})
|
||||||
|
type_def = full_config.get("types", {}).get(note_type.lower(), {})
|
||||||
|
|
||||||
|
# WP-24c v4.2.5: Priorität: Frontmatter > Type-Def > Defaults
|
||||||
|
profile_name = None
|
||||||
|
if frontmatter and "chunking_profile" in frontmatter:
|
||||||
|
profile_name = frontmatter.get("chunking_profile") or frontmatter.get("chunk_profile")
|
||||||
|
if not profile_name:
|
||||||
|
profile_name = type_def.get("chunking_profile")
|
||||||
|
if not profile_name:
|
||||||
|
profile_name = full_config.get("defaults", {}).get("chunking_profile", "sliding_standard")
|
||||||
|
|
||||||
|
config = profiles.get(profile_name, DEFAULT_PROFILE).copy()
|
||||||
|
if "overlap" in config and isinstance(config["overlap"], list):
|
||||||
|
config["overlap"] = tuple(config["overlap"])
|
||||||
|
return config
|
||||||
|
|
||||||
|
def estimate_tokens(text: str) -> int:
|
||||||
|
"""Grobe Schätzung der Token-Anzahl."""
|
||||||
|
return max(1, math.ceil(len(text.strip()) / 4))
|
||||||
|
|
||||||
|
def extract_frontmatter_from_text(md_text: str) -> Tuple[Dict[str, Any], str]:
|
||||||
|
"""Trennt YAML-Frontmatter vom Text."""
|
||||||
|
import re
|
||||||
|
fm_match = re.match(r'^\s*---\s*\n(.*?)\n---', md_text, re.DOTALL)
|
||||||
|
if not fm_match: return {}, md_text
|
||||||
|
try:
|
||||||
|
frontmatter = yaml.safe_load(fm_match.group(1))
|
||||||
|
if not isinstance(frontmatter, dict): frontmatter = {}
|
||||||
|
except Exception: frontmatter = {}
|
||||||
|
text_without_fm = re.sub(r'^\s*---\s*\n(.*?)\n---', '', md_text, flags=re.DOTALL)
|
||||||
|
return frontmatter, text_without_fm.strip()
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
"""
|
||||||
|
PACKAGE: app.core.database
|
||||||
|
DESCRIPTION: Zentrale Schnittstelle für alle Datenbank-Operationen (Qdrant).
|
||||||
|
Bündelt Client-Initialisierung und Point-Konvertierung.
|
||||||
|
"""
|
||||||
|
from .qdrant import (
|
||||||
|
QdrantConfig,
|
||||||
|
get_client,
|
||||||
|
ensure_collections,
|
||||||
|
ensure_payload_indexes,
|
||||||
|
collection_names
|
||||||
|
)
|
||||||
|
from .qdrant_points import (
|
||||||
|
points_for_note,
|
||||||
|
points_for_chunks,
|
||||||
|
points_for_edges,
|
||||||
|
upsert_batch,
|
||||||
|
get_edges_for_sources,
|
||||||
|
search_chunks_by_vector
|
||||||
|
)
|
||||||
|
|
||||||
|
# Öffentlicher Export für das Gesamtsystem
|
||||||
|
__all__ = [
|
||||||
|
"QdrantConfig",
|
||||||
|
"get_client",
|
||||||
|
"ensure_collections",
|
||||||
|
"ensure_payload_indexes",
|
||||||
|
"collection_names",
|
||||||
|
"points_for_note",
|
||||||
|
"points_for_chunks",
|
||||||
|
"points_for_edges",
|
||||||
|
"upsert_batch",
|
||||||
|
"get_edges_for_sources",
|
||||||
|
"search_chunks_by_vector"
|
||||||
|
]
|
||||||
@@ -1,20 +1,23 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/core/qdrant.py
|
FILE: app/core/database/qdrant.py
|
||||||
DESCRIPTION: Qdrant-Client Factory und Schema-Management. Erstellt Collections und Payload-Indizes.
|
DESCRIPTION: Qdrant-Client Factory und Schema-Management.
|
||||||
VERSION: 2.2.0
|
Erstellt Collections und Payload-Indizes.
|
||||||
|
MODULARISIERUNG: Verschoben in das database-Paket für WP-14.
|
||||||
|
VERSION: 2.2.2 (WP-Fix: Index für target_section)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: qdrant_client, dataclasses, os
|
DEPENDENCIES: qdrant_client, dataclasses, os
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import logging
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Optional, Tuple, Dict, List
|
from typing import Optional, Tuple, Dict, List
|
||||||
|
|
||||||
from qdrant_client import QdrantClient
|
from qdrant_client import QdrantClient
|
||||||
from qdrant_client.http import models as rest
|
from qdrant_client.http import models as rest
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Konfiguration
|
# Konfiguration
|
||||||
@@ -22,6 +25,7 @@ from qdrant_client.http import models as rest
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class QdrantConfig:
|
class QdrantConfig:
|
||||||
|
"""Konfigurationsobjekt für den Qdrant-Verbindungsaufbau."""
|
||||||
host: Optional[str] = None
|
host: Optional[str] = None
|
||||||
port: Optional[int] = None
|
port: Optional[int] = None
|
||||||
url: Optional[str] = None
|
url: Optional[str] = None
|
||||||
@@ -33,16 +37,20 @@ class QdrantConfig:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_env(cls) -> "QdrantConfig":
|
def from_env(cls) -> "QdrantConfig":
|
||||||
|
"""Erstellt die Konfiguration aus Umgebungsvariablen."""
|
||||||
# Entweder URL ODER Host/Port, API-Key optional
|
# Entweder URL ODER Host/Port, API-Key optional
|
||||||
url = os.getenv("QDRANT_URL") or None
|
url = os.getenv("QDRANT_URL") or None
|
||||||
host = os.getenv("QDRANT_HOST") or None
|
host = os.getenv("QDRANT_HOST") or None
|
||||||
port = os.getenv("QDRANT_PORT")
|
port = os.getenv("QDRANT_PORT")
|
||||||
port = int(port) if port else None
|
port = int(port) if port else None
|
||||||
api_key = os.getenv("QDRANT_API_KEY") or None
|
api_key = os.getenv("QDRANT_API_KEY") or None
|
||||||
prefix = os.getenv("COLLECTION_PREFIX") or "mindnet"
|
# WP-24c v4.5.10: Harmonisierung - Unterstützt beide Umgebungsvariablen für Abwärtskompatibilität
|
||||||
|
# COLLECTION_PREFIX hat Priorität, MINDNET_PREFIX als Fallback
|
||||||
|
prefix = os.getenv("COLLECTION_PREFIX") or os.getenv("MINDNET_PREFIX") or "mindnet"
|
||||||
dim = int(os.getenv("VECTOR_DIM") or 384)
|
dim = int(os.getenv("VECTOR_DIM") or 384)
|
||||||
distance = os.getenv("DISTANCE", "Cosine")
|
distance = os.getenv("DISTANCE", "Cosine")
|
||||||
on_disk_payload = (os.getenv("ON_DISK_PAYLOAD", "true").lower() == "true")
|
on_disk_payload = (os.getenv("ON_DISK_PAYLOAD", "true").lower() == "true")
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
host=host, port=port, url=url, api_key=api_key,
|
host=host, port=port, url=url, api_key=api_key,
|
||||||
prefix=prefix, dim=dim, distance=distance, on_disk_payload=on_disk_payload
|
prefix=prefix, dim=dim, distance=distance, on_disk_payload=on_disk_payload
|
||||||
@@ -50,6 +58,7 @@ class QdrantConfig:
|
|||||||
|
|
||||||
|
|
||||||
def get_client(cfg: QdrantConfig) -> QdrantClient:
|
def get_client(cfg: QdrantConfig) -> QdrantClient:
|
||||||
|
"""Initialisiert den Qdrant-Client basierend auf der Konfiguration."""
|
||||||
# QdrantClient akzeptiert entweder url=... oder host/port
|
# QdrantClient akzeptiert entweder url=... oder host/port
|
||||||
if cfg.url:
|
if cfg.url:
|
||||||
return QdrantClient(url=cfg.url, api_key=cfg.api_key, timeout=60.0)
|
return QdrantClient(url=cfg.url, api_key=cfg.api_key, timeout=60.0)
|
||||||
@@ -61,17 +70,19 @@ def get_client(cfg: QdrantConfig) -> QdrantClient:
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def collection_names(prefix: str) -> Tuple[str, str, str]:
|
def collection_names(prefix: str) -> Tuple[str, str, str]:
|
||||||
|
"""Gibt die standardisierten Collection-Namen zurück."""
|
||||||
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
|
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
|
||||||
|
|
||||||
|
|
||||||
def _vector_params(dim: int, distance: str) -> rest.VectorParams:
|
def _vector_params(dim: int, distance: str) -> rest.VectorParams:
|
||||||
|
"""Erstellt Vektor-Parameter für das Collection-Schema."""
|
||||||
# Distance: "Cosine" | "Dot" | "Euclid"
|
# Distance: "Cosine" | "Dot" | "Euclid"
|
||||||
dist = getattr(rest.Distance, distance.capitalize(), rest.Distance.COSINE)
|
dist = getattr(rest.Distance, distance.capitalize(), rest.Distance.COSINE)
|
||||||
return rest.VectorParams(size=dim, distance=dist)
|
return rest.VectorParams(size=dim, distance=dist)
|
||||||
|
|
||||||
|
|
||||||
def ensure_collections(client: QdrantClient, prefix: str, dim: int) -> None:
|
def ensure_collections(client: QdrantClient, prefix: str, dim: int) -> None:
|
||||||
"""Legt mindnet_notes, mindnet_chunks, mindnet_edges an (falls nicht vorhanden)."""
|
"""Legt notes, chunks und edges Collections an, falls nicht vorhanden."""
|
||||||
notes, chunks, edges = collection_names(prefix)
|
notes, chunks, edges = collection_names(prefix)
|
||||||
|
|
||||||
# notes
|
# notes
|
||||||
@@ -88,7 +99,7 @@ def ensure_collections(client: QdrantClient, prefix: str, dim: int) -> None:
|
|||||||
vectors_config=_vector_params(dim, os.getenv("DISTANCE", "Cosine")),
|
vectors_config=_vector_params(dim, os.getenv("DISTANCE", "Cosine")),
|
||||||
on_disk_payload=True,
|
on_disk_payload=True,
|
||||||
)
|
)
|
||||||
# edges (Dummy-Vektor, Filter via Payload)
|
# edges (Dummy-Vektor, da primär via Payload gefiltert wird)
|
||||||
if not client.collection_exists(edges):
|
if not client.collection_exists(edges):
|
||||||
client.create_collection(
|
client.create_collection(
|
||||||
collection_name=edges,
|
collection_name=edges,
|
||||||
@@ -102,21 +113,20 @@ def ensure_collections(client: QdrantClient, prefix: str, dim: int) -> None:
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def _ensure_index(client: QdrantClient, collection: str, field: str, schema: rest.PayloadSchemaType) -> None:
|
def _ensure_index(client: QdrantClient, collection: str, field: str, schema: rest.PayloadSchemaType) -> None:
|
||||||
"""Idempotentes Anlegen eines Payload-Indexes für ein Feld."""
|
"""Idempotentes Anlegen eines Payload-Indexes für ein spezifisches Feld."""
|
||||||
try:
|
try:
|
||||||
client.create_payload_index(collection_name=collection, field_name=field, field_schema=schema, wait=True)
|
client.create_payload_index(collection_name=collection, field_name=field, field_schema=schema, wait=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Fehler ignorieren, falls Index bereits existiert oder Server "already indexed" meldet.
|
# Fehler ignorieren, falls Index bereits existiert
|
||||||
# Für Debugging ggf. Logging ergänzen.
|
logger.debug(f"Index check for {field} in {collection}: {e}")
|
||||||
_ = e
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_payload_indexes(client: QdrantClient, prefix: str) -> None:
|
def ensure_payload_indexes(client: QdrantClient, prefix: str) -> None:
|
||||||
"""
|
"""
|
||||||
Stellt sicher, dass alle benötigten Payload-Indizes existieren.
|
Stellt sicher, dass alle benötigten Payload-Indizes für die Suche existieren.
|
||||||
- notes: note_id(KEYWORD), type(KEYWORD), title(TEXT), updated(INTEGER), tags(KEYWORD)
|
- notes: note_id, type, title, updated, tags
|
||||||
- chunks: note_id(KEYWORD), chunk_id(KEYWORD), index(INTEGER), type(KEYWORD), tags(KEYWORD)
|
- chunks: note_id, chunk_id, index, type, tags
|
||||||
- edges: note_id(KEYWORD), kind(KEYWORD), scope(KEYWORD), source_id(KEYWORD), target_id(KEYWORD), chunk_id(KEYWORD)
|
- edges: note_id, kind, scope, source_id, target_id, chunk_id, target_section
|
||||||
"""
|
"""
|
||||||
notes, chunks, edges = collection_names(prefix)
|
notes, chunks, edges = collection_names(prefix)
|
||||||
|
|
||||||
@@ -148,6 +158,8 @@ def ensure_payload_indexes(client: QdrantClient, prefix: str) -> None:
|
|||||||
("source_id", rest.PayloadSchemaType.KEYWORD),
|
("source_id", rest.PayloadSchemaType.KEYWORD),
|
||||||
("target_id", rest.PayloadSchemaType.KEYWORD),
|
("target_id", rest.PayloadSchemaType.KEYWORD),
|
||||||
("chunk_id", rest.PayloadSchemaType.KEYWORD),
|
("chunk_id", rest.PayloadSchemaType.KEYWORD),
|
||||||
|
# NEU: Index für Section-Links (WP-15b)
|
||||||
|
("target_section", rest.PayloadSchemaType.KEYWORD),
|
||||||
]:
|
]:
|
||||||
_ensure_index(client, edges, field, schema)
|
_ensure_index(client, edges, field, schema)
|
||||||
|
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/core/qdrant_points.py
|
FILE: app/core/database/qdrant_points.py
|
||||||
DESCRIPTION: Object-Mapper für Qdrant. Konvertiert JSON-Payloads (Notes, Chunks, Edges) in PointStructs und generiert deterministische UUIDs.
|
DESCRIPTION: Object-Mapper für Qdrant. Konvertiert JSON-Payloads (Notes, Chunks, Edges)
|
||||||
VERSION: 1.5.0
|
in PointStructs und generiert deterministische UUIDs.
|
||||||
|
VERSION: 4.1.0 (WP-24c: Gold-Standard Identity v4.1.0 - target_section Support)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: qdrant_client, uuid, os
|
DEPENDENCIES: qdrant_client, uuid, os, app.core.graph.graph_utils
|
||||||
LAST_ANALYSIS: 2025-12-15
|
LAST_ANALYSIS: 2026-01-10
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
@@ -14,25 +15,44 @@ from typing import List, Tuple, Iterable, Optional, Dict, Any
|
|||||||
from qdrant_client.http import models as rest
|
from qdrant_client.http import models as rest
|
||||||
from qdrant_client import QdrantClient
|
from qdrant_client import QdrantClient
|
||||||
|
|
||||||
|
# WP-24c: Import der zentralen Identitäts-Logik zur Vermeidung von ID-Drift
|
||||||
|
from app.core.graph.graph_utils import _mk_edge_id
|
||||||
|
|
||||||
# --------------------- ID helpers ---------------------
|
# --------------------- ID helpers ---------------------
|
||||||
|
|
||||||
def _to_uuid(stable_key: str) -> str:
|
def _to_uuid(stable_key: str) -> str:
|
||||||
return str(uuid.uuid5(uuid.NAMESPACE_URL, stable_key))
|
"""
|
||||||
|
Erzeugt eine deterministische UUIDv5 basierend auf einem stabilen Schlüssel.
|
||||||
|
Härtung v1.5.2: Guard gegen leere Schlüssel zur Vermeidung von Pydantic-Fehlern.
|
||||||
|
"""
|
||||||
|
if not stable_key:
|
||||||
|
raise ValueError("UUID generation failed: stable_key is empty or None")
|
||||||
|
return str(uuid.uuid5(uuid.NAMESPACE_URL, str(stable_key)))
|
||||||
|
|
||||||
def _names(prefix: str) -> Tuple[str, str, str]:
|
def _names(prefix: str) -> Tuple[str, str, str]:
|
||||||
|
"""Interne Auflösung der Collection-Namen basierend auf dem Präfix."""
|
||||||
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
|
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
|
||||||
|
|
||||||
# --------------------- Points builders ---------------------
|
# --------------------- Points builders ---------------------
|
||||||
|
|
||||||
def points_for_note(prefix: str, note_payload: dict, note_vec: List[float] | None, dim: int) -> Tuple[str, List[rest.PointStruct]]:
|
def points_for_note(prefix: str, note_payload: dict, note_vec: List[float] | None, dim: int) -> Tuple[str, List[rest.PointStruct]]:
|
||||||
|
"""Konvertiert Note-Metadaten in Qdrant Points."""
|
||||||
notes_col, _, _ = _names(prefix)
|
notes_col, _, _ = _names(prefix)
|
||||||
|
# Nutzt Null-Vektor als Fallback, falls kein Embedding vorhanden ist
|
||||||
vector = note_vec if note_vec is not None else [0.0] * int(dim)
|
vector = note_vec if note_vec is not None else [0.0] * int(dim)
|
||||||
|
|
||||||
raw_note_id = note_payload.get("note_id") or note_payload.get("id") or "missing-note-id"
|
raw_note_id = note_payload.get("note_id") or note_payload.get("id") or "missing-note-id"
|
||||||
point_id = _to_uuid(raw_note_id)
|
point_id = _to_uuid(raw_note_id)
|
||||||
pt = rest.PointStruct(id=point_id, vector=vector, payload=note_payload)
|
|
||||||
|
pt = rest.PointStruct(
|
||||||
|
id=point_id,
|
||||||
|
vector=vector,
|
||||||
|
payload=note_payload
|
||||||
|
)
|
||||||
return notes_col, [pt]
|
return notes_col, [pt]
|
||||||
|
|
||||||
def points_for_chunks(prefix: str, chunk_payloads: List[dict], vectors: List[List[float]]) -> Tuple[str, List[rest.PointStruct]]:
|
def points_for_chunks(prefix: str, chunk_payloads: List[dict], vectors: List[List[float]]) -> Tuple[str, List[rest.PointStruct]]:
|
||||||
|
"""Konvertiert Chunks und deren Vektoren in Qdrant Points."""
|
||||||
_, chunks_col, _ = _names(prefix)
|
_, chunks_col, _ = _names(prefix)
|
||||||
points: List[rest.PointStruct] = []
|
points: List[rest.PointStruct] = []
|
||||||
for i, (pl, vec) in enumerate(zip(chunk_payloads, vectors), start=1):
|
for i, (pl, vec) in enumerate(zip(chunk_payloads, vectors), start=1):
|
||||||
@@ -41,43 +61,93 @@ def points_for_chunks(prefix: str, chunk_payloads: List[dict], vectors: List[Lis
|
|||||||
note_id = pl.get("note_id") or pl.get("parent_note_id") or "missing-note"
|
note_id = pl.get("note_id") or pl.get("parent_note_id") or "missing-note"
|
||||||
chunk_id = f"{note_id}#{i}"
|
chunk_id = f"{note_id}#{i}"
|
||||||
pl["chunk_id"] = chunk_id
|
pl["chunk_id"] = chunk_id
|
||||||
|
|
||||||
point_id = _to_uuid(chunk_id)
|
point_id = _to_uuid(chunk_id)
|
||||||
points.append(rest.PointStruct(id=point_id, vector=vec, payload=pl))
|
points.append(rest.PointStruct(
|
||||||
|
id=point_id,
|
||||||
|
vector=vec,
|
||||||
|
payload=pl
|
||||||
|
))
|
||||||
return chunks_col, points
|
return chunks_col, points
|
||||||
|
|
||||||
def _normalize_edge_payload(pl: dict) -> dict:
|
def _normalize_edge_payload(pl: dict) -> dict:
|
||||||
|
"""Normalisiert Edge-Felder und sichert Schema-Konformität."""
|
||||||
kind = pl.get("kind") or pl.get("edge_type") or "edge"
|
kind = pl.get("kind") or pl.get("edge_type") or "edge"
|
||||||
source_id = pl.get("source_id") or pl.get("src_id") or "unknown-src"
|
source_id = pl.get("source_id") or pl.get("src_id") or "unknown-src"
|
||||||
target_id = pl.get("target_id") or pl.get("dst_id") or "unknown-tgt"
|
target_id = pl.get("target_id") or pl.get("dst_id") or "unknown-tgt"
|
||||||
seq = pl.get("seq") or pl.get("order") or pl.get("index")
|
seq = pl.get("seq") or pl.get("order") or pl.get("index")
|
||||||
|
|
||||||
|
# WP-Fix: target_section explizit durchreichen
|
||||||
|
target_section = pl.get("target_section")
|
||||||
|
|
||||||
pl.setdefault("kind", kind)
|
pl.setdefault("kind", kind)
|
||||||
pl.setdefault("source_id", source_id)
|
pl.setdefault("source_id", source_id)
|
||||||
pl.setdefault("target_id", target_id)
|
pl.setdefault("target_id", target_id)
|
||||||
|
|
||||||
if seq is not None and "seq" not in pl:
|
if seq is not None and "seq" not in pl:
|
||||||
pl["seq"] = seq
|
pl["seq"] = seq
|
||||||
|
|
||||||
|
if target_section is not None:
|
||||||
|
pl["target_section"] = target_section
|
||||||
|
|
||||||
return pl
|
return pl
|
||||||
|
|
||||||
def points_for_edges(prefix: str, edge_payloads: List[dict]) -> Tuple[str, List[rest.PointStruct]]:
|
def points_for_edges(prefix: str, edge_payloads: List[dict]) -> Tuple[str, List[rest.PointStruct]]:
|
||||||
|
"""
|
||||||
|
Konvertiert Kanten-Payloads in PointStructs.
|
||||||
|
WP-24c v4.1.0: Nutzt die zentrale _mk_edge_id Funktion aus graph_utils.
|
||||||
|
Dies eliminiert den ID-Drift zwischen manuellen und virtuellen Kanten.
|
||||||
|
|
||||||
|
GOLD-STANDARD v4.1.0: Die ID-Generierung verwendet 4 Parameter + optional target_section
|
||||||
|
(kind, source_id, target_id, scope, target_section).
|
||||||
|
rule_id und variant werden ignoriert, target_section fließt ein (Multigraph-Support).
|
||||||
|
"""
|
||||||
_, _, edges_col = _names(prefix)
|
_, _, edges_col = _names(prefix)
|
||||||
points: List[rest.PointStruct] = []
|
points: List[rest.PointStruct] = []
|
||||||
|
|
||||||
for raw in edge_payloads:
|
for raw in edge_payloads:
|
||||||
pl = _normalize_edge_payload(raw)
|
pl = _normalize_edge_payload(raw)
|
||||||
edge_id = pl.get("edge_id")
|
|
||||||
if not edge_id:
|
# Extraktion der Identitäts-Parameter (GOLD-STANDARD v4.1.0)
|
||||||
kind = pl.get("kind", "edge")
|
kind = pl.get("kind", "edge")
|
||||||
s = pl.get("source_id", "unknown-src")
|
s = pl.get("source_id", "unknown-src")
|
||||||
t = pl.get("target_id", "unknown-tgt")
|
t = pl.get("target_id", "unknown-tgt")
|
||||||
seq = pl.get("seq") or ""
|
scope = pl.get("scope", "note")
|
||||||
edge_id = f"{kind}:{s}->{t}#{seq}"
|
target_section = pl.get("target_section") # WP-24c v4.1.0: target_section für Section-Links
|
||||||
pl["edge_id"] = edge_id
|
|
||||||
point_id = _to_uuid(edge_id)
|
# Hinweis: rule_id und variant werden im Payload gespeichert,
|
||||||
points.append(rest.PointStruct(id=point_id, vector=[0.0], payload=pl))
|
# fließen aber NICHT in die ID-Generierung ein (v4.0.0 Standard)
|
||||||
|
# target_section fließt in die ID ein (v4.1.0: Multigraph-Support für Section-Links)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Aufruf der Single-Source-of-Truth für IDs
|
||||||
|
# GOLD-STANDARD v4.1.0: 4 Parameter + optional target_section
|
||||||
|
point_id = _mk_edge_id(
|
||||||
|
kind=kind,
|
||||||
|
s=s,
|
||||||
|
t=t,
|
||||||
|
scope=scope,
|
||||||
|
target_section=target_section
|
||||||
|
)
|
||||||
|
|
||||||
|
# Synchronisierung des Payloads mit der berechneten ID
|
||||||
|
pl["edge_id"] = point_id
|
||||||
|
|
||||||
|
points.append(rest.PointStruct(
|
||||||
|
id=point_id,
|
||||||
|
vector=[0.0],
|
||||||
|
payload=pl
|
||||||
|
))
|
||||||
|
except ValueError as e:
|
||||||
|
# Fehlerhaft definierte Kanten werden übersprungen, um Pydantic-Crashes zu vermeiden
|
||||||
|
continue
|
||||||
|
|
||||||
return edges_col, points
|
return edges_col, points
|
||||||
|
|
||||||
# --------------------- Vector schema & overrides ---------------------
|
# --------------------- Vector schema & overrides ---------------------
|
||||||
|
|
||||||
def _preferred_name(candidates: List[str]) -> str:
|
def _preferred_name(candidates: List[str]) -> str:
|
||||||
|
"""Ermittelt den primären Vektor-Namen aus einer Liste von Kandidaten."""
|
||||||
for k in ("text", "default", "embedding", "content"):
|
for k in ("text", "default", "embedding", "content"):
|
||||||
if k in candidates:
|
if k in candidates:
|
||||||
return k
|
return k
|
||||||
@@ -85,10 +155,11 @@ def _preferred_name(candidates: List[str]) -> str:
|
|||||||
|
|
||||||
def _env_override_for_collection(collection: str) -> Optional[str]:
|
def _env_override_for_collection(collection: str) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
|
Prüft auf Umgebungsvariablen-Overrides für Vektor-Namen.
|
||||||
Returns:
|
Returns:
|
||||||
- "__single__" to force single-vector
|
- "__single__" für erzwungenen Single-Vector Modus
|
||||||
- concrete name (str) to force named-vector with that name
|
- Name (str) für spezifischen Named-Vector
|
||||||
- None to auto-detect
|
- None für automatische Erkennung
|
||||||
"""
|
"""
|
||||||
base = os.getenv("MINDNET_VECTOR_NAME")
|
base = os.getenv("MINDNET_VECTOR_NAME")
|
||||||
if collection.endswith("_notes"):
|
if collection.endswith("_notes"):
|
||||||
@@ -103,19 +174,17 @@ def _env_override_for_collection(collection: str) -> Optional[str]:
|
|||||||
val = base.strip()
|
val = base.strip()
|
||||||
if val.lower() in ("__single__", "single"):
|
if val.lower() in ("__single__", "single"):
|
||||||
return "__single__"
|
return "__single__"
|
||||||
return val # concrete name
|
return val
|
||||||
|
|
||||||
def _get_vector_schema(client: QdrantClient, collection_name: str) -> dict:
|
def _get_vector_schema(client: QdrantClient, collection_name: str) -> dict:
|
||||||
"""
|
"""Ermittelt das Vektor-Schema einer existierenden Collection via API."""
|
||||||
Return {"kind": "single", "size": int} or {"kind": "named", "names": [...], "primary": str}.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
info = client.get_collection(collection_name=collection_name)
|
info = client.get_collection(collection_name=collection_name)
|
||||||
vecs = getattr(info, "vectors", None)
|
vecs = getattr(info, "vectors", None)
|
||||||
# Single-vector config
|
# Prüfung auf Single-Vector Konfiguration
|
||||||
if hasattr(vecs, "size") and isinstance(vecs.size, int):
|
if hasattr(vecs, "size") and isinstance(vecs.size, int):
|
||||||
return {"kind": "single", "size": vecs.size}
|
return {"kind": "single", "size": vecs.size}
|
||||||
# Named-vectors config (dict-like in .config)
|
# Prüfung auf Named-Vectors Konfiguration
|
||||||
cfg = getattr(vecs, "config", None)
|
cfg = getattr(vecs, "config", None)
|
||||||
if isinstance(cfg, dict) and cfg:
|
if isinstance(cfg, dict) and cfg:
|
||||||
names = list(cfg.keys())
|
names = list(cfg.keys())
|
||||||
@@ -126,6 +195,7 @@ def _get_vector_schema(client: QdrantClient, collection_name: str) -> dict:
|
|||||||
return {"kind": "single", "size": None}
|
return {"kind": "single", "size": None}
|
||||||
|
|
||||||
def _as_named(points: List[rest.PointStruct], name: str) -> List[rest.PointStruct]:
|
def _as_named(points: List[rest.PointStruct], name: str) -> List[rest.PointStruct]:
|
||||||
|
"""Transformiert PointStructs in das Named-Vector Format."""
|
||||||
out: List[rest.PointStruct] = []
|
out: List[rest.PointStruct] = []
|
||||||
for pt in points:
|
for pt in points:
|
||||||
vec = getattr(pt, "vector", None)
|
vec = getattr(pt, "vector", None)
|
||||||
@@ -133,7 +203,6 @@ def _as_named(points: List[rest.PointStruct], name: str) -> List[rest.PointStruc
|
|||||||
if name in vec:
|
if name in vec:
|
||||||
out.append(pt)
|
out.append(pt)
|
||||||
else:
|
else:
|
||||||
# take any existing entry; if empty dict fallback to [0.0]
|
|
||||||
fallback_vec = None
|
fallback_vec = None
|
||||||
try:
|
try:
|
||||||
fallback_vec = list(next(iter(vec.values())))
|
fallback_vec = list(next(iter(vec.values())))
|
||||||
@@ -148,35 +217,42 @@ def _as_named(points: List[rest.PointStruct], name: str) -> List[rest.PointStruc
|
|||||||
|
|
||||||
# --------------------- Qdrant ops ---------------------
|
# --------------------- Qdrant ops ---------------------
|
||||||
|
|
||||||
def upsert_batch(client: QdrantClient, collection: str, points: List[rest.PointStruct]) -> None:
|
def upsert_batch(client: QdrantClient, collection: str, points: List[rest.PointStruct], wait: bool = True) -> None:
|
||||||
|
"""
|
||||||
|
Schreibt Points hocheffizient in eine Collection.
|
||||||
|
Unterstützt automatische Schema-Erkennung und Named-Vector Transformation.
|
||||||
|
WP-Fix: 'wait=True' ist Default für Datenkonsistenz zwischen den Ingest-Phasen.
|
||||||
|
"""
|
||||||
if not points:
|
if not points:
|
||||||
return
|
return
|
||||||
|
|
||||||
# 1) ENV overrides come first
|
# 1) ENV overrides prüfen
|
||||||
override = _env_override_for_collection(collection)
|
override = _env_override_for_collection(collection)
|
||||||
if override == "__single__":
|
if override == "__single__":
|
||||||
client.upsert(collection_name=collection, points=points, wait=True)
|
client.upsert(collection_name=collection, points=points, wait=wait)
|
||||||
return
|
return
|
||||||
elif isinstance(override, str):
|
elif isinstance(override, str):
|
||||||
client.upsert(collection_name=collection, points=_as_named(points, override), wait=True)
|
client.upsert(collection_name=collection, points=_as_named(points, override), wait=wait)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 2) Auto-detect schema
|
# 2) Automatische Schema-Erkennung (Live-Check)
|
||||||
schema = _get_vector_schema(client, collection)
|
schema = _get_vector_schema(client, collection)
|
||||||
if schema.get("kind") == "named":
|
if schema.get("kind") == "named":
|
||||||
name = schema.get("primary") or _preferred_name(schema.get("names") or [])
|
name = schema.get("primary") or _preferred_name(schema.get("names") or [])
|
||||||
client.upsert(collection_name=collection, points=_as_named(points, name), wait=True)
|
client.upsert(collection_name=collection, points=_as_named(points, name), wait=wait)
|
||||||
return
|
return
|
||||||
|
|
||||||
# 3) Fallback single-vector
|
# 3) Fallback: Single-Vector Upsert
|
||||||
client.upsert(collection_name=collection, points=points, wait=True)
|
client.upsert(collection_name=collection, points=points, wait=wait)
|
||||||
|
|
||||||
# --- Optional search helpers ---
|
# --- Optional search helpers ---
|
||||||
|
|
||||||
def _filter_any(field: str, values: Iterable[str]) -> rest.Filter:
|
def _filter_any(field: str, values: Iterable[str]) -> rest.Filter:
|
||||||
|
"""Hilfsfunktion für händische Filter-Konstruktion (Logical OR)."""
|
||||||
return rest.Filter(should=[rest.FieldCondition(key=field, match=rest.MatchValue(value=v)) for v in values])
|
return rest.Filter(should=[rest.FieldCondition(key=field, match=rest.MatchValue(value=v)) for v in values])
|
||||||
|
|
||||||
def _merge_filters(*filters: Optional[rest.Filter]) -> Optional[rest.Filter]:
|
def _merge_filters(*filters: Optional[rest.Filter]) -> Optional[rest.Filter]:
|
||||||
|
"""Führt mehrere Filter-Objekte zu einem konsolidierten Filter zusammen."""
|
||||||
fs = [f for f in filters if f is not None]
|
fs = [f for f in filters if f is not None]
|
||||||
if not fs:
|
if not fs:
|
||||||
return None
|
return None
|
||||||
@@ -191,6 +267,7 @@ def _merge_filters(*filters: Optional[rest.Filter]) -> Optional[rest.Filter]:
|
|||||||
return rest.Filter(must=must)
|
return rest.Filter(must=must)
|
||||||
|
|
||||||
def _filter_from_dict(filters: Optional[Dict[str, Any]]) -> Optional[rest.Filter]:
|
def _filter_from_dict(filters: Optional[Dict[str, Any]]) -> Optional[rest.Filter]:
|
||||||
|
"""Konvertiert ein Python-Dict in ein Qdrant-Filter Objekt."""
|
||||||
if not filters:
|
if not filters:
|
||||||
return None
|
return None
|
||||||
parts = []
|
parts = []
|
||||||
@@ -202,9 +279,17 @@ def _filter_from_dict(filters: Optional[Dict[str, Any]]) -> Optional[rest.Filter
|
|||||||
return _merge_filters(*parts)
|
return _merge_filters(*parts)
|
||||||
|
|
||||||
def search_chunks_by_vector(client: QdrantClient, prefix: str, vector: List[float], top: int = 10, filters: Optional[Dict[str, Any]] = None) -> List[Tuple[str, float, dict]]:
|
def search_chunks_by_vector(client: QdrantClient, prefix: str, vector: List[float], top: int = 10, filters: Optional[Dict[str, Any]] = None) -> List[Tuple[str, float, dict]]:
|
||||||
|
"""Sucht semantisch ähnliche Chunks in der Vektordatenbank."""
|
||||||
_, chunks_col, _ = _names(prefix)
|
_, chunks_col, _ = _names(prefix)
|
||||||
flt = _filter_from_dict(filters)
|
flt = _filter_from_dict(filters)
|
||||||
res = client.search(collection_name=chunks_col, query_vector=vector, limit=top, with_payload=True, with_vectors=False, query_filter=flt)
|
res = client.search(
|
||||||
|
collection_name=chunks_col,
|
||||||
|
query_vector=vector,
|
||||||
|
limit=top,
|
||||||
|
with_payload=True,
|
||||||
|
with_vectors=False,
|
||||||
|
query_filter=flt
|
||||||
|
)
|
||||||
out: List[Tuple[str, float, dict]] = []
|
out: List[Tuple[str, float, dict]] = []
|
||||||
for r in res:
|
for r in res:
|
||||||
out.append((str(r.id), float(r.score), dict(r.payload or {})))
|
out.append((str(r.id), float(r.score), dict(r.payload or {})))
|
||||||
@@ -220,41 +305,18 @@ def get_edges_for_sources(
|
|||||||
edge_types: Optional[Iterable[str]] = None,
|
edge_types: Optional[Iterable[str]] = None,
|
||||||
limit: int = 2048,
|
limit: int = 2048,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""Retrieve edge payloads from the <prefix>_edges collection.
|
"""Ruft alle Kanten ab, die von einer Menge von Quell-Notizen ausgehen."""
|
||||||
|
|
||||||
Args:
|
|
||||||
client: QdrantClient instance.
|
|
||||||
prefix: Mindnet collection prefix (e.g. "mindnet").
|
|
||||||
source_ids: Iterable of source_id values (typically chunk_ids or note_ids).
|
|
||||||
edge_types: Optional iterable of edge kinds (e.g. ["references", "depends_on"]). If None,
|
|
||||||
all kinds are returned.
|
|
||||||
limit: Maximum number of edge payloads to return.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A list of edge payload dicts, e.g.:
|
|
||||||
{
|
|
||||||
"note_id": "...",
|
|
||||||
"chunk_id": "...",
|
|
||||||
"kind": "references" | "depends_on" | ...,
|
|
||||||
"scope": "chunk",
|
|
||||||
"source_id": "...",
|
|
||||||
"target_id": "...",
|
|
||||||
"rule_id": "...",
|
|
||||||
"confidence": 0.7,
|
|
||||||
...
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
source_ids = list(source_ids)
|
source_ids = list(source_ids)
|
||||||
if not source_ids or limit <= 0:
|
if not source_ids or limit <= 0:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Resolve collection name
|
# Namen der Edges-Collection auflösen
|
||||||
_, _, edges_col = _names(prefix)
|
_, _, edges_col = _names(prefix)
|
||||||
|
|
||||||
# Build filter: source_id IN source_ids
|
# Filter-Bau: source_id IN source_ids
|
||||||
src_filter = _filter_any("source_id", [str(s) for s in source_ids])
|
src_filter = _filter_any("source_id", [str(s) for s in source_ids])
|
||||||
|
|
||||||
# Optional: kind IN edge_types
|
# Optionaler Filter auf den Kanten-Typ
|
||||||
kind_filter = None
|
kind_filter = None
|
||||||
if edge_types:
|
if edge_types:
|
||||||
kind_filter = _filter_any("kind", [str(k) for k in edge_types])
|
kind_filter = _filter_any("kind", [str(k) for k in edge_types])
|
||||||
@@ -265,7 +327,7 @@ def get_edges_for_sources(
|
|||||||
next_page = None
|
next_page = None
|
||||||
remaining = int(limit)
|
remaining = int(limit)
|
||||||
|
|
||||||
# Use paginated scroll API; we don't need vectors, only payloads.
|
# Paginated Scroll API (NUR Payload, keine Vektoren)
|
||||||
while remaining > 0:
|
while remaining > 0:
|
||||||
batch_limit = min(256, remaining)
|
batch_limit = min(256, remaining)
|
||||||
res, next_page = client.scroll(
|
res, next_page = client.scroll(
|
||||||
@@ -1,394 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/derive_edges.py
|
|
||||||
DESCRIPTION: Extrahiert Graph-Kanten aus Text. Unterstützt Wikilinks, Inline-Relations ([[rel:type|target]]) und Obsidian Callouts.
|
|
||||||
WP-15b: Integration des Candidate-Pools und Provenance-Priorisierung.
|
|
||||||
Sichert die Graph-Integrität durch confidence-basiertes De-Duplicating.
|
|
||||||
VERSION: 2.1.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: re, os, yaml, typing, hashlib
|
|
||||||
EXTERNAL_CONFIG: config/types.yaml
|
|
||||||
LAST_ANALYSIS: 2025-12-26
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import hashlib
|
|
||||||
from typing import Iterable, List, Optional, Tuple, Set, Dict
|
|
||||||
|
|
||||||
try:
|
|
||||||
import yaml # optional, nur für types.yaml
|
|
||||||
except Exception: # pragma: no cover
|
|
||||||
yaml = None
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# 1. Utilities & ID Generation
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
|
|
||||||
def _get(d: dict, *keys, default=None):
|
|
||||||
"""Sicherer Zugriff auf verschachtelte Dictionary-Keys."""
|
|
||||||
for k in keys:
|
|
||||||
if isinstance(d, dict) and k in d and d[k] is not None:
|
|
||||||
return d[k]
|
|
||||||
return default
|
|
||||||
|
|
||||||
def _chunk_text_for_refs(chunk: dict) -> str:
|
|
||||||
"""Extrahiert den relevanten Text für die Referenzsuche (bevorzugt Window)."""
|
|
||||||
return (
|
|
||||||
_get(chunk, "window")
|
|
||||||
or _get(chunk, "text")
|
|
||||||
or _get(chunk, "content")
|
|
||||||
or _get(chunk, "raw")
|
|
||||||
or ""
|
|
||||||
)
|
|
||||||
|
|
||||||
def _dedupe_seq(seq: Iterable[str]) -> List[str]:
|
|
||||||
"""Dedupliziert eine Sequenz von Strings unter Beibehaltung der Reihenfolge."""
|
|
||||||
seen: Set[str] = set()
|
|
||||||
out: List[str] = []
|
|
||||||
for s in seq:
|
|
||||||
if s not in seen:
|
|
||||||
seen.add(s)
|
|
||||||
out.append(s)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def _edge(kind: str, scope: str, source_id: str, target_id: str, note_id: str, extra: Optional[dict] = None) -> dict:
|
|
||||||
"""Konstruiert ein valides Kanten-Payload-Objekt für Qdrant."""
|
|
||||||
pl = {
|
|
||||||
"kind": kind,
|
|
||||||
"relation": kind, # Alias für Abwärtskompatibilität (v2)
|
|
||||||
"scope": scope, # "chunk" | "note"
|
|
||||||
"source_id": source_id,
|
|
||||||
"target_id": target_id,
|
|
||||||
"note_id": note_id, # Träger-Note der Kante
|
|
||||||
}
|
|
||||||
if extra:
|
|
||||||
pl.update(extra)
|
|
||||||
return pl
|
|
||||||
|
|
||||||
def _mk_edge_id(kind: str, s: str, t: str, scope: str, rule_id: Optional[str] = None) -> str:
|
|
||||||
"""Erzeugt eine deterministische 12-Byte ID mittels BLAKE2s."""
|
|
||||||
base = f"{kind}:{s}->{t}#{scope}"
|
|
||||||
if rule_id:
|
|
||||||
base += f"|{rule_id}"
|
|
||||||
try:
|
|
||||||
return hashlib.blake2s(base.encode("utf-8"), digest_size=12).hexdigest()
|
|
||||||
except Exception: # pragma: no cover
|
|
||||||
return base
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# 2. Konfiguration & Provenance-Skala
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
|
|
||||||
# WP-15b: Prioritäten-Ranking für die De-Duplizierung
|
|
||||||
PROVENANCE_PRIORITY = {
|
|
||||||
"explicit:wikilink": 1.00,
|
|
||||||
"inline:rel": 0.95,
|
|
||||||
"callout:edge": 0.90,
|
|
||||||
"semantic_ai": 0.90, # Validierte KI-Kanten
|
|
||||||
"structure:belongs_to": 1.00,
|
|
||||||
"structure:order": 0.95, # next/prev
|
|
||||||
"explicit:note_scope": 1.00,
|
|
||||||
"derived:backlink": 0.90,
|
|
||||||
"edge_defaults": 0.70 # Heuristik (types.yaml)
|
|
||||||
}
|
|
||||||
|
|
||||||
def _env(n: str, default: Optional[str] = None) -> str:
|
|
||||||
v = os.getenv(n)
|
|
||||||
return v if v is not None else (default or "")
|
|
||||||
|
|
||||||
def _load_types_registry() -> dict:
|
|
||||||
"""Lädt die YAML-Registry zur Ermittlung von Standard-Kanten."""
|
|
||||||
p = _env("MINDNET_TYPES_FILE", "./config/types.yaml")
|
|
||||||
if not os.path.isfile(p) or yaml is None:
|
|
||||||
return {}
|
|
||||||
try:
|
|
||||||
with open(p, "r", encoding="utf-8") as f:
|
|
||||||
data = yaml.safe_load(f) or {}
|
|
||||||
return data
|
|
||||||
except Exception:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def _get_types_map(reg: dict) -> dict:
|
|
||||||
if isinstance(reg, dict) and isinstance(reg.get("types"), dict):
|
|
||||||
return reg["types"]
|
|
||||||
return reg if isinstance(reg, dict) else {}
|
|
||||||
|
|
||||||
def _edge_defaults_for(note_type: Optional[str], reg: dict) -> List[str]:
|
|
||||||
"""Liefert die edge_defaults-Liste für den gegebenen Notiztyp."""
|
|
||||||
types_map = _get_types_map(reg)
|
|
||||||
if note_type and isinstance(types_map, dict):
|
|
||||||
t = types_map.get(note_type)
|
|
||||||
if isinstance(t, dict) and isinstance(t.get("edge_defaults"), list):
|
|
||||||
return [str(x) for x in t["edge_defaults"] if isinstance(x, str)]
|
|
||||||
for key in ("defaults", "default", "global"):
|
|
||||||
v = reg.get(key)
|
|
||||||
if isinstance(v, dict) and isinstance(v.get("edge_defaults"), list):
|
|
||||||
return [str(x) for x in v["edge_defaults"] if isinstance(x, str)]
|
|
||||||
return []
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# 3. Parser für Links / Relationen (Core Logik v2.0.0)
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
|
|
||||||
# Normale Wikilinks (Fallback)
|
|
||||||
_WIKILINK_RE = re.compile(r"\[\[(?:[^\|\]]+\|)?([a-zA-Z0-9_\-#:. ]+)\]\]")
|
|
||||||
|
|
||||||
# Getypte Inline-Relationen
|
|
||||||
_REL_PIPE = re.compile(r"\[\[\s*rel:(?P<kind>[a-z_]+)\s*\|\s*(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
|
||||||
_REL_SPACE = re.compile(r"\[\[\s*rel:(?P<kind>[a-z_]+)\s+(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
|
||||||
_REL_TEXT = re.compile(r"rel\s*:\s*(?P<kind>[a-z_]+)\s*\[\[\s*(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
|
||||||
|
|
||||||
def _extract_typed_relations(text: str) -> Tuple[List[Tuple[str,str]], str]:
|
|
||||||
"""Extrahiert [[rel:KIND|Target]] und entfernt sie zur Vermeidung von Dubletten."""
|
|
||||||
pairs: List[Tuple[str,str]] = []
|
|
||||||
def _collect(m):
|
|
||||||
k = (m.group("kind") or "").strip().lower()
|
|
||||||
t = (m.group("target") or "").strip()
|
|
||||||
if k and t:
|
|
||||||
pairs.append((k, t))
|
|
||||||
return "" # Link entfernen
|
|
||||||
|
|
||||||
text = _REL_PIPE.sub(_collect, text)
|
|
||||||
text = _REL_SPACE.sub(_collect, text)
|
|
||||||
text = _REL_TEXT.sub(_collect, text)
|
|
||||||
return pairs, text
|
|
||||||
|
|
||||||
# Obsidian Callout Parser für mehrzeilige Blöcke
|
|
||||||
_CALLOUT_START = re.compile(r"^\s*>\s*\[!edge\]\s*(.*)$", re.IGNORECASE)
|
|
||||||
_REL_LINE = re.compile(r"^(?P<kind>[a-z_]+)\s*:\s*(?P<targets>.+?)\s*$", re.IGNORECASE)
|
|
||||||
_WIKILINKS_IN_LINE = re.compile(r"\[\[([^\]]+)\]\]")
|
|
||||||
|
|
||||||
def _extract_callout_relations(text: str) -> Tuple[List[Tuple[str,str]], str]:
|
|
||||||
"""Verarbeitet [!edge]-Callouts und entfernt diese aus dem Textfluss."""
|
|
||||||
if not text:
|
|
||||||
return [], text
|
|
||||||
|
|
||||||
lines = text.splitlines()
|
|
||||||
out_pairs: List[Tuple[str,str]] = []
|
|
||||||
keep_lines: List[str] = []
|
|
||||||
i = 0
|
|
||||||
|
|
||||||
while i < len(lines):
|
|
||||||
m = _CALLOUT_START.match(lines[i])
|
|
||||||
if not m:
|
|
||||||
keep_lines.append(lines[i])
|
|
||||||
i += 1
|
|
||||||
continue
|
|
||||||
|
|
||||||
block_lines: List[str] = []
|
|
||||||
first_rest = m.group(1) or ""
|
|
||||||
if first_rest.strip():
|
|
||||||
block_lines.append(first_rest)
|
|
||||||
|
|
||||||
i += 1
|
|
||||||
while i < len(lines) and lines[i].lstrip().startswith('>'):
|
|
||||||
block_lines.append(lines[i].lstrip()[1:].lstrip())
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
for bl in block_lines:
|
|
||||||
mrel = _REL_LINE.match(bl)
|
|
||||||
if not mrel:
|
|
||||||
continue
|
|
||||||
kind = (mrel.group("kind") or "").strip().lower()
|
|
||||||
targets = mrel.group("targets") or ""
|
|
||||||
found = _WIKILINKS_IN_LINE.findall(targets)
|
|
||||||
if found:
|
|
||||||
for t in found:
|
|
||||||
t = t.strip()
|
|
||||||
if t:
|
|
||||||
out_pairs.append((kind, t))
|
|
||||||
else:
|
|
||||||
for raw in re.split(r"[,;]", targets):
|
|
||||||
t = raw.strip()
|
|
||||||
if t:
|
|
||||||
out_pairs.append((kind, t))
|
|
||||||
continue
|
|
||||||
|
|
||||||
remainder = "\n".join(keep_lines)
|
|
||||||
return out_pairs, remainder
|
|
||||||
|
|
||||||
def _extract_wikilinks(text: str) -> List[str]:
|
|
||||||
"""Extrahiert Standard-Wikilinks aus dem verbleibenden Text."""
|
|
||||||
ids: List[str] = []
|
|
||||||
for m in _WIKILINK_RE.finditer(text or ""):
|
|
||||||
ids.append(m.group(1).strip())
|
|
||||||
return ids
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
# 4. Hauptfunktion (build_edges_for_note)
|
|
||||||
# --------------------------------------------------------------------------- #
|
|
||||||
|
|
||||||
def build_edges_for_note(
|
|
||||||
note_id: str,
|
|
||||||
chunks: List[dict],
|
|
||||||
note_level_references: Optional[List[str]] = None,
|
|
||||||
include_note_scope_refs: bool = False,
|
|
||||||
) -> List[dict]:
|
|
||||||
"""
|
|
||||||
Erzeugt und aggregiert alle Kanten für eine Note inklusive WP-15b Candidate-Processing.
|
|
||||||
Setzt Provenance-Ranking zur Graph-Stabilisierung ein.
|
|
||||||
"""
|
|
||||||
edges: List[dict] = []
|
|
||||||
note_type = _get(chunks[0], "type") if chunks else "concept"
|
|
||||||
|
|
||||||
# 1) Struktur-Kanten: belongs_to (Chunk -> Note)
|
|
||||||
for ch in chunks:
|
|
||||||
cid = _get(ch, "chunk_id", "id")
|
|
||||||
if not cid:
|
|
||||||
continue
|
|
||||||
edges.append(_edge("belongs_to", "chunk", cid, note_id, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"edge_id": _mk_edge_id("belongs_to", cid, note_id, "chunk", "structure:belongs_to"),
|
|
||||||
"provenance": "structure",
|
|
||||||
"rule_id": "structure:belongs_to",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["structure:belongs_to"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 2) Struktur-Kanten: next / prev (Sequenz)
|
|
||||||
for i in range(len(chunks) - 1):
|
|
||||||
a, b = chunks[i], chunks[i + 1]
|
|
||||||
a_id = _get(a, "chunk_id", "id")
|
|
||||||
b_id = _get(b, "chunk_id", "id")
|
|
||||||
if not a_id or not b_id:
|
|
||||||
continue
|
|
||||||
edges.append(_edge("next", "chunk", a_id, b_id, note_id, {
|
|
||||||
"chunk_id": a_id,
|
|
||||||
"edge_id": _mk_edge_id("next", a_id, b_id, "chunk", "structure:order"),
|
|
||||||
"provenance": "structure",
|
|
||||||
"rule_id": "structure:order",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["structure:order"],
|
|
||||||
}))
|
|
||||||
edges.append(_edge("prev", "chunk", b_id, a_id, note_id, {
|
|
||||||
"chunk_id": b_id,
|
|
||||||
"edge_id": _mk_edge_id("prev", b_id, a_id, "chunk", "structure:order"),
|
|
||||||
"provenance": "structure",
|
|
||||||
"rule_id": "structure:order",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["structure:order"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 3) Inhaltliche Kanten (Refs, Inlines, Callouts, Candidates)
|
|
||||||
reg = _load_types_registry()
|
|
||||||
defaults = _edge_defaults_for(note_type, reg)
|
|
||||||
refs_all: List[str] = []
|
|
||||||
|
|
||||||
for ch in chunks:
|
|
||||||
cid = _get(ch, "chunk_id", "id")
|
|
||||||
if not cid:
|
|
||||||
continue
|
|
||||||
raw = _chunk_text_for_refs(ch)
|
|
||||||
|
|
||||||
# 3a) Typed Inline Relations
|
|
||||||
typed, remainder = _extract_typed_relations(raw)
|
|
||||||
for kind, target in typed:
|
|
||||||
k = kind.strip().lower()
|
|
||||||
if not k or not target: continue
|
|
||||||
edges.append(_edge(k, "chunk", cid, target, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"edge_id": _mk_edge_id(k, cid, target, "chunk", "inline:rel"),
|
|
||||||
"provenance": "explicit",
|
|
||||||
"rule_id": "inline:rel",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["inline:rel"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 3b) WP-15b Candidate Pool Integration (KI-validierte Kanten)
|
|
||||||
# Verarbeitet Kanten, die bereits in der Ingestion semantisch geprüft wurden.
|
|
||||||
pool = ch.get("candidate_pool") or ch.get("candidate_edges") or []
|
|
||||||
for cand in pool:
|
|
||||||
target = cand.get("to")
|
|
||||||
kind = cand.get("kind", "related_to")
|
|
||||||
prov = cand.get("provenance", "semantic_ai")
|
|
||||||
if not target: continue
|
|
||||||
edges.append(_edge(kind, "chunk", cid, target, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"edge_id": _mk_edge_id(kind, cid, target, "chunk", f"candidate:{prov}"),
|
|
||||||
"provenance": prov,
|
|
||||||
"rule_id": f"candidate:{prov}",
|
|
||||||
"confidence": PROVENANCE_PRIORITY.get(prov, 0.90),
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 3c) Obsidian Callouts
|
|
||||||
call_pairs, remainder2 = _extract_callout_relations(remainder)
|
|
||||||
for kind, target in call_pairs:
|
|
||||||
k = (kind or "").strip().lower()
|
|
||||||
if not k or not target: continue
|
|
||||||
edges.append(_edge(k, "chunk", cid, target, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"edge_id": _mk_edge_id(k, cid, target, "chunk", "callout:edge"),
|
|
||||||
"provenance": "explicit",
|
|
||||||
"rule_id": "callout:edge",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["callout:edge"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 3d) Standard-Wikilinks -> references (+ defaults)
|
|
||||||
refs = _extract_wikilinks(remainder2)
|
|
||||||
for r in refs:
|
|
||||||
edges.append(_edge("references", "chunk", cid, r, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"ref_text": r,
|
|
||||||
"edge_id": _mk_edge_id("references", cid, r, "chunk", "explicit:wikilink"),
|
|
||||||
"provenance": "explicit",
|
|
||||||
"rule_id": "explicit:wikilink",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["explicit:wikilink"],
|
|
||||||
}))
|
|
||||||
# Regelbasierte Kanten aus types.yaml anhängen
|
|
||||||
for rel in defaults:
|
|
||||||
if rel == "references": continue
|
|
||||||
edges.append(_edge(rel, "chunk", cid, r, note_id, {
|
|
||||||
"chunk_id": cid,
|
|
||||||
"edge_id": _mk_edge_id(rel, cid, r, "chunk", f"edge_defaults:{note_type}:{rel}"),
|
|
||||||
"provenance": "rule",
|
|
||||||
"rule_id": f"edge_defaults:{note_type}:{rel}",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["edge_defaults"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
refs_all.extend(refs)
|
|
||||||
|
|
||||||
# 4) Optionale Note-Scope Referenzen & Backlinks
|
|
||||||
if include_note_scope_refs:
|
|
||||||
refs_note = list(refs_all or [])
|
|
||||||
if note_level_references:
|
|
||||||
refs_note.extend([r for r in note_level_references if isinstance(r, str) and r])
|
|
||||||
refs_note = _dedupe_seq(refs_note)
|
|
||||||
|
|
||||||
for r in refs_note:
|
|
||||||
edges.append(_edge("references", "note", note_id, r, note_id, {
|
|
||||||
"edge_id": _mk_edge_id("references", note_id, r, "note", "explicit:note_scope"),
|
|
||||||
"provenance": "explicit",
|
|
||||||
"rule_id": "explicit:note_scope",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["explicit:note_scope"],
|
|
||||||
}))
|
|
||||||
# Backlink-Erzeugung zur Graphen-Stärkung
|
|
||||||
edges.append(_edge("backlink", "note", r, note_id, note_id, {
|
|
||||||
"edge_id": _mk_edge_id("backlink", r, note_id, "note", "derived:backlink"),
|
|
||||||
"provenance": "rule",
|
|
||||||
"rule_id": "derived:backlink",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["derived:backlink"],
|
|
||||||
}))
|
|
||||||
for rel in defaults:
|
|
||||||
if rel == "references": continue
|
|
||||||
edges.append(_edge(rel, "note", note_id, r, note_id, {
|
|
||||||
"edge_id": _mk_edge_id(rel, note_id, r, "note", f"edge_defaults:{note_type}:{rel}"),
|
|
||||||
"provenance": "rule",
|
|
||||||
"rule_id": f"edge_defaults:{note_type}:{rel}",
|
|
||||||
"confidence": PROVENANCE_PRIORITY["edge_defaults"],
|
|
||||||
}))
|
|
||||||
|
|
||||||
# 5) WP-15b: Confidence-basierte De-Duplizierung
|
|
||||||
# Wenn dieselbe Relation mehrfach existiert, gewinnt die mit der höchsten Confidence.
|
|
||||||
unique_map: Dict[Tuple[str, str, str], dict] = {}
|
|
||||||
|
|
||||||
for e in edges:
|
|
||||||
s, t = str(e.get("source_id")), str(e.get("target_id"))
|
|
||||||
rel = str(e.get("relation") or e.get("kind") or "edge")
|
|
||||||
key = (s, t, rel)
|
|
||||||
|
|
||||||
if key not in unique_map:
|
|
||||||
unique_map[key] = e
|
|
||||||
else:
|
|
||||||
# Vergleich der Vertrauenswürdigkeit (Provenance Ranking)
|
|
||||||
if e.get("confidence", 0) > unique_map[key].get("confidence", 0):
|
|
||||||
unique_map[key] = e
|
|
||||||
|
|
||||||
return list(unique_map.values())
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/__init__.py
|
||||||
|
DESCRIPTION: Unified Graph Package. Exportiert Kanten-Ableitung und Graph-Adapter.
|
||||||
|
"""
|
||||||
|
from .graph_derive_edges import build_edges_for_note
|
||||||
|
from .graph_utils import PROVENANCE_PRIORITY
|
||||||
|
from .graph_subgraph import Subgraph, expand
|
||||||
|
from .graph_weights import EDGE_BASE_WEIGHTS
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"build_edges_for_note",
|
||||||
|
"PROVENANCE_PRIORITY",
|
||||||
|
"Subgraph",
|
||||||
|
"expand",
|
||||||
|
"EDGE_BASE_WEIGHTS"
|
||||||
|
]
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/graph_db_adapter.py
|
||||||
|
DESCRIPTION: Datenbeschaffung aus Qdrant für den Graphen.
|
||||||
|
AUDIT v1.2.0: Gold-Standard v4.1.0 - Scope-Awareness & Section-Filtering.
|
||||||
|
- Erweiterte Suche nach chunk_id-Edges für Scope-Awareness
|
||||||
|
- Optionales target_section-Filtering für präzise Section-Links
|
||||||
|
- Vollständige Metadaten-Unterstützung (provenance, confidence, virtual)
|
||||||
|
VERSION: 1.2.0 (WP-24c: Gold-Standard v4.1.0)
|
||||||
|
"""
|
||||||
|
from typing import List, Dict, Optional
|
||||||
|
from qdrant_client import QdrantClient
|
||||||
|
from qdrant_client.http import models as rest
|
||||||
|
|
||||||
|
# Nutzt die zentrale Infrastruktur für konsistente Collection-Namen (WP-14)
|
||||||
|
from app.core.database import collection_names
|
||||||
|
|
||||||
|
def fetch_edges_from_qdrant(
|
||||||
|
client: QdrantClient,
|
||||||
|
prefix: str,
|
||||||
|
seeds: List[str],
|
||||||
|
edge_types: Optional[List[str]] = None,
|
||||||
|
target_section: Optional[str] = None,
|
||||||
|
chunk_ids: Optional[List[str]] = None,
|
||||||
|
limit: int = 2048,
|
||||||
|
) -> List[Dict]:
|
||||||
|
"""
|
||||||
|
Holt Edges aus der Datenbank basierend auf Seed-IDs.
|
||||||
|
WP-24c v4.1.0: Scope-Aware Edge Retrieval mit Section-Filtering.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client: Qdrant Client
|
||||||
|
prefix: Collection-Präfix
|
||||||
|
seeds: Liste von Note-IDs für die Suche
|
||||||
|
edge_types: Optionale Filterung nach Kanten-Typen
|
||||||
|
target_section: Optionales Section-Filtering (für präzise Section-Links)
|
||||||
|
chunk_ids: Optionale Liste von Chunk-IDs für Scope-Awareness (Chunk-Level Edges)
|
||||||
|
limit: Maximale Anzahl zurückgegebener Edges
|
||||||
|
"""
|
||||||
|
if not seeds or limit <= 0:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# Konsistente Namensauflösung via database-Paket
|
||||||
|
# Rückgabe: (notes_col, chunks_col, edges_col)
|
||||||
|
_, _, edges_col = collection_names(prefix)
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Scope-Awareness - Suche nach Note- UND Chunk-Level Edges
|
||||||
|
seed_conditions = []
|
||||||
|
for field in ("source_id", "target_id", "note_id"):
|
||||||
|
for s in seeds:
|
||||||
|
seed_conditions.append(
|
||||||
|
rest.FieldCondition(key=field, match=rest.MatchValue(value=str(s)))
|
||||||
|
)
|
||||||
|
|
||||||
|
# Chunk-Level Edges: Wenn chunk_ids angegeben, suche auch nach chunk_id als source_id
|
||||||
|
if chunk_ids:
|
||||||
|
for cid in chunk_ids:
|
||||||
|
seed_conditions.append(
|
||||||
|
rest.FieldCondition(key="source_id", match=rest.MatchValue(value=str(cid)))
|
||||||
|
)
|
||||||
|
|
||||||
|
seeds_filter = rest.Filter(should=seed_conditions) if seed_conditions else None
|
||||||
|
|
||||||
|
# Optionaler Filter auf spezifische Kanten-Typen (z.B. für Intent-Routing)
|
||||||
|
type_filter = None
|
||||||
|
if edge_types:
|
||||||
|
type_conds = [
|
||||||
|
rest.FieldCondition(key="kind", match=rest.MatchValue(value=str(k)))
|
||||||
|
for k in edge_types
|
||||||
|
]
|
||||||
|
type_filter = rest.Filter(should=type_conds)
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Section-Filtering für präzise Section-Links
|
||||||
|
section_filter = None
|
||||||
|
if target_section:
|
||||||
|
section_filter = rest.Filter(must=[
|
||||||
|
rest.FieldCondition(key="target_section", match=rest.MatchValue(value=str(target_section)))
|
||||||
|
])
|
||||||
|
|
||||||
|
must = []
|
||||||
|
if seeds_filter:
|
||||||
|
must.append(seeds_filter)
|
||||||
|
if type_filter:
|
||||||
|
must.append(type_filter)
|
||||||
|
if section_filter:
|
||||||
|
must.append(section_filter)
|
||||||
|
|
||||||
|
flt = rest.Filter(must=must) if must else None
|
||||||
|
|
||||||
|
# Abfrage via Qdrant Scroll API
|
||||||
|
# WICHTIG: with_payload=True lädt alle Metadaten (target_section, provenance etc.)
|
||||||
|
pts, _ = client.scroll(
|
||||||
|
collection_name=edges_col,
|
||||||
|
scroll_filter=flt,
|
||||||
|
limit=limit,
|
||||||
|
with_payload=True,
|
||||||
|
with_vectors=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Wir geben das vollständige Payload zurück, damit der Retriever
|
||||||
|
# alle Signale für die Super-Edge-Aggregation und das Scoring hat.
|
||||||
|
return [dict(p.payload) for p in pts if p.payload]
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,164 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/graph_extractors.py
|
||||||
|
DESCRIPTION: Regex-basierte Extraktion von Relationen aus Text.
|
||||||
|
AUDIT:
|
||||||
|
- Regex für Wikilinks liberalisiert (Umlaute, Sonderzeichen).
|
||||||
|
- Callout-Parser erweitert für Multi-Line-Listen und Header-Typen.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
# Erlaube alle Zeichen außer ']' im Target (fängt Umlaute, Emojis, '&', '#' ab)
|
||||||
|
_WIKILINK_RE = re.compile(r"\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]")
|
||||||
|
|
||||||
|
_REL_PIPE = re.compile(r"\[\[\s*rel:(?P<kind>[a-z_]+)\s*\|\s*(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
||||||
|
_REL_SPACE = re.compile(r"\[\[\s*rel:(?P<kind>[a-z_]+)\s+(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
||||||
|
_REL_TEXT = re.compile(r"rel\s*:\s*(?P<kind>[a-z_]+)\s*\[\[\s*(?P<target>[^\]]+?)\s*\]\]", re.IGNORECASE)
|
||||||
|
|
||||||
|
# Erkennt [!edge] Callouts mit einem oder mehreren '>' am Anfang (für verschachtelte Callouts)
|
||||||
|
_CALLOUT_START = re.compile(r"^\s*>{1,}\s*\[!edge\]\s*(.*)$", re.IGNORECASE)
|
||||||
|
# Erkennt "kind: targets..."
|
||||||
|
_REL_LINE = re.compile(r"^(?P<kind>[a-z_]+)\s*:\s*(?P<targets>.+?)\s*$", re.IGNORECASE)
|
||||||
|
# Erkennt reine Typen (z.B. "depends_on" im Header)
|
||||||
|
_SIMPLE_KIND = re.compile(r"^[a-z_]+$", re.IGNORECASE)
|
||||||
|
|
||||||
|
def extract_typed_relations(text: str) -> Tuple[List[Tuple[str, str]], str]:
|
||||||
|
"""
|
||||||
|
Findet Inline-Relationen wie [[rel:depends_on Target]].
|
||||||
|
Gibt (Liste[(kind, target)], bereinigter_text) zurück.
|
||||||
|
"""
|
||||||
|
if not text: return [], ""
|
||||||
|
pairs = []
|
||||||
|
def _collect(m):
|
||||||
|
k, t = m.group("kind").strip().lower(), m.group("target").strip()
|
||||||
|
pairs.append((k, t))
|
||||||
|
return ""
|
||||||
|
text = _REL_PIPE.sub(_collect, text)
|
||||||
|
text = _REL_SPACE.sub(_collect, text)
|
||||||
|
text = _REL_TEXT.sub(_collect, text)
|
||||||
|
return pairs, text
|
||||||
|
|
||||||
|
def extract_callout_relations(text: str) -> Tuple[List[Tuple[str,str]], str]:
|
||||||
|
"""
|
||||||
|
Verarbeitet Obsidian [!edge]-Callouts.
|
||||||
|
Unterstützt zwei Formate:
|
||||||
|
1. Explizit: "kind: [[Target]]"
|
||||||
|
2. Implizit (Header): "> [!edge] kind" gefolgt von "[[Target]]" Zeilen
|
||||||
|
3. Verschachtelt: ">> [!edge] kind" in verschachtelten Callouts
|
||||||
|
"""
|
||||||
|
if not text: return [], text
|
||||||
|
lines = text.splitlines()
|
||||||
|
out_pairs = []
|
||||||
|
keep_lines = []
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while i < len(lines):
|
||||||
|
line = lines[i]
|
||||||
|
m = _CALLOUT_START.match(line)
|
||||||
|
if not m:
|
||||||
|
keep_lines.append(line)
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Callout-Block gefunden. Wir sammeln alle relevanten Zeilen.
|
||||||
|
block_lines = []
|
||||||
|
|
||||||
|
# Header Content prüfen (z.B. "type" aus "> [!edge] type" oder ">> [!edge] type")
|
||||||
|
header_raw = m.group(1).strip()
|
||||||
|
if header_raw:
|
||||||
|
block_lines.append(header_raw)
|
||||||
|
|
||||||
|
# Bestimme die Einrückungsebene (Anzahl der '>' am Anfang der ersten Zeile)
|
||||||
|
leading_gt_count = len(line) - len(line.lstrip('>'))
|
||||||
|
if leading_gt_count == 0:
|
||||||
|
leading_gt_count = 1 # Fallback für den Fall, dass kein '>' gefunden wurde
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
# Sammle alle Zeilen, die mit mindestens der gleichen Anzahl '>' beginnen
|
||||||
|
while i < len(lines):
|
||||||
|
next_line = lines[i]
|
||||||
|
stripped = next_line.lstrip()
|
||||||
|
# Prüfe, ob die Zeile mit mindestens der gleichen Anzahl '>' beginnt
|
||||||
|
if not stripped.startswith('>'):
|
||||||
|
break
|
||||||
|
next_leading_gt_count = len(next_line) - len(next_line.lstrip('>'))
|
||||||
|
# Wenn die Einrückung kleiner wird, haben wir den Block verlassen
|
||||||
|
if next_leading_gt_count < leading_gt_count:
|
||||||
|
break
|
||||||
|
# Entferne genau die Anzahl der führenden '>' entsprechend der Einrückungsebene
|
||||||
|
# und dann führende Leerzeichen
|
||||||
|
if next_leading_gt_count >= leading_gt_count:
|
||||||
|
# Entferne die führenden '>' (entsprechend der Einrückungsebene)
|
||||||
|
content = stripped[leading_gt_count:].lstrip()
|
||||||
|
if content:
|
||||||
|
block_lines.append(content)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Verarbeitung des Blocks
|
||||||
|
current_kind = None
|
||||||
|
|
||||||
|
# Heuristik: Ist die allererste Zeile (meist aus dem Header) ein reiner Typ?
|
||||||
|
# Dann setzen wir diesen als Default für den Block.
|
||||||
|
if block_lines:
|
||||||
|
first = block_lines[0]
|
||||||
|
# Wenn es NICHT wie "Key: Value" aussieht, aber wie ein Wort:
|
||||||
|
if not _REL_LINE.match(first) and _SIMPLE_KIND.match(first):
|
||||||
|
current_kind = first.lower()
|
||||||
|
|
||||||
|
for bl in block_lines:
|
||||||
|
# Prüfe, ob diese Zeile selbst ein neuer [!edge] Callout ist (für verschachtelte Blöcke)
|
||||||
|
edge_match = re.match(r"^\s*\[!edge\]\s*(.*)$", bl, re.IGNORECASE)
|
||||||
|
if edge_match:
|
||||||
|
# Neuer Edge-Callout gefunden, setze den Typ
|
||||||
|
edge_content = edge_match.group(1).strip()
|
||||||
|
if edge_content:
|
||||||
|
# Prüfe, ob es ein "kind: targets" Format ist
|
||||||
|
mrel = _REL_LINE.match(edge_content)
|
||||||
|
if mrel:
|
||||||
|
current_kind = mrel.group("kind").strip().lower()
|
||||||
|
targets = mrel.group("targets")
|
||||||
|
# Links extrahieren
|
||||||
|
found = _WIKILINK_RE.findall(targets)
|
||||||
|
if found:
|
||||||
|
for t in found: out_pairs.append((current_kind, t.strip()))
|
||||||
|
elif _SIMPLE_KIND.match(edge_content):
|
||||||
|
# Reiner Typ ohne Targets
|
||||||
|
current_kind = edge_content.lower()
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 1. Prüfen auf explizites "Kind: Targets" (überschreibt Header-Typ für diese Zeile)
|
||||||
|
mrel = _REL_LINE.match(bl)
|
||||||
|
if mrel:
|
||||||
|
line_kind = mrel.group("kind").strip().lower()
|
||||||
|
targets = mrel.group("targets")
|
||||||
|
|
||||||
|
# Links extrahieren
|
||||||
|
found = _WIKILINK_RE.findall(targets)
|
||||||
|
if found:
|
||||||
|
for t in found: out_pairs.append((line_kind, t.strip()))
|
||||||
|
else:
|
||||||
|
# Fallback für kommagetrennten Plaintext
|
||||||
|
for raw in re.split(r"[,;]", targets):
|
||||||
|
if raw.strip(): out_pairs.append((line_kind, raw.strip()))
|
||||||
|
|
||||||
|
# Aktualisiere current_kind für nachfolgende Zeilen
|
||||||
|
current_kind = line_kind
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 2. Kein Key:Value Muster -> Prüfen auf Links, die den current_kind nutzen
|
||||||
|
found = _WIKILINK_RE.findall(bl)
|
||||||
|
if found:
|
||||||
|
if current_kind:
|
||||||
|
for t in found: out_pairs.append((current_kind, t.strip()))
|
||||||
|
else:
|
||||||
|
# Link ohne Typ und ohne Header-Typ.
|
||||||
|
# Wird ignoriert oder könnte als 'related_to' fallback dienen.
|
||||||
|
# Aktuell: Ignorieren, um False Positives zu vermeiden.
|
||||||
|
pass
|
||||||
|
|
||||||
|
return out_pairs, "\n".join(keep_lines)
|
||||||
|
|
||||||
|
def extract_wikilinks(text: str) -> List[str]:
|
||||||
|
"""Findet Standard-Wikilinks [[Target]] oder [[Alias|Target]]."""
|
||||||
|
if not text: return []
|
||||||
|
return [m.strip() for m in _WIKILINK_RE.findall(text) if m.strip()]
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/graph_subgraph.py
|
||||||
|
DESCRIPTION: In-Memory Repräsentation eines Graphen für Scoring und Analyse.
|
||||||
|
Zentrale Komponente für die Graph-Expansion (BFS) und Bonus-Berechnung.
|
||||||
|
WP-15c Update: Erhalt von Metadaten (target_section, provenance)
|
||||||
|
für präzises Retrieval-Reasoning.
|
||||||
|
WP-24c v4.1.0: Scope-Awareness und Section-Filtering Support.
|
||||||
|
VERSION: 1.3.0 (WP-24c: Gold-Standard v4.1.0)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
from collections import defaultdict
|
||||||
|
from typing import Dict, List, Optional, DefaultDict, Any, Set
|
||||||
|
from qdrant_client import QdrantClient
|
||||||
|
|
||||||
|
# Lokale Paket-Imports
|
||||||
|
from .graph_weights import EDGE_BASE_WEIGHTS, calculate_edge_weight
|
||||||
|
from .graph_db_adapter import fetch_edges_from_qdrant
|
||||||
|
|
||||||
|
class Subgraph:
|
||||||
|
"""
|
||||||
|
Leichtgewichtiger Subgraph mit Adjazenzlisten & Kennzahlen.
|
||||||
|
Wird für die Berechnung von Graph-Boni im Retriever genutzt.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
# adj speichert nun vollständige Payloads statt nur Tripel
|
||||||
|
self.adj: DefaultDict[str, List[Dict]] = defaultdict(list)
|
||||||
|
self.reverse_adj: DefaultDict[str, List[Dict]] = defaultdict(list)
|
||||||
|
self.in_degree: DefaultDict[str, int] = defaultdict(int)
|
||||||
|
self.out_degree: DefaultDict[str, int] = defaultdict(int)
|
||||||
|
# WP-24c v4.1.0: Chunk-Level In-Degree für präzise Scoring-Aggregation
|
||||||
|
self.chunk_level_in_degree: DefaultDict[str, int] = defaultdict(int)
|
||||||
|
|
||||||
|
def add_edge(self, e: Dict) -> None:
|
||||||
|
"""
|
||||||
|
Fügt eine Kante hinzu und aktualisiert Indizes.
|
||||||
|
WP-15c: Speichert das vollständige Payload für den Explanation Layer.
|
||||||
|
"""
|
||||||
|
src = e.get("source")
|
||||||
|
tgt = e.get("target")
|
||||||
|
kind = e.get("kind")
|
||||||
|
|
||||||
|
# Das gesamte Payload wird als Kanten-Objekt behalten
|
||||||
|
# Wir stellen sicher, dass alle relevanten Metadaten vorhanden sind
|
||||||
|
edge_data = {
|
||||||
|
"source": src,
|
||||||
|
"target": tgt,
|
||||||
|
"kind": kind,
|
||||||
|
"weight": e.get("weight", EDGE_BASE_WEIGHTS.get(kind, 0.0)),
|
||||||
|
"provenance": e.get("provenance", "rule"),
|
||||||
|
"confidence": e.get("confidence", 1.0),
|
||||||
|
"target_section": e.get("target_section"), # Essentiell für Präzision
|
||||||
|
"is_super_edge": e.get("is_super_edge", False),
|
||||||
|
"virtual": e.get("virtual", False), # WP-24c v4.1.0: Für Authority-Priorisierung
|
||||||
|
"chunk_id": e.get("chunk_id") # WP-24c v4.1.0: Für RAG-Kontext
|
||||||
|
}
|
||||||
|
|
||||||
|
owner = e.get("note_id")
|
||||||
|
|
||||||
|
if not src or not tgt:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 1. Forward-Kante
|
||||||
|
self.adj[src].append(edge_data)
|
||||||
|
self.out_degree[src] += 1
|
||||||
|
self.in_degree[tgt] += 1
|
||||||
|
|
||||||
|
# 2. Reverse-Kante (für Explanation Layer & Backlinks)
|
||||||
|
self.reverse_adj[tgt].append(edge_data)
|
||||||
|
|
||||||
|
# 3. Kontext-Note Handling (erhöht die Zentralität der Parent-Note)
|
||||||
|
if owner and owner != src:
|
||||||
|
# Wir erstellen eine virtuelle Kontext-Kante
|
||||||
|
ctx_edge = edge_data.copy()
|
||||||
|
ctx_edge["source"] = owner
|
||||||
|
ctx_edge["via_context"] = True
|
||||||
|
|
||||||
|
self.adj[owner].append(ctx_edge)
|
||||||
|
self.out_degree[owner] += 1
|
||||||
|
if owner != tgt:
|
||||||
|
self.reverse_adj[tgt].append(ctx_edge)
|
||||||
|
self.in_degree[owner] += 1
|
||||||
|
|
||||||
|
def aggregate_edge_bonus(self, node_id: str) -> float:
|
||||||
|
"""Summe der ausgehenden Kantengewichte (Hub-Score)."""
|
||||||
|
return sum(edge["weight"] for edge in self.adj.get(node_id, []))
|
||||||
|
|
||||||
|
def edge_bonus(self, node_id: str) -> float:
|
||||||
|
"""API für Retriever (WP-04a Kompatibilität)."""
|
||||||
|
return self.aggregate_edge_bonus(node_id)
|
||||||
|
|
||||||
|
def centrality_bonus(self, node_id: str) -> float:
|
||||||
|
"""
|
||||||
|
Log-gedämpfte Zentralität basierend auf dem In-Degree.
|
||||||
|
Begrenzt auf einen maximalen Boost von 0.15.
|
||||||
|
"""
|
||||||
|
indeg = self.in_degree.get(node_id, 0)
|
||||||
|
if indeg <= 0:
|
||||||
|
return 0.0
|
||||||
|
# math.log1p(x) entspricht log(1+x)
|
||||||
|
return min(math.log1p(indeg) / 10.0, 0.15)
|
||||||
|
|
||||||
|
def get_outgoing_edges(self, node_id: str) -> List[Dict[str, Any]]:
|
||||||
|
"""Gibt alle ausgehenden Kanten einer Node inkl. Metadaten zurück."""
|
||||||
|
return self.adj.get(node_id, [])
|
||||||
|
|
||||||
|
def get_incoming_edges(self, node_id: str) -> List[Dict[str, Any]]:
|
||||||
|
"""Gibt alle eingehenden Kanten einer Node inkl. Metadaten zurück."""
|
||||||
|
return self.reverse_adj.get(node_id, [])
|
||||||
|
|
||||||
|
|
||||||
|
def expand(
|
||||||
|
client: QdrantClient,
|
||||||
|
prefix: str,
|
||||||
|
seeds: List[str],
|
||||||
|
depth: int = 1,
|
||||||
|
edge_types: Optional[List[str]] = None,
|
||||||
|
chunk_ids: Optional[List[str]] = None,
|
||||||
|
target_section: Optional[str] = None,
|
||||||
|
) -> Subgraph:
|
||||||
|
"""
|
||||||
|
Expandiert ab Seeds entlang von Edges bis zu einer bestimmten Tiefe.
|
||||||
|
WP-24c v4.1.0: Unterstützt Scope-Awareness (chunk_ids) und Section-Filtering.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client: Qdrant Client
|
||||||
|
prefix: Collection-Präfix
|
||||||
|
seeds: Liste von Note-IDs für die Expansion
|
||||||
|
depth: Maximale Tiefe der Expansion
|
||||||
|
edge_types: Optionale Filterung nach Kanten-Typen
|
||||||
|
chunk_ids: Optionale Liste von Chunk-IDs für Scope-Awareness
|
||||||
|
target_section: Optionales Section-Filtering
|
||||||
|
"""
|
||||||
|
sg = Subgraph()
|
||||||
|
frontier = set(seeds)
|
||||||
|
visited = set()
|
||||||
|
|
||||||
|
for _ in range(max(depth, 0)):
|
||||||
|
if not frontier:
|
||||||
|
break
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Erweiterte Edge-Retrieval mit Scope-Awareness und Section-Filtering
|
||||||
|
payloads = fetch_edges_from_qdrant(
|
||||||
|
client, prefix, list(frontier),
|
||||||
|
edge_types=edge_types,
|
||||||
|
chunk_ids=chunk_ids,
|
||||||
|
target_section=target_section
|
||||||
|
)
|
||||||
|
next_frontier: Set[str] = set()
|
||||||
|
|
||||||
|
for pl in payloads:
|
||||||
|
src, tgt = pl.get("source_id"), pl.get("target_id")
|
||||||
|
if not src or not tgt: continue
|
||||||
|
|
||||||
|
# WP-15c: Wir übergeben das vollständige Payload an add_edge
|
||||||
|
# WP-24c v4.1.0: virtual Flag wird für Authority-Priorisierung benötigt
|
||||||
|
edge_payload = {
|
||||||
|
"source": src,
|
||||||
|
"target": tgt,
|
||||||
|
"kind": pl.get("kind", "edge"),
|
||||||
|
"weight": calculate_edge_weight(pl),
|
||||||
|
"note_id": pl.get("note_id"),
|
||||||
|
"provenance": pl.get("provenance", "rule"),
|
||||||
|
"confidence": pl.get("confidence", 1.0),
|
||||||
|
"target_section": pl.get("target_section"),
|
||||||
|
"virtual": pl.get("virtual", False), # WP-24c v4.1.0: Für Authority-Priorisierung
|
||||||
|
"chunk_id": pl.get("chunk_id") # WP-24c v4.1.0: Für RAG-Kontext
|
||||||
|
}
|
||||||
|
|
||||||
|
sg.add_edge(edge_payload)
|
||||||
|
|
||||||
|
# BFS Logik: Neue Ziele in die nächste Frontier aufnehmen
|
||||||
|
if tgt not in visited:
|
||||||
|
next_frontier.add(str(tgt))
|
||||||
|
|
||||||
|
visited |= frontier
|
||||||
|
frontier = next_frontier - visited
|
||||||
|
|
||||||
|
return sg
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/graph_utils.py
|
||||||
|
DESCRIPTION: Basale Werkzeuge, ID-Generierung und Provenance-Konfiguration für den Graphen.
|
||||||
|
AUDIT v4.0.0:
|
||||||
|
- GOLD-STANDARD v4.0.0: Strikte 4-Parameter-ID für Kanten (kind, source, target, scope).
|
||||||
|
- Eliminiert ID-Inkonsistenz zwischen Phase 1 (Autorität) und Phase 2 (Symmetrie).
|
||||||
|
- rule_id und variant werden ignoriert in der ID-Generierung (nur im Payload gespeichert).
|
||||||
|
- Fix für das "Steinzeitaxt"-Problem durch konsistente ID-Generierung.
|
||||||
|
VERSION: 4.0.0 (WP-24c: Gold-Standard Identity)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
import hashlib
|
||||||
|
from typing import Iterable, List, Optional, Set, Any, Tuple
|
||||||
|
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
except ImportError:
|
||||||
|
yaml = None
|
||||||
|
|
||||||
|
# WP-15b: Prioritäten-Ranking für die De-Duplizierung von Kanten unterschiedlicher Herkunft
|
||||||
|
PROVENANCE_PRIORITY = {
|
||||||
|
"explicit:wikilink": 1.00,
|
||||||
|
"inline:rel": 0.95,
|
||||||
|
"callout:edge": 0.90,
|
||||||
|
"explicit:callout": 0.90, # WP-24c v4.2.7: Callout-Kanten aus candidate_pool
|
||||||
|
"semantic_ai": 0.90, # Validierte KI-Kanten
|
||||||
|
"structure:belongs_to": 1.00,
|
||||||
|
"structure:order": 0.95, # next/prev
|
||||||
|
"explicit:note_scope": 1.00,
|
||||||
|
"explicit:note_zone": 1.00, # WP-24c v4.2.0: Note-Scope Zonen (höchste Priorität)
|
||||||
|
"derived:backlink": 0.90,
|
||||||
|
"edge_defaults": 0.70 # Heuristik basierend auf types.yaml
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Pfad-Auflösung (Integration der .env Umgebungsvariablen)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def get_vocab_path() -> str:
|
||||||
|
"""Liefert den Pfad zum Edge-Vokabular aus der .env oder den Default."""
|
||||||
|
return os.getenv("MINDNET_VOCAB_PATH", "/mindnet/vault/mindnet/_system/dictionary/edge_vocabulary.md")
|
||||||
|
|
||||||
|
def get_schema_path() -> str:
|
||||||
|
"""Liefert den Pfad zum Graph-Schema aus der .env oder den Default."""
|
||||||
|
return os.getenv("MINDNET_SCHEMA_PATH", "/mindnet/vault/mindnet/_system/dictionary/graph_schema.md")
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# ID & String Helper
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _get(d: dict, *keys, default=None):
|
||||||
|
"""Sicherer Zugriff auf tief verschachtelte Dictionary-Keys."""
|
||||||
|
for k in keys:
|
||||||
|
if isinstance(d, dict) and k in d and d[k] is not None:
|
||||||
|
return d[k]
|
||||||
|
return default
|
||||||
|
|
||||||
|
def _dedupe_seq(seq: Iterable[str]) -> List[str]:
|
||||||
|
"""Dedupliziert eine Sequenz von Strings unter Beibehaltung der Reihenfolge."""
|
||||||
|
seen: Set[str] = set()
|
||||||
|
out: List[str] = []
|
||||||
|
for s in seq:
|
||||||
|
if s not in seen:
|
||||||
|
seen.add(s)
|
||||||
|
out.append(s)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def parse_link_target(raw: str, current_note_id: Optional[str] = None) -> Tuple[str, Optional[str]]:
|
||||||
|
"""
|
||||||
|
Trennt einen Obsidian-Link [[Target#Section]] in seine Bestandteile Target und Section.
|
||||||
|
Behandelt Self-Links (z.B. [[#Ziele]]), indem die aktuelle note_id eingesetzt wird.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple (target_id, target_section)
|
||||||
|
"""
|
||||||
|
if not raw:
|
||||||
|
return "", None
|
||||||
|
|
||||||
|
parts = raw.split("#", 1)
|
||||||
|
target = parts[0].strip()
|
||||||
|
section = parts[1].strip() if len(parts) > 1 else None
|
||||||
|
|
||||||
|
# Spezialfall: Self-Link innerhalb derselben Datei
|
||||||
|
if not target and section and current_note_id:
|
||||||
|
target = current_note_id
|
||||||
|
|
||||||
|
return target, section
|
||||||
|
|
||||||
|
def _mk_edge_id(kind: str, s: str, t: str, scope: str, target_section: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
WP-24c v4.0.0: DER GLOBALE STANDARD für Kanten-IDs.
|
||||||
|
Erzeugt eine deterministische UUIDv5. Dies stellt sicher, dass manuelle Links
|
||||||
|
und systemgenerierte Symmetrien dieselbe Point-ID in Qdrant erhalten.
|
||||||
|
|
||||||
|
GOLD-STANDARD v4.0.0: Die ID basiert STRICT auf vier Parametern:
|
||||||
|
f"edge:{kind}:{source}:{target}:{scope}"
|
||||||
|
|
||||||
|
Die Parameter rule_id und variant werden IGNORIERT und fließen NICHT in die ID ein.
|
||||||
|
Sie können weiterhin im Payload gespeichert werden, haben aber keinen Einfluss auf die Identität.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
kind: Typ der Relation (z.B. 'mastered_by')
|
||||||
|
s: Kanonische ID der Quell-Note
|
||||||
|
t: Kanonische ID der Ziel-Note
|
||||||
|
scope: Granularität (Standard: 'note')
|
||||||
|
rule_id: Optionale ID der Regel (aus graph_derive_edges) - IGNORIERT in ID-Generierung
|
||||||
|
variant: Optionale Variante für multiple Links zum selben Ziel - IGNORIERT in ID-Generierung
|
||||||
|
"""
|
||||||
|
if not all([kind, s, t]):
|
||||||
|
raise ValueError(f"Incomplete data for edge ID: kind={kind}, src={s}, tgt={t}")
|
||||||
|
|
||||||
|
# Der String enthält nun alle distinkten semantischen Merkmale
|
||||||
|
base = f"edge:{kind}:{s}:{t}:{scope}"
|
||||||
|
|
||||||
|
# Wenn ein Link auf eine spezifische Sektion zeigt, ist es eine andere Relation
|
||||||
|
if target_section:
|
||||||
|
base += f":{target_section}"
|
||||||
|
|
||||||
|
return str(uuid.uuid5(uuid.NAMESPACE_URL, base))
|
||||||
|
|
||||||
|
def _edge(kind: str, scope: str, source_id: str, target_id: str, note_id: str, extra: Optional[dict] = None) -> dict:
|
||||||
|
"""
|
||||||
|
Konstruiert ein standardisiertes Kanten-Payload für Qdrant.
|
||||||
|
Wird von graph_derive_edges.py benötigt.
|
||||||
|
"""
|
||||||
|
pl = {
|
||||||
|
"kind": kind,
|
||||||
|
"relation": kind,
|
||||||
|
"scope": scope,
|
||||||
|
"source_id": source_id,
|
||||||
|
"target_id": target_id,
|
||||||
|
"note_id": note_id,
|
||||||
|
"virtual": False # Standardmäßig explizit, solange nicht anders in Phase 2 gesetzt
|
||||||
|
}
|
||||||
|
if extra:
|
||||||
|
pl.update(extra)
|
||||||
|
return pl
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Registry Operations
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def load_types_registry() -> dict:
|
||||||
|
"""
|
||||||
|
Lädt die zentrale YAML-Registry (types.yaml).
|
||||||
|
Pfad wird über die Umgebungsvariable MINDNET_TYPES_FILE gesteuert.
|
||||||
|
"""
|
||||||
|
p = os.getenv("MINDNET_TYPES_FILE", "./config/types.yaml")
|
||||||
|
if not os.path.isfile(p) or yaml is None:
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
with open(p, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f)
|
||||||
|
return data if data is not None else {}
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_edge_defaults_for(note_type: Optional[str], reg: dict) -> List[str]:
|
||||||
|
"""
|
||||||
|
Ermittelt die konfigurierten Standard-Kanten für einen Note-Typ.
|
||||||
|
Greift bei Bedarf auf die globalen Defaults in der Registry zurück.
|
||||||
|
"""
|
||||||
|
types_map = reg.get("types", reg) if isinstance(reg, dict) else {}
|
||||||
|
if note_type and isinstance(types_map, dict):
|
||||||
|
t_cfg = types_map.get(note_type)
|
||||||
|
if isinstance(t_cfg, dict) and isinstance(t_cfg.get("edge_defaults"), list):
|
||||||
|
return [str(x) for x in t_cfg["edge_defaults"]]
|
||||||
|
|
||||||
|
# Fallback auf globale Defaults
|
||||||
|
for key in ("defaults", "default", "global"):
|
||||||
|
v = reg.get(key)
|
||||||
|
if isinstance(v, dict) and isinstance(v.get("edge_defaults"), list):
|
||||||
|
return [str(x) for x in v["edge_defaults"] if isinstance(x, str)]
|
||||||
|
|
||||||
|
return []
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/graph/graph_weights.py
|
||||||
|
DESCRIPTION: Definition der Basisgewichte und Berechnung der Kanteneffektivität.
|
||||||
|
"""
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
# Basisgewichte je Edge-Typ (WP-04a Config)
|
||||||
|
EDGE_BASE_WEIGHTS: Dict[str, float] = {
|
||||||
|
# Struktur
|
||||||
|
"belongs_to": 0.10,
|
||||||
|
"next": 0.06,
|
||||||
|
"prev": 0.06,
|
||||||
|
"backlink": 0.04,
|
||||||
|
"references_at": 0.08,
|
||||||
|
|
||||||
|
# Wissen
|
||||||
|
"references": 0.20,
|
||||||
|
"depends_on": 0.18,
|
||||||
|
"related_to": 0.15,
|
||||||
|
"similar_to": 0.12,
|
||||||
|
}
|
||||||
|
|
||||||
|
def calculate_edge_weight(pl: Dict) -> float:
|
||||||
|
"""Berechnet das effektive Edge-Gewicht aus kind + confidence."""
|
||||||
|
kind = pl.get("kind", "edge")
|
||||||
|
base = EDGE_BASE_WEIGHTS.get(kind, 0.0)
|
||||||
|
|
||||||
|
conf_raw = pl.get("confidence", None)
|
||||||
|
try:
|
||||||
|
conf = float(conf_raw) if conf_raw is not None else None
|
||||||
|
except Exception:
|
||||||
|
conf = None
|
||||||
|
|
||||||
|
if conf is None:
|
||||||
|
return base
|
||||||
|
|
||||||
|
# Clamp confidence 0.0 - 1.0
|
||||||
|
conf = max(0.0, min(1.0, conf))
|
||||||
|
return base * conf
|
||||||
@@ -1,249 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/graph_adapter.py
|
|
||||||
DESCRIPTION: Lädt Kanten aus Qdrant und baut einen In-Memory Subgraphen für Scoring (Centrality) und Explanation.
|
|
||||||
VERSION: 0.4.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: qdrant_client, app.core.qdrant
|
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Dict, List, Optional, DefaultDict, Any
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
from qdrant_client import QdrantClient
|
|
||||||
from qdrant_client.http import models as rest
|
|
||||||
|
|
||||||
from app.core.qdrant import collection_names
|
|
||||||
|
|
||||||
# Legacy-Import Fallback
|
|
||||||
try: # pragma: no cover
|
|
||||||
from app.core.qdrant_points import get_edges_for_sources # type: ignore
|
|
||||||
except Exception: # pragma: no cover
|
|
||||||
get_edges_for_sources = None # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
# Basisgewichte je Edge-Typ (WP-04a Config)
|
|
||||||
EDGE_BASE_WEIGHTS: Dict[str, float] = {
|
|
||||||
# Struktur
|
|
||||||
"belongs_to": 0.10,
|
|
||||||
"next": 0.06,
|
|
||||||
"prev": 0.06,
|
|
||||||
"backlink": 0.04,
|
|
||||||
"references_at": 0.08,
|
|
||||||
|
|
||||||
# Wissen
|
|
||||||
"references": 0.20,
|
|
||||||
"depends_on": 0.18,
|
|
||||||
"related_to": 0.15,
|
|
||||||
"similar_to": 0.12,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _edge_weight(pl: Dict) -> float:
|
|
||||||
"""Berechnet das effektive Edge-Gewicht aus kind + confidence."""
|
|
||||||
kind = pl.get("kind", "edge")
|
|
||||||
base = EDGE_BASE_WEIGHTS.get(kind, 0.0)
|
|
||||||
|
|
||||||
conf_raw = pl.get("confidence", None)
|
|
||||||
try:
|
|
||||||
conf = float(conf_raw) if conf_raw is not None else None
|
|
||||||
except Exception:
|
|
||||||
conf = None
|
|
||||||
|
|
||||||
if conf is None:
|
|
||||||
return base
|
|
||||||
|
|
||||||
if conf < 0.0: conf = 0.0
|
|
||||||
if conf > 1.0: conf = 1.0
|
|
||||||
|
|
||||||
return base * conf
|
|
||||||
|
|
||||||
|
|
||||||
def _fetch_edges(
|
|
||||||
client: QdrantClient,
|
|
||||||
prefix: str,
|
|
||||||
seeds: List[str],
|
|
||||||
edge_types: Optional[List[str]] = None,
|
|
||||||
limit: int = 2048,
|
|
||||||
) -> List[Dict]:
|
|
||||||
"""
|
|
||||||
Holt Edges direkt aus der *_edges Collection.
|
|
||||||
Filter: source_id IN seeds OR target_id IN seeds OR note_id IN seeds
|
|
||||||
"""
|
|
||||||
if not seeds or limit <= 0:
|
|
||||||
return []
|
|
||||||
|
|
||||||
_, _, edges_col = collection_names(prefix)
|
|
||||||
|
|
||||||
seed_conditions = []
|
|
||||||
for field in ("source_id", "target_id", "note_id"):
|
|
||||||
for s in seeds:
|
|
||||||
seed_conditions.append(
|
|
||||||
rest.FieldCondition(key=field, match=rest.MatchValue(value=str(s)))
|
|
||||||
)
|
|
||||||
seeds_filter = rest.Filter(should=seed_conditions) if seed_conditions else None
|
|
||||||
|
|
||||||
type_filter = None
|
|
||||||
if edge_types:
|
|
||||||
type_conds = [
|
|
||||||
rest.FieldCondition(key="kind", match=rest.MatchValue(value=str(k)))
|
|
||||||
for k in edge_types
|
|
||||||
]
|
|
||||||
type_filter = rest.Filter(should=type_conds)
|
|
||||||
|
|
||||||
must = []
|
|
||||||
if seeds_filter: must.append(seeds_filter)
|
|
||||||
if type_filter: must.append(type_filter)
|
|
||||||
|
|
||||||
flt = rest.Filter(must=must) if must else None
|
|
||||||
|
|
||||||
pts, _ = client.scroll(
|
|
||||||
collection_name=edges_col,
|
|
||||||
scroll_filter=flt,
|
|
||||||
limit=limit,
|
|
||||||
with_payload=True,
|
|
||||||
with_vectors=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
out: List[Dict] = []
|
|
||||||
for p in pts or []:
|
|
||||||
pl = dict(p.payload or {})
|
|
||||||
if pl:
|
|
||||||
out.append(pl)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
class Subgraph:
|
|
||||||
"""Leichtgewichtiger Subgraph mit Adjazenzlisten & Kennzahlen."""
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
# Forward: source -> [targets]
|
|
||||||
self.adj: DefaultDict[str, List[Dict]] = defaultdict(list)
|
|
||||||
# Reverse: target -> [sources] (Neu für WP-04b Explanation)
|
|
||||||
self.reverse_adj: DefaultDict[str, List[Dict]] = defaultdict(list)
|
|
||||||
|
|
||||||
self.in_degree: DefaultDict[str, int] = defaultdict(int)
|
|
||||||
self.out_degree: DefaultDict[str, int] = defaultdict(int)
|
|
||||||
|
|
||||||
def add_edge(self, e: Dict) -> None:
|
|
||||||
"""
|
|
||||||
Fügt eine Kante hinzu und aktualisiert Forward/Reverse Indizes.
|
|
||||||
e muss enthalten: source, target, kind, weight.
|
|
||||||
"""
|
|
||||||
src = e.get("source")
|
|
||||||
tgt = e.get("target")
|
|
||||||
kind = e.get("kind")
|
|
||||||
weight = e.get("weight", EDGE_BASE_WEIGHTS.get(kind, 0.0))
|
|
||||||
owner = e.get("note_id")
|
|
||||||
|
|
||||||
if not src or not tgt:
|
|
||||||
return
|
|
||||||
|
|
||||||
# 1. Primäre Adjazenz (Forward)
|
|
||||||
edge_data = {"target": tgt, "kind": kind, "weight": weight}
|
|
||||||
self.adj[src].append(edge_data)
|
|
||||||
self.out_degree[src] += 1
|
|
||||||
self.in_degree[tgt] += 1
|
|
||||||
|
|
||||||
# 2. Reverse Adjazenz (Neu für Explanation)
|
|
||||||
# Wir speichern, woher die Kante kam.
|
|
||||||
rev_data = {"source": src, "kind": kind, "weight": weight}
|
|
||||||
self.reverse_adj[tgt].append(rev_data)
|
|
||||||
|
|
||||||
# 3. Kontext-Note Handling (Forward & Reverse)
|
|
||||||
# Wenn eine Kante "im Kontext einer Note" (owner) definiert ist,
|
|
||||||
# schreiben wir sie der Note gut, damit der Retriever Scores auf Note-Ebene findet.
|
|
||||||
if owner and owner != src:
|
|
||||||
# Forward: Owner -> Target
|
|
||||||
self.adj[owner].append(edge_data)
|
|
||||||
self.out_degree[owner] += 1
|
|
||||||
|
|
||||||
# Reverse: Target wird vom Owner referenziert (indirekt)
|
|
||||||
if owner != tgt:
|
|
||||||
rev_owner_data = {"source": owner, "kind": kind, "weight": weight, "via_context": True}
|
|
||||||
self.reverse_adj[tgt].append(rev_owner_data)
|
|
||||||
self.in_degree[owner] += 1 # Leichter Centrality Boost für den Owner
|
|
||||||
|
|
||||||
def aggregate_edge_bonus(self, node_id: str) -> float:
|
|
||||||
"""Summe der ausgehenden Kantengewichte (Hub-Score)."""
|
|
||||||
return sum(edge["weight"] for edge in self.adj.get(node_id, []))
|
|
||||||
|
|
||||||
def edge_bonus(self, node_id: str) -> float:
|
|
||||||
"""API für Retriever (WP-04a Kompatibilität)."""
|
|
||||||
return self.aggregate_edge_bonus(node_id)
|
|
||||||
|
|
||||||
def centrality_bonus(self, node_id: str) -> float:
|
|
||||||
"""Log-gedämpfte Zentralität (In-Degree)."""
|
|
||||||
import math
|
|
||||||
indeg = self.in_degree.get(node_id, 0)
|
|
||||||
if indeg <= 0:
|
|
||||||
return 0.0
|
|
||||||
return min(math.log1p(indeg) / 10.0, 0.15)
|
|
||||||
|
|
||||||
# --- WP-04b Explanation Helpers ---
|
|
||||||
|
|
||||||
def get_outgoing_edges(self, node_id: str) -> List[Dict[str, Any]]:
|
|
||||||
"""Liefert Liste aller Ziele, auf die dieser Knoten zeigt."""
|
|
||||||
return self.adj.get(node_id, [])
|
|
||||||
|
|
||||||
def get_incoming_edges(self, node_id: str) -> List[Dict[str, Any]]:
|
|
||||||
"""Liefert Liste aller Quellen, die auf diesen Knoten zeigen."""
|
|
||||||
return self.reverse_adj.get(node_id, [])
|
|
||||||
|
|
||||||
|
|
||||||
def expand(
|
|
||||||
client: QdrantClient,
|
|
||||||
prefix: str,
|
|
||||||
seeds: List[str],
|
|
||||||
depth: int = 1,
|
|
||||||
edge_types: Optional[List[str]] = None,
|
|
||||||
) -> Subgraph:
|
|
||||||
"""
|
|
||||||
Expandiert ab Seeds entlang von Edges (bis `depth`).
|
|
||||||
"""
|
|
||||||
sg = Subgraph()
|
|
||||||
frontier = set(seeds)
|
|
||||||
visited = set()
|
|
||||||
|
|
||||||
max_depth = max(depth, 0)
|
|
||||||
|
|
||||||
for _ in range(max_depth):
|
|
||||||
if not frontier:
|
|
||||||
break
|
|
||||||
|
|
||||||
edges_payloads = _fetch_edges(
|
|
||||||
client=client,
|
|
||||||
prefix=prefix,
|
|
||||||
seeds=list(frontier),
|
|
||||||
edge_types=edge_types,
|
|
||||||
limit=2048,
|
|
||||||
)
|
|
||||||
|
|
||||||
next_frontier = set()
|
|
||||||
for pl in edges_payloads:
|
|
||||||
src = pl.get("source_id")
|
|
||||||
tgt = pl.get("target_id")
|
|
||||||
|
|
||||||
# Skip invalid edges
|
|
||||||
if not src or not tgt:
|
|
||||||
continue
|
|
||||||
|
|
||||||
e = {
|
|
||||||
"source": src,
|
|
||||||
"target": tgt,
|
|
||||||
"kind": pl.get("kind", "edge"),
|
|
||||||
"weight": _edge_weight(pl),
|
|
||||||
"note_id": pl.get("note_id"),
|
|
||||||
}
|
|
||||||
sg.add_edge(e)
|
|
||||||
|
|
||||||
# Nur weitersuchen, wenn Target noch nicht besucht
|
|
||||||
if tgt and tgt not in visited:
|
|
||||||
next_frontier.add(tgt)
|
|
||||||
|
|
||||||
visited |= frontier
|
|
||||||
frontier = next_frontier - visited
|
|
||||||
|
|
||||||
return sg
|
|
||||||
@@ -1,373 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/ingestion.py
|
|
||||||
DESCRIPTION: Haupt-Ingestion-Logik. Transformiert Markdown in den Graphen.
|
|
||||||
WP-20: Optimiert für OpenRouter (mistralai/mistral-7b-instruct:free).
|
|
||||||
WP-22: Content Lifecycle, Edge Registry Validation & Multi-Hash.
|
|
||||||
WP-15b: Two-Pass Ingestion mit LocalBatchCache & Candidate-Validation.
|
|
||||||
Sichert, dass explizite Kanten direkt übernommen und nur Pool-Kanten validiert werden.
|
|
||||||
FIX: Deep Fallback Logic (v2.11.14) für JSON-Recovery.
|
|
||||||
Robust Lookup Fix: Adressiert Notizen im Cache via ID, Titel und Dateiname.
|
|
||||||
VERSION: 2.12.2
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: app.core.parser, app.core.note_payload, app.core.chunker,
|
|
||||||
app.services.llm_service, app.services.edge_registry
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
import logging
|
|
||||||
import asyncio
|
|
||||||
import time
|
|
||||||
from typing import Dict, List, Optional, Tuple, Any
|
|
||||||
|
|
||||||
# Core Module Imports
|
|
||||||
from app.core.parser import (
|
|
||||||
read_markdown,
|
|
||||||
pre_scan_markdown,
|
|
||||||
normalize_frontmatter,
|
|
||||||
validate_required_frontmatter,
|
|
||||||
extract_edges_with_context,
|
|
||||||
NoteContext
|
|
||||||
)
|
|
||||||
from app.core.note_payload import make_note_payload
|
|
||||||
from app.core.chunker import assemble_chunks, get_chunk_config
|
|
||||||
from app.core.chunk_payload import make_chunk_payloads
|
|
||||||
|
|
||||||
# Fallback für Edges
|
|
||||||
try:
|
|
||||||
from app.core.derive_edges import build_edges_for_note
|
|
||||||
except ImportError:
|
|
||||||
def build_edges_for_note(*args, **kwargs): return []
|
|
||||||
|
|
||||||
from app.core.qdrant import QdrantConfig, get_client, ensure_collections, ensure_payload_indexes
|
|
||||||
from app.core.qdrant_points import (
|
|
||||||
points_for_chunks,
|
|
||||||
points_for_note,
|
|
||||||
points_for_edges,
|
|
||||||
upsert_batch,
|
|
||||||
)
|
|
||||||
|
|
||||||
from app.services.embeddings_client import EmbeddingsClient
|
|
||||||
from app.services.edge_registry import registry as edge_registry
|
|
||||||
from app.services.llm_service import LLMService
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# --- Global Helpers (Full Compatibility v2.11.14) ---
|
|
||||||
def extract_json_from_response(text: str) -> Any:
|
|
||||||
"""
|
|
||||||
Extrahiert JSON-Daten und bereinigt LLM-Steuerzeichen (Mistral/Llama).
|
|
||||||
Entfernt <s>, [OUT], [/OUT] und Markdown-Blöcke für maximale Robustheit.
|
|
||||||
"""
|
|
||||||
if not text or not isinstance(text, str):
|
|
||||||
return []
|
|
||||||
|
|
||||||
# 1. Entferne Mistral/Llama Steuerzeichen und Tags
|
|
||||||
clean = text.replace("<s>", "").replace("</s>", "")
|
|
||||||
clean = clean.replace("[OUT]", "").replace("[/OUT]", "")
|
|
||||||
clean = clean.strip()
|
|
||||||
|
|
||||||
# 2. Suche nach Markdown JSON-Blöcken (```json ... ```)
|
|
||||||
match = re.search(r"```(?:json)?\s*(.*?)\s*```", clean, re.DOTALL)
|
|
||||||
payload = match.group(1) if match else clean
|
|
||||||
|
|
||||||
try:
|
|
||||||
return json.loads(payload.strip())
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# 3. Recovery: Suche nach der ersten [ und letzten ] (Liste)
|
|
||||||
start = payload.find('[')
|
|
||||||
end = payload.rfind(']') + 1
|
|
||||||
if start != -1 and end > start:
|
|
||||||
try:
|
|
||||||
return json.loads(payload[start:end])
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
# 4. Zweite Recovery: Suche nach der ersten { und letzten } (Objekt)
|
|
||||||
start_obj = payload.find('{')
|
|
||||||
end_obj = payload.rfind('}') + 1
|
|
||||||
if start_obj != -1 and end_obj > start_obj:
|
|
||||||
try:
|
|
||||||
return json.loads(payload[start_obj:end_obj])
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
return []
|
|
||||||
|
|
||||||
def load_type_registry(custom_path: Optional[str] = None) -> dict:
|
|
||||||
"""Lädt die types.yaml zur Steuerung der typ-spezifischen Ingestion."""
|
|
||||||
import yaml
|
|
||||||
from app.config import get_settings
|
|
||||||
settings = get_settings()
|
|
||||||
path = custom_path or settings.MINDNET_TYPES_FILE
|
|
||||||
if not os.path.exists(path): return {}
|
|
||||||
try:
|
|
||||||
with open(path, "r", encoding="utf-8") as f: return yaml.safe_load(f) or {}
|
|
||||||
except Exception: return {}
|
|
||||||
|
|
||||||
# --- Service Class ---
|
|
||||||
class IngestionService:
|
|
||||||
def __init__(self, collection_prefix: str = None):
|
|
||||||
from app.config import get_settings
|
|
||||||
self.settings = get_settings()
|
|
||||||
|
|
||||||
self.prefix = collection_prefix or self.settings.COLLECTION_PREFIX
|
|
||||||
self.cfg = QdrantConfig.from_env()
|
|
||||||
self.cfg.prefix = self.prefix
|
|
||||||
self.client = get_client(self.cfg)
|
|
||||||
self.dim = self.settings.VECTOR_SIZE
|
|
||||||
self.registry = load_type_registry()
|
|
||||||
self.embedder = EmbeddingsClient()
|
|
||||||
self.llm = LLMService()
|
|
||||||
|
|
||||||
self.active_hash_mode = self.settings.CHANGE_DETECTION_MODE
|
|
||||||
self.batch_cache: Dict[str, NoteContext] = {} # WP-15b LocalBatchCache
|
|
||||||
|
|
||||||
try:
|
|
||||||
ensure_collections(self.client, self.prefix, self.dim)
|
|
||||||
ensure_payload_indexes(self.client, self.prefix)
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"DB init warning: {e}")
|
|
||||||
|
|
||||||
async def run_batch(self, file_paths: List[str], vault_root: str) -> List[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
WP-15b: Implementiert den Two-Pass Ingestion Workflow.
|
|
||||||
Pass 1: Pre-Scan baut flüchtigen Kontext-Cache auf.
|
|
||||||
Pass 2: Processing führt die eigentliche semantische Validierung durch.
|
|
||||||
"""
|
|
||||||
logger.info(f"🔍 [Pass 1] Pre-Scanning {len(file_paths)} files for Context Cache...")
|
|
||||||
for path in file_paths:
|
|
||||||
ctx = pre_scan_markdown(path)
|
|
||||||
if ctx:
|
|
||||||
# Mehrfache Indizierung für robusten Look-up (WP-15b Fix)
|
|
||||||
self.batch_cache[ctx.note_id] = ctx
|
|
||||||
self.batch_cache[ctx.title] = ctx
|
|
||||||
# Dateiname ohne Endung als dritter Schlüssel
|
|
||||||
fname = os.path.splitext(os.path.basename(path))[0]
|
|
||||||
self.batch_cache[fname] = ctx
|
|
||||||
|
|
||||||
logger.info(f"🚀 [Pass 2] Semantic Processing of {len(file_paths)} files...")
|
|
||||||
results = []
|
|
||||||
for path in file_paths:
|
|
||||||
res = await self.process_file(path, vault_root, apply=True)
|
|
||||||
results.append(res)
|
|
||||||
return results
|
|
||||||
|
|
||||||
async def _validate_candidate(self, chunk_text: str, edge: Dict) -> bool:
|
|
||||||
"""
|
|
||||||
WP-15b: Validiert einen Kanten-Kandidaten semantisch gegen das Ziel.
|
|
||||||
Nutzt den Cache aus Pass 1, um dem LLM Kontext der Ziel-Note zu geben.
|
|
||||||
"""
|
|
||||||
target_id = edge.get("to")
|
|
||||||
target_ctx = self.batch_cache.get(target_id)
|
|
||||||
|
|
||||||
# Fallback Look-up für Links mit Ankern (Anchor entfernen)
|
|
||||||
if not target_ctx and "#" in target_id:
|
|
||||||
base_id = target_id.split("#")[0]
|
|
||||||
target_ctx = self.batch_cache.get(base_id)
|
|
||||||
|
|
||||||
# Sicherheits-Fallback: Wenn Zielnotiz nicht im aktuellen Batch ist,
|
|
||||||
# lassen wir die Kante als 'explicit' durch (Hard-Link Integrity).
|
|
||||||
if not target_ctx:
|
|
||||||
logger.info(f"ℹ️ [VALIDATION SKIP] No context for '{target_id}' - allowing link.")
|
|
||||||
return True
|
|
||||||
|
|
||||||
provider = self.settings.MINDNET_LLM_PROVIDER
|
|
||||||
template = self.llm.get_prompt("edge_validation", provider)
|
|
||||||
|
|
||||||
try:
|
|
||||||
logger.info(f"⚖️ [VALIDATING] Relation '{edge.get('kind')}' -> '{target_id}'...")
|
|
||||||
prompt = template.format(
|
|
||||||
chunk_text=chunk_text[:1500],
|
|
||||||
target_title=target_ctx.title,
|
|
||||||
target_summary=target_ctx.summary,
|
|
||||||
edge_kind=edge.get("kind", "related_to")
|
|
||||||
)
|
|
||||||
|
|
||||||
response = await self.llm.generate_raw_response(prompt, priority="background")
|
|
||||||
is_valid = "YES" in response.upper()
|
|
||||||
|
|
||||||
if is_valid:
|
|
||||||
logger.info(f"✅ [VALIDATED] Relation to '{target_id}' confirmed.")
|
|
||||||
else:
|
|
||||||
logger.info(f"🚫 [REJECTED] Relation to '{target_id}' irrelevant for this chunk.")
|
|
||||||
|
|
||||||
return is_valid
|
|
||||||
except Exception as e:
|
|
||||||
logger.warning(f"⚠️ Semantic validation error for {target_id}: {e}")
|
|
||||||
return True # Fallback: Im Zweifel Link behalten
|
|
||||||
|
|
||||||
def _resolve_note_type(self, requested: Optional[str]) -> str:
|
|
||||||
"""Bestimmt den finalen Notiz-Typ (Fallback auf 'concept')."""
|
|
||||||
types = self.registry.get("types", {})
|
|
||||||
if requested and requested in types: return requested
|
|
||||||
return "concept"
|
|
||||||
|
|
||||||
def _get_chunk_config_by_profile(self, profile_name: str, note_type: str) -> Dict[str, Any]:
|
|
||||||
"""Holt die Chunker-Parameter für ein spezifisches Profil aus der Registry."""
|
|
||||||
profiles = self.registry.get("chunking_profiles", {})
|
|
||||||
if profile_name in profiles:
|
|
||||||
cfg = profiles[profile_name].copy()
|
|
||||||
if "overlap" in cfg and isinstance(cfg["overlap"], list):
|
|
||||||
cfg["overlap"] = tuple(cfg["overlap"])
|
|
||||||
return cfg
|
|
||||||
return get_chunk_config(note_type)
|
|
||||||
|
|
||||||
async def process_file(
|
|
||||||
self, file_path: str, vault_root: str,
|
|
||||||
force_replace: bool = False, apply: bool = False, purge_before: bool = False,
|
|
||||||
note_scope_refs: bool = False, hash_source: str = "parsed", hash_normalize: str = "canonical"
|
|
||||||
) -> Dict[str, Any]:
|
|
||||||
"""Transformiert eine Markdown-Datei in den Graphen."""
|
|
||||||
result = {"path": file_path, "status": "skipped", "changed": False, "error": None}
|
|
||||||
|
|
||||||
# 1. Parse & Lifecycle Gate
|
|
||||||
try:
|
|
||||||
parsed = read_markdown(file_path)
|
|
||||||
if not parsed: return {**result, "error": "Empty file"}
|
|
||||||
fm = normalize_frontmatter(parsed.frontmatter)
|
|
||||||
validate_required_frontmatter(fm)
|
|
||||||
except Exception as e:
|
|
||||||
return {**result, "error": f"Validation failed: {str(e)}"}
|
|
||||||
|
|
||||||
# Lifecycle Filter (WP-22)
|
|
||||||
status = fm.get("status", "draft").lower().strip()
|
|
||||||
if status in ["system", "template", "archive", "hidden"]:
|
|
||||||
return {**result, "status": "skipped", "reason": f"lifecycle_{status}"}
|
|
||||||
|
|
||||||
# 2. Config Resolution & Payload
|
|
||||||
note_type = self._resolve_note_type(fm.get("type"))
|
|
||||||
fm["type"] = note_type
|
|
||||||
|
|
||||||
try:
|
|
||||||
note_pl = make_note_payload(parsed, vault_root=vault_root, hash_normalize=hash_normalize, hash_source=hash_source, file_path=file_path)
|
|
||||||
note_id = note_pl["note_id"]
|
|
||||||
except Exception as e:
|
|
||||||
return {**result, "error": f"Payload failed: {str(e)}"}
|
|
||||||
|
|
||||||
# 3. Change Detection (v2.11.14 Logic)
|
|
||||||
old_payload = None if force_replace else self._fetch_note_payload(note_id)
|
|
||||||
check_key = f"{self.active_hash_mode}:{hash_source}:{hash_normalize}"
|
|
||||||
old_hash = (old_payload or {}).get("hashes", {}).get(check_key)
|
|
||||||
new_hash = note_pl.get("hashes", {}).get(check_key)
|
|
||||||
|
|
||||||
chunks_missing, edges_missing = self._artifacts_missing(note_id)
|
|
||||||
should_write = force_replace or (not old_payload) or (old_hash != new_hash) or chunks_missing or edges_missing
|
|
||||||
|
|
||||||
if not should_write:
|
|
||||||
return {**result, "status": "unchanged", "note_id": note_id}
|
|
||||||
|
|
||||||
if not apply:
|
|
||||||
return {**result, "status": "dry-run", "changed": True, "note_id": note_id}
|
|
||||||
|
|
||||||
# 4. Processing (Chunking, Embedding, Validated Edges)
|
|
||||||
try:
|
|
||||||
body_text = getattr(parsed, "body", "") or ""
|
|
||||||
edge_registry.ensure_latest()
|
|
||||||
|
|
||||||
# Chunker Resolution
|
|
||||||
profile = fm.get("chunk_profile") or fm.get("chunking_profile") or "sliding_standard"
|
|
||||||
chunk_cfg = self._get_chunk_config_by_profile(profile, note_type)
|
|
||||||
enable_smart_edges = chunk_cfg.get("enable_smart_edge_allocation", False)
|
|
||||||
|
|
||||||
# WP-15b: Chunker bereitet nun den Candidate-Pool vor (inkl. Inheritance).
|
|
||||||
chunks = await assemble_chunks(fm["id"], body_text, fm["type"], config=chunk_cfg)
|
|
||||||
|
|
||||||
# WP-15b: Validierung NUR für Kandidaten aus dem global_pool (Unzugeordnete Kanten)
|
|
||||||
for ch_obj in chunks:
|
|
||||||
filtered_pool = []
|
|
||||||
for cand in getattr(ch_obj, "candidate_pool", []):
|
|
||||||
# Nur 'global_pool' erfordert LLM-Validierung.
|
|
||||||
# 'explicit' und 'inherited' werden direkt akzeptiert.
|
|
||||||
if cand.get("provenance") == "global_pool" and enable_smart_edges:
|
|
||||||
if await self._validate_candidate(ch_obj.text, cand):
|
|
||||||
filtered_pool.append(cand)
|
|
||||||
else:
|
|
||||||
filtered_pool.append(cand)
|
|
||||||
ch_obj.candidate_pool = filtered_pool
|
|
||||||
|
|
||||||
chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks, note_text=body_text)
|
|
||||||
|
|
||||||
# Embeddings generieren
|
|
||||||
vecs = []
|
|
||||||
if chunk_pls:
|
|
||||||
texts = [c.get("window") or c.get("text") or "" for c in chunk_pls]
|
|
||||||
vecs = await self.embedder.embed_documents(texts)
|
|
||||||
|
|
||||||
# Kanten finalisieren via derive_edges Aggregator (WP-15b kompatibel)
|
|
||||||
# Nutzt das Provenance-Ranking (v2.1.0).
|
|
||||||
edges = build_edges_for_note(
|
|
||||||
note_id,
|
|
||||||
chunk_pls,
|
|
||||||
note_level_references=note_pl.get("references", []),
|
|
||||||
include_note_scope_refs=note_scope_refs
|
|
||||||
)
|
|
||||||
|
|
||||||
# Alias-Auflösung & Registry Enforcement
|
|
||||||
context = {"file": file_path, "note_id": note_id}
|
|
||||||
for e in edges:
|
|
||||||
e["kind"] = edge_registry.resolve(
|
|
||||||
edge_type=e.get("kind", "related_to"),
|
|
||||||
provenance=e.get("provenance", "explicit"),
|
|
||||||
context={**context, "line": e.get("line", "system")}
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Processing failed for {file_path}: {e}", exc_info=True)
|
|
||||||
return {**result, "error": f"Processing failed: {str(e)}"}
|
|
||||||
|
|
||||||
# 5. DB Upsert
|
|
||||||
try:
|
|
||||||
if purge_before and old_payload: self._purge_artifacts(note_id)
|
|
||||||
|
|
||||||
n_name, n_pts = points_for_note(self.prefix, note_pl, None, self.dim)
|
|
||||||
upsert_batch(self.client, n_name, n_pts)
|
|
||||||
|
|
||||||
if chunk_pls and vecs:
|
|
||||||
# v2.11.14 Points-Extraction Logic
|
|
||||||
c_pts = points_for_chunks(self.prefix, chunk_pls, vecs)[1]
|
|
||||||
upsert_batch(self.client, f"{self.prefix}_chunks", c_pts)
|
|
||||||
|
|
||||||
if edges:
|
|
||||||
# v2.11.14 Points-Extraction Logic
|
|
||||||
e_pts = points_for_edges(self.prefix, edges)[1]
|
|
||||||
upsert_batch(self.client, f"{self.prefix}_edges", e_pts)
|
|
||||||
|
|
||||||
return {"path": file_path, "status": "success", "changed": True, "note_id": note_id, "chunks_count": len(chunk_pls), "edges_count": len(edges)}
|
|
||||||
except Exception as e:
|
|
||||||
return {**result, "error": f"DB Upsert failed: {e}"}
|
|
||||||
|
|
||||||
def _fetch_note_payload(self, note_id: str) -> Optional[dict]:
|
|
||||||
"""Holt die Metadaten einer Note aus Qdrant."""
|
|
||||||
from qdrant_client.http import models as rest
|
|
||||||
try:
|
|
||||||
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
|
|
||||||
pts, _ = self.client.scroll(collection_name=f"{self.prefix}_notes", scroll_filter=f, limit=1, with_payload=True)
|
|
||||||
return pts[0].payload if pts else None
|
|
||||||
except: return None
|
|
||||||
|
|
||||||
def _artifacts_missing(self, note_id: str) -> Tuple[bool, bool]:
|
|
||||||
"""Prüft Qdrant aktiv auf vorhandene Chunks und Edges."""
|
|
||||||
from qdrant_client.http import models as rest
|
|
||||||
try:
|
|
||||||
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
|
|
||||||
c_pts, _ = self.client.scroll(collection_name=f"{self.prefix}_chunks", scroll_filter=f, limit=1)
|
|
||||||
e_pts, _ = self.client.scroll(collection_name=f"{self.prefix}_edges", scroll_filter=f, limit=1)
|
|
||||||
return (not bool(c_pts)), (not bool(e_pts))
|
|
||||||
except: return True, True
|
|
||||||
|
|
||||||
def _purge_artifacts(self, note_id: str):
|
|
||||||
"""Löscht verwaiste Chunks/Edges vor einem Re-Import."""
|
|
||||||
from qdrant_client.http import models as rest
|
|
||||||
f = rest.Filter(must=[rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))])
|
|
||||||
for suffix in ["chunks", "edges"]:
|
|
||||||
try: self.client.delete(collection_name=f"{self.prefix}_{suffix}", points_selector=rest.FilterSelector(filter=f))
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
async def create_from_text(self, markdown_content: str, filename: str, vault_root: str, folder: str = "00_Inbox") -> Dict[str, Any]:
|
|
||||||
"""Hilfsmethode zur Erstellung einer Note aus einem Textstream."""
|
|
||||||
target_dir = os.path.join(vault_root, folder)
|
|
||||||
os.makedirs(target_dir, exist_ok=True)
|
|
||||||
file_path = os.path.join(target_dir, filename)
|
|
||||||
with open(file_path, "w", encoding="utf-8") as f:
|
|
||||||
f.write(markdown_content)
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
return await self.process_file(file_path=file_path, vault_root=vault_root, apply=True, force_replace=True, purge_before=True)
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/__init__.py
|
||||||
|
DESCRIPTION: Package-Einstiegspunkt für Ingestion. Exportiert den IngestionService.
|
||||||
|
AUDIT v2.13.10: Abschluss der Modularisierung (WP-14).
|
||||||
|
Bricht Zirkelbezüge durch Nutzung der neutralen registry.py auf.
|
||||||
|
VERSION: 2.13.10
|
||||||
|
"""
|
||||||
|
# Der IngestionService ist der primäre Orchestrator für den Datenimport
|
||||||
|
from .ingestion_processor import IngestionService
|
||||||
|
|
||||||
|
# Hilfswerkzeuge für JSON-Verarbeitung und Konfigurations-Management
|
||||||
|
# load_type_registry wird hier re-exportiert, um die Abwärtskompatibilität zu wahren,
|
||||||
|
# obwohl die Implementierung nun in app.core.registry liegt.
|
||||||
|
from .ingestion_utils import (
|
||||||
|
extract_json_from_response,
|
||||||
|
load_type_registry,
|
||||||
|
resolve_note_type
|
||||||
|
)
|
||||||
|
|
||||||
|
# Öffentliche API des Pakets
|
||||||
|
__all__ = [
|
||||||
|
"IngestionService",
|
||||||
|
"extract_json_from_response",
|
||||||
|
"load_type_registry",
|
||||||
|
"resolve_note_type"
|
||||||
|
]
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_chunk_payload.py
|
||||||
|
DESCRIPTION: Baut das JSON-Objekt für 'mindnet_chunks'.
|
||||||
|
Fix v2.4.3: Integration der zentralen Registry (WP-14) für konsistente Defaults.
|
||||||
|
WP-24c v4.3.0: candidate_pool wird explizit übernommen für Chunk-Attribution.
|
||||||
|
VERSION: 2.4.4 (WP-24c v4.3.0)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
import logging
|
||||||
|
|
||||||
|
# ENTSCHEIDENDER FIX: Import der neutralen Registry-Logik zur Vermeidung von Circular Imports
|
||||||
|
from app.core.registry import load_type_registry
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Resolution Helpers (Audited)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _as_list(x):
|
||||||
|
"""Sichert die Listen-Integrität für Metadaten wie Tags."""
|
||||||
|
if x is None: return []
|
||||||
|
return x if isinstance(x, list) else [x]
|
||||||
|
|
||||||
|
def _resolve_val(note_type: str, reg: dict, key: str, default: Any) -> Any:
|
||||||
|
"""
|
||||||
|
Hierarchische Suche in der Registry: Type-Spezifisch > Globaler Default.
|
||||||
|
WP-14: Erlaubt dynamische Konfiguration via types.yaml.
|
||||||
|
"""
|
||||||
|
types = reg.get("types", {})
|
||||||
|
if isinstance(types, dict):
|
||||||
|
t_cfg = types.get(note_type, {})
|
||||||
|
if isinstance(t_cfg, dict):
|
||||||
|
# Fallback für Key-Varianten (z.B. chunking_profile vs chunk_profile)
|
||||||
|
val = t_cfg.get(key) or t_cfg.get(key.replace("ing", ""))
|
||||||
|
if val is not None: return val
|
||||||
|
|
||||||
|
defs = reg.get("defaults", {}) or reg.get("global", {})
|
||||||
|
if isinstance(defs, dict):
|
||||||
|
val = defs.get(key) or defs.get(key.replace("ing", ""))
|
||||||
|
if val is not None: return val
|
||||||
|
|
||||||
|
return default
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Haupt-API
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def make_chunk_payloads(note: Dict[str, Any], note_path: str, chunks_from_chunker: List[Any], **kwargs) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Erstellt die Payloads für die Chunks inklusive Audit-Resolution.
|
||||||
|
Nutzt nun die zentrale Registry für alle Fallbacks.
|
||||||
|
"""
|
||||||
|
if isinstance(note, dict) and "frontmatter" in note:
|
||||||
|
fm = note["frontmatter"]
|
||||||
|
else:
|
||||||
|
fm = note or {}
|
||||||
|
|
||||||
|
# WP-14 Fix: Nutzt übergebene Registry oder lädt sie global
|
||||||
|
reg = kwargs.get("types_cfg") or load_type_registry()
|
||||||
|
|
||||||
|
note_type = fm.get("type") or "concept"
|
||||||
|
title = fm.get("title") or fm.get("id") or "Untitled"
|
||||||
|
tags = _as_list(fm.get("tags") or [])
|
||||||
|
|
||||||
|
# Audit: Resolution Hierarchie (Frontmatter > Registry)
|
||||||
|
cp = fm.get("chunking_profile") or fm.get("chunk_profile")
|
||||||
|
if not cp:
|
||||||
|
cp = _resolve_val(note_type, reg, "chunking_profile", "sliding_standard")
|
||||||
|
|
||||||
|
rw = fm.get("retriever_weight")
|
||||||
|
if rw is None:
|
||||||
|
rw = _resolve_val(note_type, reg, "retriever_weight", 1.0)
|
||||||
|
try:
|
||||||
|
rw = float(rw)
|
||||||
|
except:
|
||||||
|
rw = 1.0
|
||||||
|
|
||||||
|
out: List[Dict[str, Any]] = []
|
||||||
|
for idx, ch in enumerate(chunks_from_chunker):
|
||||||
|
is_dict = isinstance(ch, dict)
|
||||||
|
cid = getattr(ch, "id", None) if not is_dict else ch.get("id")
|
||||||
|
nid = getattr(ch, "note_id", None) if not is_dict else ch.get("note_id")
|
||||||
|
index = getattr(ch, "index", idx) if not is_dict else ch.get("index", idx)
|
||||||
|
text = getattr(ch, "text", "") if not is_dict else ch.get("text", "")
|
||||||
|
window = getattr(ch, "window", text) if not is_dict else ch.get("window", text)
|
||||||
|
prev_id = getattr(ch, "neighbors_prev", None) if not is_dict else ch.get("neighbors_prev")
|
||||||
|
next_id = getattr(ch, "neighbors_next", None) if not is_dict else ch.get("neighbors_next")
|
||||||
|
section = getattr(ch, "section_title", "") if not is_dict else ch.get("section", "")
|
||||||
|
# WP-24c v4.3.0: candidate_pool muss erhalten bleiben für Chunk-Attribution
|
||||||
|
candidate_pool = getattr(ch, "candidate_pool", []) if not is_dict else ch.get("candidate_pool", [])
|
||||||
|
|
||||||
|
pl: Dict[str, Any] = {
|
||||||
|
"note_id": nid or fm.get("id"),
|
||||||
|
"chunk_id": cid,
|
||||||
|
"title": title,
|
||||||
|
"index": int(index),
|
||||||
|
"ord": int(index) + 1,
|
||||||
|
"type": note_type,
|
||||||
|
"tags": tags,
|
||||||
|
"text": text,
|
||||||
|
"window": window,
|
||||||
|
"neighbors_prev": _as_list(prev_id),
|
||||||
|
"neighbors_next": _as_list(next_id),
|
||||||
|
"section": section,
|
||||||
|
"path": note_path,
|
||||||
|
"source_path": kwargs.get("file_path") or note_path,
|
||||||
|
"retriever_weight": rw,
|
||||||
|
"chunk_profile": cp,
|
||||||
|
"candidate_pool": candidate_pool # WP-24c v4.3.0: Kritisch für Chunk-Attribution
|
||||||
|
}
|
||||||
|
|
||||||
|
# Audit: Cleanup Pop (Vermeidung von redundanten Alias-Feldern)
|
||||||
|
for alias in ("chunk_num", "Chunk_Number"):
|
||||||
|
pl.pop(alias, None)
|
||||||
|
|
||||||
|
# WP-24c v4.4.0-DEBUG: Schnittstelle 2 - Transfer
|
||||||
|
# Log-Output unmittelbar bevor das Dictionary zurückgegeben wird
|
||||||
|
pool_size = len(candidate_pool) if candidate_pool else 0
|
||||||
|
pool_content = candidate_pool if candidate_pool else []
|
||||||
|
explicit_callout_in_pool = [c for c in pool_content if isinstance(c, dict) and c.get("provenance") == "explicit:callout"]
|
||||||
|
logger.debug(f"DEBUG-TRACER [Payload]: Chunk ID: {cid}, Index: {index}, Pool-Size: {pool_size}, Pool-Inhalt: {pool_content}, Explicit-Callout-Count: {len(explicit_callout_in_pool)}, Has_Candidate_Pool_Key: {'candidate_pool' in pl}")
|
||||||
|
if explicit_callout_in_pool:
|
||||||
|
for ec in explicit_callout_in_pool:
|
||||||
|
logger.debug(f"DEBUG-TRACER [Payload]: Explicit-Callout Detail - Kind: {ec.get('kind')}, To: {ec.get('to')}, Provenance: {ec.get('provenance')}")
|
||||||
|
|
||||||
|
out.append(pl)
|
||||||
|
|
||||||
|
return out
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_db.py
|
||||||
|
DESCRIPTION: Datenbank-Schnittstelle für Note-Metadaten und Artefakt-Prüfung.
|
||||||
|
WP-14: Umstellung auf zentrale database-Infrastruktur.
|
||||||
|
WP-24c: Integration der Authority-Prüfung für Point-IDs.
|
||||||
|
Ermöglicht dem Prozessor die Unterscheidung zwischen
|
||||||
|
manueller Nutzer-Autorität und virtuellen Symmetrien.
|
||||||
|
VERSION: 2.2.0 (WP-24c: Authority Lookup Integration)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
from typing import Optional, Tuple, List
|
||||||
|
from qdrant_client import QdrantClient
|
||||||
|
from qdrant_client.http import models as rest
|
||||||
|
|
||||||
|
# Import der modularisierten Namen-Logik zur Sicherstellung der Konsistenz
|
||||||
|
from app.core.database import collection_names
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def fetch_note_payload(client: QdrantClient, prefix: str, note_id: str) -> Optional[dict]:
|
||||||
|
"""
|
||||||
|
Holt die Metadaten einer Note aus Qdrant via Scroll-API.
|
||||||
|
Wird primär für die Change-Detection (Hash-Vergleich) genutzt.
|
||||||
|
"""
|
||||||
|
notes_col, _, _ = collection_names(prefix)
|
||||||
|
try:
|
||||||
|
f = rest.Filter(must=[
|
||||||
|
rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))
|
||||||
|
])
|
||||||
|
pts, _ = client.scroll(
|
||||||
|
collection_name=notes_col,
|
||||||
|
scroll_filter=f,
|
||||||
|
limit=1,
|
||||||
|
with_payload=True
|
||||||
|
)
|
||||||
|
return pts[0].payload if pts else None
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Note {note_id} not found or error during fetch: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def artifacts_missing(client: QdrantClient, prefix: str, note_id: str) -> Tuple[bool, bool]:
|
||||||
|
"""
|
||||||
|
Prüft Qdrant aktiv auf vorhandene Chunks und Edges für eine Note.
|
||||||
|
Gibt (chunks_missing, edges_missing) als Boolean-Tupel zurück.
|
||||||
|
"""
|
||||||
|
_, chunks_col, edges_col = collection_names(prefix)
|
||||||
|
try:
|
||||||
|
# Filter für die note_id Suche
|
||||||
|
f = rest.Filter(must=[
|
||||||
|
rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))
|
||||||
|
])
|
||||||
|
c_pts, _ = client.scroll(collection_name=chunks_col, scroll_filter=f, limit=1)
|
||||||
|
e_pts, _ = client.scroll(collection_name=edges_col, scroll_filter=f, limit=1)
|
||||||
|
return (not bool(c_pts)), (not bool(e_pts))
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error checking artifacts for {note_id}: {e}")
|
||||||
|
return True, True
|
||||||
|
|
||||||
|
def is_explicit_edge_present(client: QdrantClient, prefix: str, edge_id: str) -> bool:
|
||||||
|
"""
|
||||||
|
WP-24c: Prüft via Point-ID, ob bereits eine explizite Kante existiert.
|
||||||
|
Wird vom IngestionProcessor in Phase 2 genutzt, um das Überschreiben
|
||||||
|
von manuellem Wissen durch virtuelle Symmetrie-Kanten zu verhindern.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
edge_id: Die deterministisch berechnete UUID der Kante.
|
||||||
|
Returns:
|
||||||
|
True, wenn eine physische Kante (virtual=False) existiert.
|
||||||
|
"""
|
||||||
|
if not edge_id:
|
||||||
|
return False
|
||||||
|
|
||||||
|
_, _, edges_col = collection_names(prefix)
|
||||||
|
try:
|
||||||
|
# retrieve ist die effizienteste Methode für den Zugriff via ID
|
||||||
|
res = client.retrieve(
|
||||||
|
collection_name=edges_col,
|
||||||
|
ids=[edge_id],
|
||||||
|
with_payload=True
|
||||||
|
)
|
||||||
|
|
||||||
|
if res and len(res) > 0:
|
||||||
|
# Wir prüfen das 'virtual' Flag im Payload
|
||||||
|
is_virtual = res[0].payload.get("virtual", False)
|
||||||
|
if not is_virtual:
|
||||||
|
return True # Es ist eine explizite Nutzer-Kante
|
||||||
|
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"Authority check failed for ID {edge_id}: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def purge_artifacts(client: QdrantClient, prefix: str, note_id: str):
|
||||||
|
"""
|
||||||
|
Löscht verwaiste Chunks und Edges einer Note vor einem Re-Import.
|
||||||
|
Stellt sicher, dass keine Duplikate bei Inhaltsänderungen entstehen.
|
||||||
|
"""
|
||||||
|
_, chunks_col, edges_col = collection_names(prefix)
|
||||||
|
try:
|
||||||
|
f = rest.Filter(must=[
|
||||||
|
rest.FieldCondition(key="note_id", match=rest.MatchValue(value=note_id))
|
||||||
|
])
|
||||||
|
# Chunks löschen
|
||||||
|
client.delete(
|
||||||
|
collection_name=chunks_col,
|
||||||
|
points_selector=rest.FilterSelector(filter=f)
|
||||||
|
)
|
||||||
|
# Edges löschen
|
||||||
|
client.delete(
|
||||||
|
collection_name=edges_col,
|
||||||
|
points_selector=rest.FilterSelector(filter=f)
|
||||||
|
)
|
||||||
|
logger.info(f"🧹 [PURGE] Local artifacts for '{note_id}' cleared.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ [PURGE ERROR] Failed to clear artifacts for {note_id}: {e}")
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_note_payload.py
|
||||||
|
DESCRIPTION: Baut das JSON-Objekt für mindnet_notes.
|
||||||
|
WP-14: Integration der zentralen Registry.
|
||||||
|
WP-24c: Dynamische Ermittlung von edge_defaults aus dem Graph-Schema.
|
||||||
|
VERSION: 2.5.0 (WP-24c: Dynamic Topology Integration)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import Any, Dict, Tuple, Optional
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import pathlib
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
# Import der zentralen Registry-Logik
|
||||||
|
from app.core.registry import load_type_registry
|
||||||
|
# WP-24c: Zugriff auf das dynamische Graph-Schema
|
||||||
|
from app.services.edge_registry import registry as edge_registry
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Helper
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _as_dict(x) -> Dict[str, Any]:
|
||||||
|
"""Versucht, ein Objekt in ein Dict zu überführen."""
|
||||||
|
if isinstance(x, dict): return dict(x)
|
||||||
|
out: Dict[str, Any] = {}
|
||||||
|
for attr in ("frontmatter", "body", "id", "note_id", "title", "path", "tags", "type", "created", "modified", "date"):
|
||||||
|
if hasattr(x, attr):
|
||||||
|
val = getattr(x, attr)
|
||||||
|
if val is not None: out[attr] = val
|
||||||
|
if not out: out["raw"] = str(x)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def _ensure_list(x) -> list:
|
||||||
|
"""Sichert String-Listen Integrität."""
|
||||||
|
if x is None: return []
|
||||||
|
if isinstance(x, list): return [str(i) for i in x]
|
||||||
|
if isinstance(x, (set, tuple)): return [str(i) for i in x]
|
||||||
|
return [str(x)]
|
||||||
|
|
||||||
|
def _compute_hash(content: str) -> str:
|
||||||
|
"""SHA-256 Hash-Berechnung."""
|
||||||
|
if not content: return ""
|
||||||
|
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
def _get_hash_source_content(n: Dict[str, Any], mode: str) -> str:
|
||||||
|
"""
|
||||||
|
Generiert den Hash-Input-String basierend auf Body oder Metadaten.
|
||||||
|
Inkludiert alle entscheidungsrelevanten Profil-Parameter.
|
||||||
|
"""
|
||||||
|
body = str(n.get("body") or "").strip()
|
||||||
|
if mode == "body": return body
|
||||||
|
if mode == "full":
|
||||||
|
fm = n.get("frontmatter") or {}
|
||||||
|
meta_parts = []
|
||||||
|
# Alle Felder, die das Chunking oder Retrieval beeinflussen
|
||||||
|
keys = [
|
||||||
|
"title", "type", "status", "tags",
|
||||||
|
"chunking_profile", "chunk_profile",
|
||||||
|
"retriever_weight", "split_level", "strict_heading_split"
|
||||||
|
]
|
||||||
|
for k in sorted(keys):
|
||||||
|
val = fm.get(k)
|
||||||
|
if val is not None: meta_parts.append(f"{k}:{val}")
|
||||||
|
return f"{'|'.join(meta_parts)}||{body}"
|
||||||
|
return body
|
||||||
|
|
||||||
|
def _cfg_for_type(note_type: str, reg: dict) -> dict:
|
||||||
|
"""Extrahiert Typ-spezifische Config aus der Registry."""
|
||||||
|
if not isinstance(reg, dict): return {}
|
||||||
|
types = reg.get("types") if isinstance(reg.get("types"), dict) else reg
|
||||||
|
return types.get(note_type, {}) if isinstance(types, dict) else {}
|
||||||
|
|
||||||
|
def _cfg_defaults(reg: dict) -> dict:
|
||||||
|
"""Extrahiert globale Default-Werte aus der Registry."""
|
||||||
|
if not isinstance(reg, dict): return {}
|
||||||
|
for key in ("defaults", "default", "global"):
|
||||||
|
v = reg.get(key)
|
||||||
|
if isinstance(v, dict): return v
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Haupt-API
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Baut das Note-Payload inklusive Multi-Hash und Audit-Validierung.
|
||||||
|
WP-24c: Nutzt die EdgeRegistry zur dynamischen Auflösung von Typical Edges.
|
||||||
|
"""
|
||||||
|
n = _as_dict(note)
|
||||||
|
|
||||||
|
# Registry & Context Settings
|
||||||
|
reg = kwargs.get("types_cfg") or load_type_registry()
|
||||||
|
hash_source = kwargs.get("hash_source", "parsed")
|
||||||
|
hash_normalize = kwargs.get("hash_normalize", "canonical")
|
||||||
|
|
||||||
|
fm = n.get("frontmatter") or {}
|
||||||
|
note_type = str(fm.get("type") or n.get("type") or "concept")
|
||||||
|
|
||||||
|
cfg_type = _cfg_for_type(note_type, reg)
|
||||||
|
cfg_def = _cfg_defaults(reg)
|
||||||
|
ingest_cfg = reg.get("ingestion_settings", {})
|
||||||
|
|
||||||
|
# --- retriever_weight Audit ---
|
||||||
|
default_rw = float(os.environ.get("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0))
|
||||||
|
retriever_weight = fm.get("retriever_weight")
|
||||||
|
if retriever_weight is None:
|
||||||
|
retriever_weight = cfg_type.get("retriever_weight", cfg_def.get("retriever_weight", default_rw))
|
||||||
|
try:
|
||||||
|
retriever_weight = float(retriever_weight)
|
||||||
|
except:
|
||||||
|
retriever_weight = default_rw
|
||||||
|
|
||||||
|
# --- chunk_profile Audit ---
|
||||||
|
chunk_profile = fm.get("chunking_profile") or fm.get("chunk_profile")
|
||||||
|
if chunk_profile is None:
|
||||||
|
chunk_profile = cfg_type.get("chunking_profile") or cfg_type.get("chunk_profile")
|
||||||
|
if chunk_profile is None:
|
||||||
|
chunk_profile = ingest_cfg.get("default_chunk_profile", cfg_def.get("chunking_profile", "sliding_standard"))
|
||||||
|
|
||||||
|
# --- WP-24c: edge_defaults Dynamisierung ---
|
||||||
|
# 1. Priorität: Manuelle Definition im Frontmatter
|
||||||
|
edge_defaults = fm.get("edge_defaults")
|
||||||
|
|
||||||
|
# 2. Priorität: Dynamische Abfrage der 'Typical Edges' aus dem Graph-Schema
|
||||||
|
if edge_defaults is None:
|
||||||
|
topology = edge_registry.get_topology_info(note_type, "any")
|
||||||
|
edge_defaults = topology.get("typical", [])
|
||||||
|
|
||||||
|
# 3. Fallback: Leere Liste, falls kein Schema-Eintrag existiert
|
||||||
|
edge_defaults = _ensure_list(edge_defaults)
|
||||||
|
|
||||||
|
# --- Basis-Metadaten ---
|
||||||
|
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 kwargs.get("file_path") or ""
|
||||||
|
if isinstance(path, pathlib.Path): path = str(path)
|
||||||
|
|
||||||
|
payload: Dict[str, Any] = {
|
||||||
|
"note_id": note_id,
|
||||||
|
"title": title,
|
||||||
|
"type": note_type,
|
||||||
|
"path": path,
|
||||||
|
"retriever_weight": retriever_weight,
|
||||||
|
"chunk_profile": chunk_profile,
|
||||||
|
"edge_defaults": edge_defaults,
|
||||||
|
"hashes": {}
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- MULTI-HASH ---
|
||||||
|
# Generiert Hashes für Change Detection (WP-15b)
|
||||||
|
for mode in ["body", "full"]:
|
||||||
|
content = _get_hash_source_content(n, mode)
|
||||||
|
payload["hashes"][f"{mode}:{hash_source}:{hash_normalize}"] = _compute_hash(content)
|
||||||
|
|
||||||
|
# Metadaten Anreicherung (Tags, Aliases, Zeitstempel)
|
||||||
|
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
||||||
|
if tags: payload["tags"] = _ensure_list(tags)
|
||||||
|
|
||||||
|
aliases = fm.get("aliases")
|
||||||
|
if aliases: payload["aliases"] = _ensure_list(aliases)
|
||||||
|
|
||||||
|
for k in ("created", "modified", "date"):
|
||||||
|
v = fm.get(k) or n.get(k)
|
||||||
|
if v: payload[k] = str(v)
|
||||||
|
|
||||||
|
if n.get("body"):
|
||||||
|
payload["fulltext"] = str(n["body"])
|
||||||
|
|
||||||
|
# Final JSON Validation Audit
|
||||||
|
json.loads(json.dumps(payload, ensure_ascii=False))
|
||||||
|
|
||||||
|
return payload
|
||||||
@@ -0,0 +1,652 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_processor.py
|
||||||
|
DESCRIPTION: Der zentrale IngestionService (Orchestrator).
|
||||||
|
WP-25a: Integration der Mixture of Experts (MoE) Architektur.
|
||||||
|
WP-15b: Two-Pass Workflow mit globalem Kontext-Cache.
|
||||||
|
WP-20/22: Cloud-Resilienz und Content-Lifecycle integriert.
|
||||||
|
AUDIT v4.2.4:
|
||||||
|
- GOLD-STANDARD v4.2.4: Hash-basierte Change-Detection (MINDNET_CHANGE_DETECTION_MODE).
|
||||||
|
- Wiederherstellung des iterativen Abgleichs basierend auf Inhalts-Hashes.
|
||||||
|
- Phase 2 verwendet exakt dieselbe ID-Generierung wie Phase 1 (inkl. target_section).
|
||||||
|
- Authority-Check in Phase 2 prüft mit konsistenter ID-Generierung.
|
||||||
|
- Eliminiert Duplikate durch inkonsistente ID-Generierung (Steinzeitaxt-Problem).
|
||||||
|
VERSION: 4.2.4 (WP-24c: Hash-Integrität)
|
||||||
|
STATUS: Active
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from typing import Dict, List, Optional, Tuple, Any
|
||||||
|
|
||||||
|
# Core Module Imports
|
||||||
|
from app.core.parser import (
|
||||||
|
read_markdown, pre_scan_markdown, normalize_frontmatter,
|
||||||
|
validate_required_frontmatter, NoteContext
|
||||||
|
)
|
||||||
|
from app.core.chunking import assemble_chunks
|
||||||
|
# WP-24c: Import der zentralen Identitäts-Logik
|
||||||
|
from app.core.graph.graph_utils import _mk_edge_id
|
||||||
|
|
||||||
|
# Datenbank-Ebene (Modularisierte database-Infrastruktur)
|
||||||
|
from app.core.database.qdrant import QdrantConfig, get_client, ensure_collections, ensure_payload_indexes
|
||||||
|
from app.core.database.qdrant_points import points_for_chunks, points_for_note, points_for_edges, upsert_batch
|
||||||
|
from qdrant_client.http import models as rest
|
||||||
|
|
||||||
|
# Services
|
||||||
|
from app.services.embeddings_client import EmbeddingsClient
|
||||||
|
from app.services.edge_registry import registry as edge_registry
|
||||||
|
from app.services.llm_service import LLMService
|
||||||
|
|
||||||
|
# Package-Interne Imports (Refactoring WP-14)
|
||||||
|
from .ingestion_utils import load_type_registry, resolve_note_type, get_chunk_config_by_profile
|
||||||
|
from .ingestion_db import fetch_note_payload, artifacts_missing, purge_artifacts, is_explicit_edge_present
|
||||||
|
from .ingestion_validation import validate_edge_candidate
|
||||||
|
from .ingestion_note_payload import make_note_payload
|
||||||
|
from .ingestion_chunk_payload import make_chunk_payloads
|
||||||
|
|
||||||
|
# Fallback für Edges (Struktur-Verknüpfung)
|
||||||
|
try:
|
||||||
|
from app.core.graph.graph_derive_edges import build_edges_for_note
|
||||||
|
except ImportError:
|
||||||
|
def build_edges_for_note(*args, **kwargs): return []
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class IngestionService:
|
||||||
|
def __init__(self, collection_prefix: str = None):
|
||||||
|
"""Initialisiert den Service und nutzt die neue database-Infrastruktur."""
|
||||||
|
from app.config import get_settings
|
||||||
|
self.settings = get_settings()
|
||||||
|
|
||||||
|
# --- LOGGING CLEANUP ---
|
||||||
|
# Unterdrückt Bibliotheks-Lärm, erhält aber inhaltliche Service-Logs
|
||||||
|
for lib in ["httpx", "httpcore", "qdrant_client", "urllib3", "openai"]:
|
||||||
|
logging.getLogger(lib).setLevel(logging.WARNING)
|
||||||
|
|
||||||
|
self.prefix = collection_prefix or self.settings.COLLECTION_PREFIX
|
||||||
|
self.cfg = QdrantConfig.from_env()
|
||||||
|
self.cfg.prefix = self.prefix
|
||||||
|
self.client = get_client(self.cfg)
|
||||||
|
|
||||||
|
self.registry = load_type_registry()
|
||||||
|
self.embedder = EmbeddingsClient()
|
||||||
|
self.llm = LLMService()
|
||||||
|
|
||||||
|
# WP-25a: Auflösung der Dimension über das Embedding-Profil (MoE)
|
||||||
|
embed_cfg = self.llm.profiles.get("embedding_expert", {})
|
||||||
|
self.dim = embed_cfg.get("dimensions") or self.settings.VECTOR_SIZE
|
||||||
|
|
||||||
|
self.active_hash_mode = self.settings.CHANGE_DETECTION_MODE
|
||||||
|
|
||||||
|
# WP-15b: Kontext-Gedächtnis für ID-Auflösung (Globaler Cache)
|
||||||
|
self.batch_cache: Dict[str, NoteContext] = {}
|
||||||
|
|
||||||
|
# WP-24c: Puffer für Phase 2 (Symmetrie-Injektion am Ende des gesamten Imports)
|
||||||
|
self.symmetry_buffer: List[Dict[str, Any]] = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
ensure_collections(self.client, self.prefix, self.dim)
|
||||||
|
ensure_payload_indexes(self.client, self.prefix)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"DB initialization warning: {e}")
|
||||||
|
|
||||||
|
def _log_id_collision(
|
||||||
|
self,
|
||||||
|
note_id: str,
|
||||||
|
existing_path: str,
|
||||||
|
conflicting_path: str,
|
||||||
|
action: str = "ERROR"
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
WP-24c v4.5.10: Loggt ID-Kollisionen in eine dedizierte Log-Datei.
|
||||||
|
|
||||||
|
Schreibt alle ID-Kollisionen in logs/id_collisions.log für manuelle Analyse.
|
||||||
|
Format: JSONL (eine Kollision pro Zeile) mit allen relevanten Metadaten.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
note_id: Die doppelte note_id
|
||||||
|
existing_path: Pfad der bereits vorhandenen Datei
|
||||||
|
conflicting_path: Pfad der kollidierenden Datei
|
||||||
|
action: Gewählte Aktion (z.B. "ERROR", "SKIPPED")
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# Erstelle Log-Verzeichnis falls nicht vorhanden
|
||||||
|
log_dir = "logs"
|
||||||
|
if not os.path.exists(log_dir):
|
||||||
|
os.makedirs(log_dir)
|
||||||
|
|
||||||
|
log_file = os.path.join(log_dir, "id_collisions.log")
|
||||||
|
|
||||||
|
# Erstelle Log-Eintrag mit allen relevanten Informationen
|
||||||
|
log_entry = {
|
||||||
|
"timestamp": datetime.now().isoformat(),
|
||||||
|
"note_id": note_id,
|
||||||
|
"existing_file": {
|
||||||
|
"path": existing_path,
|
||||||
|
"filename": os.path.basename(existing_path) if existing_path else None
|
||||||
|
},
|
||||||
|
"conflicting_file": {
|
||||||
|
"path": conflicting_path,
|
||||||
|
"filename": os.path.basename(conflicting_path) if conflicting_path else None
|
||||||
|
},
|
||||||
|
"action": action,
|
||||||
|
"collection_prefix": self.prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
# Schreibe als JSONL (eine Zeile pro Eintrag)
|
||||||
|
try:
|
||||||
|
with open(log_file, "a", encoding="utf-8") as f:
|
||||||
|
f.write(json.dumps(log_entry, ensure_ascii=False) + "\n")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Konnte ID-Kollision nicht in Log-Datei schreiben: {e}")
|
||||||
|
|
||||||
|
def _persist_rejected_edges(self, note_id: str, rejected_edges: List[Dict[str, Any]]) -> None:
|
||||||
|
"""
|
||||||
|
WP-24c v4.5.9: Persistiert abgelehnte Kanten für Audit-Zwecke.
|
||||||
|
|
||||||
|
Schreibt rejected_edges in eine JSONL-Datei im _system Ordner oder logs/rejected_edges.log.
|
||||||
|
Dies ermöglicht die Analyse der Ablehnungsgründe und Verbesserung der Validierungs-Logik.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
note_id: ID der Note, zu der die abgelehnten Kanten gehören
|
||||||
|
rejected_edges: Liste von abgelehnten Edge-Dicts
|
||||||
|
"""
|
||||||
|
if not rejected_edges:
|
||||||
|
return
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
# WP-24c v4.5.9: Erstelle Log-Verzeichnis falls nicht vorhanden
|
||||||
|
log_dir = "logs"
|
||||||
|
if not os.path.exists(log_dir):
|
||||||
|
os.makedirs(log_dir)
|
||||||
|
|
||||||
|
log_file = os.path.join(log_dir, "rejected_edges.log")
|
||||||
|
|
||||||
|
# WP-24c v4.5.9: Schreibe als JSONL (eine Kante pro Zeile)
|
||||||
|
try:
|
||||||
|
with open(log_file, "a", encoding="utf-8") as f:
|
||||||
|
for edge in rejected_edges:
|
||||||
|
log_entry = {
|
||||||
|
"timestamp": datetime.now().isoformat(),
|
||||||
|
"note_id": note_id,
|
||||||
|
"edge": {
|
||||||
|
"kind": edge.get("kind", "unknown"),
|
||||||
|
"source_id": edge.get("source_id", "unknown"),
|
||||||
|
"target_id": edge.get("target_id") or edge.get("to", "unknown"),
|
||||||
|
"scope": edge.get("scope", "unknown"),
|
||||||
|
"provenance": edge.get("provenance", "unknown"),
|
||||||
|
"rule_id": edge.get("rule_id", "unknown"),
|
||||||
|
"confidence": edge.get("confidence", 0.0),
|
||||||
|
"target_section": edge.get("target_section")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.write(json.dumps(log_entry, ensure_ascii=False) + "\n")
|
||||||
|
|
||||||
|
logger.debug(f"📝 [AUDIT] {len(rejected_edges)} abgelehnte Kanten für '{note_id}' in {log_file} gespeichert")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ [AUDIT] Fehler beim Speichern der rejected_edges: {e}")
|
||||||
|
|
||||||
|
def _is_valid_id(self, text: Optional[str]) -> bool:
|
||||||
|
"""WP-24c: Prüft IDs auf fachliche Validität (Ghost-ID Schutz)."""
|
||||||
|
if not text or not isinstance(text, str) or len(text.strip()) < 2:
|
||||||
|
return False
|
||||||
|
blacklisted = {"none", "unknown", "insight", "source", "task", "project", "person", "concept"}
|
||||||
|
if text.lower().strip() in blacklisted:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def run_batch(self, file_paths: List[str], vault_root: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
WP-15b: Phase 1 des Two-Pass Workflows.
|
||||||
|
Verarbeitet Batches und schreibt NUR Nutzer-Autorität (explizite Kanten).
|
||||||
|
"""
|
||||||
|
self.batch_cache.clear()
|
||||||
|
logger.info(f"--- 🔍 START BATCH PHASE 1 ({len(file_paths)} Dateien) ---")
|
||||||
|
|
||||||
|
# 1. Schritt: Pre-Scan (Context-Cache füllen)
|
||||||
|
for path in file_paths:
|
||||||
|
try:
|
||||||
|
ctx = pre_scan_markdown(path, registry=self.registry)
|
||||||
|
if ctx:
|
||||||
|
self.batch_cache[ctx.note_id] = ctx
|
||||||
|
self.batch_cache[ctx.title] = ctx
|
||||||
|
self.batch_cache[os.path.splitext(os.path.basename(path))[0]] = ctx
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f" ⚠️ Pre-scan fehlgeschlagen für {path}: {e}")
|
||||||
|
|
||||||
|
# 2. Schritt: Batch Processing (Authority Only)
|
||||||
|
processed_count = 0
|
||||||
|
success_count = 0
|
||||||
|
for p in file_paths:
|
||||||
|
processed_count += 1
|
||||||
|
res = await self.process_file(p, vault_root, apply=True, purge_before=True)
|
||||||
|
if res.get("status") == "success":
|
||||||
|
success_count += 1
|
||||||
|
|
||||||
|
logger.info(f"--- ✅ Batch Phase 1 abgeschlossen ({success_count}/{processed_count}) ---")
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
"processed": processed_count,
|
||||||
|
"success": success_count,
|
||||||
|
"buffered_symmetries": len(self.symmetry_buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
async def commit_vault_symmetries(self) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
WP-24c: Führt PHASE 2 (Globale Symmetrie-Injektion) aus.
|
||||||
|
Wird am Ende des gesamten Imports aufgerufen.
|
||||||
|
"""
|
||||||
|
if not self.symmetry_buffer:
|
||||||
|
return {"status": "skipped", "reason": "buffer_empty"}
|
||||||
|
|
||||||
|
logger.info(f"🔄 PHASE 2: Validiere {len(self.symmetry_buffer)} Symmetrien gegen Live-DB...")
|
||||||
|
final_virtuals = []
|
||||||
|
for v_edge in self.symmetry_buffer:
|
||||||
|
# WP-24c v4.1.0: Korrekte Extraktion der Identitäts-Parameter
|
||||||
|
src = v_edge.get("source_id") or v_edge.get("note_id") # source_id hat Priorität
|
||||||
|
tgt = v_edge.get("target_id")
|
||||||
|
kind = v_edge.get("kind")
|
||||||
|
scope = v_edge.get("scope", "note")
|
||||||
|
target_section = v_edge.get("target_section") # WP-24c v4.1.0: target_section berücksichtigen
|
||||||
|
|
||||||
|
if not all([src, tgt, kind]):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Nutzung der zentralisierten ID-Logik aus graph_utils
|
||||||
|
# GOLD-STANDARD v4.1.0: ID-Generierung muss absolut synchron zu Phase 1 sein
|
||||||
|
# - Wenn target_section vorhanden, muss es in die ID einfließen
|
||||||
|
# - Dies stellt sicher, dass der Authority-Check korrekt funktioniert
|
||||||
|
try:
|
||||||
|
v_id = _mk_edge_id(kind, src, tgt, scope, target_section=target_section)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# AUTHORITY-CHECK: Nur schreiben, wenn keine manuelle Kante existiert
|
||||||
|
# Prüft mit exakt derselben ID, die in Phase 1 verwendet wurde (inkl. target_section)
|
||||||
|
if not is_explicit_edge_present(self.client, self.prefix, v_id):
|
||||||
|
final_virtuals.append(v_edge)
|
||||||
|
section_info = f" (section: {target_section})" if target_section else ""
|
||||||
|
logger.info(f" 🔄 [SYMMETRY] Add inverse: {src} --({kind})--> {tgt}{section_info}")
|
||||||
|
else:
|
||||||
|
logger.info(f" 🛡️ [PROTECTED] Manuelle Kante gefunden. Symmetrie für {kind} unterdrückt.")
|
||||||
|
|
||||||
|
if final_virtuals:
|
||||||
|
col, pts = points_for_edges(self.prefix, final_virtuals)
|
||||||
|
upsert_batch(self.client, col, pts, wait=True)
|
||||||
|
|
||||||
|
count = len(final_virtuals)
|
||||||
|
self.symmetry_buffer.clear()
|
||||||
|
return {"status": "success", "added": count}
|
||||||
|
|
||||||
|
async def process_file(self, file_path: str, vault_root: str, **kwargs) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Transformiert eine Markdown-Datei (Phase 1).
|
||||||
|
Schreibt Notes/Chunks/Explicit Edges sofort.
|
||||||
|
"""
|
||||||
|
apply = kwargs.get("apply", False)
|
||||||
|
force_replace = kwargs.get("force_replace", False)
|
||||||
|
purge_before = kwargs.get("purge_before", False)
|
||||||
|
|
||||||
|
result = {"path": file_path, "status": "skipped", "changed": False, "error": None}
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Ordner-Filter (.trash / .obsidian)
|
||||||
|
if ".trash" in file_path or any(part.startswith('.') for part in file_path.split(os.sep)):
|
||||||
|
return {**result, "status": "skipped", "reason": "ignored_folder"}
|
||||||
|
|
||||||
|
# WP-24c v4.5.9: Path-Normalization für konsistente Hash-Prüfung
|
||||||
|
# Normalisiere file_path zu absolutem Pfad für konsistente Verarbeitung
|
||||||
|
normalized_file_path = os.path.abspath(file_path) if not os.path.isabs(file_path) else file_path
|
||||||
|
|
||||||
|
parsed = read_markdown(normalized_file_path)
|
||||||
|
if not parsed: return {**result, "error": "Empty file"}
|
||||||
|
fm = normalize_frontmatter(parsed.frontmatter)
|
||||||
|
validate_required_frontmatter(fm)
|
||||||
|
|
||||||
|
note_pl = make_note_payload(parsed, vault_root=vault_root, file_path=normalized_file_path, types_cfg=self.registry)
|
||||||
|
note_id = note_pl.get("note_id")
|
||||||
|
|
||||||
|
if not note_id:
|
||||||
|
return {**result, "status": "error", "error": "missing_id"}
|
||||||
|
|
||||||
|
logger.info(f"📄 Bearbeite: '{note_id}' | Pfad: {normalized_file_path} | Title: {note_pl.get('title', 'N/A')}")
|
||||||
|
|
||||||
|
# WP-24c v4.5.9: Strikte Change Detection (Hash-basierte Inhaltsprüfung)
|
||||||
|
# Prüft Hash VOR der Verarbeitung, um redundante Ingestion zu vermeiden
|
||||||
|
old_payload = None if force_replace else fetch_note_payload(self.client, self.prefix, note_id)
|
||||||
|
|
||||||
|
# WP-24c v4.5.10: Prüfe auf ID-Kollisionen (zwei Dateien mit derselben note_id)
|
||||||
|
if old_payload and not force_replace:
|
||||||
|
old_path = old_payload.get("path", "")
|
||||||
|
if old_path and old_path != normalized_file_path:
|
||||||
|
# ID-Kollision erkannt: Zwei verschiedene Dateien haben dieselbe note_id
|
||||||
|
# Logge die Kollision in dedizierte Log-Datei
|
||||||
|
self._log_id_collision(
|
||||||
|
note_id=note_id,
|
||||||
|
existing_path=old_path,
|
||||||
|
conflicting_path=normalized_file_path,
|
||||||
|
action="ERROR"
|
||||||
|
)
|
||||||
|
logger.error(
|
||||||
|
f"❌ [ID-KOLLISION] Kritischer Fehler: Die note_id '{note_id}' wird bereits von einer anderen Datei verwendet!\n"
|
||||||
|
f" Bereits vorhanden: '{old_path}'\n"
|
||||||
|
f" Konflikt mit: '{normalized_file_path}'\n"
|
||||||
|
f" Lösung: Bitte ändern Sie die 'id' im Frontmatter einer der beiden Dateien, um eine eindeutige ID zu gewährleisten.\n"
|
||||||
|
f" Details wurden in logs/id_collisions.log gespeichert."
|
||||||
|
)
|
||||||
|
return {**result, "status": "error", "error": "id_collision", "note_id": note_id, "existing_path": old_path, "conflicting_path": normalized_file_path}
|
||||||
|
|
||||||
|
logger.debug(f"🔍 [CHANGE-DETECTION] Start für '{note_id}': force_replace={force_replace}, old_payload={old_payload is not None}")
|
||||||
|
|
||||||
|
content_changed = True
|
||||||
|
hash_match = False
|
||||||
|
if old_payload and not force_replace:
|
||||||
|
# Nutzt die über MINDNET_CHANGE_DETECTION_MODE gesteuerte Genauigkeit
|
||||||
|
# Mapping: 'full' -> 'full:parsed:canonical', 'body' -> 'body:parsed:canonical'
|
||||||
|
h_key = f"{self.active_hash_mode or 'full'}:parsed:canonical"
|
||||||
|
new_h = note_pl.get("hashes", {}).get(h_key)
|
||||||
|
old_h = old_payload.get("hashes", {}).get(h_key)
|
||||||
|
|
||||||
|
# WP-24c v4.5.9-DEBUG: Detaillierte Hash-Diagnose (INFO-Level)
|
||||||
|
logger.info(f"🔍 [CHANGE-DETECTION] Hash-Vergleich für '{note_id}':")
|
||||||
|
logger.debug(f" -> Hash-Key: '{h_key}'")
|
||||||
|
logger.debug(f" -> Active Hash-Mode: '{self.active_hash_mode or 'full'}'")
|
||||||
|
logger.debug(f" -> New Hash vorhanden: {bool(new_h)}")
|
||||||
|
logger.debug(f" -> Old Hash vorhanden: {bool(old_h)}")
|
||||||
|
if new_h:
|
||||||
|
logger.debug(f" -> New Hash (erste 32 Zeichen): {new_h[:32]}...")
|
||||||
|
if old_h:
|
||||||
|
logger.debug(f" -> Old Hash (erste 32 Zeichen): {old_h[:32]}...")
|
||||||
|
logger.debug(f" -> Verfügbare Hash-Keys in new: {list(note_pl.get('hashes', {}).keys())}")
|
||||||
|
logger.debug(f" -> Verfügbare Hash-Keys in old: {list(old_payload.get('hashes', {}).keys())}")
|
||||||
|
|
||||||
|
if new_h and old_h:
|
||||||
|
hash_match = (new_h == old_h)
|
||||||
|
if hash_match:
|
||||||
|
content_changed = False
|
||||||
|
logger.info(f"🔍 [CHANGE-DETECTION] ✅ Hash identisch für '{note_id}': {h_key} = {new_h[:16]}...")
|
||||||
|
else:
|
||||||
|
logger.warning(f"🔍 [CHANGE-DETECTION] ❌ Hash geändert für '{note_id}': alt={old_h[:16]}..., neu={new_h[:16]}...")
|
||||||
|
# Finde erste unterschiedliche Position
|
||||||
|
diff_pos = next((i for i, (a, b) in enumerate(zip(new_h, old_h)) if a != b), None)
|
||||||
|
if diff_pos is not None:
|
||||||
|
logger.debug(f" -> Hash-Unterschied: Erste unterschiedliche Position: {diff_pos}")
|
||||||
|
else:
|
||||||
|
logger.debug(f" -> Hash-Unterschied: Längen unterschiedlich (new={len(new_h)}, old={len(old_h)})")
|
||||||
|
|
||||||
|
# WP-24c v4.5.10: Logge Hash-Input für Diagnose (DEBUG-Level)
|
||||||
|
# WICHTIG: _get_hash_source_content benötigt ein Dictionary, nicht das ParsedNote-Objekt!
|
||||||
|
from app.core.ingestion.ingestion_note_payload import _get_hash_source_content, _as_dict
|
||||||
|
hash_mode = self.active_hash_mode or 'full'
|
||||||
|
# Konvertiere parsed zu Dictionary für _get_hash_source_content
|
||||||
|
parsed_dict = _as_dict(parsed)
|
||||||
|
hash_input = _get_hash_source_content(parsed_dict, hash_mode)
|
||||||
|
logger.debug(f" -> Hash-Input (erste 200 Zeichen): {hash_input[:200]}...")
|
||||||
|
logger.debug(f" -> Hash-Input Länge: {len(hash_input)}")
|
||||||
|
|
||||||
|
# WP-24c v4.5.10: Vergleiche auch Body-Länge und Frontmatter (DEBUG-Level)
|
||||||
|
# Verwende parsed.body statt note_pl.get("body")
|
||||||
|
new_body = str(getattr(parsed, "body", "") or "").strip()
|
||||||
|
old_body = str(old_payload.get("body", "")).strip() if old_payload else ""
|
||||||
|
logger.debug(f" -> Body-Länge: new={len(new_body)}, old={len(old_body)}")
|
||||||
|
if len(new_body) != len(old_body):
|
||||||
|
logger.debug(f" -> ⚠️ Body-Länge unterschiedlich! Mögliche Ursache: Parsing-Unterschiede")
|
||||||
|
|
||||||
|
# Verwende parsed.frontmatter statt note_pl.get("frontmatter")
|
||||||
|
new_fm = getattr(parsed, "frontmatter", {}) or {}
|
||||||
|
old_fm = old_payload.get("frontmatter", {}) if old_payload else {}
|
||||||
|
logger.debug(f" -> Frontmatter-Keys: new={sorted(new_fm.keys())}, old={sorted(old_fm.keys())}")
|
||||||
|
# Prüfe relevante Frontmatter-Felder
|
||||||
|
relevant_keys = ["title", "type", "status", "tags", "chunking_profile", "chunk_profile", "retriever_weight", "split_level", "strict_heading_split"]
|
||||||
|
for key in relevant_keys:
|
||||||
|
new_val = new_fm.get(key) if isinstance(new_fm, dict) else getattr(new_fm, key, None)
|
||||||
|
old_val = old_fm.get(key) if isinstance(old_fm, dict) else None
|
||||||
|
if new_val != old_val:
|
||||||
|
logger.debug(f" -> ⚠️ Frontmatter '{key}' unterschiedlich: new={new_val}, old={old_val}")
|
||||||
|
else:
|
||||||
|
# WP-24c v4.5.10: Wenn Hash fehlt, als geändert behandeln (Sicherheit)
|
||||||
|
logger.debug(f"⚠️ [CHANGE-DETECTION] Hash fehlt für '{note_id}': new_h={bool(new_h)}, old_h={bool(old_h)}")
|
||||||
|
logger.debug(f" -> Grund: Hash wird als 'geändert' behandelt, da Hash-Werte fehlen")
|
||||||
|
else:
|
||||||
|
if force_replace:
|
||||||
|
logger.debug(f"🔍 [CHANGE-DETECTION] '{note_id}': force_replace=True -> überspringe Hash-Check")
|
||||||
|
elif not old_payload:
|
||||||
|
logger.debug(f"🔍 [CHANGE-DETECTION] '{note_id}': ⚠️ Keine alte Payload gefunden -> erste Verarbeitung oder gelöscht")
|
||||||
|
|
||||||
|
# WP-24c v4.5.9: Strikte Logik - überspringe komplett wenn Hash identisch
|
||||||
|
# WICHTIG: Artifact-Check NACH Hash-Check, da purge_before die Artefakte löschen kann
|
||||||
|
# Wenn Hash identisch ist, sind die Artefakte entweder vorhanden oder werden gerade neu geschrieben
|
||||||
|
if not force_replace and hash_match and old_payload:
|
||||||
|
# WP-24c v4.5.9: Hash identisch -> überspringe komplett (auch wenn Artefakte nach PURGE fehlen)
|
||||||
|
# Der Hash ist die autoritative Quelle für "Inhalt unverändert"
|
||||||
|
# Artefakte werden beim nächsten normalen Import wieder erstellt, wenn nötig
|
||||||
|
logger.info(f"⏭️ [SKIP] '{note_id}' unverändert (Hash identisch - überspringe komplett, auch wenn Artefakte fehlen)")
|
||||||
|
return {**result, "status": "unchanged", "note_id": note_id, "reason": "hash_identical"}
|
||||||
|
elif not force_replace and old_payload and not hash_match:
|
||||||
|
# WP-24c v4.5.10: Hash geändert - erlaube Verarbeitung (DEBUG-Level)
|
||||||
|
logger.debug(f"🔍 [CHANGE-DETECTION] '{note_id}': Hash geändert -> erlaube Verarbeitung")
|
||||||
|
|
||||||
|
# WP-24c v4.5.10: Hash geändert oder keine alte Payload - prüfe Artefakte für normale Verarbeitung
|
||||||
|
c_miss, e_miss = artifacts_missing(self.client, self.prefix, note_id)
|
||||||
|
logger.debug(f"🔍 [CHANGE-DETECTION] '{note_id}': Artifact-Check: c_miss={c_miss}, e_miss={e_miss}")
|
||||||
|
|
||||||
|
if not apply:
|
||||||
|
return {**result, "status": "dry-run", "changed": True, "note_id": note_id}
|
||||||
|
|
||||||
|
# Chunks & MoE
|
||||||
|
profile = note_pl.get("chunk_profile", "sliding_standard")
|
||||||
|
note_type = resolve_note_type(self.registry, fm.get("type"))
|
||||||
|
chunk_cfg = get_chunk_config_by_profile(self.registry, profile, note_type)
|
||||||
|
enable_smart = chunk_cfg.get("enable_smart_edge_allocation", False)
|
||||||
|
chunks = await assemble_chunks(note_id, getattr(parsed, "body", ""), note_type, config=chunk_cfg)
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Validierung in Chunk-Schleife entfernt
|
||||||
|
# Alle candidate: Kanten werden jetzt in Phase 3 (nach build_edges_for_note) validiert
|
||||||
|
# Dies stellt sicher, dass auch Note-Scope Kanten aus LLM-Validierungs-Zonen geprüft werden
|
||||||
|
# Der candidate_pool wird unverändert weitergegeben, damit build_edges_for_note alle Kanten erkennt
|
||||||
|
# WP-24c v4.5.8: Nur ID-Validierung bleibt (Ghost-ID Schutz), keine LLM-Validierung mehr hier
|
||||||
|
for ch in chunks:
|
||||||
|
new_pool = []
|
||||||
|
for cand in getattr(ch, "candidate_pool", []):
|
||||||
|
# WP-24c v4.5.8: Nur ID-Validierung (Ghost-ID Schutz)
|
||||||
|
t_id = cand.get('target_id') or cand.get('to') or cand.get('note_id')
|
||||||
|
if not self._is_valid_id(t_id):
|
||||||
|
continue
|
||||||
|
# WP-24c v4.5.8: Alle Kanten gehen durch - LLM-Validierung erfolgt in Phase 3
|
||||||
|
new_pool.append(cand)
|
||||||
|
ch.candidate_pool = new_pool
|
||||||
|
|
||||||
|
# chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks, file_path=file_path, types_cfg=self.registry)
|
||||||
|
# v4.2.8 Fix C: Explizite Übergabe des Profil-Namens für den Chunk-Payload
|
||||||
|
chunk_pls = make_chunk_payloads(fm, note_pl["path"], chunks, file_path=file_path, types_cfg=self.registry, chunk_profile=profile)
|
||||||
|
|
||||||
|
vecs = await self.embedder.embed_documents([c.get("window") or "" for c in chunk_pls]) if chunk_pls else []
|
||||||
|
|
||||||
|
# WP-24c v4.2.0: Kanten-Extraktion mit Note-Scope Zonen Support
|
||||||
|
# Übergabe des Original-Markdown-Texts für Note-Scope Zonen-Extraktion
|
||||||
|
markdown_body = getattr(parsed, "body", "")
|
||||||
|
raw_edges = build_edges_for_note(
|
||||||
|
note_id,
|
||||||
|
chunk_pls,
|
||||||
|
note_level_references=note_pl.get("references", []),
|
||||||
|
markdown_body=markdown_body
|
||||||
|
)
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Phase 3 - Finaler Validierungs-Gate für candidate: Kanten
|
||||||
|
# Prüfe alle Kanten mit rule_id ODER provenance beginnend mit "candidate:"
|
||||||
|
# Dies schließt alle Kandidaten ein, unabhängig von ihrer Herkunft (global_pool, explicit:callout, etc.)
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Kontext-Optimierung für Note-Scope Kanten
|
||||||
|
# Aggregiere den gesamten Note-Text für bessere Validierungs-Entscheidungen
|
||||||
|
note_text = markdown_body or " ".join([c.get("text", "") or c.get("window", "") for c in chunk_pls])
|
||||||
|
# Erstelle eine Note-Summary aus den wichtigsten Chunks (für bessere Kontext-Qualität)
|
||||||
|
note_summary = " ".join([c.get("window", "") or c.get("text", "") for c in chunk_pls[:5]]) # Top 5 Chunks
|
||||||
|
|
||||||
|
validated_edges = []
|
||||||
|
rejected_edges = []
|
||||||
|
|
||||||
|
for e in raw_edges:
|
||||||
|
rule_id = e.get("rule_id", "")
|
||||||
|
provenance = e.get("provenance", "")
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Trigger-Kriterium - rule_id ODER provenance beginnt mit "candidate:"
|
||||||
|
is_candidate = (rule_id and rule_id.startswith("candidate:")) or (provenance and provenance.startswith("candidate:"))
|
||||||
|
|
||||||
|
if is_candidate:
|
||||||
|
# Extrahiere target_id für Validierung (aus verschiedenen möglichen Feldern)
|
||||||
|
target_id = e.get("target_id") or e.get("to")
|
||||||
|
if not target_id:
|
||||||
|
# Fallback: Versuche aus Payload zu extrahieren
|
||||||
|
payload = e.get("extra", {}) if isinstance(e.get("extra"), dict) else {}
|
||||||
|
target_id = payload.get("target_id") or payload.get("to")
|
||||||
|
|
||||||
|
if not target_id:
|
||||||
|
logger.warning(f"⚠️ [PHASE 3] Keine target_id gefunden für Kante: {e}")
|
||||||
|
rejected_edges.append(e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
kind = e.get("kind", "related_to")
|
||||||
|
source_id = e.get("source_id", note_id)
|
||||||
|
scope = e.get("scope", "chunk")
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Kontext-Optimierung für Note-Scope Kanten
|
||||||
|
# Für scope: note verwende Note-Summary oder gesamten Note-Text
|
||||||
|
# Für scope: chunk verwende den spezifischen Chunk-Text (falls verfügbar)
|
||||||
|
if scope == "note":
|
||||||
|
validation_text = note_summary or note_text
|
||||||
|
context_info = "Note-Scope (aggregiert)"
|
||||||
|
else:
|
||||||
|
# Für Chunk-Scope: Versuche Chunk-Text zu finden, sonst Note-Text
|
||||||
|
chunk_id = e.get("chunk_id") or source_id
|
||||||
|
chunk_text = None
|
||||||
|
for ch in chunk_pls:
|
||||||
|
if ch.get("chunk_id") == chunk_id or ch.get("id") == chunk_id:
|
||||||
|
chunk_text = ch.get("text") or ch.get("window", "")
|
||||||
|
break
|
||||||
|
validation_text = chunk_text or note_text
|
||||||
|
context_info = f"Chunk-Scope ({chunk_id})"
|
||||||
|
|
||||||
|
# Erstelle Edge-Dict für Validierung (kompatibel mit validate_edge_candidate)
|
||||||
|
edge_for_validation = {
|
||||||
|
"kind": kind,
|
||||||
|
"to": target_id, # validate_edge_candidate erwartet "to"
|
||||||
|
"target_id": target_id,
|
||||||
|
"provenance": provenance if not provenance.startswith("candidate:") else provenance.replace("candidate:", "").strip(),
|
||||||
|
"confidence": e.get("confidence", 0.9)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(f"🚀 [PHASE 3] Validierung: {source_id} -> {target_id} ({kind}) | Scope: {scope} | Kontext: {context_info}")
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Validiere gegen optimierten Kontext
|
||||||
|
is_valid = await validate_edge_candidate(
|
||||||
|
chunk_text=validation_text,
|
||||||
|
edge=edge_for_validation,
|
||||||
|
batch_cache=self.batch_cache,
|
||||||
|
llm_service=self.llm,
|
||||||
|
profile_name="ingest_validator"
|
||||||
|
)
|
||||||
|
|
||||||
|
if is_valid:
|
||||||
|
# WP-24c v4.5.8: Entferne candidate: Präfix (Kante wird zum Fakt)
|
||||||
|
new_rule_id = rule_id.replace("candidate:", "").strip() if rule_id else provenance.replace("candidate:", "").strip() if provenance.startswith("candidate:") else provenance
|
||||||
|
if not new_rule_id:
|
||||||
|
new_rule_id = e.get("provenance", "explicit").replace("candidate:", "").strip()
|
||||||
|
|
||||||
|
# Aktualisiere rule_id und provenance im Edge
|
||||||
|
e["rule_id"] = new_rule_id
|
||||||
|
if provenance.startswith("candidate:"):
|
||||||
|
e["provenance"] = provenance.replace("candidate:", "").strip()
|
||||||
|
|
||||||
|
validated_edges.append(e)
|
||||||
|
logger.info(f"✅ [PHASE 3] VERIFIED: {source_id} -> {target_id} ({kind}) | rule_id: {new_rule_id}")
|
||||||
|
else:
|
||||||
|
# WP-24c v4.5.8: Kante ablehnen (nicht zu validated_edges hinzufügen)
|
||||||
|
rejected_edges.append(e)
|
||||||
|
logger.info(f"🚫 [PHASE 3] REJECTED: {source_id} -> {target_id} ({kind})")
|
||||||
|
else:
|
||||||
|
# WP-24c v4.5.8: Keine candidate: Kante -> direkt übernehmen
|
||||||
|
validated_edges.append(e)
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Phase 3 abgeschlossen - rejected_edges werden NICHT weiterverarbeitet
|
||||||
|
# WP-24c v4.5.9: Persistierung von rejected_edges für Audit-Zwecke
|
||||||
|
if rejected_edges:
|
||||||
|
logger.info(f"🚫 [PHASE 3] {len(rejected_edges)} Kanten abgelehnt und werden nicht in die DB geschrieben")
|
||||||
|
self._persist_rejected_edges(note_id, rejected_edges)
|
||||||
|
|
||||||
|
# WP-24c v4.5.8: Verwende validated_edges statt raw_edges für weitere Verarbeitung
|
||||||
|
# Nur verified Kanten (ohne candidate: Präfix) werden in Phase 2 (Symmetrie) verarbeitet
|
||||||
|
explicit_edges = []
|
||||||
|
for e in validated_edges:
|
||||||
|
t_raw = e.get("target_id")
|
||||||
|
t_ctx = self.batch_cache.get(t_raw)
|
||||||
|
t_id = t_ctx.note_id if t_ctx else t_raw
|
||||||
|
|
||||||
|
if not self._is_valid_id(t_id): continue
|
||||||
|
|
||||||
|
resolved_kind = edge_registry.resolve(e.get("kind", "related_to"), provenance="explicit")
|
||||||
|
# WP-24c v4.1.0: target_section aus dem Edge-Payload extrahieren und beibehalten
|
||||||
|
target_section = e.get("target_section")
|
||||||
|
e.update({
|
||||||
|
"kind": resolved_kind,
|
||||||
|
"relation": resolved_kind, # Konsistenz: kind und relation identisch
|
||||||
|
"target_id": t_id,
|
||||||
|
"source_id": e.get("source_id") or note_id, # Sicherstellen, dass source_id gesetzt ist
|
||||||
|
"origin_note_id": note_id,
|
||||||
|
"virtual": False
|
||||||
|
})
|
||||||
|
explicit_edges.append(e)
|
||||||
|
|
||||||
|
# Symmetrie puffern (WP-24c v4.1.0: Korrekte Symmetrie-Integrität)
|
||||||
|
inv_kind = edge_registry.get_inverse(resolved_kind)
|
||||||
|
if inv_kind and t_id != note_id:
|
||||||
|
# GOLD-STANDARD v4.1.0: Symmetrie-Integrität
|
||||||
|
v_edge = {
|
||||||
|
"note_id": t_id, # Besitzer-Wechsel: Symmetrie gehört zum Link-Ziel
|
||||||
|
"source_id": t_id, # Neue Quelle ist das Link-Ziel
|
||||||
|
"target_id": note_id, # Ziel ist die ursprüngliche Quelle
|
||||||
|
"kind": inv_kind, # Inverser Kanten-Typ
|
||||||
|
"relation": inv_kind, # Konsistenz: kind und relation identisch
|
||||||
|
"scope": "note", # Symmetrien sind immer Note-Level
|
||||||
|
"virtual": True,
|
||||||
|
"origin_note_id": note_id, # Tracking: Woher kommt die Symmetrie
|
||||||
|
}
|
||||||
|
# target_section beibehalten, falls vorhanden (für Section-Links)
|
||||||
|
if target_section:
|
||||||
|
v_edge["target_section"] = target_section
|
||||||
|
self.symmetry_buffer.append(v_edge)
|
||||||
|
|
||||||
|
# DB Upsert
|
||||||
|
if purge_before and old_payload: purge_artifacts(self.client, self.prefix, note_id)
|
||||||
|
|
||||||
|
col_n, pts_n = points_for_note(self.prefix, note_pl, None, self.dim)
|
||||||
|
upsert_batch(self.client, col_n, pts_n, wait=True)
|
||||||
|
|
||||||
|
if chunk_pls and vecs:
|
||||||
|
col_c, pts_c = points_for_chunks(self.prefix, chunk_pls, vecs)
|
||||||
|
upsert_batch(self.client, col_c, pts_c, wait=True)
|
||||||
|
|
||||||
|
if explicit_edges:
|
||||||
|
col_e, pts_e = points_for_edges(self.prefix, explicit_edges)
|
||||||
|
upsert_batch(self.client, col_e, pts_e, wait=True)
|
||||||
|
|
||||||
|
logger.info(f" ✨ Phase 1 fertig: {len(explicit_edges)} explizite Kanten für '{note_id}'.")
|
||||||
|
return {"status": "success", "note_id": note_id}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Fehler bei {file_path}: {e}", exc_info=True)
|
||||||
|
return {**result, "status": "error", "error": str(e)}
|
||||||
|
|
||||||
|
async def create_from_text(self, markdown_content: str, filename: str, vault_root: str, folder: str = "00_Inbox") -> Dict[str, Any]:
|
||||||
|
"""Erstellt eine Note aus einem Textstream."""
|
||||||
|
target_path = os.path.join(vault_root, folder, filename)
|
||||||
|
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
||||||
|
with open(target_path, "w", encoding="utf-8") as f:
|
||||||
|
f.write(markdown_content)
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
return await self.process_file(file_path=target_path, vault_root=vault_root, apply=True, force_replace=True, purge_before=True)
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_utils.py
|
||||||
|
DESCRIPTION: Hilfswerkzeuge für JSON-Recovery, Typ-Registry und Konfigurations-Lookups.
|
||||||
|
AUDIT v2.13.9: Behebung des Circular Imports durch Nutzung der app.core.registry.
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from typing import Any, Optional, Dict
|
||||||
|
|
||||||
|
# ENTSCHEIDENDER FIX: Import der Basis-Logik aus dem neutralen Registry-Modul.
|
||||||
|
# Dies bricht den Zirkelbezug auf, da dieses Modul keine Services mehr importiert.
|
||||||
|
from app.core.registry import load_type_registry, clean_llm_text
|
||||||
|
|
||||||
|
def extract_json_from_response(text: str, registry: Optional[dict] = None) -> Any:
|
||||||
|
"""
|
||||||
|
Extrahiert JSON-Daten und bereinigt LLM-Steuerzeichen.
|
||||||
|
WP-14: Nutzt nun die zentrale clean_llm_text Funktion aus app.core.registry.
|
||||||
|
"""
|
||||||
|
if not text:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# 1. Text zentral bereinigen via neutralem Modul
|
||||||
|
clean = clean_llm_text(text, registry)
|
||||||
|
|
||||||
|
# 2. Markdown-Code-Blöcke extrahieren
|
||||||
|
match = re.search(r"```(?:json)?\s*(.*?)\s*```", clean, re.DOTALL)
|
||||||
|
payload = match.group(1) if match else clean
|
||||||
|
|
||||||
|
try:
|
||||||
|
return json.loads(payload.strip())
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# Recovery: Suche nach Liste
|
||||||
|
start = payload.find('[')
|
||||||
|
end = payload.rfind(']') + 1
|
||||||
|
if start != -1 and end > start:
|
||||||
|
try: return json.loads(payload[start:end])
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
# Recovery: Suche nach Objekt
|
||||||
|
start_obj = payload.find('{')
|
||||||
|
end_obj = payload.rfind('}') + 1
|
||||||
|
if start_obj != -1 and end_obj > start_obj:
|
||||||
|
try: return json.loads(payload[start_obj:end_obj])
|
||||||
|
except: pass
|
||||||
|
return []
|
||||||
|
|
||||||
|
def resolve_note_type(registry: dict, requested: Optional[str]) -> str:
|
||||||
|
"""
|
||||||
|
Bestimmt den finalen Notiz-Typ.
|
||||||
|
WP-14: Fallback wird nun über ingestion_settings.default_note_type gesteuert.
|
||||||
|
"""
|
||||||
|
types = registry.get("types", {})
|
||||||
|
if requested and requested in types:
|
||||||
|
return requested
|
||||||
|
|
||||||
|
# Dynamischer Fallback aus der Registry (Standard: 'concept')
|
||||||
|
ingest_cfg = registry.get("ingestion_settings", {})
|
||||||
|
return ingest_cfg.get("default_note_type", "concept")
|
||||||
|
|
||||||
|
def get_chunk_config_by_profile(registry: dict, profile_name: str, note_type: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Holt die Chunker-Parameter für ein spezifisches Profil aus der Registry.
|
||||||
|
"""
|
||||||
|
from app.core.chunking import get_chunk_config
|
||||||
|
profiles = registry.get("chunking_profiles", {})
|
||||||
|
if profile_name in profiles:
|
||||||
|
cfg = profiles[profile_name].copy()
|
||||||
|
if "overlap" in cfg and isinstance(cfg["overlap"], list):
|
||||||
|
cfg["overlap"] = tuple(cfg["overlap"])
|
||||||
|
return cfg
|
||||||
|
return get_chunk_config(note_type)
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/ingestion/ingestion_validation.py
|
||||||
|
DESCRIPTION: WP-15b semantische Validierung von Kanten gegen den LocalBatchCache.
|
||||||
|
WP-24c: Erweiterung um automatische Symmetrie-Generierung (Inverse Kanten).
|
||||||
|
WP-25b: Konsequente Lazy-Prompt-Orchestration (prompt_key + variables).
|
||||||
|
VERSION: 3.0.0 (WP-24c: Symmetric Edge Management)
|
||||||
|
STATUS: Active
|
||||||
|
FIX:
|
||||||
|
- WP-24c: Integration der EdgeRegistry zur dynamischen Inversions-Ermittlung.
|
||||||
|
- WP-24c: Implementierung von validate_and_symmetrize für bidirektionale Graphen.
|
||||||
|
- WP-25b: Beibehaltung der hierarchischen Prompt-Resolution und Modell-Spezi-Logik.
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
from typing import Dict, Any, Optional, List
|
||||||
|
from app.core.parser import NoteContext
|
||||||
|
|
||||||
|
# Import der neutralen Bereinigungs-Logik zur Vermeidung von Circular Imports
|
||||||
|
from app.core.registry import clean_llm_text
|
||||||
|
# WP-24c: Zugriff auf das dynamische Vokabular
|
||||||
|
from app.services.edge_registry import registry as edge_registry
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
async def validate_edge_candidate(
|
||||||
|
chunk_text: str,
|
||||||
|
edge: Dict,
|
||||||
|
batch_cache: Dict[str, NoteContext],
|
||||||
|
llm_service: Any,
|
||||||
|
provider: Optional[str] = None,
|
||||||
|
profile_name: str = "ingest_validator"
|
||||||
|
) -> bool:
|
||||||
|
"""
|
||||||
|
WP-15b/25b: Validiert einen Kandidaten semantisch gegen das Ziel im Cache.
|
||||||
|
Nutzt Lazy-Prompt-Loading (PROMPT-TRACE) für deterministische YES/NO Entscheidungen.
|
||||||
|
"""
|
||||||
|
target_id = edge.get("to")
|
||||||
|
target_ctx = batch_cache.get(target_id)
|
||||||
|
|
||||||
|
# Robust Lookup Fix (v2.12.2): Support für Anker (Note#Section)
|
||||||
|
if not target_ctx and "#" in str(target_id):
|
||||||
|
base_id = target_id.split("#")[0]
|
||||||
|
target_ctx = batch_cache.get(base_id)
|
||||||
|
|
||||||
|
# Sicherheits-Fallback (Hard-Link Integrity)
|
||||||
|
# Wenn das Ziel nicht im Cache ist, erlauben wir die Kante (Link-Erhalt).
|
||||||
|
if not target_ctx:
|
||||||
|
logger.info(f"ℹ️ [VALIDATION SKIP] No context for '{target_id}' - allowing link.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info(f"⚖️ [VALIDATING] Relation '{edge.get('kind')}' -> '{target_id}' (Profile: {profile_name})...")
|
||||||
|
|
||||||
|
# WP-25b: Lazy-Prompt Aufruf.
|
||||||
|
# Übergabe von prompt_key und Variablen für modell-optimierte Formatierung.
|
||||||
|
raw_response = await llm_service.generate_raw_response(
|
||||||
|
prompt_key="edge_validation",
|
||||||
|
variables={
|
||||||
|
"chunk_text": chunk_text[:1500],
|
||||||
|
"target_title": target_ctx.title,
|
||||||
|
"target_summary": target_ctx.summary,
|
||||||
|
"edge_kind": edge.get("kind", "related_to")
|
||||||
|
},
|
||||||
|
priority="background",
|
||||||
|
profile_name=profile_name
|
||||||
|
)
|
||||||
|
|
||||||
|
# Bereinigung zur Sicherstellung der Interpretierbarkeit (Mistral/Qwen Safe)
|
||||||
|
response = clean_llm_text(raw_response)
|
||||||
|
|
||||||
|
# Semantische Prüfung des Ergebnisses
|
||||||
|
is_valid = "YES" in response.upper()
|
||||||
|
|
||||||
|
if is_valid:
|
||||||
|
logger.info(f"✅ [VALIDATED] Relation to '{target_id}' confirmed.")
|
||||||
|
else:
|
||||||
|
logger.info(f"🚫 [REJECTED] Relation to '{target_id}' irrelevant for this chunk.")
|
||||||
|
return is_valid
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_str = str(e).lower()
|
||||||
|
error_type = type(e).__name__
|
||||||
|
|
||||||
|
# WP-25b: Differenzierung zwischen transienten und permanenten Fehlern
|
||||||
|
# Transiente Fehler (Netzwerk) → erlauben (Integrität vor Präzision)
|
||||||
|
if any(x in error_str for x in ["timeout", "connection", "network", "unreachable", "refused"]):
|
||||||
|
logger.warning(f"⚠️ Transient error for {target_id}: {error_type} - {e}. Allowing edge.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Permanente Fehler → ablehnen (Graph-Qualität schützen)
|
||||||
|
logger.error(f"❌ Permanent validation error for {target_id}: {error_type} - {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def validate_and_symmetrize(
|
||||||
|
chunk_text: str,
|
||||||
|
edge: Dict,
|
||||||
|
source_id: str,
|
||||||
|
batch_cache: Dict[str, NoteContext],
|
||||||
|
llm_service: Any,
|
||||||
|
profile_name: str = "ingest_validator"
|
||||||
|
) -> List[Dict]:
|
||||||
|
"""
|
||||||
|
WP-24c: Erweitertes Validierungs-Gateway.
|
||||||
|
Prüft die Primärkante und erzeugt bei Erfolg automatisch die inverse Kante.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict]: Eine Liste mit 0, 1 (nur Primär) oder 2 (Primär + Invers) Kanten.
|
||||||
|
"""
|
||||||
|
# 1. Semantische Prüfung der Primärkante (A -> B)
|
||||||
|
is_valid = await validate_edge_candidate(
|
||||||
|
chunk_text=chunk_text,
|
||||||
|
edge=edge,
|
||||||
|
batch_cache=batch_cache,
|
||||||
|
llm_service=llm_service,
|
||||||
|
profile_name=profile_name
|
||||||
|
)
|
||||||
|
|
||||||
|
if not is_valid:
|
||||||
|
return []
|
||||||
|
|
||||||
|
validated_edges = [edge]
|
||||||
|
|
||||||
|
# 2. WP-24c: Symmetrie-Generierung (B -> A)
|
||||||
|
# Wir laden den inversen Typ dynamisch aus der EdgeRegistry (Single Source of Truth)
|
||||||
|
original_kind = edge.get("kind", "related_to")
|
||||||
|
inverse_kind = edge_registry.get_inverse(original_kind)
|
||||||
|
|
||||||
|
# Wir erzeugen eine inverse Kante nur, wenn ein sinnvoller inverser Typ existiert
|
||||||
|
# und das Ziel der Primärkante (to) valide ist.
|
||||||
|
target_id = edge.get("to")
|
||||||
|
|
||||||
|
if target_id and source_id:
|
||||||
|
# Die inverse Kante zeigt vom Ziel der Primärkante zurück zur Quelle.
|
||||||
|
# Sie wird als 'virtual' markiert, um sie im Retrieval/UI identifizierbar zu machen.
|
||||||
|
inverse_edge = {
|
||||||
|
"to": source_id,
|
||||||
|
"kind": inverse_kind,
|
||||||
|
"provenance": "structure", # System-generiert, geschützt durch Firewall
|
||||||
|
"confidence": edge.get("confidence", 0.9) * 0.9, # Leichte Dämpfung für virtuelle Pfade
|
||||||
|
"virtual": True,
|
||||||
|
"note_id": target_id, # Die Note, von der die inverse Kante ausgeht
|
||||||
|
"rule_id": f"symmetry:{original_kind}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Wir fügen die Symmetrie nur hinzu, wenn sie einen echten Mehrwert bietet
|
||||||
|
# (Vermeidung von redundanten related_to -> related_to Loops)
|
||||||
|
if inverse_kind != original_kind or original_kind not in ["related_to", "references"]:
|
||||||
|
validated_edges.append(inverse_edge)
|
||||||
|
logger.info(f"🔄 [SYMMETRY] Generated inverse edge: '{target_id}' --({inverse_kind})--> '{source_id}'")
|
||||||
|
|
||||||
|
return validated_edges
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
|
def setup_logging(log_level: int = None):
|
||||||
|
"""
|
||||||
|
Konfiguriert das Logging-System mit File- und Console-Handler.
|
||||||
|
WP-24c v4.4.0-DEBUG: Unterstützt DEBUG-Level für End-to-End Tracing.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
log_level: Optionales Log-Level (logging.DEBUG, logging.INFO, etc.)
|
||||||
|
Falls nicht gesetzt, wird aus DEBUG Umgebungsvariable gelesen.
|
||||||
|
"""
|
||||||
|
# 1. Log-Level bestimmen
|
||||||
|
if log_level is None:
|
||||||
|
# WP-24c v4.4.0-DEBUG: Unterstützung für DEBUG-Level via Umgebungsvariable
|
||||||
|
debug_mode = os.getenv("DEBUG", "false").lower() == "true"
|
||||||
|
log_level = logging.DEBUG if debug_mode else logging.INFO
|
||||||
|
|
||||||
|
# 2. Log-Verzeichnis erstellen (falls nicht vorhanden)
|
||||||
|
log_dir = "logs"
|
||||||
|
if not os.path.exists(log_dir):
|
||||||
|
os.makedirs(log_dir)
|
||||||
|
|
||||||
|
log_file = os.path.join(log_dir, "mindnet.log")
|
||||||
|
|
||||||
|
# 3. Formatter definieren (Zeitstempel | Level | Modul | Nachricht)
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
'%(asctime)s | %(levelname)-8s | %(name)s | %(message)s',
|
||||||
|
datefmt='%Y-%m-%d %H:%M:%S'
|
||||||
|
)
|
||||||
|
|
||||||
|
# 4. File Handler: Schreibt in Datei (max. 5MB pro Datei, behält 5 Backups)
|
||||||
|
file_handler = RotatingFileHandler(
|
||||||
|
log_file, maxBytes=5*1024*1024, backupCount=5, encoding='utf-8'
|
||||||
|
)
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
file_handler.setLevel(log_level) # WP-24c v4.4.0-DEBUG: Respektiert log_level
|
||||||
|
|
||||||
|
# 5. Stream Handler: Schreibt weiterhin auf die Konsole
|
||||||
|
console_handler = logging.StreamHandler()
|
||||||
|
console_handler.setFormatter(formatter)
|
||||||
|
console_handler.setLevel(log_level) # WP-24c v4.4.0-DEBUG: Respektiert log_level
|
||||||
|
|
||||||
|
# 6. Root Logger konfigurieren
|
||||||
|
logging.basicConfig(
|
||||||
|
level=log_level,
|
||||||
|
handlers=[file_handler, console_handler],
|
||||||
|
force=True # Überschreibt bestehende Konfiguration
|
||||||
|
)
|
||||||
|
|
||||||
|
level_name = "DEBUG" if log_level == logging.DEBUG else "INFO"
|
||||||
|
logging.info(f"📝 Logging initialized (Level: {level_name}). Writing to {log_file}")
|
||||||
@@ -1,268 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/note_payload.py
|
|
||||||
DESCRIPTION: Baut das JSON-Objekt.
|
|
||||||
FEATURES:
|
|
||||||
1. Multi-Hash: Berechnet immer 'body' AND 'full' Hashes für flexible Change Detection.
|
|
||||||
2. Config-Fix: Liest korrekt 'chunking_profile' aus types.yaml (statt Legacy 'chunk_profile').
|
|
||||||
VERSION: 2.3.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: yaml, os, json, pathlib, hashlib
|
|
||||||
EXTERNAL_CONFIG: config/types.yaml
|
|
||||||
"""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import Any, Dict, Tuple, Optional
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
import pathlib
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
try:
|
|
||||||
import yaml # type: ignore
|
|
||||||
except Exception:
|
|
||||||
yaml = None
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Helper
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def _as_dict(x) -> Dict[str, Any]:
|
|
||||||
"""Versucht, ein ParsedMarkdown-ähnliches Objekt in ein Dict zu überführen."""
|
|
||||||
if isinstance(x, dict):
|
|
||||||
return dict(x)
|
|
||||||
|
|
||||||
out: Dict[str, Any] = {}
|
|
||||||
for attr in (
|
|
||||||
"frontmatter",
|
|
||||||
"body",
|
|
||||||
"id",
|
|
||||||
"note_id",
|
|
||||||
"title",
|
|
||||||
"path",
|
|
||||||
"tags",
|
|
||||||
"type",
|
|
||||||
"created",
|
|
||||||
"modified",
|
|
||||||
"date",
|
|
||||||
):
|
|
||||||
if hasattr(x, attr):
|
|
||||||
val = getattr(x, attr)
|
|
||||||
if val is not None:
|
|
||||||
out[attr] = val
|
|
||||||
|
|
||||||
if not out:
|
|
||||||
out["raw"] = str(x)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def _pick_args(*args, **kwargs) -> Tuple[Optional[str], Optional[dict]]:
|
|
||||||
path = kwargs.get("path") or (args[0] if args else None)
|
|
||||||
types_cfg = kwargs.get("types_cfg") or kwargs.get("types") or None
|
|
||||||
return path, types_cfg
|
|
||||||
|
|
||||||
|
|
||||||
def _env_float(name: str, default: float) -> float:
|
|
||||||
try:
|
|
||||||
return float(os.environ.get(name, default))
|
|
||||||
except Exception:
|
|
||||||
return default
|
|
||||||
|
|
||||||
|
|
||||||
def _ensure_list(x) -> list:
|
|
||||||
if x is None:
|
|
||||||
return []
|
|
||||||
if isinstance(x, list):
|
|
||||||
return [str(i) for i in x]
|
|
||||||
if isinstance(x, (set, tuple)):
|
|
||||||
return [str(i) for i in x]
|
|
||||||
return [str(x)]
|
|
||||||
|
|
||||||
# --- Hash Logic ---
|
|
||||||
def _compute_hash(content: str) -> str:
|
|
||||||
"""Berechnet einen SHA-256 Hash für den gegebenen String."""
|
|
||||||
if not content:
|
|
||||||
return ""
|
|
||||||
return hashlib.sha256(content.encode("utf-8")).hexdigest()
|
|
||||||
|
|
||||||
def _get_hash_source_content(n: Dict[str, Any], mode: str) -> str:
|
|
||||||
"""
|
|
||||||
Stellt den String zusammen, der gehasht werden soll.
|
|
||||||
"""
|
|
||||||
body = str(n.get("body") or "")
|
|
||||||
|
|
||||||
if mode == "body":
|
|
||||||
return body
|
|
||||||
|
|
||||||
if mode == "full":
|
|
||||||
fm = n.get("frontmatter") or {}
|
|
||||||
# Wichtig: Sortierte Keys für deterministisches Verhalten!
|
|
||||||
# Wir nehmen alle steuernden Metadaten auf
|
|
||||||
meta_parts = []
|
|
||||||
# Hier checken wir keys, die eine Neu-Indizierung rechtfertigen würden
|
|
||||||
for k in sorted(["title", "type", "status", "tags", "chunking_profile", "chunk_profile", "retriever_weight"]):
|
|
||||||
val = fm.get(k)
|
|
||||||
if val is not None:
|
|
||||||
meta_parts.append(f"{k}:{val}")
|
|
||||||
|
|
||||||
meta_str = "|".join(meta_parts)
|
|
||||||
return f"{meta_str}||{body}"
|
|
||||||
|
|
||||||
return body
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Type-Registry laden
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def _load_types_config(explicit_cfg: Optional[dict] = None) -> dict:
|
|
||||||
if explicit_cfg and isinstance(explicit_cfg, dict):
|
|
||||||
return explicit_cfg
|
|
||||||
|
|
||||||
path = os.getenv("MINDNET_TYPES_FILE") or "./config/types.yaml"
|
|
||||||
if not os.path.isfile(path) or yaml is None:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
|
||||||
data = yaml.safe_load(f) or {}
|
|
||||||
return data if isinstance(data, dict) else {}
|
|
||||||
except Exception:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def _cfg_for_type(note_type: str, reg: dict) -> dict:
|
|
||||||
if not isinstance(reg, dict):
|
|
||||||
return {}
|
|
||||||
types = reg.get("types") if isinstance(reg.get("types"), dict) else reg
|
|
||||||
return types.get(note_type, {}) if isinstance(types, dict) else {}
|
|
||||||
|
|
||||||
|
|
||||||
def _cfg_defaults(reg: dict) -> dict:
|
|
||||||
if not isinstance(reg, dict):
|
|
||||||
return {}
|
|
||||||
for key in ("defaults", "default", "global"):
|
|
||||||
v = reg.get(key)
|
|
||||||
if isinstance(v, dict):
|
|
||||||
return v
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Haupt-API
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def make_note_payload(note: Any, *args, **kwargs) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Baut das Note-Payload für mindnet_notes auf.
|
|
||||||
Inkludiert Hash-Berechnung (Body & Full) und korrigierte Config-Lookups.
|
|
||||||
"""
|
|
||||||
n = _as_dict(note)
|
|
||||||
path_arg, types_cfg_explicit = _pick_args(*args, **kwargs)
|
|
||||||
reg = _load_types_config(types_cfg_explicit)
|
|
||||||
|
|
||||||
# Hash Config (Parameter für Source/Normalize, Mode ist hardcoded auf 'beide')
|
|
||||||
hash_source = kwargs.get("hash_source", "parsed")
|
|
||||||
hash_normalize = kwargs.get("hash_normalize", "canonical")
|
|
||||||
|
|
||||||
fm = n.get("frontmatter") or {}
|
|
||||||
fm_type = fm.get("type") or n.get("type") or "concept"
|
|
||||||
note_type = str(fm_type)
|
|
||||||
|
|
||||||
cfg_type = _cfg_for_type(note_type, reg)
|
|
||||||
cfg_def = _cfg_defaults(reg)
|
|
||||||
|
|
||||||
# --- retriever_weight ---
|
|
||||||
default_rw = _env_float("MINDNET_DEFAULT_RETRIEVER_WEIGHT", 1.0)
|
|
||||||
retriever_weight = fm.get("retriever_weight")
|
|
||||||
if retriever_weight is None:
|
|
||||||
retriever_weight = cfg_type.get(
|
|
||||||
"retriever_weight",
|
|
||||||
cfg_def.get("retriever_weight", default_rw),
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
retriever_weight = float(retriever_weight)
|
|
||||||
except Exception:
|
|
||||||
retriever_weight = default_rw
|
|
||||||
|
|
||||||
# --- chunk_profile (FIXED LOGIC) ---
|
|
||||||
# 1. Frontmatter Override (beide Schreibweisen erlaubt)
|
|
||||||
chunk_profile = fm.get("chunking_profile") or fm.get("chunk_profile")
|
|
||||||
|
|
||||||
# 2. Type Config (Korrekter Key 'chunking_profile' aus types.yaml)
|
|
||||||
if chunk_profile is None:
|
|
||||||
chunk_profile = cfg_type.get("chunking_profile")
|
|
||||||
|
|
||||||
# 3. Default Config (Fallback auf sliding_standard statt medium)
|
|
||||||
if chunk_profile is None:
|
|
||||||
chunk_profile = cfg_def.get("chunking_profile", "sliding_standard")
|
|
||||||
|
|
||||||
# 4. Safety Fallback
|
|
||||||
if not isinstance(chunk_profile, str) or not chunk_profile:
|
|
||||||
chunk_profile = "sliding_standard"
|
|
||||||
|
|
||||||
# --- edge_defaults ---
|
|
||||||
edge_defaults = fm.get("edge_defaults")
|
|
||||||
if edge_defaults is None:
|
|
||||||
edge_defaults = cfg_type.get(
|
|
||||||
"edge_defaults",
|
|
||||||
cfg_def.get("edge_defaults", []),
|
|
||||||
)
|
|
||||||
edge_defaults = _ensure_list(edge_defaults)
|
|
||||||
|
|
||||||
# --- Basis-Metadaten ---
|
|
||||||
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: Dict[str, Any] = {
|
|
||||||
"note_id": note_id,
|
|
||||||
"title": title,
|
|
||||||
"type": note_type,
|
|
||||||
"path": path or "",
|
|
||||||
"retriever_weight": retriever_weight,
|
|
||||||
"chunk_profile": chunk_profile,
|
|
||||||
"edge_defaults": edge_defaults,
|
|
||||||
"hashes": {} # Init Hash Dict
|
|
||||||
}
|
|
||||||
|
|
||||||
# --- MULTI-HASH CALCULATION (Strategy Decoupling) ---
|
|
||||||
# Wir berechnen immer BEIDE Strategien und speichern sie.
|
|
||||||
# ingestion.py entscheidet dann anhand der ENV-Variable, welcher verglichen wird.
|
|
||||||
modes_to_calc = ["body", "full"]
|
|
||||||
|
|
||||||
for mode in modes_to_calc:
|
|
||||||
content_to_hash = _get_hash_source_content(n, mode)
|
|
||||||
computed_hash = _compute_hash(content_to_hash)
|
|
||||||
# Key Schema: mode:source:normalize (z.B. "full:parsed:canonical")
|
|
||||||
key = f"{mode}:{hash_source}:{hash_normalize}"
|
|
||||||
payload["hashes"][key] = computed_hash
|
|
||||||
|
|
||||||
# Tags / Keywords
|
|
||||||
tags = fm.get("tags") or fm.get("keywords") or n.get("tags")
|
|
||||||
if tags:
|
|
||||||
payload["tags"] = _ensure_list(tags)
|
|
||||||
|
|
||||||
# Aliases
|
|
||||||
aliases = fm.get("aliases")
|
|
||||||
if aliases:
|
|
||||||
payload["aliases"] = _ensure_list(aliases)
|
|
||||||
|
|
||||||
# Zeit
|
|
||||||
for k in ("created", "modified", "date"):
|
|
||||||
v = fm.get(k) or n.get(k)
|
|
||||||
if v:
|
|
||||||
payload[k] = str(v)
|
|
||||||
|
|
||||||
# Fulltext
|
|
||||||
if "body" in n and n["body"]:
|
|
||||||
payload["fulltext"] = str(n["body"])
|
|
||||||
|
|
||||||
# JSON Validation
|
|
||||||
json.loads(json.dumps(payload, ensure_ascii=False))
|
|
||||||
|
|
||||||
return payload
|
|
||||||
@@ -1,293 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/parser.py
|
|
||||||
DESCRIPTION: Liest Markdown-Dateien fehlertolerant (Encoding-Fallback). Trennt Frontmatter (YAML) vom Body.
|
|
||||||
WP-22 Erweiterung: Kanten-Extraktion mit Zeilennummern für die EdgeRegistry.
|
|
||||||
WP-15b: Implementierung NoteContext und pre_scan_markdown für Pass 1 Ingestion.
|
|
||||||
VERSION: 1.9.0
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: yaml, re, dataclasses, json, io, os
|
|
||||||
LAST_ANALYSIS: 2025-12-26
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Any, Dict, Optional, Tuple, Iterable, List
|
|
||||||
import io
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
|
|
||||||
try:
|
|
||||||
import yaml # PyYAML
|
|
||||||
except Exception as e: # pragma: no cover
|
|
||||||
yaml = None # Fehler wird zur Laufzeit geworfen, falls wirklich benötigt
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
# Datamodell
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ParsedNote:
|
|
||||||
frontmatter: Dict[str, Any]
|
|
||||||
body: str
|
|
||||||
path: str
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class NoteContext:
|
|
||||||
"""Metadaten-Container für den flüchtigen LocalBatchCache (Pass 1)."""
|
|
||||||
note_id: str
|
|
||||||
title: str
|
|
||||||
type: str
|
|
||||||
summary: str
|
|
||||||
tags: List[str]
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
# Frontmatter-Erkennung
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
# Öffentliche Kompatibilitäts-Konstante: frühere Skripte importieren FRONTMATTER_RE
|
|
||||||
FRONTMATTER_RE = re.compile(r"^\s*---\s*$") # <— public
|
|
||||||
# Zusätzlich interner Alias (falls jemand ihn referenziert)
|
|
||||||
FRONTMATTER_END = FRONTMATTER_RE # <— public alias
|
|
||||||
|
|
||||||
# interne Namen bleiben bestehen
|
|
||||||
_FRONTMATTER_HEAD = FRONTMATTER_RE
|
|
||||||
_FRONTMATTER_END = FRONTMATTER_RE
|
|
||||||
|
|
||||||
|
|
||||||
def _split_frontmatter(text: str) -> Tuple[Dict[str, Any], str]:
|
|
||||||
"""
|
|
||||||
Zerlegt Text in (frontmatter: dict, body: str).
|
|
||||||
Erkennt Frontmatter nur, wenn die erste Zeile '---' ist und später ein zweites '---' folgt.
|
|
||||||
YAML-Fehler im Frontmatter führen NICHT zum Abbruch: es wird dann ein leeres dict benutzt.
|
|
||||||
"""
|
|
||||||
lines = text.splitlines(True) # keep line endings
|
|
||||||
if not lines:
|
|
||||||
return {}, ""
|
|
||||||
|
|
||||||
if not _FRONTMATTER_HEAD.match(lines[0]):
|
|
||||||
# kein Frontmatter-Header → gesamter Text ist Body
|
|
||||||
return {}, text
|
|
||||||
|
|
||||||
end_idx = None
|
|
||||||
# Suche nach nächstem '---' (max. 2000 Zeilen als Sicherheitslimit)
|
|
||||||
for i in range(1, min(len(lines), 2000)):
|
|
||||||
if _FRONTMATTER_END.match(lines[i]):
|
|
||||||
end_idx = i
|
|
||||||
break
|
|
||||||
|
|
||||||
if end_idx is None:
|
|
||||||
# unvollständiger Frontmatter-Block → behandle alles als Body
|
|
||||||
return {}, text
|
|
||||||
|
|
||||||
fm_raw = "".join(lines[1:end_idx])
|
|
||||||
body = "".join(lines[end_idx + 1:])
|
|
||||||
|
|
||||||
data: Dict[str, Any] = {}
|
|
||||||
if yaml is None:
|
|
||||||
raise RuntimeError("PyYAML ist nicht installiert (pip install pyyaml).")
|
|
||||||
|
|
||||||
try:
|
|
||||||
loaded = yaml.safe_load(fm_raw) or {}
|
|
||||||
if isinstance(loaded, dict):
|
|
||||||
data = loaded
|
|
||||||
else:
|
|
||||||
data = {}
|
|
||||||
except Exception as e:
|
|
||||||
# YAML-Fehler nicht fatal machen
|
|
||||||
print(json.dumps({"warn": "frontmatter_yaml_parse_failed", "error": str(e)}))
|
|
||||||
data = {}
|
|
||||||
|
|
||||||
# optionales kosmetisches Trim: eine führende Leerzeile im Body entfernen
|
|
||||||
if body.startswith("\n"):
|
|
||||||
body = body[1:]
|
|
||||||
|
|
||||||
return data, body
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
# Robustes Lesen mit Encoding-Fallback
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
_FALLBACK_ENCODINGS: Tuple[str, ...] = ("utf-8", "utf-8-sig", "cp1252", "latin-1")
|
|
||||||
|
|
||||||
|
|
||||||
def _read_text_with_fallback(path: str) -> Tuple[str, str, bool]:
|
|
||||||
"""
|
|
||||||
Liest Datei mit mehreren Decodierungsversuchen.
|
|
||||||
Rückgabe: (text, used_encoding, had_fallback)
|
|
||||||
- had_fallback=True, falls NICHT 'utf-8' verwendet wurde (oder 'utf-8-sig').
|
|
||||||
"""
|
|
||||||
last_err: Optional[str] = None
|
|
||||||
for enc in _FALLBACK_ENCODINGS:
|
|
||||||
try:
|
|
||||||
with io.open(path, "r", encoding=enc, errors="strict") as f:
|
|
||||||
text = f.read()
|
|
||||||
# 'utf-8-sig' zählt hier als Fallback (weil BOM), aber ist unproblematisch
|
|
||||||
return text, enc, (enc != "utf-8")
|
|
||||||
except UnicodeDecodeError as e:
|
|
||||||
last_err = f"{type(e).__name__}: {e}"
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Letzter, extrem defensiver Fallback: Bytes → UTF-8 mit REPLACE (keine Exception)
|
|
||||||
with open(path, "rb") as fb:
|
|
||||||
raw = fb.read()
|
|
||||||
text = raw.decode("utf-8", errors="replace")
|
|
||||||
print(json.dumps({
|
|
||||||
"path": path,
|
|
||||||
"warn": "encoding_fallback_exhausted",
|
|
||||||
"info": last_err or "unknown"
|
|
||||||
}, ensure_ascii=False))
|
|
||||||
return text, "utf-8(replace)", True
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
# Öffentliche API
|
|
||||||
# ---------------------------------------------------------------------
|
|
||||||
|
|
||||||
def read_markdown(path: str) -> Optional[ParsedNote]:
|
|
||||||
"""
|
|
||||||
Liest eine Markdown-Datei fehlertolerant.
|
|
||||||
"""
|
|
||||||
if not os.path.exists(path):
|
|
||||||
return None
|
|
||||||
|
|
||||||
text, enc, had_fb = _read_text_with_fallback(path)
|
|
||||||
if had_fb:
|
|
||||||
print(json.dumps({"path": path, "warn": "encoding_fallback_used", "used": enc}, ensure_ascii=False))
|
|
||||||
|
|
||||||
fm, body = _split_frontmatter(text)
|
|
||||||
return ParsedNote(frontmatter=fm or {}, body=body or "", path=path)
|
|
||||||
|
|
||||||
|
|
||||||
def pre_scan_markdown(path: str) -> Optional[NoteContext]:
|
|
||||||
"""
|
|
||||||
WP-15b: Schneller Scan für den LocalBatchCache (Pass 1).
|
|
||||||
Extrahiert nur Identität und Kurz-Kontext zur semantischen Validierung.
|
|
||||||
"""
|
|
||||||
parsed = read_markdown(path)
|
|
||||||
if not parsed:
|
|
||||||
return None
|
|
||||||
|
|
||||||
fm = parsed.frontmatter
|
|
||||||
# ID-Findung: Frontmatter ID oder Dateiname als Fallback
|
|
||||||
note_id = str(fm.get("id") or os.path.splitext(os.path.basename(path))[0])
|
|
||||||
|
|
||||||
# Erstelle Kurz-Zusammenfassung (erste 500 Zeichen des Body, bereinigt)
|
|
||||||
clean_body = re.sub(r'[#*`>]', '', parsed.body[:600]).strip()
|
|
||||||
summary = clean_body[:500] + "..." if len(clean_body) > 500 else clean_body
|
|
||||||
|
|
||||||
return NoteContext(
|
|
||||||
note_id=note_id,
|
|
||||||
title=str(fm.get("title", note_id)),
|
|
||||||
type=str(fm.get("type", "concept")),
|
|
||||||
summary=summary,
|
|
||||||
tags=fm.get("tags", []) if isinstance(fm.get("tags"), list) else []
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_required_frontmatter(fm: Dict[str, Any],
|
|
||||||
required: Tuple[str, ...] = ("id", "title")) -> None:
|
|
||||||
"""
|
|
||||||
Prüft, ob alle Pflichtfelder vorhanden sind.
|
|
||||||
"""
|
|
||||||
if fm is None:
|
|
||||||
fm = {}
|
|
||||||
missing = []
|
|
||||||
for k in required:
|
|
||||||
v = fm.get(k)
|
|
||||||
if v is None:
|
|
||||||
missing.append(k)
|
|
||||||
elif isinstance(v, str) and not v.strip():
|
|
||||||
missing.append(k)
|
|
||||||
if missing:
|
|
||||||
raise ValueError(f"Missing required frontmatter fields: {', '.join(missing)}")
|
|
||||||
|
|
||||||
if "tags" in fm and fm["tags"] not in (None, "") and not isinstance(fm["tags"], (list, tuple)):
|
|
||||||
raise ValueError("frontmatter 'tags' must be a list of strings")
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_frontmatter(fm: Dict[str, Any]) -> Dict[str, Any]:
|
|
||||||
"""
|
|
||||||
Normalisierung von Tags und anderen Feldern.
|
|
||||||
"""
|
|
||||||
out = dict(fm or {})
|
|
||||||
if "tags" in out:
|
|
||||||
if isinstance(out["tags"], str):
|
|
||||||
out["tags"] = [out["tags"].strip()] if out["tags"].strip() else []
|
|
||||||
elif isinstance(out["tags"], list):
|
|
||||||
out["tags"] = [str(t).strip() for t in out["tags"] if t is not None]
|
|
||||||
else:
|
|
||||||
out["tags"] = [str(out["tags"]).strip()] if out["tags"] not in (None, "") else []
|
|
||||||
if "embedding_exclude" in out:
|
|
||||||
out["embedding_exclude"] = bool(out["embedding_exclude"])
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------ Wikilinks ---------------------------- #
|
|
||||||
|
|
||||||
_WIKILINK_RE = re.compile(r"\[\[([^\]]+)\]\]")
|
|
||||||
|
|
||||||
|
|
||||||
def extract_wikilinks(text: str) -> List[str]:
|
|
||||||
"""
|
|
||||||
Extrahiert Wikilinks als einfache Liste von IDs.
|
|
||||||
"""
|
|
||||||
if not text:
|
|
||||||
return []
|
|
||||||
out: List[str] = []
|
|
||||||
for m in _WIKILINK_RE.finditer(text):
|
|
||||||
raw = (m.group(1) or "").strip()
|
|
||||||
if not raw:
|
|
||||||
continue
|
|
||||||
if "|" in raw:
|
|
||||||
raw = raw.split("|", 1)[0].strip()
|
|
||||||
if "#" in raw:
|
|
||||||
raw = raw.split("#", 1)[0].strip()
|
|
||||||
if raw:
|
|
||||||
out.append(raw)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def extract_edges_with_context(parsed: ParsedNote) -> List[Dict[str, Any]]:
|
|
||||||
"""
|
|
||||||
WP-22: Extrahiert Wikilinks [[Ziel|Typ]] aus dem Body und speichert die Zeilennummer.
|
|
||||||
Gibt eine Liste von Dictionaries zurück, die direkt von der Ingestion verarbeitet werden können.
|
|
||||||
"""
|
|
||||||
edges = []
|
|
||||||
if not parsed or not parsed.body:
|
|
||||||
return edges
|
|
||||||
|
|
||||||
# Wir nutzen splitlines(True), um Zeilenumbrüche für die Positionsberechnung zu erhalten,
|
|
||||||
# oder einfaches splitlines() für die reine Zeilennummerierung.
|
|
||||||
lines = parsed.body.splitlines()
|
|
||||||
|
|
||||||
for line_num, line_content in enumerate(lines, 1):
|
|
||||||
for match in _WIKILINK_RE.finditer(line_content):
|
|
||||||
raw = (match.group(1) or "").strip()
|
|
||||||
if not raw:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Syntax: [[Ziel|Typ]]
|
|
||||||
if "|" in raw:
|
|
||||||
parts = raw.split("|", 1)
|
|
||||||
target = parts[0].strip()
|
|
||||||
kind = parts[1].strip()
|
|
||||||
else:
|
|
||||||
target = raw.strip()
|
|
||||||
kind = "related_to" # Default-Typ
|
|
||||||
|
|
||||||
# Anchor (#) entfernen, da Relationen auf Notiz-Ebene (ID) basieren
|
|
||||||
if "#" in target:
|
|
||||||
target = target.split("#", 1)[0].strip()
|
|
||||||
|
|
||||||
if target:
|
|
||||||
edges.append({
|
|
||||||
"to": target,
|
|
||||||
"kind": kind,
|
|
||||||
"line": line_num,
|
|
||||||
"provenance": "explicit"
|
|
||||||
})
|
|
||||||
return edges
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/parser/__init__.py
|
||||||
|
DESCRIPTION: Package-Einstiegspunkt für den Parser.
|
||||||
|
Ermöglicht das Löschen der parser.py Facade.
|
||||||
|
VERSION: 1.10.0
|
||||||
|
"""
|
||||||
|
from .parsing_models import ParsedNote, NoteContext
|
||||||
|
from .parsing_utils import (
|
||||||
|
FRONTMATTER_RE, validate_required_frontmatter,
|
||||||
|
normalize_frontmatter, extract_wikilinks, extract_edges_with_context
|
||||||
|
)
|
||||||
|
from .parsing_markdown import read_markdown
|
||||||
|
from .parsing_scanner import pre_scan_markdown
|
||||||
|
|
||||||
|
# Kompatibilitäts-Alias
|
||||||
|
FRONTMATTER_END = FRONTMATTER_RE
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"ParsedNote", "NoteContext", "FRONTMATTER_RE", "FRONTMATTER_END",
|
||||||
|
"read_markdown", "pre_scan_markdown", "validate_required_frontmatter",
|
||||||
|
"normalize_frontmatter", "extract_wikilinks", "extract_edges_with_context"
|
||||||
|
]
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/parsing/parsing_markdown.py
|
||||||
|
DESCRIPTION: Fehlertolerantes Einlesen von Markdown und Frontmatter-Splitting.
|
||||||
|
"""
|
||||||
|
import io
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
from typing import Any, Dict, Optional, Tuple
|
||||||
|
from .parsing_models import ParsedNote
|
||||||
|
from .parsing_utils import FRONTMATTER_RE
|
||||||
|
|
||||||
|
try:
|
||||||
|
import yaml
|
||||||
|
except ImportError:
|
||||||
|
yaml = None
|
||||||
|
|
||||||
|
_FALLBACK_ENCODINGS: Tuple[str, ...] = ("utf-8", "utf-8-sig", "cp1252", "latin-1")
|
||||||
|
|
||||||
|
def _split_frontmatter(text: str) -> Tuple[Dict[str, Any], str]:
|
||||||
|
"""Zerlegt Text in Frontmatter-Dict und Body."""
|
||||||
|
lines = text.splitlines(True)
|
||||||
|
if not lines or not FRONTMATTER_RE.match(lines[0]):
|
||||||
|
return {}, text
|
||||||
|
end_idx = None
|
||||||
|
for i in range(1, min(len(lines), 2000)):
|
||||||
|
if FRONTMATTER_RE.match(lines[i]):
|
||||||
|
end_idx = i
|
||||||
|
break
|
||||||
|
if end_idx is None: return {}, text
|
||||||
|
fm_raw = "".join(lines[1:end_idx])
|
||||||
|
body = "".join(lines[end_idx + 1:])
|
||||||
|
if yaml is None: raise RuntimeError("PyYAML not installed.")
|
||||||
|
try:
|
||||||
|
loaded = yaml.safe_load(fm_raw) or {}
|
||||||
|
data = loaded if isinstance(loaded, dict) else {}
|
||||||
|
except Exception as e:
|
||||||
|
print(json.dumps({"warn": "frontmatter_yaml_parse_failed", "error": str(e)}))
|
||||||
|
data = {}
|
||||||
|
if body.startswith("\n"): body = body[1:]
|
||||||
|
return data, body
|
||||||
|
|
||||||
|
def _read_text_with_fallback(path: str) -> Tuple[str, str, bool]:
|
||||||
|
"""Liest Datei mit Encoding-Fallback-Kette."""
|
||||||
|
last_err = None
|
||||||
|
for enc in _FALLBACK_ENCODINGS:
|
||||||
|
try:
|
||||||
|
with io.open(path, "r", encoding=enc, errors="strict") as f:
|
||||||
|
return f.read(), enc, (enc != "utf-8")
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
last_err = str(e); continue
|
||||||
|
with open(path, "rb") as fb:
|
||||||
|
text = fb.read().decode("utf-8", errors="replace")
|
||||||
|
return text, "utf-8(replace)", True
|
||||||
|
|
||||||
|
def read_markdown(path: str) -> Optional[ParsedNote]:
|
||||||
|
"""Öffentliche API zum Einlesen einer Datei."""
|
||||||
|
if not os.path.exists(path): return None
|
||||||
|
text, enc, had_fb = _read_text_with_fallback(path)
|
||||||
|
fm, body = _split_frontmatter(text)
|
||||||
|
return ParsedNote(frontmatter=fm or {}, body=body or "", path=path)
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/parsing/parsing_models.py
|
||||||
|
DESCRIPTION: Datenklassen für das Parsing-System.
|
||||||
|
"""
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ParsedNote:
|
||||||
|
"""Container für eine vollständig eingelesene Markdown-Datei."""
|
||||||
|
frontmatter: Dict[str, Any]
|
||||||
|
body: str
|
||||||
|
path: str
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class NoteContext:
|
||||||
|
"""Metadaten-Container für den flüchtigen LocalBatchCache (Pass 1)."""
|
||||||
|
note_id: str
|
||||||
|
title: str
|
||||||
|
type: str
|
||||||
|
summary: str
|
||||||
|
tags: List[str]
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/parsing/parsing_scanner.py
|
||||||
|
DESCRIPTION: Pre-Scan für den LocalBatchCache (Pass 1).
|
||||||
|
AUDIT v1.1.0: Dynamisierung der Scan-Parameter (WP-14).
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from typing import Optional, Dict, Any
|
||||||
|
from .parsing_models import NoteContext
|
||||||
|
from .parsing_markdown import read_markdown
|
||||||
|
|
||||||
|
def pre_scan_markdown(path: str, registry: Optional[Dict[str, Any]] = None) -> Optional[NoteContext]:
|
||||||
|
"""
|
||||||
|
Extrahiert Identität und Kurz-Kontext zur Validierung.
|
||||||
|
WP-14: Scan-Tiefe und Summary-Länge sind nun über die Registry steuerbar.
|
||||||
|
"""
|
||||||
|
parsed = read_markdown(path)
|
||||||
|
if not parsed: return None
|
||||||
|
|
||||||
|
# WP-14: Konfiguration laden oder Standardwerte nutzen
|
||||||
|
reg = registry or {}
|
||||||
|
summary_cfg = reg.get("summary_settings", {})
|
||||||
|
scan_depth = summary_cfg.get("pre_scan_depth", 600)
|
||||||
|
max_len = summary_cfg.get("max_summary_length", 500)
|
||||||
|
|
||||||
|
fm = parsed.frontmatter
|
||||||
|
# ID-Findung: Frontmatter ID oder Dateiname als Fallback
|
||||||
|
note_id = str(fm.get("id") or os.path.splitext(os.path.basename(path))[0])
|
||||||
|
|
||||||
|
# Erstelle Kurz-Zusammenfassung mit dynamischen Limits
|
||||||
|
clean_body = re.sub(r'[#*`>]', '', parsed.body[:scan_depth]).strip()
|
||||||
|
summary = clean_body[:max_len] + "..." if len(clean_body) > max_len else clean_body
|
||||||
|
|
||||||
|
return NoteContext(
|
||||||
|
note_id=note_id,
|
||||||
|
title=str(fm.get("title", note_id)),
|
||||||
|
type=str(fm.get("type", "concept")),
|
||||||
|
summary=summary,
|
||||||
|
tags=fm.get("tags", []) if isinstance(fm.get("tags"), list) else []
|
||||||
|
)
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/parsing/parsing_utils.py
|
||||||
|
DESCRIPTION: Werkzeuge zur Validierung, Normalisierung und Wikilink-Extraktion.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
from typing import Any, Dict, List, Tuple, Optional
|
||||||
|
from .parsing_models import ParsedNote
|
||||||
|
|
||||||
|
# Öffentliche Konstanten für Abwärtskompatibilität
|
||||||
|
FRONTMATTER_RE = re.compile(r"^\s*---\s*$")
|
||||||
|
_WIKILINK_RE = re.compile(r"\[\[([^\]]+)\]\]")
|
||||||
|
|
||||||
|
def validate_required_frontmatter(fm: Dict[str, Any], required: Tuple[str, ...] = ("id", "title")) -> None:
|
||||||
|
"""Prüft, ob alle Pflichtfelder vorhanden sind."""
|
||||||
|
if fm is None: fm = {}
|
||||||
|
missing = []
|
||||||
|
for k in required:
|
||||||
|
v = fm.get(k)
|
||||||
|
if v is None or (isinstance(v, str) and not v.strip()):
|
||||||
|
missing.append(k)
|
||||||
|
if missing:
|
||||||
|
raise ValueError(f"Missing required frontmatter fields: {', '.join(missing)}")
|
||||||
|
if "tags" in fm and fm["tags"] not in (None, "") and not isinstance(fm["tags"], (list, tuple)):
|
||||||
|
raise ValueError("frontmatter 'tags' must be a list of strings")
|
||||||
|
|
||||||
|
def normalize_frontmatter(fm: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Normalisierung von Tags und Boolean-Feldern."""
|
||||||
|
out = dict(fm or {})
|
||||||
|
if "tags" in out:
|
||||||
|
if isinstance(out["tags"], str):
|
||||||
|
out["tags"] = [out["tags"].strip()] if out["tags"].strip() else []
|
||||||
|
elif isinstance(out["tags"], list):
|
||||||
|
out["tags"] = [str(t).strip() for t in out["tags"] if t is not None]
|
||||||
|
else:
|
||||||
|
out["tags"] = [str(out["tags"]).strip()] if out["tags"] not in (None, "") else []
|
||||||
|
if "embedding_exclude" in out:
|
||||||
|
out["embedding_exclude"] = bool(out["embedding_exclude"])
|
||||||
|
return out
|
||||||
|
|
||||||
|
def extract_wikilinks(text: str) -> List[str]:
|
||||||
|
"""Extrahiert Wikilinks als einfache Liste von IDs."""
|
||||||
|
if not text: return []
|
||||||
|
out: List[str] = []
|
||||||
|
for m in _WIKILINK_RE.finditer(text):
|
||||||
|
raw = (m.group(1) or "").strip()
|
||||||
|
if not raw: continue
|
||||||
|
if "|" in raw: raw = raw.split("|", 1)[0].strip()
|
||||||
|
if "#" in raw: raw = raw.split("#", 1)[0].strip()
|
||||||
|
if raw: out.append(raw)
|
||||||
|
return out
|
||||||
|
|
||||||
|
def extract_edges_with_context(parsed: ParsedNote) -> List[Dict[str, Any]]:
|
||||||
|
"""WP-22: Extrahiert Wikilinks mit Zeilennummern für die EdgeRegistry."""
|
||||||
|
edges = []
|
||||||
|
if not parsed or not parsed.body: return edges
|
||||||
|
lines = parsed.body.splitlines()
|
||||||
|
for line_num, line_content in enumerate(lines, 1):
|
||||||
|
for match in _WIKILINK_RE.finditer(line_content):
|
||||||
|
raw = (match.group(1) or "").strip()
|
||||||
|
if not raw: continue
|
||||||
|
if "|" in raw:
|
||||||
|
parts = raw.split("|", 1)
|
||||||
|
target, kind = parts[0].strip(), parts[1].strip()
|
||||||
|
else:
|
||||||
|
target, kind = raw.strip(), "related_to"
|
||||||
|
if "#" in target: target = target.split("#", 1)[0].strip()
|
||||||
|
if target:
|
||||||
|
edges.append({"to": target, "kind": kind, "line": line_num, "provenance": "explicit"})
|
||||||
|
return edges
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/registry.py
|
||||||
|
DESCRIPTION: Zentraler Base-Layer für Konfigurations-Loading und Text-Bereinigung.
|
||||||
|
Bricht Zirkelbezüge zwischen Ingestion und LLMService auf.
|
||||||
|
VERSION: 1.0.0
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import yaml
|
||||||
|
from typing import Optional, List
|
||||||
|
|
||||||
|
def load_type_registry(custom_path: Optional[str] = None) -> dict:
|
||||||
|
"""Lädt die types.yaml zur Steuerung der typ-spezifischen Logik."""
|
||||||
|
# Wir nutzen hier einen direkten Import von Settings, um Zyklen zu vermeiden
|
||||||
|
from app.config import get_settings
|
||||||
|
settings = get_settings()
|
||||||
|
path = custom_path or settings.MINDNET_TYPES_FILE
|
||||||
|
if not os.path.exists(path):
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
return yaml.safe_load(f) or {}
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def clean_llm_text(text: str, registry: Optional[dict] = None) -> str:
|
||||||
|
"""
|
||||||
|
Entfernt LLM-Steuerzeichen (<s>, [OUT] etc.) aus einem Text.
|
||||||
|
Wird sowohl für JSON-Parsing als auch für Chat-Antworten genutzt.
|
||||||
|
"""
|
||||||
|
if not text or not isinstance(text, str):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
default_patterns = ["<s>", "</s>", "[OUT]", "[/OUT]"]
|
||||||
|
reg = registry or load_type_registry()
|
||||||
|
|
||||||
|
# Lade Patterns aus llm_settings (WP-14)
|
||||||
|
patterns: List[str] = reg.get("llm_settings", {}).get("cleanup_patterns", default_patterns)
|
||||||
|
|
||||||
|
clean = text
|
||||||
|
for p in patterns:
|
||||||
|
clean = clean.replace(p, "")
|
||||||
|
|
||||||
|
return clean.strip()
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
"""
|
||||||
|
PACKAGE: app.core.retrieval
|
||||||
|
DESCRIPTION: Zentrale Schnittstelle für Retrieval-Operationen (Vektor- & Graph-Suche).
|
||||||
|
Bündelt Suche und mathematische Scoring-Engine.
|
||||||
|
"""
|
||||||
|
from .retriever import (
|
||||||
|
Retriever,
|
||||||
|
hybrid_retrieve,
|
||||||
|
semantic_retrieve
|
||||||
|
)
|
||||||
|
|
||||||
|
from .retriever_scoring import (
|
||||||
|
get_weights,
|
||||||
|
compute_wp22_score,
|
||||||
|
get_status_multiplier
|
||||||
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"Retriever",
|
||||||
|
"hybrid_retrieve",
|
||||||
|
"semantic_retrieve",
|
||||||
|
"get_weights",
|
||||||
|
"compute_wp22_score",
|
||||||
|
"get_status_multiplier"
|
||||||
|
]
|
||||||
@@ -0,0 +1,378 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/retrieval/decision_engine.py
|
||||||
|
DESCRIPTION: Der Agentic Orchestrator für MindNet (WP-25b Edition).
|
||||||
|
Realisiert Multi-Stream Retrieval, Intent-basiertes Routing
|
||||||
|
und die neue Lazy-Prompt Orchestrierung (Module A & B).
|
||||||
|
VERSION: 1.3.2 (WP-25b: Full Robustness Recovery & Regex Parsing)
|
||||||
|
STATUS: Active
|
||||||
|
FIX:
|
||||||
|
- WP-25b: ULTRA-Robustes Intent-Parsing via Regex (Fix: 'CODING[/S]' -> 'CODING').
|
||||||
|
- WP-25b: Wiederherstellung der prepend_instruction Logik via variables.
|
||||||
|
- WP-25a: Voller Erhalt der Profil-Kaskade via LLMService v3.5.5.
|
||||||
|
- WP-25: Beibehaltung von Stream-Tracing, Edge-Boosts und Pre-Initialization.
|
||||||
|
- RECOVERY: Wiederherstellung der lokalen Sicherheits-Gates aus v1.2.1.
|
||||||
|
"""
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import yaml
|
||||||
|
import os
|
||||||
|
import re # Neu für robustes Intent-Parsing
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
|
||||||
|
# Core & Service Imports
|
||||||
|
from app.models.dto import QueryRequest, QueryResponse
|
||||||
|
from app.core.retrieval.retriever import Retriever
|
||||||
|
from app.services.llm_service import LLMService
|
||||||
|
from app.config import get_settings
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class DecisionEngine:
|
||||||
|
def __init__(self):
|
||||||
|
"""Initialisiert die Engine und lädt die modularen Konfigurationen."""
|
||||||
|
self.settings = get_settings()
|
||||||
|
self.retriever = Retriever()
|
||||||
|
self.llm_service = LLMService()
|
||||||
|
self.config = self._load_engine_config()
|
||||||
|
|
||||||
|
def _load_engine_config(self) -> Dict[str, Any]:
|
||||||
|
"""Lädt die Multi-Stream Konfiguration (WP-25/25a)."""
|
||||||
|
path = os.getenv("MINDNET_DECISION_CONFIG", "config/decision_engine.yaml")
|
||||||
|
if not os.path.exists(path):
|
||||||
|
logger.error(f"❌ Decision Engine Config not found at {path}")
|
||||||
|
return {"strategies": {}, "streams_library": {}}
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
config = yaml.safe_load(f) or {}
|
||||||
|
|
||||||
|
# WP-25b FIX: Schema-Validierung
|
||||||
|
required_keys = ["strategies", "streams_library"]
|
||||||
|
missing = [k for k in required_keys if k not in config]
|
||||||
|
if missing:
|
||||||
|
logger.error(f"❌ Missing required keys in decision_engine.yaml: {missing}")
|
||||||
|
return {"strategies": {}, "streams_library": {}}
|
||||||
|
|
||||||
|
# Warnung bei unbekannten Top-Level-Keys
|
||||||
|
known_keys = {"version", "settings", "strategies", "streams_library"}
|
||||||
|
unknown = set(config.keys()) - known_keys
|
||||||
|
if unknown:
|
||||||
|
logger.warning(f"⚠️ Unknown keys in decision_engine.yaml: {unknown}")
|
||||||
|
|
||||||
|
logger.info(f"⚙️ Decision Engine Config loaded (v{config.get('version', 'unknown')})")
|
||||||
|
return config
|
||||||
|
except yaml.YAMLError as e:
|
||||||
|
logger.error(f"❌ YAML syntax error in decision_engine.yaml: {e}")
|
||||||
|
return {"strategies": {}, "streams_library": {}}
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Failed to load decision_engine.yaml: {e}")
|
||||||
|
return {"strategies": {}, "streams_library": {}}
|
||||||
|
|
||||||
|
async def ask(self, query: str) -> str:
|
||||||
|
"""
|
||||||
|
Hauptmethode des MindNet Chats.
|
||||||
|
Orchestriert den agentischen Prozess: Routing -> Retrieval -> Kompression -> Synthese.
|
||||||
|
"""
|
||||||
|
# 1. Intent Recognition (Strategy Routing)
|
||||||
|
strategy_key = await self._determine_strategy(query)
|
||||||
|
|
||||||
|
strategies = self.config.get("strategies", {})
|
||||||
|
strategy = strategies.get(strategy_key)
|
||||||
|
|
||||||
|
if not strategy:
|
||||||
|
logger.warning(f"⚠️ Unknown strategy '{strategy_key}'. Fallback to FACT_WHAT.")
|
||||||
|
strategy_key = "FACT_WHAT"
|
||||||
|
strategy = strategies.get("FACT_WHAT")
|
||||||
|
|
||||||
|
if not strategy and strategies:
|
||||||
|
strategy_key = next(iter(strategies))
|
||||||
|
strategy = strategies[strategy_key]
|
||||||
|
|
||||||
|
if not strategy:
|
||||||
|
return "Entschuldigung, meine Wissensbasis ist aktuell nicht konfiguriert."
|
||||||
|
|
||||||
|
# 2. Multi-Stream Retrieval & Pre-Synthesis (Parallel Tasks inkl. Kompression)
|
||||||
|
stream_results = await self._execute_parallel_streams(strategy, query)
|
||||||
|
|
||||||
|
# 3. Finale Synthese
|
||||||
|
return await self._generate_final_answer(strategy_key, strategy, query, stream_results)
|
||||||
|
|
||||||
|
async def _determine_strategy(self, query: str) -> str:
|
||||||
|
"""WP-25b: Nutzt den LLM-Router via Lazy-Loading und bereinigt Modell-Artefakte via Regex."""
|
||||||
|
settings_cfg = self.config.get("settings", {})
|
||||||
|
prompt_key = settings_cfg.get("router_prompt_key", "intent_router_v1")
|
||||||
|
router_profile = settings_cfg.get("router_profile")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Delegation an LLMService ohne manuelle Vor-Formatierung
|
||||||
|
response = await self.llm_service.generate_raw_response(
|
||||||
|
prompt_key=prompt_key,
|
||||||
|
variables={"query": query},
|
||||||
|
max_retries=1,
|
||||||
|
priority="realtime",
|
||||||
|
profile_name=router_profile
|
||||||
|
)
|
||||||
|
|
||||||
|
# --- ULTRA-ROBUST PARSING (Fix für 'CODING[/S]') ---
|
||||||
|
# 1. Alles in Großbuchstaben umwandeln
|
||||||
|
raw_text = str(response).upper()
|
||||||
|
|
||||||
|
# 2. Regex: Suche das erste Wort, das nur aus A-Z und Unterstrichen besteht
|
||||||
|
# Dies ignoriert [/S], </s>, Newlines oder Plaudereien des Modells
|
||||||
|
match = re.search(r'\b(FACT_WHEN|FACT_WHAT|DECISION|EMPATHY|CODING|INTERVIEW)\b', raw_text)
|
||||||
|
|
||||||
|
if match:
|
||||||
|
intent = match.group(1)
|
||||||
|
logger.info(f"🎯 [ROUTING] Parsed Intent: '{intent}' from raw response: '{response.strip()}'")
|
||||||
|
return intent
|
||||||
|
|
||||||
|
# Fallback, falls Regex nicht greift
|
||||||
|
logger.warning(f"⚠️ Unmapped intent '{response.strip()}' from router. Falling back to FACT_WHAT.")
|
||||||
|
return "FACT_WHAT"
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Strategy Routing failed: {e}")
|
||||||
|
return "FACT_WHAT"
|
||||||
|
|
||||||
|
async def _execute_parallel_streams(self, strategy: Dict, query: str) -> Dict[str, str]:
|
||||||
|
"""Führt Such-Streams aus und komprimiert überlange Ergebnisse (Pre-Synthesis)."""
|
||||||
|
stream_keys = strategy.get("use_streams", [])
|
||||||
|
library = self.config.get("streams_library", {})
|
||||||
|
|
||||||
|
# Phase 1: Retrieval Tasks starten
|
||||||
|
retrieval_tasks = []
|
||||||
|
active_streams = []
|
||||||
|
for key in stream_keys:
|
||||||
|
stream_cfg = library.get(key)
|
||||||
|
if stream_cfg:
|
||||||
|
active_streams.append(key)
|
||||||
|
retrieval_tasks.append(self._run_single_stream(key, stream_cfg, query))
|
||||||
|
|
||||||
|
# Ergebnisse sammeln
|
||||||
|
retrieval_results = await asyncio.gather(*retrieval_tasks, return_exceptions=True)
|
||||||
|
|
||||||
|
# Phase 2: Formatierung und optionale Kompression
|
||||||
|
# WP-24c v4.5.5: Context-Reuse - Sicherstellen, dass formatted_context auch bei Kompressions-Fehlern erhalten bleibt
|
||||||
|
final_stream_tasks = []
|
||||||
|
formatted_contexts = {} # WP-24c v4.5.5: Persistenz für Fallback-Zugriff
|
||||||
|
|
||||||
|
for name, res in zip(active_streams, retrieval_results):
|
||||||
|
if isinstance(res, Exception):
|
||||||
|
logger.error(f"Stream '{name}' failed during retrieval: {res}")
|
||||||
|
error_msg = f"[Fehler im Wissens-Stream {name}]"
|
||||||
|
formatted_contexts[name] = error_msg
|
||||||
|
async def _err(msg=error_msg): return msg
|
||||||
|
final_stream_tasks.append(_err())
|
||||||
|
continue
|
||||||
|
|
||||||
|
formatted_context = self._format_stream_context(res)
|
||||||
|
formatted_contexts[name] = formatted_context # WP-24c v4.5.5: Persistenz für Fallback
|
||||||
|
|
||||||
|
# WP-25a: Kompressions-Check (Inhaltsverdichtung)
|
||||||
|
stream_cfg = library.get(name, {})
|
||||||
|
threshold = stream_cfg.get("compression_threshold", 4000)
|
||||||
|
|
||||||
|
if len(formatted_context) > threshold:
|
||||||
|
logger.info(f"⚙️ [WP-25b] Triggering Lazy-Compression for stream '{name}'...")
|
||||||
|
comp_profile = stream_cfg.get("compression_profile")
|
||||||
|
# WP-24c v4.5.5: Kompression mit Context-Reuse - bei Fehler wird formatted_context zurückgegeben
|
||||||
|
final_stream_tasks.append(
|
||||||
|
self._compress_stream_content(name, formatted_context, query, comp_profile)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
async def _direct(c=formatted_context): return c
|
||||||
|
final_stream_tasks.append(_direct())
|
||||||
|
|
||||||
|
# Finale Inhalte parallel fertigstellen
|
||||||
|
# WP-24c v4.5.5: Bei Kompressions-Fehlern wird der Original-Content zurückgegeben (siehe _compress_stream_content)
|
||||||
|
final_contents = await asyncio.gather(*final_stream_tasks, return_exceptions=True)
|
||||||
|
|
||||||
|
# WP-24c v4.5.5: Exception-Handling für finale Inhalte - verwende Original-Content bei Fehlern
|
||||||
|
final_results = {}
|
||||||
|
for name, content in zip(active_streams, final_contents):
|
||||||
|
if isinstance(content, Exception):
|
||||||
|
logger.warning(f"⚠️ [CONTEXT-REUSE] Stream '{name}' Fehler in finaler Verarbeitung: {content}. Verwende Original-Context.")
|
||||||
|
final_results[name] = formatted_contexts.get(name, f"[Fehler im Stream {name}]")
|
||||||
|
else:
|
||||||
|
final_results[name] = content
|
||||||
|
|
||||||
|
logger.debug(f"📊 [STREAMS] Finale Stream-Ergebnisse: {[(k, len(v)) for k, v in final_results.items()]}")
|
||||||
|
return final_results
|
||||||
|
|
||||||
|
async def _compress_stream_content(self, stream_name: str, content: str, query: str, profile: Optional[str]) -> str:
|
||||||
|
"""
|
||||||
|
WP-25b: Inhaltsverdichtung via Lazy-Loading 'compression_template'.
|
||||||
|
WP-24c v4.5.5: Context-Reuse - Bei Fehlern wird der Original-Content zurückgegeben,
|
||||||
|
um Re-Retrieval zu vermeiden.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# WP-24c v4.5.5: Logging für LLM-Trace im Kompressions-Modus
|
||||||
|
logger.debug(f"🔧 [COMPRESSION] Starte Kompression für Stream '{stream_name}' (Content-Länge: {len(content)})")
|
||||||
|
|
||||||
|
summary = await self.llm_service.generate_raw_response(
|
||||||
|
prompt_key="compression_template",
|
||||||
|
variables={
|
||||||
|
"stream_name": stream_name,
|
||||||
|
"content": content,
|
||||||
|
"query": query
|
||||||
|
},
|
||||||
|
profile_name=profile,
|
||||||
|
priority="background",
|
||||||
|
max_retries=1
|
||||||
|
)
|
||||||
|
|
||||||
|
# WP-24c v4.5.5: Validierung des Kompressions-Ergebnisses
|
||||||
|
if summary and len(summary.strip()) > 10:
|
||||||
|
logger.debug(f"✅ [COMPRESSION] Kompression erfolgreich für '{stream_name}' (Original: {len(content)}, Komprimiert: {len(summary)})")
|
||||||
|
return summary.strip()
|
||||||
|
else:
|
||||||
|
logger.warning(f"⚠️ [COMPRESSION] Kompressions-Ergebnis zu kurz für '{stream_name}', verwende Original-Content")
|
||||||
|
return content
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
# WP-24c v4.5.5: Context-Reuse - Bei Fehlern Original-Content zurückgeben (kein Re-Retrieval)
|
||||||
|
logger.error(f"❌ [COMPRESSION] Kompression von '{stream_name}' fehlgeschlagen: {e}")
|
||||||
|
logger.info(f"🔄 [CONTEXT-REUSE] Verwende Original-Content für '{stream_name}' (Länge: {len(content)}) - KEIN Re-Retrieval")
|
||||||
|
return content
|
||||||
|
|
||||||
|
async def _run_single_stream(self, name: str, cfg: Dict, query: str) -> QueryResponse:
|
||||||
|
"""Spezialisierte Graph-Suche mit Stream-Tracing und Edge-Boosts."""
|
||||||
|
transformed_query = cfg.get("query_template", "{query}").format(query=query)
|
||||||
|
|
||||||
|
request = QueryRequest(
|
||||||
|
query=transformed_query,
|
||||||
|
top_k=cfg.get("top_k", 5),
|
||||||
|
filters={"type": cfg.get("filter_types", [])},
|
||||||
|
expand={"depth": 1},
|
||||||
|
boost_edges=cfg.get("edge_boosts", {}), # Erhalt der Gewichtung
|
||||||
|
explain=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Protokollierung vor der Suche
|
||||||
|
logger.info(f"🔍 [RETRIEVAL] Starte Stream: '{name}'")
|
||||||
|
logger.info(f" -> Transformierte Query: '{transformed_query}'")
|
||||||
|
logger.debug(f" ⚙️ [FILTER] Angewandte Metadaten-Filter: {request.filters}")
|
||||||
|
logger.debug(f" ⚙️ [FILTER] Top-K: {request.top_k}, Expand-Depth: {request.expand.get('depth') if request.expand else None}")
|
||||||
|
|
||||||
|
response = await self.retriever.search(request)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Protokollierung nach der Suche
|
||||||
|
if not response.results:
|
||||||
|
logger.warning(f"⚠️ [EMPTY] Stream '{name}' lieferte 0 Ergebnisse.")
|
||||||
|
else:
|
||||||
|
logger.info(f"✨ [SUCCESS] Stream '{name}' lieferte {len(response.results)} Treffer.")
|
||||||
|
# Top 3 Treffer im DEBUG-Level loggen
|
||||||
|
# WP-24c v4.5.4: QueryHit hat kein chunk_id Feld - verwende node_id (enthält die Chunk-ID)
|
||||||
|
for i, hit in enumerate(response.results[:3]):
|
||||||
|
chunk_id = hit.node_id # node_id ist die Chunk-ID (pid)
|
||||||
|
score = hit.total_score # QueryHit hat total_score, nicht score
|
||||||
|
logger.debug(f" [{i+1}] Chunk: {chunk_id} | Score: {score:.4f} | Path: {hit.source.get('path', 'N/A') if hit.source else 'N/A'}")
|
||||||
|
|
||||||
|
for hit in response.results:
|
||||||
|
hit.stream_origin = name
|
||||||
|
return response
|
||||||
|
|
||||||
|
def _format_stream_context(self, response: QueryResponse) -> str:
|
||||||
|
"""Wandelt QueryHits in einen formatierten Kontext-String um."""
|
||||||
|
if not response.results:
|
||||||
|
return "Keine spezifischen Informationen gefunden."
|
||||||
|
lines = []
|
||||||
|
for i, hit in enumerate(response.results, 1):
|
||||||
|
source = hit.source.get("path", "Unbekannt")
|
||||||
|
content = hit.source.get("text", "").strip()
|
||||||
|
lines.append(f"[{i}] QUELLE: {source}\nINHALT: {content}")
|
||||||
|
return "\n\n".join(lines)
|
||||||
|
|
||||||
|
async def _generate_final_answer(
|
||||||
|
self,
|
||||||
|
strategy_key: str,
|
||||||
|
strategy: Dict,
|
||||||
|
query: str,
|
||||||
|
stream_results: Dict[str, str]
|
||||||
|
) -> str:
|
||||||
|
"""WP-25b: Finale Synthese via Lazy-Prompt mit Robustheit aus v1.2.1."""
|
||||||
|
profile = strategy.get("llm_profile")
|
||||||
|
template_key = strategy.get("prompt_template", "fact_synthesis_v1")
|
||||||
|
system_prompt = self.llm_service.get_prompt("system_prompt")
|
||||||
|
|
||||||
|
# WP-25 ROBUSTNESS: Pre-Initialization der Variablen
|
||||||
|
all_possible_streams = ["values_stream", "facts_stream", "biography_stream", "risk_stream", "tech_stream"]
|
||||||
|
template_vars = {s: "" for s in all_possible_streams}
|
||||||
|
template_vars.update(stream_results)
|
||||||
|
template_vars["query"] = query
|
||||||
|
|
||||||
|
# WP-25a Erhalt: Prepend Instructions aus der strategy_config
|
||||||
|
prepend = strategy.get("prepend_instruction", "")
|
||||||
|
template_vars["prepend_instruction"] = prepend
|
||||||
|
|
||||||
|
try:
|
||||||
|
# WP-25b: Delegation der Synthese an den LLMService
|
||||||
|
response = await self.llm_service.generate_raw_response(
|
||||||
|
prompt_key=template_key,
|
||||||
|
variables=template_vars,
|
||||||
|
system=system_prompt,
|
||||||
|
profile_name=profile,
|
||||||
|
priority="realtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
# WP-25a RECOVERY: Falls dieprepend_instruction nicht im Template-Key
|
||||||
|
# der prompts.yaml enthalten ist (WP-25b Lazy Loading), fügen wir sie
|
||||||
|
# hier manuell an den Anfang, um die Logik aus v1.2.1 zu bewahren.
|
||||||
|
if prepend and prepend not in response[:len(prepend)+50]:
|
||||||
|
logger.info("ℹ️ Adding prepend_instruction manually (not found in response).")
|
||||||
|
response = f"{prepend}\n\n{response}"
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Final Synthesis failed: {e}")
|
||||||
|
# WP-24c v4.5.5: ROBUST FALLBACK mit Context-Reuse
|
||||||
|
# WICHTIG: stream_results werden Wiederverwendet - KEIN Re-Retrieval
|
||||||
|
logger.info(f"🔄 [FALLBACK] Verwende vorhandene stream_results (KEIN Re-Retrieval)")
|
||||||
|
logger.debug(f" -> Verfügbare Streams: {list(stream_results.keys())}")
|
||||||
|
logger.debug(f" -> Stream-Längen: {[(k, len(v)) for k, v in stream_results.items()]}")
|
||||||
|
|
||||||
|
# WP-24c v4.5.5: Context-Reuse - Nutze vorhandene stream_results
|
||||||
|
fallback_context = "\n\n".join([v for v in stream_results.values() if len(v) > 20])
|
||||||
|
|
||||||
|
if not fallback_context or len(fallback_context.strip()) < 20:
|
||||||
|
logger.warning(f"⚠️ [FALLBACK] Fallback-Context zu kurz ({len(fallback_context)} Zeichen). Stream-Ergebnisse möglicherweise leer.")
|
||||||
|
return f"Entschuldigung, ich konnte keine relevanten Informationen zu Ihrer Anfrage finden. (Fehler: {str(e)})"
|
||||||
|
|
||||||
|
try:
|
||||||
|
# WP-24c v4.5.5: Fallback-Synthese mit LLM-Trace-Logging
|
||||||
|
logger.info(f"🔄 [FALLBACK] Starte Fallback-Synthese mit vorhandenem Context (Länge: {len(fallback_context)})")
|
||||||
|
logger.debug(f" -> Fallback-Profile: {profile}, Template: fallback_synthesis")
|
||||||
|
|
||||||
|
result = await self.llm_service.generate_raw_response(
|
||||||
|
prompt_key="fallback_synthesis",
|
||||||
|
variables={"query": query, "context": fallback_context},
|
||||||
|
system=system_prompt, priority="realtime", profile_name=profile
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(f"✅ [FALLBACK] Fallback-Synthese erfolgreich (Antwort-Länge: {len(result) if result else 0})")
|
||||||
|
return result
|
||||||
|
|
||||||
|
except (ValueError, KeyError) as template_error:
|
||||||
|
# WP-24c v4.5.9: Fallback auf generisches Template mit variables
|
||||||
|
# Nutzt Lazy-Loading aus WP-25b für modell-spezifische Fallback-Prompts
|
||||||
|
logger.warning(f"⚠️ [FALLBACK] Template 'fallback_synthesis' nicht gefunden: {template_error}. Versuche generisches Template.")
|
||||||
|
logger.debug(f" -> Fallback-Profile: {profile}, Context-Länge: {len(fallback_context)}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# WP-24c v4.5.9: Versuche generisches Template mit variables (Lazy-Loading)
|
||||||
|
result = await self.llm_service.generate_raw_response(
|
||||||
|
prompt_key="fallback_synthesis_generic", # Fallback-Template
|
||||||
|
variables={"query": query, "context": fallback_context},
|
||||||
|
system=system_prompt, priority="realtime", profile_name=profile
|
||||||
|
)
|
||||||
|
logger.info(f"✅ [FALLBACK] Generisches Template erfolgreich (Antwort-Länge: {len(result) if result else 0})")
|
||||||
|
return result
|
||||||
|
except (ValueError, KeyError) as fallback_error:
|
||||||
|
# WP-24c v4.5.9: Letzter Fallback - direkter Prompt (nur wenn beide Templates fehlen)
|
||||||
|
logger.error(f"❌ [FALLBACK] Auch generisches Template nicht gefunden: {fallback_error}. Verwende direkten Prompt als letzten Fallback.")
|
||||||
|
result = await self.llm_service.generate_raw_response(
|
||||||
|
prompt=f"Beantworte: {query}\n\nKontext:\n{fallback_context}",
|
||||||
|
system=system_prompt, priority="realtime", profile_name=profile
|
||||||
|
)
|
||||||
|
logger.info(f"✅ [FALLBACK] Direkter Prompt erfolgreich (Antwort-Länge: {len(result) if result else 0})")
|
||||||
|
return result
|
||||||
@@ -0,0 +1,587 @@
|
|||||||
|
"""
|
||||||
|
FILE: app/core/retrieval/retriever.py
|
||||||
|
DESCRIPTION: Haupt-Schnittstelle für die Suche. Orchestriert Vektorsuche und Graph-Expansion.
|
||||||
|
WP-15c Update: Note-Level Diversity Pooling & Super-Edge Aggregation.
|
||||||
|
WP-24c v4.1.0: Gold-Standard - Scope-Awareness, Section-Filtering, Authority-Priorisierung.
|
||||||
|
VERSION: 0.8.0 (WP-24c: Gold-Standard v4.1.0)
|
||||||
|
STATUS: Active
|
||||||
|
DEPENDENCIES: app.config, app.models.dto, app.core.database*, app.core.graph_adapter
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
from typing import Any, Dict, List, Tuple, Iterable, Optional
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
from app.config import get_settings
|
||||||
|
from app.models.dto import (
|
||||||
|
QueryRequest, QueryResponse, QueryHit,
|
||||||
|
Explanation, ScoreBreakdown, Reason, EdgeDTO
|
||||||
|
)
|
||||||
|
|
||||||
|
# MODULARISIERUNG: Neue Import-Pfade für die Datenbank-Ebene
|
||||||
|
import app.core.database.qdrant as qdr
|
||||||
|
import app.core.database.qdrant_points as qp
|
||||||
|
|
||||||
|
import app.services.embeddings_client as ec
|
||||||
|
import app.core.graph.graph_subgraph as ga
|
||||||
|
import app.core.graph.graph_db_adapter as gdb
|
||||||
|
from app.core.graph.graph_utils import PROVENANCE_PRIORITY
|
||||||
|
from qdrant_client.http import models as rest
|
||||||
|
|
||||||
|
# Mathematische Engine importieren
|
||||||
|
from app.core.retrieval.retriever_scoring import get_weights, compute_wp22_score
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 1. CORE HELPERS & CONFIG LOADERS
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
def _get_client_and_prefix() -> Tuple[Any, str]:
|
||||||
|
"""Initialisiert Qdrant Client und lädt Collection-Prefix via database-Paket."""
|
||||||
|
cfg = qdr.QdrantConfig.from_env()
|
||||||
|
return qdr.get_client(cfg), cfg.prefix
|
||||||
|
|
||||||
|
|
||||||
|
def _get_query_vector(req: QueryRequest) -> List[float]:
|
||||||
|
"""
|
||||||
|
Vektorisiert die Anfrage.
|
||||||
|
FIX: Enthält try-except Block für unterschiedliche Signaturen von ec.embed_text.
|
||||||
|
"""
|
||||||
|
if req.query_vector:
|
||||||
|
return list(req.query_vector)
|
||||||
|
if not req.query:
|
||||||
|
raise ValueError("Kein Text oder Vektor für die Suche angegeben.")
|
||||||
|
|
||||||
|
settings = get_settings()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Versuch mit modernem Interface (WP-03 kompatibel)
|
||||||
|
return ec.embed_text(req.query, model_name=settings.MODEL_NAME)
|
||||||
|
except TypeError:
|
||||||
|
# Fallback für Signaturen, die 'model_name' nicht als Keyword akzeptieren
|
||||||
|
logger.debug("ec.embed_text does not accept 'model_name' keyword. Falling back.")
|
||||||
|
return ec.embed_text(req.query)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_chunk_ids_for_notes(
|
||||||
|
client: Any,
|
||||||
|
prefix: str,
|
||||||
|
note_ids: List[str]
|
||||||
|
) -> List[str]:
|
||||||
|
"""
|
||||||
|
WP-24c v4.1.0: Lädt alle Chunk-IDs für gegebene Note-IDs.
|
||||||
|
Wird für Scope-Aware Edge Retrieval benötigt.
|
||||||
|
"""
|
||||||
|
if not note_ids:
|
||||||
|
return []
|
||||||
|
|
||||||
|
_, chunks_col, _ = qp._names(prefix)
|
||||||
|
chunk_ids = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Filter: note_id IN note_ids
|
||||||
|
note_filter = rest.Filter(should=[
|
||||||
|
rest.FieldCondition(key="note_id", match=rest.MatchValue(value=str(nid)))
|
||||||
|
for nid in note_ids
|
||||||
|
])
|
||||||
|
|
||||||
|
pts, _ = client.scroll(
|
||||||
|
collection_name=chunks_col,
|
||||||
|
scroll_filter=note_filter,
|
||||||
|
limit=2048,
|
||||||
|
with_payload=True,
|
||||||
|
with_vectors=False
|
||||||
|
)
|
||||||
|
|
||||||
|
for pt in pts:
|
||||||
|
pl = pt.payload or {}
|
||||||
|
cid = pl.get("chunk_id")
|
||||||
|
if cid:
|
||||||
|
chunk_ids.append(str(cid))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to load chunk IDs for notes: {e}")
|
||||||
|
|
||||||
|
return chunk_ids
|
||||||
|
|
||||||
|
def _semantic_hits(
|
||||||
|
client: Any,
|
||||||
|
prefix: str,
|
||||||
|
vector: List[float],
|
||||||
|
top_k: int,
|
||||||
|
filters: Optional[Dict] = None,
|
||||||
|
target_section: Optional[str] = None
|
||||||
|
) -> List[Tuple[str, float, Dict[str, Any]]]:
|
||||||
|
"""
|
||||||
|
Führt die Vektorsuche via database-Points-Modul durch.
|
||||||
|
WP-24c v4.1.0: Unterstützt optionales Section-Filtering.
|
||||||
|
"""
|
||||||
|
# WP-24c v4.1.0: Section-Filtering für präzise Section-Links
|
||||||
|
if target_section and filters:
|
||||||
|
filters = {**filters, "section": target_section}
|
||||||
|
elif target_section:
|
||||||
|
filters = {"section": target_section}
|
||||||
|
|
||||||
|
raw_hits = qp.search_chunks_by_vector(client, prefix, vector, top=top_k, filters=filters)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Protokollierung der rohen Qdrant-Antwort
|
||||||
|
logger.debug(f"📊 [RAW-HITS] Qdrant lieferte {len(raw_hits)} Roh-Treffer (Top-K: {top_k})")
|
||||||
|
if filters:
|
||||||
|
logger.debug(f" ⚙️ [FILTER] Angewandte Filter: {filters}")
|
||||||
|
|
||||||
|
# Logge die Top 3 Roh-Scores für Diagnose
|
||||||
|
for i, hit in enumerate(raw_hits[:3]):
|
||||||
|
hit_id = str(hit[0]) if hit else "N/A"
|
||||||
|
hit_score = float(hit[1]) if hit and len(hit) > 1 else 0.0
|
||||||
|
hit_payload = dict(hit[2] or {}) if hit and len(hit) > 2 else {}
|
||||||
|
hit_path = hit_payload.get('path', 'N/A')
|
||||||
|
logger.debug(f" [{i+1}] ID: {hit_id} | Raw-Score: {hit_score:.4f} | Path: {hit_path}")
|
||||||
|
|
||||||
|
# Strikte Typkonvertierung für Stabilität
|
||||||
|
return [(str(hit[0]), float(hit[1]), dict(hit[2] or {})) for hit in raw_hits]
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 2. EXPLANATION LAYER (DEBUG & VERIFIABILITY)
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
def _build_explanation(
|
||||||
|
semantic_score: float,
|
||||||
|
payload: Dict[str, Any],
|
||||||
|
scoring_debug: Dict[str, Any],
|
||||||
|
subgraph: Optional[ga.Subgraph],
|
||||||
|
target_note_id: Optional[str],
|
||||||
|
applied_boosts: Optional[Dict[str, float]] = None
|
||||||
|
) -> Explanation:
|
||||||
|
"""
|
||||||
|
Transformiert mathematische Scores und Graph-Signale in eine menschenlesbare Erklärung.
|
||||||
|
"""
|
||||||
|
_, edge_w_cfg, _ = get_weights()
|
||||||
|
base_val = scoring_debug["base_val"]
|
||||||
|
|
||||||
|
# 1. Detaillierter mathematischer Breakdown
|
||||||
|
breakdown = ScoreBreakdown(
|
||||||
|
semantic_contribution=base_val,
|
||||||
|
edge_contribution=base_val * scoring_debug["edge_impact_final"],
|
||||||
|
centrality_contribution=base_val * scoring_debug["cent_impact_final"],
|
||||||
|
raw_semantic=semantic_score,
|
||||||
|
raw_edge_bonus=scoring_debug["edge_bonus"],
|
||||||
|
raw_centrality=scoring_debug["cent_bonus"],
|
||||||
|
node_weight=float(payload.get("retriever_weight", 1.0)),
|
||||||
|
status_multiplier=scoring_debug["status_multiplier"],
|
||||||
|
graph_boost_factor=scoring_debug["graph_boost_factor"]
|
||||||
|
)
|
||||||
|
|
||||||
|
reasons: List[Reason] = []
|
||||||
|
edges_dto: List[EdgeDTO] = []
|
||||||
|
|
||||||
|
# 2. Gründe für Semantik hinzufügen
|
||||||
|
if semantic_score > 0.85:
|
||||||
|
reasons.append(Reason(kind="semantic", message="Sehr hohe textuelle Übereinstimmung.", score_impact=base_val))
|
||||||
|
elif semantic_score > 0.70:
|
||||||
|
reasons.append(Reason(kind="semantic", message="Inhaltliche Übereinstimmung.", score_impact=base_val))
|
||||||
|
|
||||||
|
# 3. Gründe für Typ und Lifecycle (WP-25 Vorbereitung)
|
||||||
|
type_weight = float(payload.get("retriever_weight", 1.0))
|
||||||
|
if type_weight != 1.0:
|
||||||
|
msg = "Bevorzugt" if type_weight > 1.0 else "De-priorisiert"
|
||||||
|
reasons.append(Reason(kind="type", message=f"{msg} durch Typ-Profil.", score_impact=base_val * (type_weight - 1.0)))
|
||||||
|
|
||||||
|
# NEU: Explizite Ausweisung des Lifecycle-Status (WP-22)
|
||||||
|
status_mult = scoring_debug.get("status_multiplier", 1.0)
|
||||||
|
if status_mult != 1.0:
|
||||||
|
status_msg = "Belohnt (Stable)" if status_mult > 1.0 else "De-priorisiert (Draft)"
|
||||||
|
reasons.append(Reason(
|
||||||
|
kind="status",
|
||||||
|
message=f"{status_msg} durch Content-Lifecycle.",
|
||||||
|
score_impact=semantic_score * (status_mult - 1.0)
|
||||||
|
))
|
||||||
|
|
||||||
|
# 4. Kanten-Verarbeitung (Graph-Intelligence)
|
||||||
|
if subgraph and target_note_id and scoring_debug["edge_bonus"] > 0:
|
||||||
|
raw_edges = []
|
||||||
|
if hasattr(subgraph, "get_incoming_edges"):
|
||||||
|
raw_edges.extend(subgraph.get_incoming_edges(target_note_id) or [])
|
||||||
|
if hasattr(subgraph, "get_outgoing_edges"):
|
||||||
|
raw_edges.extend(subgraph.get_outgoing_edges(target_note_id) or [])
|
||||||
|
|
||||||
|
for edge in raw_edges:
|
||||||
|
src = str(edge.get("source") or "note_root")
|
||||||
|
tgt = str(edge.get("target") or target_note_id or "unknown_target")
|
||||||
|
kind = str(edge.get("kind", "related_to"))
|
||||||
|
prov = str(edge.get("provenance", "rule"))
|
||||||
|
conf = float(edge.get("confidence", 1.0))
|
||||||
|
|
||||||
|
direction = "in" if tgt == target_note_id else "out"
|
||||||
|
|
||||||
|
# WP-24c v4.5.10: Robuste EdgeDTO-Erstellung mit Fehlerbehandlung
|
||||||
|
# Falls Provenance-Wert nicht unterstützt wird, verwende Fallback
|
||||||
|
try:
|
||||||
|
edge_obj = EdgeDTO(
|
||||||
|
id=f"{src}->{tgt}:{kind}",
|
||||||
|
kind=kind,
|
||||||
|
source=src,
|
||||||
|
target=tgt,
|
||||||
|
weight=conf,
|
||||||
|
direction=direction,
|
||||||
|
provenance=prov,
|
||||||
|
confidence=conf
|
||||||
|
)
|
||||||
|
edges_dto.append(edge_obj)
|
||||||
|
except Exception as e:
|
||||||
|
# WP-24c v4.5.10: Fallback bei Validierungsfehler (z.B. alte EdgeDTO-Version im Cache)
|
||||||
|
logger.warning(
|
||||||
|
f"⚠️ [EDGE-DTO] Provenance '{prov}' nicht unterstützt für Edge {src}->{tgt} ({kind}). "
|
||||||
|
f"Fehler: {e}. Verwende Fallback 'explicit'."
|
||||||
|
)
|
||||||
|
# Fallback: Verwende 'explicit' als sicheren Default
|
||||||
|
try:
|
||||||
|
edge_obj = EdgeDTO(
|
||||||
|
id=f"{src}->{tgt}:{kind}",
|
||||||
|
kind=kind,
|
||||||
|
source=src,
|
||||||
|
target=tgt,
|
||||||
|
weight=conf,
|
||||||
|
direction=direction,
|
||||||
|
provenance="explicit", # Fallback
|
||||||
|
confidence=conf
|
||||||
|
)
|
||||||
|
edges_dto.append(edge_obj)
|
||||||
|
except Exception as e2:
|
||||||
|
logger.error(f"❌ [EDGE-DTO] Auch Fallback fehlgeschlagen: {e2}. Überspringe Edge.")
|
||||||
|
# Überspringe diese Kante - besser als kompletter Fehler
|
||||||
|
|
||||||
|
# Die 3 wichtigsten Kanten als Begründung formulieren
|
||||||
|
top_edges = sorted(edges_dto, key=lambda e: e.confidence, reverse=True)
|
||||||
|
for e in top_edges[:3]:
|
||||||
|
peer = e.source if e.direction == "in" else e.target
|
||||||
|
# WP-24c v4.5.3: Unterstütze alle explicit-Varianten (explicit, explicit:callout, etc.)
|
||||||
|
prov_txt = "Bestätigte" if e.provenance and e.provenance.startswith("explicit") else "KI-basierte"
|
||||||
|
boost_txt = f" [Boost x{applied_boosts.get(e.kind)}]" if applied_boosts and e.kind in applied_boosts else ""
|
||||||
|
|
||||||
|
reasons.append(Reason(
|
||||||
|
kind="edge",
|
||||||
|
message=f"{prov_txt} Kante '{e.kind}'{boost_txt} von/zu '{peer}'.",
|
||||||
|
score_impact=edge_w_cfg * e.confidence
|
||||||
|
))
|
||||||
|
|
||||||
|
if scoring_debug["cent_bonus"] > 0.01:
|
||||||
|
reasons.append(Reason(kind="centrality", message="Die Notiz ist ein zentraler Informations-Hub.", score_impact=breakdown.centrality_contribution))
|
||||||
|
|
||||||
|
return Explanation(
|
||||||
|
breakdown=breakdown,
|
||||||
|
reasons=reasons,
|
||||||
|
related_edges=edges_dto if edges_dto else None,
|
||||||
|
applied_boosts=applied_boosts
|
||||||
|
)
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 3. CORE RETRIEVAL PIPELINE
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
def _build_hits_from_semantic(
|
||||||
|
hits: Iterable[Tuple[str, float, Dict[str, Any]]],
|
||||||
|
top_k: int,
|
||||||
|
used_mode: str,
|
||||||
|
subgraph: ga.Subgraph | None = None,
|
||||||
|
explain: bool = False,
|
||||||
|
dynamic_edge_boosts: Dict[str, float] = None
|
||||||
|
) -> QueryResponse:
|
||||||
|
"""
|
||||||
|
Wandelt semantische Roh-Treffer in bewertete QueryHits um.
|
||||||
|
WP-15c: Implementiert Note-Level Diversity Pooling.
|
||||||
|
"""
|
||||||
|
t0 = time.time()
|
||||||
|
enriched = []
|
||||||
|
|
||||||
|
# Erstes Scoring für alle Kandidaten
|
||||||
|
for pid, semantic_score, payload in hits:
|
||||||
|
edge_bonus, cent_bonus = 0.0, 0.0
|
||||||
|
target_id = payload.get("note_id")
|
||||||
|
|
||||||
|
if subgraph and target_id:
|
||||||
|
try:
|
||||||
|
edge_bonus = float(subgraph.edge_bonus(target_id))
|
||||||
|
cent_bonus = float(subgraph.centrality_bonus(target_id))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
debug_data = compute_wp22_score(
|
||||||
|
semantic_score, payload, edge_bonus, cent_bonus, dynamic_edge_boosts
|
||||||
|
)
|
||||||
|
enriched.append((pid, semantic_score, payload, debug_data))
|
||||||
|
|
||||||
|
# 1. Sortierung nach finalem mathematischen Score
|
||||||
|
enriched_sorted = sorted(enriched, key=lambda h: h[3]["total"], reverse=True)
|
||||||
|
|
||||||
|
# 2. WP-15c: Note-Level Diversity Pooling
|
||||||
|
# Wir behalten pro note_id nur den Hit mit dem höchsten total_score.
|
||||||
|
# Dies verhindert, dass 10 Chunks derselben Note andere KeyNotes verdrängen.
|
||||||
|
unique_note_hits = []
|
||||||
|
seen_notes = set()
|
||||||
|
|
||||||
|
for item in enriched_sorted:
|
||||||
|
_, _, payload, _ = item
|
||||||
|
note_id = str(payload.get("note_id", "unknown"))
|
||||||
|
|
||||||
|
if note_id not in seen_notes:
|
||||||
|
unique_note_hits.append(item)
|
||||||
|
seen_notes.add(note_id)
|
||||||
|
|
||||||
|
# 3. Begrenzung auf top_k nach dem Diversity-Pooling
|
||||||
|
limited_hits = unique_note_hits[: max(1, top_k)]
|
||||||
|
|
||||||
|
results: List[QueryHit] = []
|
||||||
|
for pid, s_score, pl, dbg in limited_hits:
|
||||||
|
explanation_obj = None
|
||||||
|
if explain:
|
||||||
|
explanation_obj = _build_explanation(
|
||||||
|
semantic_score=float(s_score),
|
||||||
|
payload=pl,
|
||||||
|
scoring_debug=dbg,
|
||||||
|
subgraph=subgraph,
|
||||||
|
target_note_id=pl.get("note_id"),
|
||||||
|
applied_boosts=dynamic_edge_boosts
|
||||||
|
)
|
||||||
|
|
||||||
|
text_content = pl.get("page_content") or pl.get("text") or pl.get("content", "[Kein Text]")
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: RAG-Kontext - source_chunk_id aus Edge-Payload extrahieren
|
||||||
|
source_chunk_id = None
|
||||||
|
if explanation_obj and explanation_obj.related_edges:
|
||||||
|
# Finde die erste Edge mit chunk_id als source
|
||||||
|
for edge in explanation_obj.related_edges:
|
||||||
|
# Prüfe, ob source eine Chunk-ID ist (enthält # oder ist chunk_id)
|
||||||
|
if edge.source and ("#" in edge.source or edge.source.startswith("chunk:")):
|
||||||
|
source_chunk_id = edge.source
|
||||||
|
break
|
||||||
|
|
||||||
|
results.append(QueryHit(
|
||||||
|
node_id=str(pid),
|
||||||
|
note_id=str(pl.get("note_id", "unknown")),
|
||||||
|
semantic_score=float(s_score),
|
||||||
|
edge_bonus=dbg["edge_bonus"],
|
||||||
|
centrality_bonus=dbg["cent_bonus"],
|
||||||
|
total_score=dbg["total"],
|
||||||
|
source={
|
||||||
|
"path": pl.get("path"),
|
||||||
|
"section": pl.get("section") or pl.get("section_title"),
|
||||||
|
"text": text_content
|
||||||
|
},
|
||||||
|
payload=pl,
|
||||||
|
explanation=explanation_obj,
|
||||||
|
source_chunk_id=source_chunk_id # WP-24c v4.1.0: RAG-Kontext
|
||||||
|
))
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Finale Ergebnisse
|
||||||
|
latency_ms = int((time.time() - t0) * 1000)
|
||||||
|
if not results:
|
||||||
|
logger.warning(f"⚠️ [EMPTY] Hybride Suche lieferte 0 Ergebnisse (Latency: {latency_ms}ms)")
|
||||||
|
else:
|
||||||
|
logger.info(f"✨ [SUCCESS] Hybride Suche lieferte {len(results)} Treffer (Latency: {latency_ms}ms)")
|
||||||
|
# Top 3 finale Scores loggen
|
||||||
|
# WP-24c v4.5.4: QueryHit hat kein chunk_id Feld - verwende node_id (enthält die Chunk-ID)
|
||||||
|
for i, hit in enumerate(results[:3]):
|
||||||
|
chunk_id = hit.node_id # node_id ist die Chunk-ID (pid)
|
||||||
|
logger.debug(f" [{i+1}] Final: Chunk={chunk_id} | Total-Score={hit.total_score:.4f} | Semantic={hit.semantic_score:.4f} | Edge={hit.edge_bonus:.4f}")
|
||||||
|
|
||||||
|
return QueryResponse(results=results, used_mode=used_mode, latency_ms=latency_ms)
|
||||||
|
|
||||||
|
|
||||||
|
def hybrid_retrieve(req: QueryRequest) -> QueryResponse:
|
||||||
|
"""
|
||||||
|
Die Haupt-Einstiegsfunktion für die hybride Suche.
|
||||||
|
WP-15c: Implementiert Edge-Aggregation (Super-Kanten).
|
||||||
|
WP-24c v4.5.0-DEBUG: Retrieval-Tracer für Diagnose.
|
||||||
|
"""
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Start der hybriden Suche
|
||||||
|
logger.info(f"🔍 [RETRIEVAL] Starte hybride Suche")
|
||||||
|
logger.info(f" -> Query: '{req.query[:100]}...' (Länge: {len(req.query)})")
|
||||||
|
logger.debug(f" ⚙️ [FILTER] Request-Filter: {req.filters}")
|
||||||
|
logger.debug(f" ⚙️ [FILTER] Top-K: {req.top_k}, Expand: {req.expand}, Target-Section: {req.target_section}")
|
||||||
|
client, prefix = _get_client_and_prefix()
|
||||||
|
vector = list(req.query_vector) if req.query_vector else _get_query_vector(req)
|
||||||
|
top_k = req.top_k or 10
|
||||||
|
|
||||||
|
# 1. Semantische Seed-Suche (Wir laden etwas mehr für das Pooling)
|
||||||
|
# WP-24c v4.1.0: Section-Filtering unterstützen
|
||||||
|
target_section = getattr(req, "target_section", None)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Vor semantischer Suche
|
||||||
|
logger.debug(f"🔍 [RETRIEVAL] Starte semantische Seed-Suche (Top-K: {top_k * 3}, Target-Section: {target_section})")
|
||||||
|
|
||||||
|
hits = _semantic_hits(client, prefix, vector, top_k=top_k * 3, filters=req.filters, target_section=target_section)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Nach semantischer Suche
|
||||||
|
logger.debug(f"📊 [SEED-HITS] Semantische Suche lieferte {len(hits)} Seed-Treffer")
|
||||||
|
|
||||||
|
# 2. Graph Expansion Konfiguration
|
||||||
|
expand_cfg = req.expand if isinstance(req.expand, dict) else {}
|
||||||
|
depth = int(expand_cfg.get("depth", 1))
|
||||||
|
boost_edges = getattr(req, "boost_edges", {}) or {}
|
||||||
|
|
||||||
|
subgraph: ga.Subgraph | None = None
|
||||||
|
if depth > 0 and hits:
|
||||||
|
# WP-24c v4.5.2: Chunk-Aware Graph Traversal
|
||||||
|
# Extrahiere sowohl note_id als auch chunk_id (pid) direkt aus den Hits
|
||||||
|
# Dies stellt sicher, dass Chunk-Scope Edges gefunden werden
|
||||||
|
seed_note_ids = list({h[2].get("note_id") for h in hits if h[2].get("note_id")})
|
||||||
|
seed_chunk_ids = list({h[0] for h in hits if h[0]}) # pid ist die Chunk-ID
|
||||||
|
|
||||||
|
# Kombiniere beide Sets für vollständige Seed-Abdeckung
|
||||||
|
# Chunk-IDs können auch als Note-IDs fungieren (für Note-Scope Edges)
|
||||||
|
all_seed_ids = list(set(seed_note_ids + seed_chunk_ids))
|
||||||
|
|
||||||
|
if all_seed_ids:
|
||||||
|
try:
|
||||||
|
# WP-24c v4.5.2: Chunk-IDs sind bereits aus Hits extrahiert
|
||||||
|
# Zusätzlich können wir noch weitere Chunk-IDs für die Note-IDs laden
|
||||||
|
# (für den Fall, dass nicht alle Chunks in den Top-K Hits sind)
|
||||||
|
additional_chunk_ids = _get_chunk_ids_for_notes(client, prefix, seed_note_ids)
|
||||||
|
# Kombiniere direkte Chunk-IDs aus Hits mit zusätzlich geladenen
|
||||||
|
all_chunk_ids = list(set(seed_chunk_ids + additional_chunk_ids))
|
||||||
|
|
||||||
|
# WP-24c v4.5.2: Erweiterte Edge-Retrieval mit Chunk-Scope und Section-Filtering
|
||||||
|
# Verwende all_seed_ids (enthält sowohl note_id als auch chunk_id)
|
||||||
|
# und all_chunk_ids für explizite Chunk-Scope Edge-Suche
|
||||||
|
subgraph = ga.expand(
|
||||||
|
client, prefix, all_seed_ids,
|
||||||
|
depth=depth,
|
||||||
|
edge_types=expand_cfg.get("edge_types"),
|
||||||
|
chunk_ids=all_chunk_ids,
|
||||||
|
target_section=target_section
|
||||||
|
)
|
||||||
|
|
||||||
|
# WP-24c v4.5.2: Debug-Logging für Chunk-Awareness
|
||||||
|
logger.debug(f"🔍 [SEEDS] Note-IDs: {len(seed_note_ids)}, Chunk-IDs: {len(seed_chunk_ids)}, Total Seeds: {len(all_seed_ids)}")
|
||||||
|
logger.debug(f" -> Zusätzliche Chunk-IDs geladen: {len(additional_chunk_ids)}, Total Chunk-IDs: {len(all_chunk_ids)}")
|
||||||
|
|
||||||
|
# --- WP-24c v4.1.0: Chunk-Level Edge-Aggregation & Deduplizierung ---
|
||||||
|
# Verhindert Score-Explosion durch multiple Links auf versch. Abschnitte.
|
||||||
|
# Logik: 1. Kante zählt voll, weitere dämpfen auf Faktor 0.1.
|
||||||
|
# Erweitert um Chunk-Level Tracking für präzise In-Degree-Berechnung.
|
||||||
|
if subgraph and hasattr(subgraph, "adj"):
|
||||||
|
# WP-24c v4.1.0: Chunk-Level In-Degree Tracking
|
||||||
|
chunk_level_in_degree = defaultdict(int) # target -> count of chunk sources
|
||||||
|
|
||||||
|
for src, edge_list in subgraph.adj.items():
|
||||||
|
# Gruppiere Kanten nach Ziel-Note (Deduplizierung ID_A -> ID_B)
|
||||||
|
by_target = defaultdict(list)
|
||||||
|
for e in edge_list:
|
||||||
|
by_target[e["target"]].append(e)
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Chunk-Level In-Degree Tracking
|
||||||
|
# Wenn source eine Chunk-ID ist, zähle für Chunk-Level In-Degree
|
||||||
|
if e.get("chunk_id") or (src and ("#" in src or src.startswith("chunk:"))):
|
||||||
|
chunk_level_in_degree[e["target"]] += 1
|
||||||
|
|
||||||
|
aggregated_list = []
|
||||||
|
for tgt, edges in by_target.items():
|
||||||
|
if len(edges) > 1:
|
||||||
|
# Sortiere: Stärkste Kante zuerst (Authority-Priorisierung)
|
||||||
|
sorted_edges = sorted(
|
||||||
|
edges,
|
||||||
|
key=lambda x: (
|
||||||
|
x.get("weight", 0.0) *
|
||||||
|
(1.0 if not x.get("virtual", False) else 0.5) * # Virtual-Penalty
|
||||||
|
float(x.get("confidence", 1.0)) # Confidence-Boost
|
||||||
|
),
|
||||||
|
reverse=True
|
||||||
|
)
|
||||||
|
primary = sorted_edges[0]
|
||||||
|
|
||||||
|
# Aggregiertes Gewicht berechnen (Sättigungs-Logik)
|
||||||
|
total_w = primary.get("weight", 0.0)
|
||||||
|
chunk_count = 0
|
||||||
|
for secondary in sorted_edges[1:]:
|
||||||
|
total_w += secondary.get("weight", 0.0) * 0.1
|
||||||
|
if secondary.get("chunk_id") or (secondary.get("source") and ("#" in secondary.get("source", "") or secondary.get("source", "").startswith("chunk:"))):
|
||||||
|
chunk_count += 1
|
||||||
|
|
||||||
|
primary["weight"] = total_w
|
||||||
|
primary["is_super_edge"] = True # Flag für Explanation Layer
|
||||||
|
primary["edge_count"] = len(edges)
|
||||||
|
primary["chunk_source_count"] = chunk_count + (1 if (primary.get("chunk_id") or (primary.get("source") and ("#" in primary.get("source", "") or primary.get("source", "").startswith("chunk:")))) else 0)
|
||||||
|
aggregated_list.append(primary)
|
||||||
|
else:
|
||||||
|
edge = edges[0]
|
||||||
|
# WP-24c v4.1.0: Chunk-Count auch für einzelne Edges
|
||||||
|
if edge.get("chunk_id") or (edge.get("source") and ("#" in edge.get("source", "") or edge.get("source", "").startswith("chunk:"))):
|
||||||
|
edge["chunk_source_count"] = 1
|
||||||
|
aggregated_list.append(edge)
|
||||||
|
|
||||||
|
# In-Place Update der Adjazenzliste des Graphen
|
||||||
|
subgraph.adj[src] = aggregated_list
|
||||||
|
|
||||||
|
# Re-Sync der In-Degrees für Centrality-Bonus (Aggregation konsistent halten)
|
||||||
|
subgraph.in_degree = defaultdict(int)
|
||||||
|
for src, edges in subgraph.adj.items():
|
||||||
|
for e in edges:
|
||||||
|
subgraph.in_degree[e["target"]] += 1
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Chunk-Level In-Degree als Attribut speichern
|
||||||
|
subgraph.chunk_level_in_degree = chunk_level_in_degree
|
||||||
|
|
||||||
|
# --- WP-24c v4.1.0: Authority-Priorisierung (Provenance & Confidence) ---
|
||||||
|
if subgraph and hasattr(subgraph, "adj"):
|
||||||
|
for src, edges in subgraph.adj.items():
|
||||||
|
for e in edges:
|
||||||
|
# A. Provenance Weighting (nutzt PROVENANCE_PRIORITY aus graph_utils)
|
||||||
|
prov = e.get("provenance", "rule")
|
||||||
|
prov_key = f"{prov}:{e.get('kind', 'related_to')}" if ":" not in prov else prov
|
||||||
|
prov_w = PROVENANCE_PRIORITY.get(prov_key, PROVENANCE_PRIORITY.get(prov, 0.7))
|
||||||
|
|
||||||
|
# B. Confidence-Weighting (aus Edge-Payload)
|
||||||
|
confidence = float(e.get("confidence", 1.0))
|
||||||
|
|
||||||
|
# C. Virtual-Flag De-Priorisierung
|
||||||
|
is_virtual = e.get("virtual", False)
|
||||||
|
virtual_penalty = 0.5 if is_virtual else 1.0
|
||||||
|
|
||||||
|
# D. Intent Boost Multiplikator
|
||||||
|
kind = e.get("kind")
|
||||||
|
intent_multiplier = boost_edges.get(kind, 1.0)
|
||||||
|
|
||||||
|
# Gewichtung anpassen (Authority-Priorisierung)
|
||||||
|
e["weight"] = e.get("weight", 1.0) * prov_w * confidence * virtual_penalty * intent_multiplier
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Graph Expansion failed: {e}")
|
||||||
|
subgraph = None
|
||||||
|
|
||||||
|
# 3. Scoring & Explanation Generierung
|
||||||
|
# top_k wird erst hier final angewandt
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Vor finaler Hit-Erstellung
|
||||||
|
if subgraph:
|
||||||
|
# WP-24c v4.5.1: Subgraph hat kein .edges Attribut, sondern .adj (Adjazenzliste)
|
||||||
|
# Zähle alle Kanten aus der Adjazenzliste
|
||||||
|
edge_count = sum(len(edges) for edges in subgraph.adj.values()) if hasattr(subgraph, 'adj') else 0
|
||||||
|
logger.debug(f"📊 [GRAPH] Subgraph enthält {edge_count} Kanten")
|
||||||
|
else:
|
||||||
|
logger.debug(f"📊 [GRAPH] Kein Subgraph (depth=0 oder keine Seed-IDs)")
|
||||||
|
|
||||||
|
result = _build_hits_from_semantic(hits, top_k, "hybrid", subgraph, req.explain, boost_edges)
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Nach finaler Hit-Erstellung
|
||||||
|
if not result.results:
|
||||||
|
logger.warning(f"⚠️ [EMPTY] Hybride Suche lieferte nach Scoring 0 finale Ergebnisse")
|
||||||
|
else:
|
||||||
|
logger.info(f"✨ [SUCCESS] Hybride Suche lieferte {len(result.results)} finale Treffer (Mode: {result.used_mode})")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def semantic_retrieve(req: QueryRequest) -> QueryResponse:
|
||||||
|
"""Standard Vektorsuche ohne Graph-Einfluss."""
|
||||||
|
client, prefix = _get_client_and_prefix()
|
||||||
|
vector = _get_query_vector(req)
|
||||||
|
hits = _semantic_hits(client, prefix, vector, req.top_k or 10, req.filters)
|
||||||
|
return _build_hits_from_semantic(hits, req.top_k or 10, "semantic", explain=req.explain)
|
||||||
|
|
||||||
|
|
||||||
|
class Retriever:
|
||||||
|
"""Schnittstelle für die asynchrone Suche."""
|
||||||
|
async def search(self, request: QueryRequest) -> QueryResponse:
|
||||||
|
return hybrid_retrieve(request)
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/core/retriever_scoring.py
|
FILE: app/core/retrieval/retriever_scoring.py
|
||||||
DESCRIPTION: Mathematische Kern-Logik für das WP-22 Scoring.
|
DESCRIPTION: Mathematische Kern-Logik für das WP-22/WP-15c Scoring.
|
||||||
Berechnet Relevanz-Scores basierend auf Semantik, Graph-Intelligence und Content Lifecycle.
|
Berechnet Relevanz-Scores basierend auf Semantik, Graph-Intelligence und Content Lifecycle.
|
||||||
VERSION: 1.0.1 (WP-22 Full Math Engine)
|
FIX v1.0.3: Optimierte Interaktion zwischen Typ-Boost und Status-Dämpfung.
|
||||||
|
VERSION: 1.0.3
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: app.config, typing
|
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
@@ -22,10 +22,6 @@ logger = logging.getLogger(__name__)
|
|||||||
def get_weights() -> Tuple[float, float, float]:
|
def get_weights() -> Tuple[float, float, float]:
|
||||||
"""
|
"""
|
||||||
Liefert die Basis-Gewichtung (semantic, edge, centrality) aus der Konfiguration.
|
Liefert die Basis-Gewichtung (semantic, edge, centrality) aus der Konfiguration.
|
||||||
Priorität:
|
|
||||||
1. config/retriever.yaml (Scoring-Sektion)
|
|
||||||
2. Umgebungsvariablen (RETRIEVER_W_*)
|
|
||||||
3. System-Defaults (1.0, 0.0, 0.0)
|
|
||||||
"""
|
"""
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
@@ -57,7 +53,7 @@ def get_status_multiplier(payload: Dict[str, Any]) -> float:
|
|||||||
|
|
||||||
- stable: 1.2 (Belohnung für verifiziertes Wissen)
|
- stable: 1.2 (Belohnung für verifiziertes Wissen)
|
||||||
- active: 1.0 (Standard-Gewichtung)
|
- active: 1.0 (Standard-Gewichtung)
|
||||||
- draft: 0.5 (Bestrafung für unfertige Fragmente)
|
- draft: 0.5 (Dämpfung für unfertige Fragmente)
|
||||||
"""
|
"""
|
||||||
status = str(payload.get("status", "active")).lower().strip()
|
status = str(payload.get("status", "active")).lower().strip()
|
||||||
if status == "stable":
|
if status == "stable":
|
||||||
@@ -74,46 +70,58 @@ def compute_wp22_score(
|
|||||||
dynamic_edge_boosts: Optional[Dict[str, float]] = None
|
dynamic_edge_boosts: Optional[Dict[str, float]] = None
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Die zentrale mathematische Scoring-Formel der Mindnet Intelligence.
|
Die zentrale mathematische Scoring-Formel (WP-15c optimiert).
|
||||||
Implementiert das WP-22 Hybrid-Scoring (Semantic * Lifecycle * Graph).
|
Implementiert das Hybrid-Scoring (Semantic * Lifecycle * Graph).
|
||||||
|
|
||||||
FORMEL:
|
LOGIK:
|
||||||
Score = (Similarity * StatusMult) * (1 + (TypeWeight - 1) + ((EdgeW * EB + CentW * CB) * IntentBoost))
|
1. Base = Similarity * StatusMult (Lifecycle-Filter).
|
||||||
|
2. Boosts = (TypeBoost - 1) + (GraphBoni * IntentFactor).
|
||||||
|
3. Final = Base * (1 + Boosts).
|
||||||
|
|
||||||
Returns:
|
Der edge_bonus_raw enthält bereits die Super-Edge-Aggregation (WP-15c).
|
||||||
Dict mit dem finalen 'total' Score und allen mathematischen Zwischenwerten für den Explanation Layer.
|
|
||||||
"""
|
"""
|
||||||
sem_w, edge_w_cfg, cent_w_cfg = get_weights()
|
sem_w, edge_w_cfg, cent_w_cfg = get_weights()
|
||||||
status_mult = get_status_multiplier(payload)
|
status_mult = get_status_multiplier(payload)
|
||||||
|
|
||||||
# Retriever Weight (Type Boost aus types.yaml, z.B. 1.1 für Decisions)
|
# Retriever Weight (Typ-Boost aus types.yaml, z.B. 1.1 für Decisions)
|
||||||
node_weight = float(payload.get("retriever_weight", 1.0))
|
node_weight = float(payload.get("retriever_weight", 1.0))
|
||||||
|
|
||||||
# 1. Berechnung des Base Scores (Semantik gewichtet durch Lifecycle-Status)
|
# 1. Berechnung des Base Scores (Semantik gewichtet durch Lifecycle-Status)
|
||||||
|
# WICHTIG: Der Status wirkt hier als Multiplikator auf die Basis-Relevanz.
|
||||||
base_val = float(semantic_score) * status_mult
|
base_val = float(semantic_score) * status_mult
|
||||||
|
|
||||||
# 2. Graph Boost Factor (Teil C: Intent-spezifische Verstärkung)
|
# 2. Graph Boost Factor (Intent-spezifische Verstärkung aus decision_engine.yaml)
|
||||||
# Erhöht das Gewicht des gesamten Graphen um 50%, wenn ein spezifischer Intent vorliegt.
|
# Erhöht das Gewicht des gesamten Graphen um 50%, wenn ein spezifischer Intent vorliegt.
|
||||||
graph_boost_factor = 1.5 if dynamic_edge_boosts and (edge_bonus_raw > 0 or cent_bonus_raw > 0) else 1.0
|
graph_boost_factor = 1.5 if dynamic_edge_boosts and (edge_bonus_raw > 0 or cent_bonus_raw > 0) else 1.0
|
||||||
|
|
||||||
# 3. Einzelne Graph-Komponenten berechnen
|
# 3. Einzelne Graph-Komponenten berechnen
|
||||||
|
# WP-15c Hinweis: edge_bonus_raw ist durch den retriever.py bereits gedämpft/aggregiert.
|
||||||
edge_impact_final = (edge_w_cfg * edge_bonus_raw) * graph_boost_factor
|
edge_impact_final = (edge_w_cfg * edge_bonus_raw) * graph_boost_factor
|
||||||
cent_impact_final = (cent_w_cfg * cent_bonus_raw) * graph_boost_factor
|
cent_impact_final = (cent_w_cfg * cent_bonus_raw) * graph_boost_factor
|
||||||
|
|
||||||
# 4. Finales Zusammenführen (Merging)
|
# 4. Finales Zusammenführen (Merging)
|
||||||
# (node_weight - 1.0) sorgt dafür, dass ein Gewicht von 1.0 keinen Einfluss hat (neutral).
|
# (node_weight - 1.0) wandelt das Gewicht in einen relativen Bonus um (z.B. 1.2 -> +0.2).
|
||||||
total = base_val * (1.0 + (node_weight - 1.0) + edge_impact_final + cent_impact_final)
|
# Alle Boni werden addiert und wirken dann auf den base_val.
|
||||||
|
type_impact = node_weight - 1.0
|
||||||
|
total_boost = 1.0 + type_impact + edge_impact_final + cent_impact_final
|
||||||
|
|
||||||
|
total = base_val * total_boost
|
||||||
|
|
||||||
# Sicherstellen, dass der Score niemals 0 oder negativ ist (Floor)
|
# Sicherstellen, dass der Score niemals 0 oder negativ ist (Floor)
|
||||||
final_score = max(0.0001, float(total))
|
final_score = max(0.0001, float(total))
|
||||||
|
|
||||||
|
# WP-24c v4.5.0-DEBUG: Retrieval-Tracer - Protokollierung der Score-Berechnung
|
||||||
|
chunk_id = payload.get("chunk_id", payload.get("id", "unknown"))
|
||||||
|
logger.debug(f"📈 [SCORE-TRACE] Chunk: {chunk_id} | Base: {base_val:.4f} | Multiplier: {total_boost:.2f} | Final: {final_score:.4f}")
|
||||||
|
logger.debug(f" -> Details: StatusMult={status_mult:.2f}, TypeImpact={type_impact:.2f}, EdgeImpact={edge_impact_final:.4f}, CentImpact={cent_impact_final:.4f}")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"total": final_score,
|
"total": final_score,
|
||||||
"edge_bonus": float(edge_bonus_raw),
|
"edge_bonus": float(edge_bonus_raw),
|
||||||
"cent_bonus": float(cent_bonus_raw),
|
"cent_bonus": float(cent_bonus_raw),
|
||||||
"status_multiplier": status_mult,
|
"status_multiplier": status_mult,
|
||||||
"graph_boost_factor": graph_boost_factor,
|
"graph_boost_factor": graph_boost_factor,
|
||||||
"type_impact": node_weight - 1.0,
|
"type_impact": type_impact,
|
||||||
"base_val": base_val,
|
"base_val": base_val,
|
||||||
"edge_impact_final": edge_impact_final,
|
"edge_impact_final": edge_impact_final,
|
||||||
"cent_impact_final": cent_impact_final
|
"cent_impact_final": cent_impact_final
|
||||||
@@ -1,310 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/core/retriever.py
|
|
||||||
DESCRIPTION: Haupt-Schnittstelle für die Suche. Orchestriert Vektorsuche und Graph-Expansion.
|
|
||||||
Nutzt retriever_scoring.py für die WP-22 Logik.
|
|
||||||
FIX: TypeError in embed_text (model_name) behoben.
|
|
||||||
FIX: Pydantic ValidationError (Target/Source) behoben.
|
|
||||||
VERSION: 0.6.15 (WP-22 Full & Stable)
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: app.config, app.models.dto, app.core.qdrant*, app.core.graph_adapter, app.core.retriever_scoring
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
import logging
|
|
||||||
from typing import Any, Dict, List, Tuple, Iterable, Optional
|
|
||||||
|
|
||||||
from app.config import get_settings
|
|
||||||
from app.models.dto import (
|
|
||||||
QueryRequest, QueryResponse, QueryHit,
|
|
||||||
Explanation, ScoreBreakdown, Reason, EdgeDTO
|
|
||||||
)
|
|
||||||
import app.core.qdrant as qdr
|
|
||||||
import app.core.qdrant_points as qp
|
|
||||||
import app.services.embeddings_client as ec
|
|
||||||
import app.core.graph_adapter as ga
|
|
||||||
|
|
||||||
# Mathematische Engine importieren
|
|
||||||
from app.core.retriever_scoring import get_weights, compute_wp22_score
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# ==============================================================================
|
|
||||||
# 1. CORE HELPERS & CONFIG LOADERS
|
|
||||||
# ==============================================================================
|
|
||||||
|
|
||||||
def _get_client_and_prefix() -> Tuple[Any, str]:
|
|
||||||
"""Initialisiert Qdrant Client und lädt Collection-Prefix."""
|
|
||||||
cfg = qdr.QdrantConfig.from_env()
|
|
||||||
return qdr.get_client(cfg), cfg.prefix
|
|
||||||
|
|
||||||
|
|
||||||
def _get_query_vector(req: QueryRequest) -> List[float]:
|
|
||||||
"""
|
|
||||||
Vektorisiert die Anfrage.
|
|
||||||
FIX: Enthält try-except Block für unterschiedliche Signaturen von ec.embed_text.
|
|
||||||
"""
|
|
||||||
if req.query_vector:
|
|
||||||
return list(req.query_vector)
|
|
||||||
if not req.query:
|
|
||||||
raise ValueError("Kein Text oder Vektor für die Suche angegeben.")
|
|
||||||
|
|
||||||
settings = get_settings()
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Versuch mit modernem Interface (WP-03 kompatibel)
|
|
||||||
return ec.embed_text(req.query, model_name=settings.MODEL_NAME)
|
|
||||||
except TypeError:
|
|
||||||
# Fallback für Signaturen, die 'model_name' nicht als Keyword akzeptieren
|
|
||||||
logger.debug("ec.embed_text does not accept 'model_name' keyword. Falling back.")
|
|
||||||
return ec.embed_text(req.query)
|
|
||||||
|
|
||||||
|
|
||||||
def _semantic_hits(
|
|
||||||
client: Any,
|
|
||||||
prefix: str,
|
|
||||||
vector: List[float],
|
|
||||||
top_k: int,
|
|
||||||
filters: Optional[Dict] = None
|
|
||||||
) -> List[Tuple[str, float, Dict[str, Any]]]:
|
|
||||||
"""Führt die Vektorsuche durch und konvertiert Qdrant-Points in ein einheitliches Format."""
|
|
||||||
raw_hits = qp.search_chunks_by_vector(client, prefix, vector, top=top_k, filters=filters)
|
|
||||||
# Strikte Typkonvertierung für Stabilität
|
|
||||||
return [(str(hit[0]), float(hit[1]), dict(hit[2] or {})) for hit in raw_hits]
|
|
||||||
|
|
||||||
# ==============================================================================
|
|
||||||
# 2. EXPLANATION LAYER (DEBUG & VERIFIABILITY)
|
|
||||||
# ==============================================================================
|
|
||||||
|
|
||||||
def _build_explanation(
|
|
||||||
semantic_score: float,
|
|
||||||
payload: Dict[str, Any],
|
|
||||||
scoring_debug: Dict[str, Any],
|
|
||||||
subgraph: Optional[ga.Subgraph],
|
|
||||||
target_note_id: Optional[str],
|
|
||||||
applied_boosts: Optional[Dict[str, float]] = None
|
|
||||||
) -> Explanation:
|
|
||||||
"""
|
|
||||||
Transformiert mathematische Scores und Graph-Signale in eine menschenlesbare Erklärung.
|
|
||||||
Behebt Pydantic ValidationErrors durch explizite String-Sicherung.
|
|
||||||
"""
|
|
||||||
_, edge_w_cfg, _ = get_weights()
|
|
||||||
base_val = scoring_debug["base_val"]
|
|
||||||
|
|
||||||
# 1. Detaillierter mathematischer Breakdown
|
|
||||||
breakdown = ScoreBreakdown(
|
|
||||||
semantic_contribution=base_val,
|
|
||||||
edge_contribution=base_val * scoring_debug["edge_impact_final"],
|
|
||||||
centrality_contribution=base_val * scoring_debug["cent_impact_final"],
|
|
||||||
raw_semantic=semantic_score,
|
|
||||||
raw_edge_bonus=scoring_debug["edge_bonus"],
|
|
||||||
raw_centrality=scoring_debug["cent_bonus"],
|
|
||||||
node_weight=float(payload.get("retriever_weight", 1.0)),
|
|
||||||
status_multiplier=scoring_debug["status_multiplier"],
|
|
||||||
graph_boost_factor=scoring_debug["graph_boost_factor"]
|
|
||||||
)
|
|
||||||
|
|
||||||
reasons: List[Reason] = []
|
|
||||||
edges_dto: List[EdgeDTO] = []
|
|
||||||
|
|
||||||
# 2. Gründe für Semantik hinzufügen
|
|
||||||
if semantic_score > 0.85:
|
|
||||||
reasons.append(Reason(kind="semantic", message="Sehr hohe textuelle Übereinstimmung.", score_impact=base_val))
|
|
||||||
elif semantic_score > 0.70:
|
|
||||||
reasons.append(Reason(kind="semantic", message="Inhaltliche Übereinstimmung.", score_impact=base_val))
|
|
||||||
|
|
||||||
# 3. Gründe für Typ und Lifecycle
|
|
||||||
type_weight = float(payload.get("retriever_weight", 1.0))
|
|
||||||
if type_weight != 1.0:
|
|
||||||
msg = "Bevorzugt" if type_weight > 1.0 else "De-priorisiert"
|
|
||||||
reasons.append(Reason(kind="type", message=f"{msg} durch Typ-Profil.", score_impact=base_val * (type_weight - 1.0)))
|
|
||||||
|
|
||||||
# 4. Kanten-Verarbeitung (Graph-Intelligence)
|
|
||||||
if subgraph and target_note_id and scoring_debug["edge_bonus"] > 0:
|
|
||||||
raw_edges = []
|
|
||||||
if hasattr(subgraph, "get_incoming_edges"):
|
|
||||||
raw_edges.extend(subgraph.get_incoming_edges(target_note_id) or [])
|
|
||||||
if hasattr(subgraph, "get_outgoing_edges"):
|
|
||||||
raw_edges.extend(subgraph.get_outgoing_edges(target_note_id) or [])
|
|
||||||
|
|
||||||
for edge in raw_edges:
|
|
||||||
# FIX: Zwingende String-Konvertierung für Pydantic-Stabilität
|
|
||||||
src = str(edge.get("source") or "note_root")
|
|
||||||
tgt = str(edge.get("target") or target_note_id or "unknown_target")
|
|
||||||
kind = str(edge.get("kind", "related_to"))
|
|
||||||
prov = str(edge.get("provenance", "rule"))
|
|
||||||
conf = float(edge.get("confidence", 1.0))
|
|
||||||
|
|
||||||
direction = "in" if tgt == target_note_id else "out"
|
|
||||||
|
|
||||||
edge_obj = EdgeDTO(
|
|
||||||
id=f"{src}->{tgt}:{kind}",
|
|
||||||
kind=kind,
|
|
||||||
source=src,
|
|
||||||
target=tgt,
|
|
||||||
weight=conf,
|
|
||||||
direction=direction,
|
|
||||||
provenance=prov,
|
|
||||||
confidence=conf
|
|
||||||
)
|
|
||||||
edges_dto.append(edge_obj)
|
|
||||||
|
|
||||||
# Die 3 wichtigsten Kanten als Begründung formulieren
|
|
||||||
top_edges = sorted(edges_dto, key=lambda e: e.confidence, reverse=True)
|
|
||||||
for e in top_edges[:3]:
|
|
||||||
peer = e.source if e.direction == "in" else e.target
|
|
||||||
prov_txt = "Bestätigte" if e.provenance == "explicit" else "KI-basierte"
|
|
||||||
boost_txt = f" [Boost x{applied_boosts.get(e.kind)}]" if applied_boosts and e.kind in applied_boosts else ""
|
|
||||||
|
|
||||||
reasons.append(Reason(
|
|
||||||
kind="edge",
|
|
||||||
message=f"{prov_txt} Kante '{e.kind}'{boost_txt} von/zu '{peer}'.",
|
|
||||||
score_impact=edge_w_cfg * e.confidence
|
|
||||||
))
|
|
||||||
|
|
||||||
if scoring_debug["cent_bonus"] > 0.01:
|
|
||||||
reasons.append(Reason(kind="centrality", message="Die Notiz ist ein zentraler Informations-Hub.", score_impact=breakdown.centrality_contribution))
|
|
||||||
|
|
||||||
return Explanation(
|
|
||||||
breakdown=breakdown,
|
|
||||||
reasons=reasons,
|
|
||||||
related_edges=edges_dto if edges_dto else None,
|
|
||||||
applied_boosts=applied_boosts
|
|
||||||
)
|
|
||||||
|
|
||||||
# ==============================================================================
|
|
||||||
# 3. CORE RETRIEVAL PIPELINE
|
|
||||||
# ==============================================================================
|
|
||||||
|
|
||||||
def _build_hits_from_semantic(
|
|
||||||
hits: Iterable[Tuple[str, float, Dict[str, Any]]],
|
|
||||||
top_k: int,
|
|
||||||
used_mode: str,
|
|
||||||
subgraph: ga.Subgraph | None = None,
|
|
||||||
explain: bool = False,
|
|
||||||
dynamic_edge_boosts: Dict[str, float] = None
|
|
||||||
) -> QueryResponse:
|
|
||||||
"""Wandelt semantische Roh-Treffer in hochgeladene, bewertete QueryHits um."""
|
|
||||||
t0 = time.time()
|
|
||||||
enriched = []
|
|
||||||
|
|
||||||
for pid, semantic_score, payload in hits:
|
|
||||||
edge_bonus, cent_bonus = 0.0, 0.0
|
|
||||||
target_id = payload.get("note_id")
|
|
||||||
|
|
||||||
if subgraph and target_id:
|
|
||||||
try:
|
|
||||||
edge_bonus = float(subgraph.edge_bonus(target_id))
|
|
||||||
cent_bonus = float(subgraph.centrality_bonus(target_id))
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# Mathematisches Scoring via WP-22 Engine
|
|
||||||
debug_data = compute_wp22_score(
|
|
||||||
semantic_score, payload, edge_bonus, cent_bonus, dynamic_edge_boosts
|
|
||||||
)
|
|
||||||
enriched.append((pid, semantic_score, payload, debug_data))
|
|
||||||
|
|
||||||
# Sortierung nach finalem mathematischen Score
|
|
||||||
enriched_sorted = sorted(enriched, key=lambda h: h[3]["total"], reverse=True)
|
|
||||||
limited_hits = enriched_sorted[: max(1, top_k)]
|
|
||||||
|
|
||||||
results: List[QueryHit] = []
|
|
||||||
for pid, s_score, pl, dbg in limited_hits:
|
|
||||||
explanation_obj = None
|
|
||||||
if explain:
|
|
||||||
explanation_obj = _build_explanation(
|
|
||||||
semantic_score=float(s_score),
|
|
||||||
payload=pl,
|
|
||||||
scoring_debug=dbg,
|
|
||||||
subgraph=subgraph,
|
|
||||||
target_note_id=pl.get("note_id"),
|
|
||||||
applied_boosts=dynamic_edge_boosts
|
|
||||||
)
|
|
||||||
|
|
||||||
# Payload Text-Feld normalisieren
|
|
||||||
text_content = pl.get("page_content") or pl.get("text") or pl.get("content", "[Kein Text]")
|
|
||||||
|
|
||||||
results.append(QueryHit(
|
|
||||||
node_id=str(pid),
|
|
||||||
note_id=str(pl.get("note_id", "unknown")),
|
|
||||||
semantic_score=float(s_score),
|
|
||||||
edge_bonus=dbg["edge_bonus"],
|
|
||||||
centrality_bonus=dbg["cent_bonus"],
|
|
||||||
total_score=dbg["total"],
|
|
||||||
source={
|
|
||||||
"path": pl.get("path"),
|
|
||||||
"section": pl.get("section") or pl.get("section_title"),
|
|
||||||
"text": text_content
|
|
||||||
},
|
|
||||||
payload=pl,
|
|
||||||
explanation=explanation_obj
|
|
||||||
))
|
|
||||||
|
|
||||||
return QueryResponse(results=results, used_mode=used_mode, latency_ms=int((time.time() - t0) * 1000))
|
|
||||||
|
|
||||||
|
|
||||||
def hybrid_retrieve(req: QueryRequest) -> QueryResponse:
|
|
||||||
"""
|
|
||||||
Die Haupt-Einstiegsfunktion für die hybride Suche.
|
|
||||||
Kombiniert Vektorsuche mit Graph-Expansion, Provenance-Weighting und Intent-Boosting.
|
|
||||||
"""
|
|
||||||
client, prefix = _get_client_and_prefix()
|
|
||||||
vector = list(req.query_vector) if req.query_vector else _get_query_vector(req)
|
|
||||||
top_k = req.top_k or 10
|
|
||||||
|
|
||||||
# 1. Semantische Seed-Suche
|
|
||||||
hits = _semantic_hits(client, prefix, vector, top_k=top_k, filters=req.filters)
|
|
||||||
|
|
||||||
# 2. Graph Expansion Konfiguration
|
|
||||||
expand_cfg = req.expand if isinstance(req.expand, dict) else {}
|
|
||||||
depth = int(expand_cfg.get("depth", 1))
|
|
||||||
boost_edges = getattr(req, "boost_edges", {}) or {}
|
|
||||||
|
|
||||||
subgraph: ga.Subgraph | None = None
|
|
||||||
if depth > 0 and hits:
|
|
||||||
# Start-IDs für den Graph-Traversal sammeln
|
|
||||||
seed_ids = list({h[2].get("note_id") for h in hits if h[2].get("note_id")})
|
|
||||||
|
|
||||||
if seed_ids:
|
|
||||||
try:
|
|
||||||
# Subgraph aus RAM/DB laden
|
|
||||||
subgraph = ga.expand(client, prefix, seed_ids, depth=depth, edge_types=expand_cfg.get("edge_types"))
|
|
||||||
|
|
||||||
# --- WP-22: Kanten-Gewichtung im RAM-Graphen vor Bonus-Berechnung ---
|
|
||||||
if subgraph and hasattr(subgraph, "graph"):
|
|
||||||
for _, _, data in subgraph.graph.edges(data=True):
|
|
||||||
# A. Provenance Weighting (WP-22 Bonus für Herkunft)
|
|
||||||
prov = data.get("provenance", "rule")
|
|
||||||
# Belohnung: Explizite Links (1.0) > Smart (0.9) > Rule (0.7)
|
|
||||||
prov_w = 1.0 if prov == "explicit" else (0.9 if prov == "smart" else 0.7)
|
|
||||||
|
|
||||||
# B. Intent Boost Multiplikator (Vom Router dynamisch injiziert)
|
|
||||||
kind = data.get("kind")
|
|
||||||
intent_multiplier = boost_edges.get(kind, 1.0)
|
|
||||||
|
|
||||||
# Finales Gewicht setzen (Basis * Provenance * Intent)
|
|
||||||
data["weight"] = data.get("weight", 1.0) * prov_w * intent_multiplier
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Graph Expansion failed: {e}")
|
|
||||||
subgraph = None
|
|
||||||
|
|
||||||
# 3. Scoring & Explanation Generierung
|
|
||||||
return _build_hits_from_semantic(hits, top_k, "hybrid", subgraph, req.explain, boost_edges)
|
|
||||||
|
|
||||||
|
|
||||||
def semantic_retrieve(req: QueryRequest) -> QueryResponse:
|
|
||||||
"""Standard Vektorsuche ohne Graph-Einfluss (WP-02 Fallback)."""
|
|
||||||
client, prefix = _get_client_and_prefix()
|
|
||||||
vector = _get_query_vector(req)
|
|
||||||
hits = _semantic_hits(client, prefix, vector, req.top_k or 10, req.filters)
|
|
||||||
return _build_hits_from_semantic(hits, req.top_k or 10, "semantic", explain=req.explain)
|
|
||||||
|
|
||||||
|
|
||||||
class Retriever:
|
|
||||||
"""Schnittstelle für die asynchrone Suche."""
|
|
||||||
async def search(self, request: QueryRequest) -> QueryResponse:
|
|
||||||
"""Führt eine hybride Suche aus."""
|
|
||||||
return hybrid_retrieve(request)
|
|
||||||
+15
-10
@@ -1,11 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/core/type_registry.py
|
FILE: app/core/type_registry.py
|
||||||
DESCRIPTION: Loader für types.yaml. Achtung: Wird in der aktuellen Pipeline meist durch lokale Loader in 'ingestion.py' oder 'note_payload.py' umgangen.
|
DESCRIPTION: Loader für types.yaml.
|
||||||
VERSION: 1.0.0
|
WP-24c: Robustheits-Fix für chunking_profile vs chunk_profile.
|
||||||
STATUS: Deprecated (Redundant)
|
WP-14: Support für zentrale Registry-Strukturen.
|
||||||
|
VERSION: 1.1.0 (Audit-Fix: Profile Key Consistency)
|
||||||
|
STATUS: Active (Support für Legacy-Loader)
|
||||||
DEPENDENCIES: yaml, os, functools
|
DEPENDENCIES: yaml, os, functools
|
||||||
EXTERNAL_CONFIG: config/types.yaml
|
EXTERNAL_CONFIG: config/types.yaml
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -18,12 +19,12 @@ try:
|
|||||||
except Exception:
|
except Exception:
|
||||||
yaml = None # wird erst benötigt, wenn eine Datei gelesen werden soll
|
yaml = None # wird erst benötigt, wenn eine Datei gelesen werden soll
|
||||||
|
|
||||||
# Konservativer Default – bewusst minimal
|
# Konservativer Default – WP-24c: Nutzt nun konsistent 'chunking_profile'
|
||||||
_DEFAULT_REGISTRY: Dict[str, Any] = {
|
_DEFAULT_REGISTRY: Dict[str, Any] = {
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"types": {
|
"types": {
|
||||||
"concept": {
|
"concept": {
|
||||||
"chunk_profile": "medium",
|
"chunking_profile": "medium",
|
||||||
"edge_defaults": ["references", "related_to"],
|
"edge_defaults": ["references", "related_to"],
|
||||||
"retriever_weight": 1.0,
|
"retriever_weight": 1.0,
|
||||||
}
|
}
|
||||||
@@ -33,7 +34,6 @@ _DEFAULT_REGISTRY: Dict[str, Any] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Chunk-Profile → Overlap-Empfehlungen (nur für synthetische Fensterbildung)
|
# Chunk-Profile → Overlap-Empfehlungen (nur für synthetische Fensterbildung)
|
||||||
# Die absoluten Chunk-Längen bleiben Aufgabe des Chunkers (assemble_chunks).
|
|
||||||
_PROFILE_TO_OVERLAP: Dict[str, Tuple[int, int]] = {
|
_PROFILE_TO_OVERLAP: Dict[str, Tuple[int, int]] = {
|
||||||
"short": (20, 30),
|
"short": (20, 30),
|
||||||
"medium": (40, 60),
|
"medium": (40, 60),
|
||||||
@@ -45,7 +45,7 @@ _PROFILE_TO_OVERLAP: Dict[str, Tuple[int, int]] = {
|
|||||||
def load_type_registry(path: str = "config/types.yaml") -> Dict[str, Any]:
|
def load_type_registry(path: str = "config/types.yaml") -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Lädt die Registry aus 'path'. Bei Fehlern wird ein konserviver Default geliefert.
|
Lädt die Registry aus 'path'. Bei Fehlern wird ein konserviver Default geliefert.
|
||||||
Die Rückgabe ist *prozessweit* gecached.
|
Die Rückgabe ist prozessweit gecached.
|
||||||
"""
|
"""
|
||||||
if not path:
|
if not path:
|
||||||
return dict(_DEFAULT_REGISTRY)
|
return dict(_DEFAULT_REGISTRY)
|
||||||
@@ -54,7 +54,6 @@ def load_type_registry(path: str = "config/types.yaml") -> Dict[str, Any]:
|
|||||||
return dict(_DEFAULT_REGISTRY)
|
return dict(_DEFAULT_REGISTRY)
|
||||||
|
|
||||||
if yaml is None:
|
if yaml is None:
|
||||||
# PyYAML fehlt → auf Default zurückfallen
|
|
||||||
return dict(_DEFAULT_REGISTRY)
|
return dict(_DEFAULT_REGISTRY)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -71,6 +70,7 @@ def load_type_registry(path: str = "config/types.yaml") -> Dict[str, Any]:
|
|||||||
|
|
||||||
|
|
||||||
def get_type_config(note_type: Optional[str], reg: Dict[str, Any]) -> Dict[str, Any]:
|
def get_type_config(note_type: Optional[str], reg: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
|
"""Extrahiert die Konfiguration für einen spezifischen Typ."""
|
||||||
t = (note_type or "concept").strip().lower()
|
t = (note_type or "concept").strip().lower()
|
||||||
types = (reg or {}).get("types", {}) if isinstance(reg, dict) else {}
|
types = (reg or {}).get("types", {}) if isinstance(reg, dict) else {}
|
||||||
return types.get(t) or types.get("concept") or _DEFAULT_REGISTRY["types"]["concept"]
|
return types.get(t) or types.get("concept") or _DEFAULT_REGISTRY["types"]["concept"]
|
||||||
@@ -84,8 +84,13 @@ def resolve_note_type(fm_type: Optional[str], reg: Dict[str, Any]) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def effective_chunk_profile(note_type: Optional[str], reg: Dict[str, Any]) -> Optional[str]:
|
def effective_chunk_profile(note_type: Optional[str], reg: Dict[str, Any]) -> Optional[str]:
|
||||||
|
"""
|
||||||
|
Ermittelt das aktive Chunking-Profil für einen Notiz-Typ.
|
||||||
|
Fix (Audit-Problem 2): Prüft beide Key-Varianten für 100% Kompatibilität.
|
||||||
|
"""
|
||||||
cfg = get_type_config(note_type, reg)
|
cfg = get_type_config(note_type, reg)
|
||||||
prof = cfg.get("chunk_profile")
|
# Check 'chunking_profile' (Standard) OR 'chunk_profile' (Legacy/Fallback)
|
||||||
|
prof = cfg.get("chunking_profile") or cfg.get("chunk_profile")
|
||||||
if isinstance(prof, str) and prof.strip():
|
if isinstance(prof, str) and prof.strip():
|
||||||
return prof.strip().lower()
|
return prof.strip().lower()
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -69,20 +69,35 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
search_term = st.text_input("Suche Notiz", placeholder="Titel eingeben...", key="cy_search")
|
search_term = st.text_input("Suche Notiz", placeholder="Titel eingeben...", key="cy_search")
|
||||||
|
|
||||||
if search_term:
|
if search_term:
|
||||||
hits, _ = graph_service.client.scroll(
|
try:
|
||||||
collection_name=f"{COLLECTION_PREFIX}_notes",
|
hits, _ = graph_service.client.scroll(
|
||||||
limit=10,
|
collection_name=f"{COLLECTION_PREFIX}_notes",
|
||||||
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchText(text=search_term))])
|
limit=10,
|
||||||
)
|
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchText(text=search_term))])
|
||||||
options = {h.payload['title']: h.payload['note_id'] for h in hits}
|
)
|
||||||
|
options = {}
|
||||||
|
for h in hits:
|
||||||
|
if h.payload and 'title' in h.payload and 'note_id' in h.payload:
|
||||||
|
title = h.payload['title']
|
||||||
|
note_id = h.payload['note_id']
|
||||||
|
# Vermeide Duplikate (falls mehrere Chunks/Notes denselben Titel haben)
|
||||||
|
if title not in options:
|
||||||
|
options[title] = note_id
|
||||||
|
|
||||||
if options:
|
if options:
|
||||||
selected_title = st.selectbox("Ergebnisse:", list(options.keys()), key="cy_select")
|
selected_title = st.selectbox("Ergebnisse:", list(options.keys()), key="cy_select")
|
||||||
if st.button("Laden", use_container_width=True, key="cy_load"):
|
if st.button("Laden", use_container_width=True, key="cy_load"):
|
||||||
new_id = options[selected_title]
|
new_id = options[selected_title]
|
||||||
st.session_state.graph_center_id = new_id
|
st.session_state.graph_center_id = new_id
|
||||||
st.session_state.graph_inspected_id = new_id
|
st.session_state.graph_inspected_id = new_id
|
||||||
st.rerun()
|
st.rerun()
|
||||||
|
else:
|
||||||
|
# Zeige Info, wenn keine Ergebnisse gefunden wurden
|
||||||
|
st.info(f"Keine Notizen mit '{search_term}' im Titel gefunden.")
|
||||||
|
except Exception as e:
|
||||||
|
st.error(f"Fehler bei der Suche: {e}")
|
||||||
|
import traceback
|
||||||
|
st.code(traceback.format_exc())
|
||||||
|
|
||||||
st.divider()
|
st.divider()
|
||||||
|
|
||||||
@@ -139,6 +154,26 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
# 2. Detail Daten (Inspector)
|
# 2. Detail Daten (Inspector)
|
||||||
inspected_data = graph_service.get_note_with_full_content(inspected_id)
|
inspected_data = graph_service.get_note_with_full_content(inspected_id)
|
||||||
|
|
||||||
|
# DEBUG: Zeige Debug-Informationen
|
||||||
|
with st.expander("🔍 Debug-Informationen", expanded=False):
|
||||||
|
st.write(f"**Gefundene Knoten:** {len(nodes_data) if nodes_data else 0}")
|
||||||
|
st.write(f"**Gefundene Kanten:** {len(edges_data) if edges_data else 0}")
|
||||||
|
if nodes_data:
|
||||||
|
st.write("**Knoten-IDs:**")
|
||||||
|
for n in nodes_data[:10]:
|
||||||
|
nid = getattr(n, 'id', 'N/A')
|
||||||
|
st.write(f" - {nid}")
|
||||||
|
if len(nodes_data) > 10:
|
||||||
|
st.write(f" ... und {len(nodes_data) - 10} weitere")
|
||||||
|
if edges_data:
|
||||||
|
st.write("**Kanten:**")
|
||||||
|
for e in edges_data[:10]:
|
||||||
|
src = getattr(e, "source", "N/A")
|
||||||
|
tgt = getattr(e, "to", getattr(e, "target", "N/A"))
|
||||||
|
st.write(f" - {src} -> {tgt}")
|
||||||
|
if len(edges_data) > 10:
|
||||||
|
st.write(f" ... und {len(edges_data) - 10} weitere")
|
||||||
|
|
||||||
# --- ACTION BAR ---
|
# --- ACTION BAR ---
|
||||||
action_container = st.container()
|
action_container = st.container()
|
||||||
with action_container:
|
with action_container:
|
||||||
@@ -174,7 +209,12 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
st.markdown(f"**ID:** `{inspected_data.get('note_id')}`")
|
st.markdown(f"**ID:** `{inspected_data.get('note_id')}`")
|
||||||
st.markdown(f"**Typ:** `{inspected_data.get('type')}`")
|
st.markdown(f"**Typ:** `{inspected_data.get('type')}`")
|
||||||
with col_i2:
|
with col_i2:
|
||||||
st.markdown(f"**Tags:** {', '.join(inspected_data.get('tags', []))}")
|
tags = inspected_data.get('tags', [])
|
||||||
|
if isinstance(tags, list):
|
||||||
|
tags_str = ', '.join(tags) if tags else "Keine"
|
||||||
|
else:
|
||||||
|
tags_str = str(tags) if tags else "Keine"
|
||||||
|
st.markdown(f"**Tags:** {tags_str}")
|
||||||
path_check = "✅" if inspected_data.get('path') else "❌"
|
path_check = "✅" if inspected_data.get('path') else "❌"
|
||||||
st.markdown(f"**Pfad:** {path_check}")
|
st.markdown(f"**Pfad:** {path_check}")
|
||||||
|
|
||||||
@@ -189,12 +229,27 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
# --- GRAPH ELEMENTS ---
|
# --- GRAPH ELEMENTS ---
|
||||||
cy_elements = []
|
cy_elements = []
|
||||||
|
|
||||||
|
# Validierung: Prüfe ob nodes_data vorhanden ist
|
||||||
|
if not nodes_data:
|
||||||
|
st.warning("⚠️ Keine Knoten gefunden. Bitte wähle eine andere Notiz.")
|
||||||
|
# Zeige trotzdem den Inspector, falls Daten vorhanden
|
||||||
|
if inspected_data:
|
||||||
|
st.info(f"**Hinweis:** Die Notiz '{inspected_data.get('title', inspected_id)}' wurde gefunden, hat aber keine Verbindungen im Graphen.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Erstelle Set aller Node-IDs für schnelle Validierung
|
||||||
|
node_ids = {n.id for n in nodes_data if hasattr(n, 'id') and n.id}
|
||||||
|
|
||||||
|
# Nodes hinzufügen
|
||||||
for n in nodes_data:
|
for n in nodes_data:
|
||||||
|
if not hasattr(n, 'id') or not n.id:
|
||||||
|
continue
|
||||||
|
|
||||||
is_center = (n.id == center_id)
|
is_center = (n.id == center_id)
|
||||||
is_inspected = (n.id == inspected_id)
|
is_inspected = (n.id == inspected_id)
|
||||||
|
|
||||||
tooltip_text = n.title if n.title else n.label
|
tooltip_text = getattr(n, 'title', None) or getattr(n, 'label', '')
|
||||||
display_label = n.label
|
display_label = getattr(n, 'label', str(n.id))
|
||||||
if len(display_label) > 15 and " " in display_label:
|
if len(display_label) > 15 and " " in display_label:
|
||||||
display_label = display_label.replace(" ", "\n", 1)
|
display_label = display_label.replace(" ", "\n", 1)
|
||||||
|
|
||||||
@@ -202,7 +257,7 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
"data": {
|
"data": {
|
||||||
"id": n.id,
|
"id": n.id,
|
||||||
"label": display_label,
|
"label": display_label,
|
||||||
"bg_color": n.color,
|
"bg_color": getattr(n, 'color', '#8395a7'),
|
||||||
"tooltip": tooltip_text
|
"tooltip": tooltip_text
|
||||||
},
|
},
|
||||||
# Wir steuern das Aussehen rein über Klassen (.inspected / .center)
|
# Wir steuern das Aussehen rein über Klassen (.inspected / .center)
|
||||||
@@ -211,18 +266,22 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
}
|
}
|
||||||
cy_elements.append(cy_node)
|
cy_elements.append(cy_node)
|
||||||
|
|
||||||
for e in edges_data:
|
# Edges hinzufügen - nur wenn beide Nodes im Graph vorhanden sind
|
||||||
target_id = getattr(e, "to", getattr(e, "target", None))
|
if edges_data:
|
||||||
if target_id:
|
for e in edges_data:
|
||||||
cy_edge = {
|
source_id = getattr(e, "source", None)
|
||||||
"data": {
|
target_id = getattr(e, "to", getattr(e, "target", None))
|
||||||
"source": e.source,
|
# Nur hinzufügen, wenn beide IDs vorhanden UND beide Nodes im Graph sind
|
||||||
"target": target_id,
|
if source_id and target_id and source_id in node_ids and target_id in node_ids:
|
||||||
"label": e.label,
|
cy_edge = {
|
||||||
"line_color": e.color
|
"data": {
|
||||||
|
"source": source_id,
|
||||||
|
"target": target_id,
|
||||||
|
"label": getattr(e, "label", ""),
|
||||||
|
"line_color": getattr(e, "color", "#bdc3c7")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
cy_elements.append(cy_edge)
|
||||||
cy_elements.append(cy_edge)
|
|
||||||
|
|
||||||
# --- STYLESHEET ---
|
# --- STYLESHEET ---
|
||||||
stylesheet = [
|
stylesheet = [
|
||||||
@@ -292,43 +351,47 @@ def render_graph_explorer_cytoscape(graph_service):
|
|||||||
]
|
]
|
||||||
|
|
||||||
# --- RENDER ---
|
# --- RENDER ---
|
||||||
graph_key = f"cy_{center_id}_{st.session_state.cy_depth}_{st.session_state.cy_ideal_edge_len}"
|
# Nur rendern, wenn Elemente vorhanden sind
|
||||||
|
if not cy_elements:
|
||||||
|
st.warning("⚠️ Keine Graph-Elemente zum Anzeigen gefunden.")
|
||||||
|
else:
|
||||||
|
graph_key = f"cy_{center_id}_{st.session_state.cy_depth}_{st.session_state.cy_ideal_edge_len}"
|
||||||
|
|
||||||
clicked_elements = cytoscape(
|
clicked_elements = cytoscape(
|
||||||
elements=cy_elements,
|
elements=cy_elements,
|
||||||
stylesheet=stylesheet,
|
stylesheet=stylesheet,
|
||||||
layout={
|
layout={
|
||||||
"name": "cose",
|
"name": "cose",
|
||||||
"idealEdgeLength": st.session_state.cy_ideal_edge_len,
|
"idealEdgeLength": st.session_state.cy_ideal_edge_len,
|
||||||
"nodeOverlap": 20,
|
"nodeOverlap": 20,
|
||||||
"refresh": 20,
|
"refresh": 20,
|
||||||
"fit": True,
|
"fit": True,
|
||||||
"padding": 50,
|
"padding": 50,
|
||||||
"randomize": False,
|
"randomize": False,
|
||||||
"componentSpacing": 100,
|
"componentSpacing": 100,
|
||||||
"nodeRepulsion": st.session_state.cy_node_repulsion,
|
"nodeRepulsion": st.session_state.cy_node_repulsion,
|
||||||
"edgeElasticity": 100,
|
"edgeElasticity": 100,
|
||||||
"nestingFactor": 5,
|
"nestingFactor": 5,
|
||||||
"gravity": 80,
|
"gravity": 80,
|
||||||
"numIter": 1000,
|
"numIter": 1000,
|
||||||
"initialTemp": 200,
|
"initialTemp": 200,
|
||||||
"coolingFactor": 0.95,
|
"coolingFactor": 0.95,
|
||||||
"minTemp": 1.0,
|
"minTemp": 1.0,
|
||||||
"animate": False
|
"animate": False
|
||||||
},
|
},
|
||||||
key=graph_key,
|
key=graph_key,
|
||||||
height="700px"
|
height="700px"
|
||||||
)
|
)
|
||||||
|
|
||||||
# --- EVENT HANDLING ---
|
# --- EVENT HANDLING ---
|
||||||
if clicked_elements:
|
if clicked_elements:
|
||||||
clicked_nodes = clicked_elements.get("nodes", [])
|
clicked_nodes = clicked_elements.get("nodes", [])
|
||||||
if clicked_nodes:
|
if clicked_nodes:
|
||||||
clicked_id = clicked_nodes[0]
|
clicked_id = clicked_nodes[0]
|
||||||
|
|
||||||
if clicked_id != st.session_state.graph_inspected_id:
|
if clicked_id != st.session_state.graph_inspected_id:
|
||||||
st.session_state.graph_inspected_id = clicked_id
|
st.session_state.graph_inspected_id = clicked_id
|
||||||
st.rerun()
|
st.rerun()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
st.info("👈 Bitte wähle links eine Notiz aus.")
|
st.info("👈 Bitte wähle links eine Notiz aus.")
|
||||||
@@ -10,15 +10,19 @@ LAST_ANALYSIS: 2025-12-15
|
|||||||
import re
|
import re
|
||||||
from qdrant_client import QdrantClient, models
|
from qdrant_client import QdrantClient, models
|
||||||
from streamlit_agraph import Node, Edge
|
from streamlit_agraph import Node, Edge
|
||||||
from ui_config import GRAPH_COLORS, get_edge_color, SYSTEM_EDGES
|
from ui_config import COLLECTION_PREFIX, GRAPH_COLORS, get_edge_color, SYSTEM_EDGES
|
||||||
|
|
||||||
class GraphExplorerService:
|
class GraphExplorerService:
|
||||||
def __init__(self, url, api_key=None, prefix="mindnet"):
|
def __init__(self, url, api_key=None, prefix=None):
|
||||||
|
"""
|
||||||
|
Initialisiert den Service. Nutzt COLLECTION_PREFIX aus der Config,
|
||||||
|
sofern kein spezifischer Prefix übergeben wurde.
|
||||||
|
"""
|
||||||
self.client = QdrantClient(url=url, api_key=api_key)
|
self.client = QdrantClient(url=url, api_key=api_key)
|
||||||
self.prefix = prefix
|
self.prefix = prefix if prefix else COLLECTION_PREFIX
|
||||||
self.notes_col = f"{prefix}_notes"
|
self.notes_col = f"{self.prefix}_notes"
|
||||||
self.chunks_col = f"{prefix}_chunks"
|
self.chunks_col = f"{self.prefix}_chunks"
|
||||||
self.edges_col = f"{prefix}_edges"
|
self.edges_col = f"{self.prefix}_edges"
|
||||||
self._note_cache = {}
|
self._note_cache = {}
|
||||||
|
|
||||||
def get_note_with_full_content(self, note_id):
|
def get_note_with_full_content(self, note_id):
|
||||||
@@ -163,31 +167,33 @@ class GraphExplorerService:
|
|||||||
return previews
|
return previews
|
||||||
|
|
||||||
def _find_connected_edges(self, note_ids, note_title=None):
|
def _find_connected_edges(self, note_ids, note_title=None):
|
||||||
"""Findet eingehende und ausgehende Kanten."""
|
"""
|
||||||
|
Findet eingehende und ausgehende Kanten.
|
||||||
|
|
||||||
|
WICHTIG: target_id enthält nur den Titel (ohne #Abschnitt).
|
||||||
|
target_section ist ein separates Feld für Abschnitt-Informationen.
|
||||||
|
"""
|
||||||
results = []
|
results = []
|
||||||
|
if not note_ids:
|
||||||
|
return results
|
||||||
|
|
||||||
# 1. OUTGOING EDGES (Der "Owner"-Fix)
|
# 1. OUTGOING EDGES (Der "Owner"-Fix)
|
||||||
# Wir suchen Kanten, die im Feld 'note_id' (Owner) eine unserer Notizen haben.
|
# Wir suchen Kanten, die im Feld 'note_id' (Owner) eine unserer Notizen haben.
|
||||||
# Das findet ALLE ausgehenden Kanten, egal ob sie an einem Chunk oder der Note hängen.
|
# Das findet ALLE ausgehenden Kanten, egal ob sie an einem Chunk oder der Note hängen.
|
||||||
if note_ids:
|
out_filter = models.Filter(must=[
|
||||||
out_filter = models.Filter(must=[
|
models.FieldCondition(key="note_id", match=models.MatchAny(any=note_ids)),
|
||||||
models.FieldCondition(key="note_id", match=models.MatchAny(any=note_ids)),
|
models.FieldCondition(key="kind", match=models.MatchExcept(**{"except": SYSTEM_EDGES}))
|
||||||
models.FieldCondition(key="kind", match=models.MatchExcept(**{"except": SYSTEM_EDGES}))
|
])
|
||||||
])
|
res_out, _ = self.client.scroll(self.edges_col, scroll_filter=out_filter, limit=2000, with_payload=True)
|
||||||
# Limit hoch, um alles zu finden
|
results.extend(res_out)
|
||||||
res_out, _ = self.client.scroll(self.edges_col, scroll_filter=out_filter, limit=500, with_payload=True)
|
|
||||||
results.extend(res_out)
|
|
||||||
|
|
||||||
# 2. INCOMING EDGES (Ziel = Chunk ID oder Titel oder Note ID)
|
# 2. INCOMING EDGES (Ziel = Chunk ID, Note ID oder Titel)
|
||||||
# Hier müssen wir Chunks auflösen, um Treffer auf Chunks zu finden.
|
# WICHTIG: target_id enthält nur den Titel, target_section ist separat
|
||||||
|
|
||||||
# Chunk IDs der aktuellen Notes holen
|
# Chunk IDs der aktuellen Notes holen
|
||||||
chunk_ids = []
|
c_filter = models.Filter(must=[models.FieldCondition(key="note_id", match=models.MatchAny(any=note_ids))])
|
||||||
if note_ids:
|
chunks, _ = self.client.scroll(self.chunks_col, scroll_filter=c_filter, limit=1000, with_payload=False)
|
||||||
c_filter = models.Filter(must=[models.FieldCondition(key="note_id", match=models.MatchAny(any=note_ids))])
|
chunk_ids = [c.id for c in chunks]
|
||||||
chunks, _ = self.client.scroll(self.chunks_col, scroll_filter=c_filter, limit=300)
|
|
||||||
chunk_ids = [c.id for c in chunks]
|
|
||||||
|
|
||||||
shoulds = []
|
shoulds = []
|
||||||
# Case A: Edge zeigt auf einen unserer Chunks
|
# Case A: Edge zeigt auf einen unserer Chunks
|
||||||
@@ -195,42 +201,92 @@ class GraphExplorerService:
|
|||||||
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=chunk_ids)))
|
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=chunk_ids)))
|
||||||
|
|
||||||
# Case B: Edge zeigt direkt auf unsere Note ID
|
# Case B: Edge zeigt direkt auf unsere Note ID
|
||||||
if note_ids:
|
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=note_ids)))
|
||||||
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchAny(any=note_ids)))
|
|
||||||
|
|
||||||
# Case C: Edge zeigt auf unseren Titel (Wikilinks)
|
# Case C: Edge zeigt auf unseren Titel
|
||||||
|
# WICHTIG: target_id enthält nur den Titel (z.B. "Meine Prinzipien 2025")
|
||||||
|
# target_section enthält die Abschnitt-Information (z.B. "P3 – Disziplin"), wenn gesetzt
|
||||||
|
|
||||||
|
# Sammle alle relevanten Titel (inkl. Aliase)
|
||||||
|
titles_to_search = []
|
||||||
if note_title:
|
if note_title:
|
||||||
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchValue(value=note_title)))
|
titles_to_search.append(note_title)
|
||||||
|
|
||||||
|
# Lade auch Titel aus den Notes selbst (falls note_title nicht übergeben wurde)
|
||||||
|
for nid in note_ids:
|
||||||
|
note = self._fetch_note_cached(nid)
|
||||||
|
if note:
|
||||||
|
note_title_from_db = note.get("title")
|
||||||
|
if note_title_from_db and note_title_from_db not in titles_to_search:
|
||||||
|
titles_to_search.append(note_title_from_db)
|
||||||
|
# Aliase hinzufügen
|
||||||
|
aliases = note.get("aliases", [])
|
||||||
|
if isinstance(aliases, str):
|
||||||
|
aliases = [aliases]
|
||||||
|
for alias in aliases:
|
||||||
|
if alias and alias not in titles_to_search:
|
||||||
|
titles_to_search.append(alias)
|
||||||
|
|
||||||
|
# Für jeden Titel: Suche nach exaktem Match
|
||||||
|
# target_id enthält nur den Titel, daher reicht MatchValue
|
||||||
|
for title in titles_to_search:
|
||||||
|
shoulds.append(models.FieldCondition(key="target_id", match=models.MatchValue(value=title)))
|
||||||
|
|
||||||
if shoulds:
|
if shoulds:
|
||||||
in_filter = models.Filter(
|
in_filter = models.Filter(
|
||||||
must=[models.FieldCondition(key="kind", match=models.MatchExcept(**{"except": SYSTEM_EDGES}))],
|
must=[models.FieldCondition(key="kind", match=models.MatchExcept(**{"except": SYSTEM_EDGES}))],
|
||||||
should=shoulds
|
should=shoulds
|
||||||
)
|
)
|
||||||
res_in, _ = self.client.scroll(self.edges_col, scroll_filter=in_filter, limit=500, with_payload=True)
|
res_in, _ = self.client.scroll(self.edges_col, scroll_filter=in_filter, limit=2000, with_payload=True)
|
||||||
results.extend(res_in)
|
results.extend(res_in)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def _find_connected_edges_batch(self, note_ids):
|
def _find_connected_edges_batch(self, note_ids):
|
||||||
# Wrapper für Level 2 Suche
|
"""
|
||||||
return self._find_connected_edges(note_ids)
|
Wrapper für Level 2 Suche.
|
||||||
|
Lädt Titel der ersten Note für Titel-basierte Suche.
|
||||||
|
"""
|
||||||
|
if not note_ids:
|
||||||
|
return []
|
||||||
|
first_note = self._fetch_note_cached(note_ids[0])
|
||||||
|
note_title = first_note.get("title") if first_note else None
|
||||||
|
return self._find_connected_edges(note_ids, note_title=note_title)
|
||||||
|
|
||||||
def _process_edge(self, record, nodes_dict, unique_edges, current_depth):
|
def _process_edge(self, record, nodes_dict, unique_edges, current_depth):
|
||||||
"""Verarbeitet eine rohe Edge, löst IDs auf und fügt sie den Dictionaries hinzu."""
|
"""
|
||||||
|
Verarbeitet eine rohe Edge, löst IDs auf und fügt sie den Dictionaries hinzu.
|
||||||
|
|
||||||
|
WICHTIG: Beide Richtungen werden unterstützt:
|
||||||
|
- Ausgehende Kanten: source_id gehört zu unserer Note (via note_id Owner)
|
||||||
|
- Eingehende Kanten: target_id zeigt auf unsere Note (via target_id Match)
|
||||||
|
"""
|
||||||
|
if not record or not record.payload:
|
||||||
|
return None, None
|
||||||
|
|
||||||
payload = record.payload
|
payload = record.payload
|
||||||
src_ref = payload.get("source_id")
|
src_ref = payload.get("source_id")
|
||||||
tgt_ref = payload.get("target_id")
|
tgt_ref = payload.get("target_id")
|
||||||
kind = payload.get("kind")
|
kind = payload.get("kind")
|
||||||
provenance = payload.get("provenance", "explicit")
|
provenance = payload.get("provenance", "explicit")
|
||||||
|
|
||||||
|
# Prüfe, ob beide Referenzen vorhanden sind
|
||||||
|
if not src_ref or not tgt_ref:
|
||||||
|
return None, None
|
||||||
|
|
||||||
# IDs zu Notes auflösen
|
# IDs zu Notes auflösen
|
||||||
|
# WICHTIG: source_id kann Chunk-ID (note_id#c01), Note-ID oder Titel sein
|
||||||
|
# WICHTIG: target_id kann Chunk-ID, Note-ID oder Titel sein (ohne #Abschnitt)
|
||||||
src_note = self._resolve_note_from_ref(src_ref)
|
src_note = self._resolve_note_from_ref(src_ref)
|
||||||
tgt_note = self._resolve_note_from_ref(tgt_ref)
|
tgt_note = self._resolve_note_from_ref(tgt_ref)
|
||||||
|
|
||||||
if src_note and tgt_note:
|
if src_note and tgt_note:
|
||||||
src_id = src_note['note_id']
|
src_id = src_note.get('note_id')
|
||||||
tgt_id = tgt_note['note_id']
|
tgt_id = tgt_note.get('note_id')
|
||||||
|
|
||||||
|
# Prüfe, ob beide IDs vorhanden sind
|
||||||
|
if not src_id or not tgt_id:
|
||||||
|
return None, None
|
||||||
|
|
||||||
if src_id != tgt_id:
|
if src_id != tgt_id:
|
||||||
# Nodes hinzufügen
|
# Nodes hinzufügen
|
||||||
@@ -245,7 +301,7 @@ class GraphExplorerService:
|
|||||||
# Bevorzuge explizite Kanten vor Smart Kanten
|
# Bevorzuge explizite Kanten vor Smart Kanten
|
||||||
is_current_explicit = (provenance in ["explicit", "rule"])
|
is_current_explicit = (provenance in ["explicit", "rule"])
|
||||||
if existing:
|
if existing:
|
||||||
is_existing_explicit = (existing['provenance'] in ["explicit", "rule"])
|
is_existing_explicit = (existing.get('provenance', '') in ["explicit", "rule"])
|
||||||
if is_existing_explicit and not is_current_explicit:
|
if is_existing_explicit and not is_current_explicit:
|
||||||
should_update = False
|
should_update = False
|
||||||
|
|
||||||
@@ -267,38 +323,109 @@ class GraphExplorerService:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def _resolve_note_from_ref(self, ref_str):
|
def _resolve_note_from_ref(self, ref_str):
|
||||||
"""Löst eine ID (Chunk, Note oder Titel) zu einer Note Payload auf."""
|
"""
|
||||||
if not ref_str: return None
|
Löst eine Referenz zu einer Note Payload auf.
|
||||||
|
|
||||||
# Fall A: Chunk ID (enthält #)
|
WICHTIG: Wenn ref_str ein Titel#Abschnitt Format hat, wird nur der Titel-Teil verwendet.
|
||||||
|
Unterstützt:
|
||||||
|
- Note-ID: "20250101-meine-note"
|
||||||
|
- Chunk-ID: "20250101-meine-note#c01"
|
||||||
|
- Titel: "Meine Prinzipien 2025"
|
||||||
|
- Titel#Abschnitt: "Meine Prinzipien 2025#P3 – Disziplin" (trennt Abschnitt ab, sucht nur nach Titel)
|
||||||
|
"""
|
||||||
|
if not ref_str:
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Fall A: Enthält # (kann Chunk-ID oder Titel#Abschnitt sein)
|
||||||
if "#" in ref_str:
|
if "#" in ref_str:
|
||||||
try:
|
try:
|
||||||
# Versuch 1: Chunk ID direkt
|
# Versuch 1: Chunk ID direkt (Format: note_id#c01)
|
||||||
res = self.client.retrieve(self.chunks_col, ids=[ref_str], with_payload=True)
|
res = self.client.retrieve(self.chunks_col, ids=[ref_str], with_payload=True)
|
||||||
if res: return self._fetch_note_cached(res[0].payload.get("note_id"))
|
if res and res[0].payload:
|
||||||
except: pass
|
note_id = res[0].payload.get("note_id")
|
||||||
|
if note_id:
|
||||||
|
return self._fetch_note_cached(note_id)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
# Versuch 2: NoteID#Section (Hash abtrennen)
|
# Versuch 2: NoteID#Section (Hash abtrennen und als Note-ID versuchen)
|
||||||
possible_note_id = ref_str.split("#")[0]
|
# z.B. "20250101-meine-note#Abschnitt" -> "20250101-meine-note"
|
||||||
if self._fetch_note_cached(possible_note_id): return self._fetch_note_cached(possible_note_id)
|
possible_note_id = ref_str.split("#")[0].strip()
|
||||||
|
note = self._fetch_note_cached(possible_note_id)
|
||||||
|
if note:
|
||||||
|
return note
|
||||||
|
|
||||||
|
# Versuch 3: Titel#Abschnitt (Hash abtrennen und als Titel suchen)
|
||||||
|
# z.B. "Meine Prinzipien 2025#P3 – Disziplin" -> "Meine Prinzipien 2025"
|
||||||
|
# WICHTIG: target_id enthält nur den Titel, daher suchen wir nur nach dem Titel-Teil
|
||||||
|
possible_title = ref_str.split("#")[0].strip()
|
||||||
|
if possible_title:
|
||||||
|
res, _ = self.client.scroll(
|
||||||
|
collection_name=self.notes_col,
|
||||||
|
scroll_filter=models.Filter(must=[
|
||||||
|
models.FieldCondition(key="title", match=models.MatchValue(value=possible_title))
|
||||||
|
]),
|
||||||
|
limit=1, with_payload=True
|
||||||
|
)
|
||||||
|
if res and res[0].payload:
|
||||||
|
payload = res[0].payload
|
||||||
|
self._note_cache[payload['note_id']] = payload
|
||||||
|
return payload
|
||||||
|
|
||||||
|
# Fallback: Text-Suche für Fuzzy-Matching
|
||||||
|
res, _ = self.client.scroll(
|
||||||
|
collection_name=self.notes_col,
|
||||||
|
scroll_filter=models.Filter(must=[
|
||||||
|
models.FieldCondition(key="title", match=models.MatchText(text=possible_title))
|
||||||
|
]),
|
||||||
|
limit=10, with_payload=True
|
||||||
|
)
|
||||||
|
if res:
|
||||||
|
# Nimm das erste Ergebnis, das exakt oder beginnend mit possible_title übereinstimmt
|
||||||
|
for r in res:
|
||||||
|
if r.payload:
|
||||||
|
note_title = r.payload.get("title", "")
|
||||||
|
if note_title == possible_title or note_title.startswith(possible_title):
|
||||||
|
payload = r.payload
|
||||||
|
self._note_cache[payload['note_id']] = payload
|
||||||
|
return payload
|
||||||
|
|
||||||
# Fall B: Note ID direkt
|
# Fall B: Note ID direkt
|
||||||
if self._fetch_note_cached(ref_str): return self._fetch_note_cached(ref_str)
|
note = self._fetch_note_cached(ref_str)
|
||||||
|
if note:
|
||||||
|
return note
|
||||||
|
|
||||||
# Fall C: Titel
|
# Fall C: Titel (exakte Übereinstimmung)
|
||||||
res, _ = self.client.scroll(
|
res, _ = self.client.scroll(
|
||||||
collection_name=self.notes_col,
|
collection_name=self.notes_col,
|
||||||
scroll_filter=models.Filter(must=[models.FieldCondition(key="title", match=models.MatchValue(value=ref_str))]),
|
scroll_filter=models.Filter(must=[
|
||||||
|
models.FieldCondition(key="title", match=models.MatchValue(value=ref_str))
|
||||||
|
]),
|
||||||
limit=1, with_payload=True
|
limit=1, with_payload=True
|
||||||
)
|
)
|
||||||
if res:
|
if res and res[0].payload:
|
||||||
self._note_cache[res[0].payload['note_id']] = res[0].payload
|
payload = res[0].payload
|
||||||
return res[0].payload
|
self._note_cache[payload['note_id']] = payload
|
||||||
|
return payload
|
||||||
|
|
||||||
|
# Fall D: Titel (Text-Suche für Fuzzy-Matching)
|
||||||
|
res, _ = self.client.scroll(
|
||||||
|
collection_name=self.notes_col,
|
||||||
|
scroll_filter=models.Filter(must=[
|
||||||
|
models.FieldCondition(key="title", match=models.MatchText(text=ref_str))
|
||||||
|
]),
|
||||||
|
limit=1, with_payload=True
|
||||||
|
)
|
||||||
|
if res and res[0].payload:
|
||||||
|
payload = res[0].payload
|
||||||
|
self._note_cache[payload['note_id']] = payload
|
||||||
|
return payload
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _add_node_to_dict(self, node_dict, note_payload, level=1):
|
def _add_node_to_dict(self, node_dict, note_payload, level=1):
|
||||||
nid = note_payload.get("note_id")
|
nid = note_payload.get("note_id")
|
||||||
if nid in node_dict: return
|
if not nid or nid in node_dict: return
|
||||||
|
|
||||||
ntype = note_payload.get("type", "default")
|
ntype = note_payload.get("type", "default")
|
||||||
color = GRAPH_COLORS.get(ntype, GRAPH_COLORS["default"])
|
color = GRAPH_COLORS.get(ntype, GRAPH_COLORS["default"])
|
||||||
|
|||||||
+107
-19
@@ -1,25 +1,29 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/main.py
|
FILE: app/main.py
|
||||||
DESCRIPTION: Bootstrap der FastAPI Anwendung. Inkludiert Router und Middleware.
|
DESCRIPTION: Bootstrap der FastAPI Anwendung für WP-25a (Agentic MoE).
|
||||||
VERSION: 0.6.0
|
Orchestriert Lifespan-Events, globale Fehlerbehandlung und Routing.
|
||||||
|
Prüft beim Start die Integrität der Mixture of Experts Konfiguration.
|
||||||
|
VERSION: 1.1.0 (WP-25a: MoE Integrity Check)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: app.config, app.routers.* (embed, qdrant, query, graph, tools, feedback, chat, ingest, admin)
|
DEPENDENCIES: app.config, app.routers.*, app.services.llm_service
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from fastapi import FastAPI
|
import logging
|
||||||
from .config import get_settings
|
import os
|
||||||
#from .routers.embed_router import router as embed_router
|
from contextlib import asynccontextmanager
|
||||||
#from .routers.qdrant_router import router as qdrant_router
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
from .config import get_settings
|
||||||
|
from .services.llm_service import LLMService
|
||||||
|
|
||||||
|
# Import der Router
|
||||||
from .routers.query import router as query_router
|
from .routers.query import router as query_router
|
||||||
from .routers.graph import router as graph_router
|
from .routers.graph import router as graph_router
|
||||||
from .routers.tools import router as tools_router
|
from .routers.tools import router as tools_router
|
||||||
from .routers.feedback import router as feedback_router
|
from .routers.feedback import router as feedback_router
|
||||||
# NEU: Chat Router (WP-05)
|
|
||||||
from .routers.chat import router as chat_router
|
from .routers.chat import router as chat_router
|
||||||
# NEU: Ingest Router (WP-11)
|
|
||||||
from .routers.ingest import router as ingest_router
|
from .routers.ingest import router as ingest_router
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -27,26 +31,109 @@ try:
|
|||||||
except Exception:
|
except Exception:
|
||||||
admin_router = None
|
admin_router = None
|
||||||
|
|
||||||
|
from .core.logging_setup import setup_logging
|
||||||
|
|
||||||
|
# Initialisierung des Loggings noch VOR create_app()
|
||||||
|
setup_logging()
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# --- WP-25a: Lifespan Management mit MoE Integritäts-Prüfung ---
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
"""
|
||||||
|
Verwaltet den Lebenszyklus der Anwendung (Startup/Shutdown).
|
||||||
|
Verifiziert die Verfügbarkeit der MoE-Experten-Profile und Strategien.
|
||||||
|
"""
|
||||||
|
settings = get_settings()
|
||||||
|
logger.info("🚀 mindnet API: Starting up (WP-25a MoE Mode)...")
|
||||||
|
|
||||||
|
# 1. Startup: Integritäts-Check der MoE Konfiguration
|
||||||
|
# Wir prüfen die drei Säulen der Agentic-RAG Architektur.
|
||||||
|
decision_cfg = os.getenv("MINDNET_DECISION_CONFIG", "config/decision_engine.yaml")
|
||||||
|
profiles_cfg = getattr(settings, "LLM_PROFILES_PATH", "config/llm_profiles.yaml")
|
||||||
|
prompts_cfg = settings.PROMPTS_PATH
|
||||||
|
|
||||||
|
missing_files = []
|
||||||
|
if not os.path.exists(decision_cfg): missing_files.append(decision_cfg)
|
||||||
|
if not os.path.exists(profiles_cfg): missing_files.append(profiles_cfg)
|
||||||
|
if not os.path.exists(prompts_cfg): missing_files.append(prompts_cfg)
|
||||||
|
|
||||||
|
if missing_files:
|
||||||
|
logger.error(f"❌ CRITICAL: Missing MoE config files: {missing_files}")
|
||||||
|
else:
|
||||||
|
logger.info("✅ MoE Configuration files verified.")
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
# 2. Shutdown: Ressourcen bereinigen
|
||||||
|
logger.info("🛑 mindnet API: Shutting down...")
|
||||||
|
try:
|
||||||
|
llm = LLMService()
|
||||||
|
await llm.close()
|
||||||
|
logger.info("✨ LLM resources cleaned up.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Error during LLMService cleanup: {e}")
|
||||||
|
|
||||||
|
logger.info("Goodbye.")
|
||||||
|
|
||||||
|
# --- App Factory ---
|
||||||
|
|
||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
app = FastAPI(title="mindnet API", version="0.6.0") # Version bump WP-11
|
"""Initialisiert die FastAPI App mit WP-25a Erweiterungen."""
|
||||||
|
app = FastAPI(
|
||||||
|
title="mindnet API",
|
||||||
|
version="1.1.0", # WP-25a Milestone
|
||||||
|
lifespan=lifespan,
|
||||||
|
description="Digital Twin Knowledge Engine mit Mixture of Experts Orchestration."
|
||||||
|
)
|
||||||
|
|
||||||
s = get_settings()
|
s = get_settings()
|
||||||
|
|
||||||
|
# --- Globale Fehlerbehandlung (WP-25a Resilienz) ---
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def global_exception_handler(request: Request, exc: Exception):
|
||||||
|
"""Fängt unerwartete Fehler in der MoE-Prozesskette ab."""
|
||||||
|
logger.error(f"❌ Unhandled Engine Error: {exc}", exc_info=True)
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=500,
|
||||||
|
content={
|
||||||
|
"detail": "Ein interner Fehler ist in der MoE-Kette aufgetreten.",
|
||||||
|
"error_type": type(exc).__name__
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Healthcheck
|
||||||
@app.get("/healthz")
|
@app.get("/healthz")
|
||||||
def healthz():
|
def healthz():
|
||||||
return {"status": "ok", "qdrant": s.QDRANT_URL, "prefix": s.COLLECTION_PREFIX}
|
"""Bietet Statusinformationen über die Engine und Datenbank-Verbindung."""
|
||||||
|
# WP-24c v4.5.10: Prüfe EdgeDTO-Version zur Laufzeit
|
||||||
|
edge_dto_supports_callout = False
|
||||||
|
try:
|
||||||
|
from app.models.dto import EdgeDTO
|
||||||
|
import inspect
|
||||||
|
source = inspect.getsource(EdgeDTO)
|
||||||
|
edge_dto_supports_callout = "explicit:callout" in source
|
||||||
|
except Exception:
|
||||||
|
pass # Fehler beim Prüfen ist nicht kritisch
|
||||||
|
|
||||||
# app.include_router(embed_router)
|
return {
|
||||||
# app.include_router(qdrant_router)
|
"status": "ok",
|
||||||
|
"version": "1.1.0",
|
||||||
|
"qdrant": s.QDRANT_URL,
|
||||||
|
"prefix": s.COLLECTION_PREFIX,
|
||||||
|
"moe_enabled": True,
|
||||||
|
"edge_dto_supports_callout": edge_dto_supports_callout # WP-24c v4.5.10: Diagnose-Hilfe
|
||||||
|
}
|
||||||
|
|
||||||
|
# Inkludieren der Router (100% Kompatibilität erhalten)
|
||||||
app.include_router(query_router, prefix="/query", tags=["query"])
|
app.include_router(query_router, prefix="/query", tags=["query"])
|
||||||
app.include_router(graph_router, prefix="/graph", tags=["graph"])
|
app.include_router(graph_router, prefix="/graph", tags=["graph"])
|
||||||
app.include_router(tools_router, prefix="/tools", tags=["tools"])
|
app.include_router(tools_router, prefix="/tools", tags=["tools"])
|
||||||
app.include_router(feedback_router, prefix="/feedback", tags=["feedback"])
|
app.include_router(feedback_router, prefix="/feedback", tags=["feedback"])
|
||||||
|
app.include_router(chat_router, prefix="/chat", tags=["chat"]) # WP-25a Agentic Chat
|
||||||
# NEU: Chat Endpoint
|
|
||||||
app.include_router(chat_router, prefix="/chat", tags=["chat"])
|
|
||||||
|
|
||||||
# NEU: Ingest Endpoint
|
|
||||||
app.include_router(ingest_router, prefix="/ingest", tags=["ingest"])
|
app.include_router(ingest_router, prefix="/ingest", tags=["ingest"])
|
||||||
|
|
||||||
if admin_router:
|
if admin_router:
|
||||||
@@ -54,4 +141,5 @@ def create_app() -> FastAPI:
|
|||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
# Instanziierung der App
|
||||||
app = create_app()
|
app = create_app()
|
||||||
+42
-23
@@ -1,10 +1,9 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/models/dto.py
|
FILE: app/models/dto.py
|
||||||
DESCRIPTION: Pydantic-Modelle (DTOs) für Request/Response Bodies. Definiert das API-Schema.
|
DESCRIPTION: Pydantic-Modelle (DTOs) für Request/Response Bodies. Definiert das API-Schema.
|
||||||
VERSION: 0.6.6 (WP-22 Debug & Stability Update)
|
VERSION: 0.7.1 (WP-25: Stream-Tracing Support)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: pydantic, typing, uuid
|
DEPENDENCIES: pydantic, typing, uuid
|
||||||
LAST_ANALYSIS: 2025-12-18
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@@ -12,8 +11,14 @@ from pydantic import BaseModel, Field
|
|||||||
from typing import List, Literal, Optional, Dict, Any
|
from typing import List, Literal, Optional, Dict, Any
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
# Gültige Kanten-Typen gemäß Manual
|
# WP-25: Erweiterte Kanten-Typen gemäß neuer decision_engine.yaml
|
||||||
EdgeKind = Literal["references", "references_at", "backlink", "next", "prev", "belongs_to", "depends_on", "related_to", "similar_to", "caused_by", "derived_from", "based_on", "solves", "blocks", "uses", "guides"]
|
EdgeKind = Literal[
|
||||||
|
"references", "references_at", "backlink", "next", "prev",
|
||||||
|
"belongs_to", "depends_on", "related_to", "similar_to",
|
||||||
|
"caused_by", "derived_from", "based_on", "solves", "blocks",
|
||||||
|
"uses", "guides", "enforced_by", "implemented_in", "part_of",
|
||||||
|
"experienced_in", "impacts", "risk_of"
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
# --- Basis-DTOs ---
|
# --- Basis-DTOs ---
|
||||||
@@ -41,15 +46,24 @@ class EdgeDTO(BaseModel):
|
|||||||
target: str
|
target: str
|
||||||
weight: float
|
weight: float
|
||||||
direction: Literal["out", "in", "undirected"] = "out"
|
direction: Literal["out", "in", "undirected"] = "out"
|
||||||
provenance: Optional[Literal["explicit", "rule", "smart", "structure"]] = "explicit"
|
# WP-24c v4.5.3: Erweiterte Provenance-Werte für Chunk-Aware Edges
|
||||||
|
# Unterstützt alle tatsächlich verwendeten Provenance-Typen im System
|
||||||
|
provenance: Optional[Literal[
|
||||||
|
"explicit", "rule", "smart", "structure",
|
||||||
|
"explicit:callout", "explicit:wikilink", "explicit:note_zone", "explicit:note_scope",
|
||||||
|
"inline:rel", "callout:edge", "semantic_ai", "structure:belongs_to", "structure:order",
|
||||||
|
"derived:backlink", "edge_defaults", "global_pool"
|
||||||
|
]] = "explicit"
|
||||||
confidence: float = 1.0
|
confidence: float = 1.0
|
||||||
|
target_section: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
# --- Request Models ---
|
# --- Request Models ---
|
||||||
|
|
||||||
class QueryRequest(BaseModel):
|
class QueryRequest(BaseModel):
|
||||||
"""
|
"""
|
||||||
Request für /query.
|
Request für /query. Unterstützt Multi-Stream Isolation via filters.
|
||||||
|
WP-24c v4.1.0: Erweitert um Section-Filtering und Scope-Awareness.
|
||||||
"""
|
"""
|
||||||
mode: Literal["semantic", "edge", "hybrid"] = "hybrid"
|
mode: Literal["semantic", "edge", "hybrid"] = "hybrid"
|
||||||
query: Optional[str] = None
|
query: Optional[str] = None
|
||||||
@@ -60,14 +74,15 @@ class QueryRequest(BaseModel):
|
|||||||
ret: Dict = {"with_paths": True, "with_notes": True, "with_chunks": True}
|
ret: Dict = {"with_paths": True, "with_notes": True, "with_chunks": True}
|
||||||
explain: bool = False
|
explain: bool = False
|
||||||
|
|
||||||
# WP-22: Semantic Graph Routing
|
# WP-22/25: Dynamische Gewichtung der Graphen-Highways
|
||||||
boost_edges: Optional[Dict[str, float]] = None
|
boost_edges: Optional[Dict[str, float]] = None
|
||||||
|
|
||||||
|
# WP-24c v4.1.0: Section-Filtering für präzise Section-Links
|
||||||
|
target_section: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class FeedbackRequest(BaseModel):
|
class FeedbackRequest(BaseModel):
|
||||||
"""
|
"""User-Feedback zu einem spezifischen Treffer oder der Gesamtantwort."""
|
||||||
User-Feedback zu einem spezifischen Treffer oder der Gesamtantwort (WP-08 Basis).
|
|
||||||
"""
|
|
||||||
query_id: str = Field(..., description="ID der ursprünglichen Suche")
|
query_id: str = Field(..., description="ID der ursprünglichen Suche")
|
||||||
node_id: str = Field(..., description="ID des bewerteten Treffers oder 'generated_answer'")
|
node_id: str = Field(..., description="ID des bewerteten Treffers oder 'generated_answer'")
|
||||||
score: int = Field(..., ge=1, le=5, description="1 (Irrelevant) bis 5 (Perfekt)")
|
score: int = Field(..., ge=1, le=5, description="1 (Irrelevant) bis 5 (Perfekt)")
|
||||||
@@ -75,16 +90,14 @@ class FeedbackRequest(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class ChatRequest(BaseModel):
|
class ChatRequest(BaseModel):
|
||||||
"""
|
"""Request für /chat (WP-25 Einstieg)."""
|
||||||
WP-05: Request für /chat.
|
|
||||||
"""
|
|
||||||
message: str = Field(..., description="Die Nachricht des Users")
|
message: str = Field(..., description="Die Nachricht des Users")
|
||||||
conversation_id: Optional[str] = Field(None, description="ID für Chat-Verlauf")
|
conversation_id: Optional[str] = Field(None, description="ID für Chat-Verlauf")
|
||||||
top_k: int = 5
|
top_k: int = 5
|
||||||
explain: bool = False
|
explain: bool = False
|
||||||
|
|
||||||
|
|
||||||
# --- WP-04b Explanation Models ---
|
# --- Explanation Models ---
|
||||||
|
|
||||||
class ScoreBreakdown(BaseModel):
|
class ScoreBreakdown(BaseModel):
|
||||||
"""Aufschlüsselung der Score-Komponenten nach der WP-22 Formel."""
|
"""Aufschlüsselung der Score-Komponenten nach der WP-22 Formel."""
|
||||||
@@ -95,14 +108,14 @@ class ScoreBreakdown(BaseModel):
|
|||||||
raw_edge_bonus: float
|
raw_edge_bonus: float
|
||||||
raw_centrality: float
|
raw_centrality: float
|
||||||
node_weight: float
|
node_weight: float
|
||||||
# WP-22 Debug Fields für Messbarkeit
|
|
||||||
status_multiplier: float = 1.0
|
status_multiplier: float = 1.0
|
||||||
graph_boost_factor: float = 1.0
|
graph_boost_factor: float = 1.0
|
||||||
|
|
||||||
|
|
||||||
class Reason(BaseModel):
|
class Reason(BaseModel):
|
||||||
"""Ein semantischer Grund für das Ranking."""
|
"""Ein semantischer Grund für das Ranking."""
|
||||||
kind: Literal["semantic", "edge", "type", "centrality", "lifecycle"]
|
# WP-25: 'status' hinzugefügt für Synchronität mit retriever.py
|
||||||
|
kind: Literal["semantic", "edge", "type", "centrality", "lifecycle", "status"]
|
||||||
message: str
|
message: str
|
||||||
score_impact: Optional[float] = None
|
score_impact: Optional[float] = None
|
||||||
details: Optional[Dict[str, Any]] = None
|
details: Optional[Dict[str, Any]] = None
|
||||||
@@ -113,7 +126,6 @@ class Explanation(BaseModel):
|
|||||||
breakdown: ScoreBreakdown
|
breakdown: ScoreBreakdown
|
||||||
reasons: List[Reason]
|
reasons: List[Reason]
|
||||||
related_edges: Optional[List[EdgeDTO]] = None
|
related_edges: Optional[List[EdgeDTO]] = None
|
||||||
# WP-22 Debug: Verifizierung des Routings
|
|
||||||
applied_intent: Optional[str] = None
|
applied_intent: Optional[str] = None
|
||||||
applied_boosts: Optional[Dict[str, float]] = None
|
applied_boosts: Optional[Dict[str, float]] = None
|
||||||
|
|
||||||
@@ -121,7 +133,11 @@ class Explanation(BaseModel):
|
|||||||
# --- Response Models ---
|
# --- Response Models ---
|
||||||
|
|
||||||
class QueryHit(BaseModel):
|
class QueryHit(BaseModel):
|
||||||
"""Einzelnes Trefferobjekt für /query."""
|
"""
|
||||||
|
Einzelnes Trefferobjekt.
|
||||||
|
WP-25: stream_origin hinzugefügt für Tracing und Feedback-Optimierung.
|
||||||
|
WP-24c v4.1.0: source_chunk_id für RAG-Kontext hinzugefügt.
|
||||||
|
"""
|
||||||
node_id: str
|
node_id: str
|
||||||
note_id: str
|
note_id: str
|
||||||
semantic_score: float
|
semantic_score: float
|
||||||
@@ -132,10 +148,12 @@ class QueryHit(BaseModel):
|
|||||||
source: Optional[Dict] = None
|
source: Optional[Dict] = None
|
||||||
payload: Optional[Dict] = None
|
payload: Optional[Dict] = None
|
||||||
explanation: Optional[Explanation] = None
|
explanation: Optional[Explanation] = None
|
||||||
|
stream_origin: Optional[str] = Field(None, description="Name des Ursprungs-Streams")
|
||||||
|
source_chunk_id: Optional[str] = Field(None, description="Chunk-ID der Quelle (für RAG-Kontext)")
|
||||||
|
|
||||||
|
|
||||||
class QueryResponse(BaseModel):
|
class QueryResponse(BaseModel):
|
||||||
"""Antwortstruktur für /query."""
|
"""Antwortstruktur für /query (wird von DecisionEngine Streams genutzt)."""
|
||||||
query_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
query_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
||||||
results: List[QueryHit]
|
results: List[QueryHit]
|
||||||
used_mode: str
|
used_mode: str
|
||||||
@@ -152,11 +170,12 @@ class GraphResponse(BaseModel):
|
|||||||
|
|
||||||
class ChatResponse(BaseModel):
|
class ChatResponse(BaseModel):
|
||||||
"""
|
"""
|
||||||
WP-05/06: Antwortstruktur für /chat.
|
Antwortstruktur für /chat.
|
||||||
|
WP-25: 'intent' spiegelt nun die gewählte Strategie wider.
|
||||||
"""
|
"""
|
||||||
query_id: str = Field(..., description="Traceability ID")
|
query_id: str = Field(..., description="Traceability ID")
|
||||||
answer: str = Field(..., description="Generierte Antwort vom LLM")
|
answer: str = Field(..., description="Generierte Antwort vom LLM")
|
||||||
sources: List[QueryHit] = Field(..., description="Die genutzten Quellen")
|
sources: List[QueryHit] = Field(..., description="Die genutzten Quellen (alle Streams)")
|
||||||
latency_ms: int
|
latency_ms: int
|
||||||
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent")
|
intent: Optional[str] = Field("FACT", description="Die gewählte WP-25 Strategie")
|
||||||
intent_source: Optional[str] = Field("Unknown", description="Quelle der Intent-Erkennung")
|
intent_source: Optional[str] = Field("LLM_Router", description="Quelle der Intent-Erkennung")
|
||||||
+215
-248
@@ -1,65 +1,78 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/routers/chat.py
|
FILE: app/routers/chat.py
|
||||||
DESCRIPTION: Haupt-Chat-Interface (RAG & Interview). Enthält Intent-Router (Keywords/LLM) und Prompt-Construction.
|
DESCRIPTION: Haupt-Chat-Interface (WP-25b Edition).
|
||||||
VERSION: 2.7.8 (Full Unabridged Stability Edition)
|
Kombiniert die spezialisierte Interview-Logik mit der neuen
|
||||||
|
Lazy-Prompt-Orchestration und MoE-Synthese.
|
||||||
|
WP-24c: Integration der Discovery API für proaktive Vernetzung.
|
||||||
|
VERSION: 3.1.0 (WP-24c: Discovery API Integration)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
FIX:
|
FIX:
|
||||||
1. Implementiert Context-Throttling für Ollama (MAX_OLLAMA_CHARS).
|
- WP-24c: Neuer Endpunkt /query/discover für proaktive Kanten-Vorschläge.
|
||||||
2. Deaktiviert LLM-Retries für den Chat (max_retries=0).
|
- WP-25b: Umstellung des Interview-Modus auf Lazy-Prompt (prompt_key + variables).
|
||||||
3. Behebt Double-Fallback-Schleifen und Silent Refusals.
|
- WP-25b: Delegation der RAG-Phase an die Engine v1.3.0 für konsistente MoE-Steuerung.
|
||||||
|
- WP-25a: Voller Erhalt der v3.0.2 Logik (Interview, Schema-Resolution, FastPaths).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Depends
|
from fastapi import APIRouter, HTTPException, Depends
|
||||||
from typing import List, Dict, Any, Optional
|
from typing import List, Dict, Any, Optional
|
||||||
|
from pydantic import BaseModel
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
import logging
|
import logging
|
||||||
import yaml
|
import yaml
|
||||||
import os
|
import os
|
||||||
|
import asyncio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
from app.models.dto import ChatRequest, ChatResponse, QueryRequest, QueryHit
|
from app.models.dto import ChatRequest, ChatResponse, QueryHit, QueryRequest
|
||||||
from app.services.llm_service import LLMService
|
from app.services.llm_service import LLMService
|
||||||
from app.core.retriever import Retriever
|
|
||||||
from app.services.feedback_service import log_search
|
from app.services.feedback_service import log_search
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# --- Helper: Config Loader ---
|
# --- EBENE 0: DTOs FÜR DISCOVERY (WP-24c) ---
|
||||||
|
|
||||||
|
class DiscoveryRequest(BaseModel):
|
||||||
|
content: str
|
||||||
|
top_k: int = 8
|
||||||
|
min_confidence: float = 0.6
|
||||||
|
|
||||||
|
class DiscoveryHit(BaseModel):
|
||||||
|
target_note: str # Note ID
|
||||||
|
target_title: str # Menschenlesbarer Titel
|
||||||
|
suggested_edge_type: str # Kanonischer Typ aus edge_vocabulary
|
||||||
|
confidence_score: float # Kombinierter Vektor- + KI-Score
|
||||||
|
reasoning: str # Kurze Begründung der KI
|
||||||
|
|
||||||
|
# --- EBENE 1: CONFIG LOADER & CACHING (WP-25 Standard) ---
|
||||||
|
|
||||||
_DECISION_CONFIG_CACHE = None
|
_DECISION_CONFIG_CACHE = None
|
||||||
_TYPES_CONFIG_CACHE = None
|
_TYPES_CONFIG_CACHE = None
|
||||||
|
|
||||||
def _load_decision_config() -> Dict[str, Any]:
|
def _load_decision_config() -> Dict[str, Any]:
|
||||||
|
"""Lädt die Strategie-Konfiguration."""
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
path = Path(settings.DECISION_CONFIG_PATH)
|
path = Path(settings.DECISION_CONFIG_PATH)
|
||||||
default_config = {
|
|
||||||
"strategies": {
|
|
||||||
"FACT": {"trigger_keywords": [], "preferred_provider": "openrouter"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if not path.exists():
|
|
||||||
logger.warning(f"Decision config not found at {path}, using defaults.")
|
|
||||||
return default_config
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
if path.exists():
|
||||||
return yaml.safe_load(f)
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
return yaml.safe_load(f) or {}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to load decision config: {e}")
|
logger.error(f"Failed to load decision config: {e}")
|
||||||
return default_config
|
return {"strategies": {}}
|
||||||
|
|
||||||
def _load_types_config() -> Dict[str, Any]:
|
def _load_types_config() -> Dict[str, Any]:
|
||||||
"""Lädt die types.yaml für Keyword-Erkennung."""
|
"""Lädt die types.yaml für die Typerkennung."""
|
||||||
path = os.getenv("MINDNET_TYPES_FILE", "config/types.yaml")
|
path = os.getenv("MINDNET_TYPES_FILE", "config/types.yaml")
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
if os.path.exists(path):
|
||||||
return yaml.safe_load(f) or {}
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
except Exception:
|
return yaml.safe_load(f) or {}
|
||||||
return {}
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to load types config: {e}")
|
||||||
|
return {}
|
||||||
|
|
||||||
def get_full_config() -> Dict[str, Any]:
|
def get_full_config() -> Dict[str, Any]:
|
||||||
global _DECISION_CONFIG_CACHE
|
global _DECISION_CONFIG_CACHE
|
||||||
@@ -76,21 +89,17 @@ def get_types_config() -> Dict[str, Any]:
|
|||||||
def get_decision_strategy(intent: str) -> Dict[str, Any]:
|
def get_decision_strategy(intent: str) -> Dict[str, Any]:
|
||||||
config = get_full_config()
|
config = get_full_config()
|
||||||
strategies = config.get("strategies", {})
|
strategies = config.get("strategies", {})
|
||||||
return strategies.get(intent, strategies.get("FACT", {}))
|
return strategies.get(intent, strategies.get("FACT_WHAT", {}))
|
||||||
|
|
||||||
# --- Helper: Target Type Detection (WP-07) ---
|
# --- EBENE 2: SPEZIAL-LOGIK (INTERVIEW & DETECTION) ---
|
||||||
|
|
||||||
def _detect_target_type(message: str, configured_schemas: Dict[str, Any]) -> str:
|
def _detect_target_type(message: str, configured_schemas: Dict[str, Any]) -> str:
|
||||||
"""
|
"""WP-07: Identifiziert den gewünschten Notiz-Typ (Keyword-basiert)."""
|
||||||
Versucht zu erraten, welchen Notiz-Typ der User erstellen will.
|
|
||||||
Nutzt Keywords aus types.yaml UND Mappings.
|
|
||||||
"""
|
|
||||||
message_lower = message.lower()
|
message_lower = message.lower()
|
||||||
|
|
||||||
# 1. Check types.yaml detection_keywords (Priority!)
|
|
||||||
types_cfg = get_types_config()
|
types_cfg = get_types_config()
|
||||||
types_def = types_cfg.get("types", {})
|
types_def = types_cfg.get("types", {})
|
||||||
|
|
||||||
|
# 1. Check types.yaml detection_keywords
|
||||||
for type_name, type_data in types_def.items():
|
for type_name, type_data in types_def.items():
|
||||||
keywords = type_data.get("detection_keywords", [])
|
keywords = type_data.get("detection_keywords", [])
|
||||||
for kw in keywords:
|
for kw in keywords:
|
||||||
@@ -103,293 +112,251 @@ def _detect_target_type(message: str, configured_schemas: Dict[str, Any]) -> str
|
|||||||
if type_key in message_lower:
|
if type_key in message_lower:
|
||||||
return type_key
|
return type_key
|
||||||
|
|
||||||
# 3. Synonym-Mapping (Legacy Fallback)
|
# 3. Synonym-Mapping (Legacy)
|
||||||
synonyms = {
|
synonyms = {
|
||||||
"projekt": "project", "vorhaben": "project",
|
"projekt": "project", "entscheidung": "decision", "ziel": "goal",
|
||||||
"entscheidung": "decision", "beschluss": "decision",
|
"erfahrung": "experience", "wert": "value", "prinzip": "principle"
|
||||||
"ziel": "goal",
|
|
||||||
"erfahrung": "experience", "lektion": "experience",
|
|
||||||
"wert": "value",
|
|
||||||
"prinzip": "principle",
|
|
||||||
"notiz": "default", "idee": "default"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for term, schema_key in synonyms.items():
|
for term, schema_key in synonyms.items():
|
||||||
if term in message_lower:
|
if term in message_lower:
|
||||||
return schema_key
|
return schema_key
|
||||||
|
|
||||||
return "default"
|
return "default"
|
||||||
|
|
||||||
# --- Dependencies ---
|
|
||||||
|
|
||||||
def get_llm_service():
|
|
||||||
return LLMService()
|
|
||||||
|
|
||||||
def get_retriever():
|
|
||||||
return Retriever()
|
|
||||||
|
|
||||||
|
|
||||||
# --- Logic ---
|
|
||||||
|
|
||||||
def _build_enriched_context(hits: List[QueryHit]) -> str:
|
|
||||||
context_parts = []
|
|
||||||
for i, hit in enumerate(hits, 1):
|
|
||||||
source = hit.source or {}
|
|
||||||
content = (
|
|
||||||
source.get("text") or source.get("content") or
|
|
||||||
source.get("page_content") or source.get("chunk_text") or
|
|
||||||
"[Kein Text]"
|
|
||||||
)
|
|
||||||
title = hit.note_id or "Unbekannt"
|
|
||||||
|
|
||||||
payload = hit.payload or {}
|
|
||||||
note_type = payload.get("type") or source.get("type", "unknown")
|
|
||||||
note_type = str(note_type).upper()
|
|
||||||
|
|
||||||
entry = (
|
|
||||||
f"### QUELLE {i}: {title}\n"
|
|
||||||
f"TYP: [{note_type}] (Score: {hit.total_score:.2f})\n"
|
|
||||||
f"INHALT:\n{content}\n"
|
|
||||||
)
|
|
||||||
context_parts.append(entry)
|
|
||||||
|
|
||||||
return "\n\n".join(context_parts)
|
|
||||||
|
|
||||||
def _is_question(query: str) -> bool:
|
def _is_question(query: str) -> bool:
|
||||||
"""Prüft, ob der Input wahrscheinlich eine Frage ist."""
|
"""Prüft, ob der Input eine Frage ist."""
|
||||||
q = query.strip().lower()
|
q = query.strip().lower()
|
||||||
if "?" in q: return True
|
if "?" in q: return True
|
||||||
|
starters = ["wer", "wie", "was", "wo", "wann", "warum", "weshalb", "wozu", "welche", "bist du"]
|
||||||
# W-Fragen Indikatoren
|
return any(q.startswith(s + " ") for s in starters)
|
||||||
starters = ["wer", "wie", "was", "wo", "wann", "warum", "weshalb", "wozu", "welche", "bist du", "entspricht"]
|
|
||||||
if any(q.startswith(s + " ") for s in starters):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
||||||
"""
|
"""Hybrid Router: Keyword-Fast-Paths & DecisionEngine LLM Router."""
|
||||||
Hybrid Router v5:
|
|
||||||
1. Decision Keywords (Strategie) -> Prio 1
|
|
||||||
2. Type Keywords (Interview Trigger) -> Prio 2
|
|
||||||
3. LLM (Fallback) -> Prio 3
|
|
||||||
"""
|
|
||||||
config = get_full_config()
|
config = get_full_config()
|
||||||
strategies = config.get("strategies", {})
|
strategies = config.get("strategies", {})
|
||||||
settings = config.get("settings", {})
|
|
||||||
|
|
||||||
query_lower = query.lower()
|
query_lower = query.lower()
|
||||||
|
|
||||||
# 1. FAST PATH A: Strategie Keywords
|
# 1. FAST PATH: Keyword Trigger
|
||||||
for intent_name, strategy in strategies.items():
|
for intent_name, strategy in strategies.items():
|
||||||
if intent_name == "FACT": continue
|
|
||||||
keywords = strategy.get("trigger_keywords", [])
|
keywords = strategy.get("trigger_keywords", [])
|
||||||
for k in keywords:
|
for k in keywords:
|
||||||
if k.lower() in query_lower:
|
if k.lower() in query_lower:
|
||||||
return intent_name, "Keyword (Strategy)"
|
return intent_name, "Keyword (FastPath)"
|
||||||
|
|
||||||
# 2. FAST PATH B: Type Keywords -> INTERVIEW
|
# 2. FAST PATH B: Type Keywords -> INTERVIEW
|
||||||
if not _is_question(query_lower):
|
if not _is_question(query_lower):
|
||||||
types_cfg = get_types_config()
|
types_cfg = get_types_config()
|
||||||
types_def = types_cfg.get("types", {})
|
for type_name, type_data in types_cfg.get("types", {}).items():
|
||||||
|
for kw in type_data.get("detection_keywords", []):
|
||||||
for type_name, type_data in types_def.items():
|
|
||||||
keywords = type_data.get("detection_keywords", [])
|
|
||||||
for kw in keywords:
|
|
||||||
if kw.lower() in query_lower:
|
if kw.lower() in query_lower:
|
||||||
return "INTERVIEW", f"Keyword (Type: {type_name})"
|
return "INTERVIEW", "Keyword (Interview)"
|
||||||
|
|
||||||
# 3. SLOW PATH: LLM Router
|
# 3. SLOW PATH: DecisionEngine LLM Router (MoE-gesteuert)
|
||||||
if settings.get("llm_fallback_enabled", False):
|
intent = await llm.decision_engine._determine_strategy(query)
|
||||||
router_prompt_template = llm.get_prompt("llm_router_prompt")
|
return intent, "DecisionEngine (LLM)"
|
||||||
|
|
||||||
if router_prompt_template:
|
# --- EBENE 3: RETRIEVAL AGGREGATION ---
|
||||||
prompt = router_prompt_template.replace("{query}", query)
|
|
||||||
logger.info("Keywords failed (or Question detected). Asking LLM for Intent...")
|
|
||||||
|
|
||||||
try:
|
def _collect_all_hits(stream_responses: Dict[str, Any]) -> List[QueryHit]:
|
||||||
# FIX: Auch beim Routing keine Retries im Chat-Fluss
|
"""Sammelt deduplizierte Treffer aus allen Streams für das Tracing."""
|
||||||
raw_response = await llm.generate_raw_response(prompt, priority="realtime", max_retries=0)
|
all_hits = []
|
||||||
llm_output_upper = raw_response.upper()
|
seen_node_ids = set()
|
||||||
|
for _, response in stream_responses.items():
|
||||||
|
# Sammeln der Hits aus den QueryResponse Objekten
|
||||||
|
if hasattr(response, 'results'):
|
||||||
|
for hit in response.results:
|
||||||
|
if hit.node_id not in seen_node_ids:
|
||||||
|
all_hits.append(hit)
|
||||||
|
seen_node_ids.add(hit.node_id)
|
||||||
|
return sorted(all_hits, key=lambda h: h.total_score, reverse=True)
|
||||||
|
|
||||||
if "INTERVIEW" in llm_output_upper or "CREATE" in llm_output_upper:
|
# --- EBENE 4: ENDPUNKTE ---
|
||||||
return "INTERVIEW", "LLM Router"
|
|
||||||
|
|
||||||
for strat_key in strategies.keys():
|
def get_llm_service():
|
||||||
if strat_key in llm_output_upper:
|
return LLMService()
|
||||||
return strat_key, "LLM Router"
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Router LLM failed: {e}")
|
|
||||||
|
|
||||||
return "FACT", "Default (No Match)"
|
|
||||||
|
|
||||||
@router.post("/", response_model=ChatResponse)
|
@router.post("/", response_model=ChatResponse)
|
||||||
async def chat_endpoint(
|
async def chat_endpoint(
|
||||||
request: ChatRequest,
|
request: ChatRequest,
|
||||||
llm: LLMService = Depends(get_llm_service),
|
llm: LLMService = Depends(get_llm_service)
|
||||||
retriever: Retriever = Depends(get_retriever)
|
|
||||||
):
|
):
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
query_id = str(uuid.uuid4())
|
query_id = str(uuid.uuid4())
|
||||||
logger.info(f"Chat request [{query_id}]: {request.message[:50]}...")
|
logger.info(f"🚀 [WP-25b] Chat request [{query_id}]: {request.message[:50]}...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 1. Intent Detection
|
# 1. Intent Detection
|
||||||
intent, intent_source = await _classify_intent(request.message, llm)
|
intent, intent_source = await _classify_intent(request.message, llm)
|
||||||
logger.info(f"[{query_id}] Final Intent: {intent} via {intent_source}")
|
logger.info(f"[{query_id}] Intent: {intent} via {intent_source}")
|
||||||
|
|
||||||
# Strategy Load
|
|
||||||
strategy = get_decision_strategy(intent)
|
strategy = get_decision_strategy(intent)
|
||||||
prompt_key = strategy.get("prompt_template", "rag_template")
|
engine = llm.decision_engine
|
||||||
preferred_provider = strategy.get("preferred_provider")
|
|
||||||
|
|
||||||
sources_hits = []
|
sources_hits = []
|
||||||
final_prompt = ""
|
answer_text = ""
|
||||||
context_str = ""
|
|
||||||
|
|
||||||
|
# 2. INTERVIEW MODE (WP-25b Lazy-Prompt Logik)
|
||||||
if intent == "INTERVIEW":
|
if intent == "INTERVIEW":
|
||||||
# --- INTERVIEW MODE ---
|
|
||||||
target_type = _detect_target_type(request.message, strategy.get("schemas", {}))
|
target_type = _detect_target_type(request.message, strategy.get("schemas", {}))
|
||||||
|
|
||||||
types_cfg = get_types_config()
|
types_cfg = get_types_config()
|
||||||
type_def = types_cfg.get("types", {}).get(target_type, {})
|
type_def = types_cfg.get("types", {}).get(target_type, {})
|
||||||
fields_list = type_def.get("schema", [])
|
fields_list = type_def.get("schema", [])
|
||||||
|
|
||||||
|
# WP-07: Restaurierte Fallback Logik
|
||||||
if not fields_list:
|
if not fields_list:
|
||||||
configured_schemas = strategy.get("schemas", {})
|
configured_schemas = strategy.get("schemas", {})
|
||||||
fallback_schema = configured_schemas.get(target_type, configured_schemas.get("default"))
|
fallback = configured_schemas.get(target_type, configured_schemas.get("default", {}))
|
||||||
if isinstance(fallback_schema, dict):
|
fields_list = fallback.get("fields", []) if isinstance(fallback, dict) else (fallback or [])
|
||||||
fields_list = fallback_schema.get("fields", [])
|
|
||||||
else:
|
|
||||||
fields_list = fallback_schema or []
|
|
||||||
|
|
||||||
logger.info(f"[{query_id}] Interview Type: {target_type}. Fields: {len(fields_list)}")
|
|
||||||
fields_str = "\n- " + "\n- ".join(fields_list)
|
fields_str = "\n- " + "\n- ".join(fields_list)
|
||||||
|
template_key = strategy.get("prompt_template", "interview_template")
|
||||||
|
|
||||||
template = llm.get_prompt(prompt_key)
|
# WP-25b: Lazy Loading Call
|
||||||
final_prompt = template.replace("{context_str}", "Dialogverlauf...") \
|
|
||||||
.replace("{query}", request.message) \
|
|
||||||
.replace("{target_type}", target_type) \
|
|
||||||
.replace("{schema_fields}", fields_str) \
|
|
||||||
.replace("{schema_hint}", "")
|
|
||||||
sources_hits = []
|
|
||||||
|
|
||||||
else:
|
|
||||||
# --- RAG MODE (FACT, DECISION, EMPATHY, CODING) ---
|
|
||||||
inject_types = strategy.get("inject_types", [])
|
|
||||||
prepend_instr = strategy.get("prepend_instruction", "")
|
|
||||||
edge_boosts = strategy.get("edge_boosts", {})
|
|
||||||
|
|
||||||
query_req = QueryRequest(
|
|
||||||
query=request.message,
|
|
||||||
mode="hybrid",
|
|
||||||
top_k=request.top_k,
|
|
||||||
explain=request.explain,
|
|
||||||
boost_edges=edge_boosts
|
|
||||||
)
|
|
||||||
retrieve_result = await retriever.search(query_req)
|
|
||||||
hits = retrieve_result.results
|
|
||||||
|
|
||||||
if inject_types:
|
|
||||||
strategy_req = QueryRequest(
|
|
||||||
query=request.message,
|
|
||||||
mode="hybrid",
|
|
||||||
top_k=3,
|
|
||||||
filters={"type": inject_types},
|
|
||||||
explain=False,
|
|
||||||
boost_edges=edge_boosts
|
|
||||||
)
|
|
||||||
strategy_result = await retriever.search(strategy_req)
|
|
||||||
existing_ids = {h.node_id for h in hits}
|
|
||||||
for strat_hit in strategy_result.results:
|
|
||||||
if strat_hit.node_id not in existing_ids:
|
|
||||||
hits.append(strat_hit)
|
|
||||||
|
|
||||||
context_str = _build_enriched_context(hits) if hits else "Keine relevanten Notizen gefunden."
|
|
||||||
|
|
||||||
# --- STABILITY FIX: OLLAMA CONTEXT THROTTLE ---
|
|
||||||
# Begrenzt den Text, um den "decode: cannot decode batches" Fehler zu vermeiden.
|
|
||||||
# MAX_OLLAMA_CHARS = 10000
|
|
||||||
|
|
||||||
settings = get_settings() # Falls noch nicht im Scope vorhanden
|
|
||||||
max_chars = getattr(settings, "MAX_OLLAMA_CHARS", 10000)
|
|
||||||
if preferred_provider == "ollama" and len(context_str) > max_chars:
|
|
||||||
logger.warning(f"⚠️ [{query_id}] Context zu groß für Ollama ({len(context_str)} chars). Kürze auf {max_chars}.")
|
|
||||||
context_str = context_str[:max_chars] + "\n[...gekürzt zur Stabilität...]"
|
|
||||||
|
|
||||||
template = llm.get_prompt(prompt_key) or "{context_str}\n\n{query}"
|
|
||||||
|
|
||||||
if prepend_instr:
|
|
||||||
context_str = f"{prepend_instr}\n\n{context_str}"
|
|
||||||
|
|
||||||
final_prompt = template.replace("{context_str}", context_str).replace("{query}", request.message)
|
|
||||||
sources_hits = hits
|
|
||||||
|
|
||||||
# --- DEBUG SPOT 1: PROMPT CONSTRUCTION ---
|
|
||||||
logger.info(f"[{query_id}] PROMPT CONSTRUCTION COMPLETE. Length: {len(final_prompt)} chars.")
|
|
||||||
if not final_prompt.strip():
|
|
||||||
logger.error(f"[{query_id}] CRITICAL: Final prompt is empty before sending to LLM!")
|
|
||||||
|
|
||||||
# --- GENERATION WITH NO-RETRY & DEEP FALLBACK ---
|
|
||||||
system_prompt = llm.get_prompt("system_prompt")
|
|
||||||
|
|
||||||
# --- DEBUG SPOT 2: PRIMARY CALL ---
|
|
||||||
logger.info(f"[{query_id}] PRIMARY CALL: Sending request to provider '{preferred_provider}' (No Retries)...")
|
|
||||||
|
|
||||||
answer_text = ""
|
|
||||||
try:
|
|
||||||
# FIX: max_retries=0 verhindert Hänger durch Retry-Kaskaden im Chat
|
|
||||||
answer_text = await llm.generate_raw_response(
|
answer_text = await llm.generate_raw_response(
|
||||||
prompt=final_prompt,
|
prompt_key=template_key,
|
||||||
system=system_prompt,
|
variables={
|
||||||
|
"query": request.message,
|
||||||
|
"target_type": target_type,
|
||||||
|
"schema_fields": fields_str
|
||||||
|
},
|
||||||
|
system=llm.get_prompt("system_prompt"),
|
||||||
priority="realtime",
|
priority="realtime",
|
||||||
provider=preferred_provider,
|
profile_name="compression_fast",
|
||||||
max_retries=0
|
max_retries=0
|
||||||
)
|
)
|
||||||
except Exception as e:
|
sources_hits = []
|
||||||
logger.error(f"🛑 [{query_id}] Primary Provider '{preferred_provider}' failed: {e}")
|
|
||||||
|
|
||||||
# DEEP FALLBACK: Wenn die Antwort leer ist (Silent Refusal) oder der Primary abgestürzt ist
|
# 3. RAG MODE (WP-25b Delegation an Engine v1.3.0)
|
||||||
if not answer_text.strip() and preferred_provider != "ollama":
|
else:
|
||||||
# --- DEBUG SPOT 3: FALLBACK TRIGGER ---
|
# Phase A & B: Retrieval & Kompression (Delegiert an Engine v1.3.0)
|
||||||
logger.warning(f"🛑 [{query_id}] PRIMARY '{preferred_provider}' returned EMPTY or FAILED. Triggering Deep Fallback to Ollama...")
|
formatted_context_map = await engine._execute_parallel_streams(strategy, request.message)
|
||||||
|
|
||||||
try:
|
# Erfassung der Quellen für das Tracing
|
||||||
answer_text = await llm.generate_raw_response(
|
raw_stream_map = {}
|
||||||
prompt=final_prompt,
|
stream_keys = strategy.get("use_streams", [])
|
||||||
system=system_prompt,
|
library = engine.config.get("streams_library", {})
|
||||||
priority="realtime",
|
|
||||||
provider="ollama",
|
retrieval_tasks = []
|
||||||
max_retries=0
|
active_streams = []
|
||||||
)
|
for key in stream_keys:
|
||||||
except Exception as e:
|
if key in library:
|
||||||
logger.error(f"🛑 [{query_id}] Deep Fallback to Ollama also failed: {e}")
|
active_streams.append(key)
|
||||||
answer_text = "Entschuldigung, das System ist aktuell überlastet. Bitte versuche es in einem Moment erneut."
|
retrieval_tasks.append(engine._run_single_stream(key, library[key], request.message))
|
||||||
|
|
||||||
|
responses = await asyncio.gather(*retrieval_tasks, return_exceptions=True)
|
||||||
|
for name, res in zip(active_streams, responses):
|
||||||
|
if not isinstance(res, Exception):
|
||||||
|
raw_stream_map[name] = res
|
||||||
|
|
||||||
|
sources_hits = _collect_all_hits(raw_stream_map)
|
||||||
|
|
||||||
|
# Phase C: Finale MoE Synthese (Delegiert an Engine v1.3.0)
|
||||||
|
answer_text = await engine._generate_final_answer(
|
||||||
|
intent, strategy, request.message, formatted_context_map
|
||||||
|
)
|
||||||
|
|
||||||
duration_ms = int((time.time() - start_time) * 1000)
|
duration_ms = int((time.time() - start_time) * 1000)
|
||||||
|
|
||||||
# Logging
|
# Logging (WP-15)
|
||||||
try:
|
try:
|
||||||
log_search(
|
log_search(
|
||||||
query_id=query_id,
|
query_id=query_id, query_text=request.message, results=sources_hits,
|
||||||
query_text=request.message,
|
mode=f"wp25b_{intent.lower()}", metadata={"strategy": intent, "source": intent_source}
|
||||||
results=sources_hits,
|
|
||||||
mode="interview" if intent == "INTERVIEW" else "chat_rag",
|
|
||||||
metadata={"intent": intent, "source": intent_source, "provider": preferred_provider}
|
|
||||||
)
|
)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
return ChatResponse(
|
return ChatResponse(
|
||||||
query_id=query_id,
|
query_id=query_id, answer=answer_text, sources=sources_hits,
|
||||||
answer=answer_text,
|
latency_ms=duration_ms, intent=intent, intent_source=intent_source
|
||||||
sources=sources_hits,
|
|
||||||
latency_ms=duration_ms,
|
|
||||||
intent=intent,
|
|
||||||
intent_source=intent_source
|
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error in chat endpoint: {e}", exc_info=True)
|
logger.error(f"❌ Chat Endpoint Failure: {e}", exc_info=True)
|
||||||
# Wir geben eine benutzerfreundliche Meldung zurück, statt nur den Error-Stack
|
raise HTTPException(status_code=500, detail="Fehler bei der Verarbeitung der Anfrage.")
|
||||||
raise HTTPException(status_code=500, detail="Das System konnte die Anfrage nicht verarbeiten.")
|
|
||||||
|
@router.post("/query/discover", response_model=List[DiscoveryHit])
|
||||||
|
async def discover_edges(
|
||||||
|
request: DiscoveryRequest,
|
||||||
|
llm: LLMService = Depends(get_llm_service)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
WP-24c: Analysiert Text auf potenzielle Kanten zu bestehendem Wissen.
|
||||||
|
Nutzt Vektor-Suche und DecisionEngine-Logik (WP-25b PROMPT-TRACE konform).
|
||||||
|
"""
|
||||||
|
start_time = time.time()
|
||||||
|
logger.info(f"🔍 [WP-24c] Discovery triggered for content: {request.content[:50]}...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 1. Kandidaten-Suche via Retriever (Vektor-Match)
|
||||||
|
search_req = QueryRequest(
|
||||||
|
query=request.content,
|
||||||
|
top_k=request.top_k,
|
||||||
|
explain=True
|
||||||
|
)
|
||||||
|
candidates = await llm.decision_engine.retriever.search(search_req)
|
||||||
|
|
||||||
|
if not candidates.results:
|
||||||
|
logger.info("ℹ️ No candidates found for discovery.")
|
||||||
|
return []
|
||||||
|
|
||||||
|
# 2. KI-gestützte Beziehungs-Extraktion (WP-25b)
|
||||||
|
discovery_results = []
|
||||||
|
|
||||||
|
# Zugriff auf gültige Kanten-Typen aus der Registry
|
||||||
|
from app.services.edge_registry import registry as edge_reg
|
||||||
|
valid_types_str = ", ".join(list(edge_reg.valid_types))
|
||||||
|
|
||||||
|
# Parallele Evaluierung der Kandidaten für maximale Performance
|
||||||
|
async def evaluate_candidate(hit: QueryHit) -> Optional[DiscoveryHit]:
|
||||||
|
if hit.total_score < request.min_confidence:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Nutzt ingest_extractor Profil für präzise semantische Analyse
|
||||||
|
# Wir verwenden das prompt_key Pattern (edge_extraction) gemäß WP-24c Vorgabe
|
||||||
|
raw_suggestion = await llm.generate_raw_response(
|
||||||
|
prompt_key="edge_extraction",
|
||||||
|
variables={
|
||||||
|
"note_id": "NEUER_INHALT",
|
||||||
|
"text": f"PROXIMITY_TARGET: {hit.source.get('text', '')}\n\nNEW_CONTENT: {request.content}",
|
||||||
|
"valid_types": valid_types_str
|
||||||
|
},
|
||||||
|
profile_name="ingest_extractor",
|
||||||
|
priority="realtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Parsing der LLM Antwort (Erwartet JSON Liste)
|
||||||
|
from app.core.ingestion.ingestion_utils import extract_json_from_response
|
||||||
|
suggestions = extract_json_from_response(raw_suggestion)
|
||||||
|
|
||||||
|
if isinstance(suggestions, list) and len(suggestions) > 0:
|
||||||
|
sugg = suggestions[0] # Wir nehmen den stärksten Vorschlag pro Hit
|
||||||
|
return DiscoveryHit(
|
||||||
|
target_note=hit.note_id,
|
||||||
|
target_title=hit.source.get("title") or hit.note_id,
|
||||||
|
suggested_edge_type=sugg.get("kind", "related_to"),
|
||||||
|
confidence_score=hit.total_score,
|
||||||
|
reasoning=f"Semantische Nähe ({int(hit.total_score*100)}%) entdeckt."
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"⚠️ Discovery evaluation failed for hit {hit.note_id}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
tasks = [evaluate_candidate(hit) for hit in candidates.results]
|
||||||
|
results = await asyncio.gather(*tasks)
|
||||||
|
|
||||||
|
# Zusammenführung und Duplikat-Bereinigung
|
||||||
|
seen_targets = set()
|
||||||
|
for r in results:
|
||||||
|
if r and r.target_note not in seen_targets:
|
||||||
|
discovery_results.append(r)
|
||||||
|
seen_targets.add(r.target_note)
|
||||||
|
|
||||||
|
duration = int((time.time() - start_time) * 1000)
|
||||||
|
logger.info(f"✨ Discovery finished: found {len(discovery_results)} edges in {duration}ms")
|
||||||
|
|
||||||
|
return discovery_results
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Discovery API failure: {e}", exc_info=True)
|
||||||
|
raise HTTPException(status_code=500, detail="Discovery-Prozess fehlgeschlagen.")
|
||||||
@@ -12,7 +12,7 @@ from typing import List, Optional
|
|||||||
from fastapi import APIRouter, Query
|
from fastapi import APIRouter, Query
|
||||||
from qdrant_client import QdrantClient
|
from qdrant_client import QdrantClient
|
||||||
from app.models.dto import GraphResponse, NodeDTO, EdgeDTO
|
from app.models.dto import GraphResponse, NodeDTO, EdgeDTO
|
||||||
from app.core.graph_adapter import expand
|
from app.core.graph.graph_subgraph import expand
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ LAST_ANALYSIS: 2025-12-15
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
||||||
from app.models.dto import QueryRequest, QueryResponse
|
from app.models.dto import QueryRequest, QueryResponse
|
||||||
from app.core.retriever import hybrid_retrieve, semantic_retrieve
|
from app.core.retrieval.retriever import hybrid_retrieve, semantic_retrieve
|
||||||
# NEU:
|
# NEU:
|
||||||
from app.services.feedback_service import log_search
|
from app.services.feedback_service import log_search
|
||||||
|
|
||||||
|
|||||||
+113
-125
@@ -1,11 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/services/discovery.py
|
FILE: app/services/discovery.py
|
||||||
DESCRIPTION: Service für WP-11. Analysiert Texte, findet Entitäten und schlägt typisierte Verbindungen vor ("Matrix-Logic").
|
DESCRIPTION: Service für WP-11 (Discovery API). Analysiert Entwürfe, findet Entitäten
|
||||||
VERSION: 0.6.0
|
und schlägt typisierte Verbindungen basierend auf der Topologie vor.
|
||||||
|
WP-24c: Vollständige Umstellung auf EdgeRegistry für dynamische Vorschläge.
|
||||||
|
WP-15b: Unterstützung für hybride Suche und Alias-Erkennung.
|
||||||
|
VERSION: 1.1.0 (WP-24c: Full Registry Integration & Audit Fix)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: app.core.qdrant, app.models.dto, app.core.retriever
|
COMPATIBILITY: 100% (Identische API-Signatur wie v0.6.0)
|
||||||
EXTERNAL_CONFIG: config/types.yaml
|
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -13,207 +14,184 @@ import os
|
|||||||
from typing import List, Dict, Any, Optional, Set
|
from typing import List, Dict, Any, Optional, Set
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from app.core.qdrant import QdrantConfig, get_client
|
from app.core.database.qdrant import QdrantConfig, get_client
|
||||||
from app.models.dto import QueryRequest
|
from app.models.dto import QueryRequest
|
||||||
from app.core.retriever import hybrid_retrieve
|
from app.core.retrieval.retriever import hybrid_retrieve
|
||||||
|
# WP-24c: Zentrale Topologie-Quelle
|
||||||
|
from app.services.edge_registry import registry as edge_registry
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class DiscoveryService:
|
class DiscoveryService:
|
||||||
def __init__(self, collection_prefix: str = None):
|
def __init__(self, collection_prefix: str = None):
|
||||||
|
"""Initialisiert den Discovery Service mit Qdrant-Anbindung."""
|
||||||
self.cfg = QdrantConfig.from_env()
|
self.cfg = QdrantConfig.from_env()
|
||||||
self.prefix = collection_prefix or self.cfg.prefix or "mindnet"
|
self.prefix = collection_prefix or self.cfg.prefix or "mindnet"
|
||||||
self.client = get_client(self.cfg)
|
self.client = get_client(self.cfg)
|
||||||
|
|
||||||
|
# Die Registry wird für Typ-Metadaten geladen (Schema-Validierung)
|
||||||
self.registry = self._load_type_registry()
|
self.registry = self._load_type_registry()
|
||||||
|
|
||||||
async def analyze_draft(self, text: str, current_type: str) -> Dict[str, Any]:
|
async def analyze_draft(self, text: str, current_type: str) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Analysiert den Text und liefert Vorschläge mit kontext-sensitiven Kanten-Typen.
|
Analysiert einen Textentwurf auf potenzielle Verbindungen.
|
||||||
|
1. Findet exakte Treffer (Titel/Aliasse).
|
||||||
|
2. Führt semantische Suchen für verschiedene Textabschnitte aus.
|
||||||
|
3. Schlägt topologisch korrekte Kanten-Typen vor.
|
||||||
"""
|
"""
|
||||||
|
if not text or len(text.strip()) < 3:
|
||||||
|
return {"suggestions": [], "status": "empty_input"}
|
||||||
|
|
||||||
suggestions = []
|
suggestions = []
|
||||||
|
seen_target_ids = set()
|
||||||
|
|
||||||
# Fallback, falls keine spezielle Regel greift
|
# --- PHASE 1: EXACT MATCHES (TITEL & ALIASSE) ---
|
||||||
default_edge_type = self._get_default_edge_type(current_type)
|
# Lädt alle bekannten Titel/Aliasse für einen schnellen Scan
|
||||||
|
|
||||||
# Tracking-Sets für Deduplizierung (Wir merken uns NOTE-IDs)
|
|
||||||
seen_target_note_ids = set()
|
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
# 1. Exact Match: Titel/Aliases
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
# Holt Titel, Aliases UND Typen aus dem Index
|
|
||||||
known_entities = self._fetch_all_titles_and_aliases()
|
known_entities = self._fetch_all_titles_and_aliases()
|
||||||
found_entities = self._find_entities_in_text(text, known_entities)
|
exact_matches = self._find_entities_in_text(text, known_entities)
|
||||||
|
|
||||||
for entity in found_entities:
|
for entity in exact_matches:
|
||||||
if entity["id"] in seen_target_note_ids:
|
target_id = entity["id"]
|
||||||
|
if target_id in seen_target_ids:
|
||||||
continue
|
continue
|
||||||
seen_target_note_ids.add(entity["id"])
|
|
||||||
|
|
||||||
# INTELLIGENTE KANTEN-LOGIK (MATRIX)
|
seen_target_ids.add(target_id)
|
||||||
target_type = entity.get("type", "concept")
|
target_type = entity.get("type", "concept")
|
||||||
smart_edge = self._resolve_edge_type(current_type, target_type)
|
|
||||||
|
# WP-24c: Dynamische Kanten-Ermittlung statt Hardcoded Matrix
|
||||||
|
suggested_kind = self._resolve_edge_type(current_type, target_type)
|
||||||
|
|
||||||
suggestions.append({
|
suggestions.append({
|
||||||
"type": "exact_match",
|
"type": "exact_match",
|
||||||
"text_found": entity["match"],
|
"text_found": entity["match"],
|
||||||
"target_title": entity["title"],
|
"target_title": entity["title"],
|
||||||
"target_id": entity["id"],
|
"target_id": target_id,
|
||||||
"suggested_edge_type": smart_edge,
|
"suggested_edge_type": suggested_kind,
|
||||||
"suggested_markdown": f"[[rel:{smart_edge} {entity['title']}]]",
|
"suggested_markdown": f"[[rel:{suggest_kind} {entity['title']}]]",
|
||||||
"confidence": 1.0,
|
"confidence": 1.0,
|
||||||
"reason": f"Exakter Treffer: '{entity['match']}' ({target_type})"
|
"reason": f"Direkte Erwähnung von '{entity['match']}' ({target_type})"
|
||||||
})
|
})
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# --- PHASE 2: SEMANTIC MATCHES (VECTOR SEARCH) ---
|
||||||
# 2. Semantic Match: Sliding Window & Footer Focus
|
# Erzeugt Suchanfragen für verschiedene Fenster des Textes
|
||||||
# ---------------------------------------------------------
|
|
||||||
search_queries = self._generate_search_queries(text)
|
search_queries = self._generate_search_queries(text)
|
||||||
|
|
||||||
# Async parallel abfragen
|
# Parallele Ausführung der Suchanfragen (Cloud-Performance)
|
||||||
tasks = [self._get_semantic_suggestions_async(q) for q in search_queries]
|
tasks = [self._get_semantic_suggestions_async(q) for q in search_queries]
|
||||||
results_list = await asyncio.gather(*tasks)
|
results_list = await asyncio.gather(*tasks)
|
||||||
|
|
||||||
# Ergebnisse verarbeiten
|
|
||||||
for hits in results_list:
|
for hits in results_list:
|
||||||
for hit in hits:
|
for hit in hits:
|
||||||
note_id = hit.payload.get("note_id")
|
payload = hit.payload or {}
|
||||||
if not note_id: continue
|
target_id = payload.get("note_id")
|
||||||
|
|
||||||
# Deduplizierung (Notiz-Ebene)
|
if not target_id or target_id in seen_target_ids:
|
||||||
if note_id in seen_target_note_ids:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Score Check (Threshold 0.50 für nomic-embed-text)
|
# Relevanz-Threshold (Modell-spezifisch für nomic)
|
||||||
if hit.total_score > 0.50:
|
if hit.total_score > 0.55:
|
||||||
seen_target_note_ids.add(note_id)
|
seen_target_ids.add(target_id)
|
||||||
|
target_type = payload.get("type", "concept")
|
||||||
|
target_title = payload.get("title") or "Unbenannt"
|
||||||
|
|
||||||
target_title = hit.payload.get("title") or "Unbekannt"
|
# WP-24c: Nutzung der Topologie-Engine
|
||||||
|
suggested_kind = self._resolve_edge_type(current_type, target_type)
|
||||||
# INTELLIGENTE KANTEN-LOGIK (MATRIX)
|
|
||||||
# Den Typ der gefundenen Notiz aus dem Payload lesen
|
|
||||||
target_type = hit.payload.get("type", "concept")
|
|
||||||
smart_edge = self._resolve_edge_type(current_type, target_type)
|
|
||||||
|
|
||||||
suggestions.append({
|
suggestions.append({
|
||||||
"type": "semantic_match",
|
"type": "semantic_match",
|
||||||
"text_found": (hit.source.get("text") or "")[:60] + "...",
|
"text_found": (hit.source.get("text") or "")[:80] + "...",
|
||||||
"target_title": target_title,
|
"target_title": target_title,
|
||||||
"target_id": note_id,
|
"target_id": target_id,
|
||||||
"suggested_edge_type": smart_edge,
|
"suggested_edge_type": suggested_kind,
|
||||||
"suggested_markdown": f"[[rel:{smart_edge} {target_title}]]",
|
"suggested_markdown": f"[[rel:{suggested_kind} {target_title}]]",
|
||||||
"confidence": round(hit.total_score, 2),
|
"confidence": round(hit.total_score, 2),
|
||||||
"reason": f"Semantisch ähnlich zu {target_type} ({hit.total_score:.2f})"
|
"reason": f"Semantischer Bezug zu {target_type} ({int(hit.total_score*100)}%)"
|
||||||
})
|
})
|
||||||
|
|
||||||
# Sortieren nach Confidence
|
# Sortierung nach Konfidenz
|
||||||
suggestions.sort(key=lambda x: x["confidence"], reverse=True)
|
suggestions.sort(key=lambda x: x["confidence"], reverse=True)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"draft_length": len(text),
|
"draft_length": len(text),
|
||||||
"analyzed_windows": len(search_queries),
|
"analyzed_windows": len(search_queries),
|
||||||
"suggestions_count": len(suggestions),
|
"suggestions_count": len(suggestions),
|
||||||
"suggestions": suggestions[:10]
|
"suggestions": suggestions[:12] # Top 12 Vorschläge
|
||||||
}
|
}
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# --- LOGIK-ZENTRALE (WP-24c) ---
|
||||||
# Core Logic: Die Matrix
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
|
|
||||||
def _resolve_edge_type(self, source_type: str, target_type: str) -> str:
|
def _resolve_edge_type(self, source_type: str, target_type: str) -> str:
|
||||||
"""
|
"""
|
||||||
Entscheidungsmatrix für komplexe Verbindungen.
|
Ermittelt den optimalen Kanten-Typ zwischen zwei Notiz-Typen.
|
||||||
Definiert, wie Typ A auf Typ B verlinken sollte.
|
Nutzt EdgeRegistry (graph_schema.md) statt lokaler Matrix.
|
||||||
"""
|
"""
|
||||||
st = source_type.lower()
|
# 1. Spezifische Prüfung: Gibt es eine Regel für Source -> Target?
|
||||||
tt = target_type.lower()
|
info = edge_registry.get_topology_info(source_type, target_type)
|
||||||
|
typical = info.get("typical", [])
|
||||||
|
if typical:
|
||||||
|
return typical[0] # Erster Vorschlag aus dem Schema
|
||||||
|
|
||||||
# Regeln für 'experience' (Erfahrungen)
|
# 2. Fallback: Was ist für den Quell-Typ generell typisch? (Source -> any)
|
||||||
if st == "experience":
|
info_fallback = edge_registry.get_topology_info(source_type, "any")
|
||||||
if tt == "value": return "based_on"
|
typical_fallback = info_fallback.get("typical", [])
|
||||||
if tt == "principle": return "derived_from"
|
if typical_fallback:
|
||||||
if tt == "trip": return "part_of"
|
return typical_fallback[0]
|
||||||
if tt == "lesson": return "learned"
|
|
||||||
if tt == "project": return "related_to" # oder belongs_to
|
|
||||||
|
|
||||||
# Regeln für 'project'
|
# 3. Globaler Fallback (Sicherheitsnetz)
|
||||||
if st == "project":
|
return "related_to"
|
||||||
if tt == "decision": return "depends_on"
|
|
||||||
if tt == "concept": return "uses"
|
|
||||||
if tt == "person": return "managed_by"
|
|
||||||
|
|
||||||
# Regeln für 'decision' (ADR)
|
# --- HELPERS (VOLLSTÄNDIG ERHALTEN) ---
|
||||||
if st == "decision":
|
|
||||||
if tt == "principle": return "compliant_with"
|
|
||||||
if tt == "requirement": return "addresses"
|
|
||||||
|
|
||||||
# Fallback: Standard aus der types.yaml für den Source-Typ
|
|
||||||
return self._get_default_edge_type(st)
|
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
# Sliding Windows
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
|
|
||||||
def _generate_search_queries(self, text: str) -> List[str]:
|
def _generate_search_queries(self, text: str) -> List[str]:
|
||||||
"""
|
"""Erzeugt überlappende Fenster für die Vektorsuche (Sliding Window)."""
|
||||||
Erzeugt intelligente Fenster + Footer Scan.
|
|
||||||
"""
|
|
||||||
text_len = len(text)
|
text_len = len(text)
|
||||||
if not text: return []
|
|
||||||
|
|
||||||
queries = []
|
queries = []
|
||||||
|
|
||||||
# 1. Start / Gesamtkontext
|
# Fokus A: Dokument-Anfang (Kontext)
|
||||||
queries.append(text[:600])
|
queries.append(text[:600])
|
||||||
|
|
||||||
# 2. Footer-Scan (Wichtig für "Projekt"-Referenzen am Ende)
|
# Fokus B: Dokument-Ende (Aktueller Schreibfokus)
|
||||||
if text_len > 150:
|
if text_len > 250:
|
||||||
footer = text[-250:]
|
footer = text[-350:]
|
||||||
if footer not in queries:
|
if footer not in queries:
|
||||||
queries.append(footer)
|
queries.append(footer)
|
||||||
|
|
||||||
# 3. Sliding Window für lange Texte
|
# Fokus C: Zwischenabschnitte bei langen Texten
|
||||||
if text_len > 800:
|
if text_len > 1200:
|
||||||
window_size = 500
|
window_size = 500
|
||||||
step = 1500
|
step = 1200
|
||||||
for i in range(window_size, text_len - window_size, step):
|
for i in range(600, text_len - 400, step):
|
||||||
end_pos = min(i + window_size, text_len)
|
chunk = text[i:i+window_size]
|
||||||
chunk = text[i:end_pos]
|
|
||||||
if len(chunk) > 100:
|
if len(chunk) > 100:
|
||||||
queries.append(chunk)
|
queries.append(chunk)
|
||||||
|
|
||||||
return queries
|
return queries
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
# Standard Helpers
|
|
||||||
# ---------------------------------------------------------
|
|
||||||
|
|
||||||
async def _get_semantic_suggestions_async(self, text: str):
|
async def _get_semantic_suggestions_async(self, text: str):
|
||||||
req = QueryRequest(query=text, top_k=5, explain=False)
|
"""Führt eine asynchrone Vektorsuche über den Retriever aus."""
|
||||||
|
req = QueryRequest(query=text, top_k=6, explain=False)
|
||||||
try:
|
try:
|
||||||
|
# Nutzt hybrid_retrieve (WP-15b Standard)
|
||||||
res = hybrid_retrieve(req)
|
res = hybrid_retrieve(req)
|
||||||
return res.results
|
return res.results
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Semantic suggestion error: {e}")
|
logger.error(f"Discovery retrieval error: {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def _load_type_registry(self) -> dict:
|
def _load_type_registry(self) -> dict:
|
||||||
|
"""Lädt die types.yaml für Typ-Definitionen."""
|
||||||
path = os.getenv("MINDNET_TYPES_FILE", "config/types.yaml")
|
path = os.getenv("MINDNET_TYPES_FILE", "config/types.yaml")
|
||||||
if not os.path.exists(path):
|
if not os.path.exists(path):
|
||||||
if os.path.exists("types.yaml"): path = "types.yaml"
|
return {}
|
||||||
else: return {}
|
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f: return yaml.safe_load(f) or {}
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
except Exception: return {}
|
return yaml.safe_load(f) or {}
|
||||||
|
except Exception:
|
||||||
def _get_default_edge_type(self, note_type: str) -> str:
|
return {}
|
||||||
types_cfg = self.registry.get("types", {})
|
|
||||||
type_def = types_cfg.get(note_type, {})
|
|
||||||
defaults = type_def.get("edge_defaults")
|
|
||||||
return defaults[0] if defaults else "related_to"
|
|
||||||
|
|
||||||
def _fetch_all_titles_and_aliases(self) -> List[Dict]:
|
def _fetch_all_titles_and_aliases(self) -> List[Dict]:
|
||||||
notes = []
|
"""Holt alle Note-IDs, Titel und Aliasse für den Exakt-Match Abgleich."""
|
||||||
|
entities = []
|
||||||
next_page = None
|
next_page = None
|
||||||
col = f"{self.prefix}_notes"
|
col = f"{self.prefix}_notes"
|
||||||
try:
|
try:
|
||||||
@@ -225,30 +203,40 @@ class DiscoveryService:
|
|||||||
for point in res:
|
for point in res:
|
||||||
pl = point.payload or {}
|
pl = point.payload or {}
|
||||||
aliases = pl.get("aliases") or []
|
aliases = pl.get("aliases") or []
|
||||||
if isinstance(aliases, str): aliases = [aliases]
|
if isinstance(aliases, str):
|
||||||
|
aliases = [aliases]
|
||||||
|
|
||||||
notes.append({
|
entities.append({
|
||||||
"id": pl.get("note_id"),
|
"id": pl.get("note_id"),
|
||||||
"title": pl.get("title"),
|
"title": pl.get("title"),
|
||||||
"aliases": aliases,
|
"aliases": aliases,
|
||||||
"type": pl.get("type", "concept") # WICHTIG: Typ laden für Matrix
|
"type": pl.get("type", "concept")
|
||||||
})
|
})
|
||||||
if next_page is None: break
|
if next_page is None:
|
||||||
except Exception: pass
|
break
|
||||||
return notes
|
except Exception as e:
|
||||||
|
logger.warning(f"Error fetching entities for discovery: {e}")
|
||||||
|
return entities
|
||||||
|
|
||||||
def _find_entities_in_text(self, text: str, entities: List[Dict]) -> List[Dict]:
|
def _find_entities_in_text(self, text: str, entities: List[Dict]) -> List[Dict]:
|
||||||
|
"""Sucht im Text nach Erwähnungen bekannter Entitäten."""
|
||||||
found = []
|
found = []
|
||||||
text_lower = text.lower()
|
text_lower = text.lower()
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
# Title Check
|
|
||||||
title = entity.get("title")
|
title = entity.get("title")
|
||||||
|
# Titel-Check
|
||||||
if title and title.lower() in text_lower:
|
if title and title.lower() in text_lower:
|
||||||
found.append({"match": title, "title": title, "id": entity["id"], "type": entity["type"]})
|
found.append({
|
||||||
|
"match": title, "title": title,
|
||||||
|
"id": entity["id"], "type": entity["type"]
|
||||||
|
})
|
||||||
continue
|
continue
|
||||||
# Alias Check
|
# Alias-Check
|
||||||
for alias in entity.get("aliases", []):
|
for alias in entity.get("aliases", []):
|
||||||
if str(alias).lower() in text_lower:
|
if str(alias).lower() in text_lower:
|
||||||
found.append({"match": alias, "title": title, "id": entity["id"], "type": entity["type"]})
|
found.append({
|
||||||
|
"match": str(alias), "title": title,
|
||||||
|
"id": entity["id"], "type": entity["type"]
|
||||||
|
})
|
||||||
break
|
break
|
||||||
return found
|
return found
|
||||||
+132
-70
@@ -1,21 +1,17 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/services/edge_registry.py
|
FILE: app/services/edge_registry.py
|
||||||
DESCRIPTION: Single Source of Truth für Kanten-Typen mit dynamischem Reload.
|
DESCRIPTION: Single Source of Truth für Kanten-Typen, Symmetrien und Graph-Topologie.
|
||||||
WP-15b: Erweiterte Provenance-Prüfung für die Candidate-Validation.
|
WP-24c: Implementierung der dualen Registry (Vocabulary & Schema).
|
||||||
Sichert die Graph-Integrität durch strikte Trennung von System- und Inhaltskanten.
|
Unterstützt dynamisches Laden von Inversen und kontextuellen Vorschlägen.
|
||||||
WP-22: Fix für absolute Pfade außerhalb des Vaults (Prod-Dictionary).
|
VERSION: 1.0.1 (WP-24c: Verified Atomic Topology)
|
||||||
WP-20: Synchronisation mit zentralen Settings (v0.6.2).
|
|
||||||
VERSION: 0.8.0
|
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: re, os, json, logging, time, app.config
|
|
||||||
LAST_ANALYSIS: 2025-12-26
|
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import Dict, Optional, Set, Tuple
|
from typing import Dict, Optional, Set, Tuple, List
|
||||||
|
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
|
|
||||||
@@ -23,11 +19,12 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class EdgeRegistry:
|
class EdgeRegistry:
|
||||||
"""
|
"""
|
||||||
Zentraler Verwalter für das Kanten-Vokabular.
|
Zentraler Verwalter für das Kanten-Vokabular und das Graph-Schema.
|
||||||
Implementiert das Singleton-Pattern für konsistente Validierung über alle Services.
|
Singleton-Pattern zur Sicherstellung konsistenter Validierung.
|
||||||
"""
|
"""
|
||||||
_instance = None
|
_instance = None
|
||||||
# System-Kanten, die nicht durch User oder KI gesetzt werden dürfen
|
|
||||||
|
# SYSTEM-SCHUTZ: Diese Kanten sind für die strukturelle Integrität reserviert (v0.8.0 Erhalt)
|
||||||
FORBIDDEN_SYSTEM_EDGES = {"next", "prev", "belongs_to"}
|
FORBIDDEN_SYSTEM_EDGES = {"next", "prev", "belongs_to"}
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
@@ -42,124 +39,189 @@ class EdgeRegistry:
|
|||||||
|
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
|
||||||
# 1. Pfad aus den zentralen Settings laden (WP-20 Synchronisation)
|
# --- Pfad-Konfiguration (WP-24c: Variable Pfade für Vault-Spiegelung) ---
|
||||||
# Priorisiert den Pfad aus der .env / config.py (v0.6.2)
|
# Das Vokabular (Semantik)
|
||||||
self.full_vocab_path = os.path.abspath(settings.MINDNET_VOCAB_PATH)
|
self.full_vocab_path = os.path.abspath(settings.MINDNET_VOCAB_PATH)
|
||||||
|
|
||||||
self.unknown_log_path = "data/logs/unknown_edges.jsonl"
|
# Das Schema (Topologie) - Konfigurierbar via ENV: MINDNET_SCHEMA_PATH
|
||||||
self.canonical_map: Dict[str, str] = {}
|
schema_env = getattr(settings, "MINDNET_SCHEMA_PATH", None)
|
||||||
self.valid_types: Set[str] = set()
|
if schema_env:
|
||||||
self._last_mtime = 0.0
|
self.full_schema_path = os.path.abspath(schema_env)
|
||||||
|
else:
|
||||||
|
# Fallback: Liegt im selben Verzeichnis wie das Vokabular
|
||||||
|
self.full_schema_path = os.path.join(os.path.dirname(self.full_vocab_path), "graph_schema.md")
|
||||||
|
|
||||||
|
self.unknown_log_path = "data/logs/unknown_edges.jsonl"
|
||||||
|
|
||||||
|
# --- Interne Datenspeicher ---
|
||||||
|
self.canonical_map: Dict[str, str] = {}
|
||||||
|
self.inverse_map: Dict[str, str] = {}
|
||||||
|
self.valid_types: Set[str] = set()
|
||||||
|
|
||||||
|
# Topologie: source_type -> { target_type -> {"typical": set, "prohibited": set} }
|
||||||
|
self.topology: Dict[str, Dict[str, Dict[str, Set[str]]]] = {}
|
||||||
|
|
||||||
|
self._last_vocab_mtime = 0.0
|
||||||
|
self._last_schema_mtime = 0.0
|
||||||
|
|
||||||
|
logger.info(f">>> [EDGE-REGISTRY] Initializing WP-24c Dual-Engine")
|
||||||
|
logger.info(f" - Vocab-Path: {self.full_vocab_path}")
|
||||||
|
logger.info(f" - Schema-Path: {self.full_schema_path}")
|
||||||
|
|
||||||
# Initialer Ladevorgang
|
|
||||||
logger.info(f">>> [EDGE-REGISTRY] Initializing with Path: {self.full_vocab_path}")
|
|
||||||
self.ensure_latest()
|
self.ensure_latest()
|
||||||
self.initialized = True
|
self.initialized = True
|
||||||
|
|
||||||
def ensure_latest(self):
|
def ensure_latest(self):
|
||||||
"""
|
"""Prüft Zeitstempel beider Dateien und führt bei Änderung Hot-Reload durch."""
|
||||||
Prüft den Zeitstempel der Vokabular-Datei und lädt bei Bedarf neu.
|
|
||||||
Verhindert Inkonsistenzen bei Laufzeit-Updates des Dictionaries.
|
|
||||||
"""
|
|
||||||
if not os.path.exists(self.full_vocab_path):
|
|
||||||
logger.error(f"!!! [EDGE-REGISTRY ERROR] File not found: {self.full_vocab_path} !!!")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_mtime = os.path.getmtime(self.full_vocab_path)
|
# Vokabular-Reload bei Änderung
|
||||||
if current_mtime > self._last_mtime:
|
if os.path.exists(self.full_vocab_path):
|
||||||
self._load_vocabulary()
|
v_mtime = os.path.getmtime(self.full_vocab_path)
|
||||||
self._last_mtime = current_mtime
|
if v_mtime > self._last_vocab_mtime:
|
||||||
|
self._load_vocabulary()
|
||||||
|
self._last_vocab_mtime = v_mtime
|
||||||
|
|
||||||
|
# Schema-Reload bei Änderung
|
||||||
|
if os.path.exists(self.full_schema_path):
|
||||||
|
s_mtime = os.path.getmtime(self.full_schema_path)
|
||||||
|
if s_mtime > self._last_schema_mtime:
|
||||||
|
self._load_schema()
|
||||||
|
self._last_schema_mtime = s_mtime
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"!!! [EDGE-REGISTRY] Error checking file time: {e}")
|
logger.error(f"!!! [EDGE-REGISTRY] Sync failure: {e}")
|
||||||
|
|
||||||
def _load_vocabulary(self):
|
def _load_vocabulary(self):
|
||||||
"""
|
"""Parst edge_vocabulary.md: | Canonical | Inverse | Aliases | Description |"""
|
||||||
Parst das Markdown-Wörterbuch und baut die Canonical-Map auf.
|
|
||||||
Erkennt Tabellen-Strukturen und extrahiert fettgedruckte System-Typen.
|
|
||||||
"""
|
|
||||||
self.canonical_map.clear()
|
self.canonical_map.clear()
|
||||||
|
self.inverse_map.clear()
|
||||||
self.valid_types.clear()
|
self.valid_types.clear()
|
||||||
|
|
||||||
# Regex für Tabellen-Struktur: | **Typ** | Aliase |
|
# Regex für die 4-Spalten Struktur (WP-24c konform)
|
||||||
pattern = re.compile(r"\|\s*\*\*`?([a-zA-Z0-9_-]+)`?\*\*\s*\|\s*([^|]+)\|")
|
# Erwartet: | **`type`** | `inverse` | alias1, alias2 | ... |
|
||||||
|
pattern = re.compile(r"\|\s*\*\*`?([a-zA-Z0-9_-]+)`?\*\*\s*\|\s*`?([a-zA-Z0-9_-]+)`?\s*\|\s*([^|]+)\|")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self.full_vocab_path, "r", encoding="utf-8") as f:
|
with open(self.full_vocab_path, "r", encoding="utf-8") as f:
|
||||||
c_types, c_aliases = 0, 0
|
c_count = 0
|
||||||
for line in f:
|
for line in f:
|
||||||
match = pattern.search(line)
|
match = pattern.search(line)
|
||||||
if match:
|
if match:
|
||||||
canonical = match.group(1).strip().lower()
|
canonical = match.group(1).strip().lower()
|
||||||
aliases_str = match.group(2).strip()
|
inverse = match.group(2).strip().lower()
|
||||||
|
aliases_raw = match.group(3).strip()
|
||||||
|
|
||||||
self.valid_types.add(canonical)
|
self.valid_types.add(canonical)
|
||||||
self.canonical_map[canonical] = canonical
|
self.canonical_map[canonical] = canonical
|
||||||
c_types += 1
|
if inverse:
|
||||||
|
self.inverse_map[canonical] = inverse
|
||||||
|
|
||||||
if aliases_str and "Kein Alias" not in aliases_str:
|
# Aliase verarbeiten (Normalisierung auf snake_case)
|
||||||
aliases = [a.strip() for a in aliases_str.split(",") if a.strip()]
|
if aliases_raw and "Kein Alias" not in aliases_raw:
|
||||||
|
aliases = [a.strip() for a in aliases_raw.split(",") if a.strip()]
|
||||||
for alias in aliases:
|
for alias in aliases:
|
||||||
# Normalisierung: Kleinschreibung, Underscores statt Leerzeichen
|
|
||||||
clean_alias = alias.replace("`", "").lower().strip().replace(" ", "_")
|
clean_alias = alias.replace("`", "").lower().strip().replace(" ", "_")
|
||||||
self.canonical_map[clean_alias] = canonical
|
if clean_alias:
|
||||||
c_aliases += 1
|
self.canonical_map[clean_alias] = canonical
|
||||||
|
c_count += 1
|
||||||
logger.info(f"=== [EDGE-REGISTRY SUCCESS] Loaded {c_types} Canonical Types and {c_aliases} Aliases ===")
|
|
||||||
|
|
||||||
|
logger.info(f"✅ [VOCAB] Loaded {c_count} edge definitions and their inverses.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"!!! [EDGE-REGISTRY FATAL] Error reading file: {e} !!!")
|
logger.error(f"❌ [VOCAB ERROR] {e}")
|
||||||
|
|
||||||
|
def _load_schema(self):
|
||||||
|
"""Parst graph_schema.md: ## Source: `type` | Target | Typical | Prohibited |"""
|
||||||
|
self.topology.clear()
|
||||||
|
current_source = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(self.full_schema_path, "r", encoding="utf-8") as f:
|
||||||
|
for line in f:
|
||||||
|
# Header erkennen (Atomare Sektionen)
|
||||||
|
src_match = re.search(r"## Source:\s*`?([a-zA-Z0-9_-]+)`?", line)
|
||||||
|
if src_match:
|
||||||
|
current_source = src_match.group(1).strip().lower()
|
||||||
|
if current_source not in self.topology:
|
||||||
|
self.topology[current_source] = {}
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Tabellenzeilen parsen
|
||||||
|
if current_source and "|" in line and not line.startswith("|-") and "Target" not in line:
|
||||||
|
cols = [c.strip().replace("`", "").lower() for c in line.split("|")]
|
||||||
|
if len(cols) >= 4:
|
||||||
|
target_type = cols[1]
|
||||||
|
typical_edges = [e.strip() for e in cols[2].split(",") if e.strip() and e != "-"]
|
||||||
|
prohibited_edges = [e.strip() for e in cols[3].split(",") if e.strip() and e != "-"]
|
||||||
|
|
||||||
|
if target_type not in self.topology[current_source]:
|
||||||
|
self.topology[current_source][target_type] = {"typical": set(), "prohibited": set()}
|
||||||
|
|
||||||
|
self.topology[current_source][target_type]["typical"].update(typical_edges)
|
||||||
|
self.topology[current_source][target_type]["prohibited"].update(prohibited_edges)
|
||||||
|
|
||||||
|
logger.info(f"✅ [SCHEMA] Topology matrix built for {len(self.topology)} source types.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ [SCHEMA ERROR] {e}")
|
||||||
|
|
||||||
def resolve(self, edge_type: str, provenance: str = "explicit", context: dict = None) -> str:
|
def resolve(self, edge_type: str, provenance: str = "explicit", context: dict = None) -> str:
|
||||||
"""
|
"""
|
||||||
WP-15b: Validiert einen Kanten-Typ gegen das Vokabular und prüft Berechtigungen.
|
Löst Aliasse auf kanonische Namen auf und schützt System-Kanten.
|
||||||
Sichert, dass nur strukturelle Prozesse System-Kanten setzen dürfen.
|
Erhalt der v0.8.0 Schutz-Logik.
|
||||||
"""
|
"""
|
||||||
self.ensure_latest()
|
self.ensure_latest()
|
||||||
if not edge_type:
|
if not edge_type:
|
||||||
return "related_to"
|
return "related_to"
|
||||||
|
|
||||||
# Normalisierung des Typs
|
|
||||||
clean_type = edge_type.lower().strip().replace(" ", "_").replace("-", "_")
|
clean_type = edge_type.lower().strip().replace(" ", "_").replace("-", "_")
|
||||||
ctx = context or {}
|
ctx = context or {}
|
||||||
|
|
||||||
# WP-15b: System-Kanten dürfen weder manuell noch durch KI/Vererbung gesetzt werden.
|
# Sicherheits-Gate: Schutz vor unerlaubter Nutzung von System-Kanten
|
||||||
# Nur Provenienz 'structure' (interne Prozesse) ist autorisiert.
|
|
||||||
# Wir blockieren hier alle Provenienzen außer 'structure'.
|
|
||||||
restricted_provenance = ["explicit", "semantic_ai", "inherited", "global_pool", "rule"]
|
restricted_provenance = ["explicit", "semantic_ai", "inherited", "global_pool", "rule"]
|
||||||
if provenance in restricted_provenance and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
|
if provenance in restricted_provenance and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
|
||||||
self._log_issue(clean_type, f"forbidden_usage_by_{provenance}", ctx)
|
self._log_issue(clean_type, f"forbidden_system_edge_manipulation_by_{provenance}", ctx)
|
||||||
return "related_to"
|
return "related_to"
|
||||||
|
|
||||||
# System-Kanten sind NUR bei struktureller Provenienz erlaubt
|
# System-Kanten sind NUR bei struktureller Provenienz (Code-generiert) erlaubt
|
||||||
if provenance == "structure" and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
|
if provenance == "structure" and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
|
||||||
return clean_type
|
return clean_type
|
||||||
|
|
||||||
# Mapping auf kanonischen Namen (Alias-Auflösung)
|
# Alias-Auflösung
|
||||||
if clean_type in self.canonical_map:
|
return self.canonical_map.get(clean_type, clean_type)
|
||||||
return self.canonical_map[clean_type]
|
|
||||||
|
|
||||||
# Fallback und Logging unbekannter Typen für Admin-Review
|
def get_inverse(self, edge_type: str) -> str:
|
||||||
self._log_issue(clean_type, "unknown_type", ctx)
|
"""WP-24c: Gibt das symmetrische Gegenstück zurück."""
|
||||||
return clean_type
|
canonical = self.resolve(edge_type)
|
||||||
|
return self.inverse_map.get(canonical, "related_to")
|
||||||
|
|
||||||
|
def get_topology_info(self, source_type: str, target_type: str) -> Dict[str, List[str]]:
|
||||||
|
"""
|
||||||
|
WP-24c: Liefert kontextuelle Kanten-Empfehlungen für Obsidian und das Backend.
|
||||||
|
"""
|
||||||
|
self.ensure_latest()
|
||||||
|
|
||||||
|
# Hierarchische Suche: Spezifisch -> 'any' -> Empty
|
||||||
|
src_cfg = self.topology.get(source_type, self.topology.get("any", {}))
|
||||||
|
tgt_cfg = src_cfg.get(target_type, src_cfg.get("any", {"typical": set(), "prohibited": set()}))
|
||||||
|
|
||||||
|
return {
|
||||||
|
"typical": sorted(list(tgt_cfg["typical"])),
|
||||||
|
"prohibited": sorted(list(tgt_cfg["prohibited"]))
|
||||||
|
}
|
||||||
|
|
||||||
def _log_issue(self, edge_type: str, error_kind: str, ctx: dict):
|
def _log_issue(self, edge_type: str, error_kind: str, ctx: dict):
|
||||||
"""Detailliertes JSONL-Logging für die Vokabular-Optimierung."""
|
"""JSONL-Logging für unbekannte/verbotene Kanten (Erhalt v0.8.0)."""
|
||||||
try:
|
try:
|
||||||
os.makedirs(os.path.dirname(self.unknown_log_path), exist_ok=True)
|
os.makedirs(os.path.dirname(self.unknown_log_path), exist_ok=True)
|
||||||
entry = {
|
entry = {
|
||||||
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
"edge_type": edge_type,
|
"edge_type": edge_type,
|
||||||
"error": error_kind,
|
"error": error_kind,
|
||||||
"file": ctx.get("file", "unknown"),
|
|
||||||
"line": ctx.get("line", "unknown"),
|
|
||||||
"note_id": ctx.get("note_id", "unknown"),
|
"note_id": ctx.get("note_id", "unknown"),
|
||||||
"provenance": ctx.get("provenance", "unknown")
|
"provenance": ctx.get("provenance", "unknown")
|
||||||
}
|
}
|
||||||
with open(self.unknown_log_path, "a", encoding="utf-8") as f:
|
with open(self.unknown_log_path, "a", encoding="utf-8") as f:
|
||||||
f.write(json.dumps(entry) + "\n")
|
f.write(json.dumps(entry) + "\n")
|
||||||
except Exception:
|
except Exception: pass
|
||||||
pass
|
|
||||||
|
|
||||||
# Singleton Export für systemweiten Zugriff
|
# Singleton Export
|
||||||
registry = EdgeRegistry()
|
registry = EdgeRegistry()
|
||||||
@@ -1,40 +1,74 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/services/embeddings_client.py
|
FILE: app/services/embeddings_client.py
|
||||||
DESCRIPTION: Unified Embedding Client. Nutzt Ollama API (HTTP). Ersetzt lokale sentence-transformers.
|
DESCRIPTION: Unified Embedding Client. Nutzt MoE-Profile zur Modellsteuerung.
|
||||||
VERSION: 2.5.0
|
WP-25a: Integration der llm_profiles.yaml für konsistente Vektoren.
|
||||||
|
VERSION: 2.6.0 (WP-25a: MoE & Profile Support)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: httpx, requests, app.config
|
DEPENDENCIES: httpx, requests, app.config, yaml
|
||||||
LAST_ANALYSIS: 2025-12-15
|
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import httpx
|
import httpx
|
||||||
import requests # Für den synchronen Fallback
|
import requests
|
||||||
from typing import List
|
import yaml
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Dict, Any
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class EmbeddingsClient:
|
class EmbeddingsClient:
|
||||||
"""
|
"""
|
||||||
Async Client für Embeddings via Ollama.
|
Async Client für Embeddings.
|
||||||
|
Steuerung erfolgt über das 'embedding_expert' Profil in llm_profiles.yaml.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.settings = get_settings()
|
self.settings = get_settings()
|
||||||
self.base_url = os.getenv("MINDNET_OLLAMA_URL", "http://127.0.0.1:11434")
|
|
||||||
self.model = os.getenv("MINDNET_EMBEDDING_MODEL")
|
# 1. MoE-Profil laden (WP-25a)
|
||||||
|
self.profile = self._load_embedding_profile()
|
||||||
|
|
||||||
|
# 2. Modell & URL auflösen
|
||||||
|
# Priorität: llm_profiles.yaml -> .env (Legacy) -> Fallback
|
||||||
|
self.model = self.profile.get("model") or os.getenv("MINDNET_EMBEDDING_MODEL")
|
||||||
|
|
||||||
|
provider = self.profile.get("provider", "ollama")
|
||||||
|
if provider == "ollama":
|
||||||
|
self.base_url = self.settings.OLLAMA_URL
|
||||||
|
else:
|
||||||
|
# Platzhalter für zukünftige Cloud-Embedding-Provider
|
||||||
|
self.base_url = os.getenv("MINDNET_OLLAMA_URL", "http://127.0.0.1:11434")
|
||||||
|
|
||||||
if not self.model:
|
if not self.model:
|
||||||
self.model = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
|
self.model = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
|
||||||
logger.warning(f"No MINDNET_EMBEDDING_MODEL set. Fallback to '{self.model}'.")
|
logger.warning(f"⚠️ Kein Embedding-Modell in Profil oder .env gefunden. Fallback auf '{self.model}'.")
|
||||||
|
else:
|
||||||
|
logger.info(f"🧬 Embedding-Experte aktiv: Model='{self.model}' via {provider}")
|
||||||
|
|
||||||
|
def _load_embedding_profile(self) -> Dict[str, Any]:
|
||||||
|
"""Lädt die Konfiguration für den embedding_expert."""
|
||||||
|
path_str = getattr(self.settings, "LLM_PROFILES_PATH", "config/llm_profiles.yaml")
|
||||||
|
path = Path(path_str)
|
||||||
|
if not path.exists():
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f) or {}
|
||||||
|
profiles = data.get("profiles", {})
|
||||||
|
return profiles.get("embedding_expert", {})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Failed to load embedding profile: {e}")
|
||||||
|
return {}
|
||||||
|
|
||||||
async def embed_query(self, text: str) -> List[float]:
|
async def embed_query(self, text: str) -> List[float]:
|
||||||
|
"""Erzeugt einen Vektor für eine Suchanfrage."""
|
||||||
return await self._request_embedding(text)
|
return await self._request_embedding(text)
|
||||||
|
|
||||||
async def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
async def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||||
|
"""Erzeugt Vektoren für einen Batch von Dokumenten."""
|
||||||
vectors = []
|
vectors = []
|
||||||
# Längeres Timeout für Batches
|
# Längeres Timeout für Batches (WP-20 Resilienz)
|
||||||
async with httpx.AsyncClient(timeout=120.0) as client:
|
async with httpx.AsyncClient(timeout=120.0) as client:
|
||||||
for text in texts:
|
for text in texts:
|
||||||
vec = await self._request_embedding_with_client(client, text)
|
vec = await self._request_embedding_with_client(client, text)
|
||||||
@@ -42,18 +76,23 @@ class EmbeddingsClient:
|
|||||||
return vectors
|
return vectors
|
||||||
|
|
||||||
async def _request_embedding(self, text: str) -> List[float]:
|
async def _request_embedding(self, text: str) -> List[float]:
|
||||||
|
"""Interner Request-Handler für Einzelabfragen."""
|
||||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||||
return await self._request_embedding_with_client(client, text)
|
return await self._request_embedding_with_client(client, text)
|
||||||
|
|
||||||
async def _request_embedding_with_client(self, client: httpx.AsyncClient, text: str) -> List[float]:
|
async def _request_embedding_with_client(self, client: httpx.AsyncClient, text: str) -> List[float]:
|
||||||
if not text or not text.strip(): return []
|
"""Führt den HTTP-Call gegen die Embedding-API durch."""
|
||||||
|
if not text or not text.strip():
|
||||||
|
return []
|
||||||
|
|
||||||
url = f"{self.base_url}/api/embeddings"
|
url = f"{self.base_url}/api/embeddings"
|
||||||
try:
|
try:
|
||||||
|
# WP-25: Aktuell optimiert für Ollama-API Struktur
|
||||||
response = await client.post(url, json={"model": self.model, "prompt": text})
|
response = await client.post(url, json={"model": self.model, "prompt": text})
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json().get("embedding", [])
|
return response.json().get("embedding", [])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Async embedding failed: {e}")
|
logger.error(f"Async embedding failed (Model: {self.model}): {e}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
@@ -62,27 +101,38 @@ class EmbeddingsClient:
|
|||||||
|
|
||||||
def embed_text(text: str) -> List[float]:
|
def embed_text(text: str) -> List[float]:
|
||||||
"""
|
"""
|
||||||
LEGACY/SYNC: Nutzt jetzt ebenfalls OLLAMA via 'requests'.
|
LEGACY/SYNC: Nutzt ebenfalls die Profil-Logik für Konsistenz.
|
||||||
Ersetzt SentenceTransformers, um Dimensionskonflikte (768 vs 384) zu lösen.
|
Ersetzt lokale sentence-transformers zur Vermeidung von Dimensionskonflikten.
|
||||||
"""
|
"""
|
||||||
if not text or not text.strip():
|
if not text or not text.strip():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
base_url = os.getenv("MINDNET_OLLAMA_URL", "http://127.0.0.1:11434")
|
settings = get_settings()
|
||||||
model = os.getenv("MINDNET_EMBEDDING_MODEL")
|
|
||||||
|
# Schneller Profil-Lookup für Sync-Mode
|
||||||
|
path = Path(getattr(settings, "LLM_PROFILES_PATH", "config/llm_profiles.yaml"))
|
||||||
|
model = os.getenv("MINDNET_EMBEDDING_MODEL")
|
||||||
|
base_url = settings.OLLAMA_URL
|
||||||
|
|
||||||
|
if path.exists():
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f) or {}
|
||||||
|
prof = data.get("profiles", {}).get("embedding_expert", {})
|
||||||
|
if prof.get("model"):
|
||||||
|
model = prof["model"]
|
||||||
|
except: pass
|
||||||
|
|
||||||
# Fallback logik identisch zur Klasse
|
|
||||||
if not model:
|
if not model:
|
||||||
model = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
|
model = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
|
||||||
|
|
||||||
url = f"{base_url}/api/embeddings"
|
url = f"{base_url}/api/embeddings"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Synchroner Request (blockierend)
|
# Synchroner Request via requests
|
||||||
response = requests.post(url, json={"model": model, "prompt": text}, timeout=30)
|
response = requests.post(url, json={"model": model, "prompt": text}, timeout=30)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
return response.json().get("embedding", [])
|
||||||
return data.get("embedding", [])
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Sync embedding (Ollama) failed: {e}")
|
logger.error(f"Sync embedding failed (Model: {model}): {e}")
|
||||||
return []
|
return []
|
||||||
+191
-163
@@ -1,17 +1,14 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/services/llm_service.py
|
FILE: app/services/llm_service.py
|
||||||
DESCRIPTION: Hybrid-Client für Ollama, Google GenAI (Gemini) und OpenRouter.
|
DESCRIPTION: Hybrid-Client für Ollama, Google GenAI (Gemini) und OpenRouter.
|
||||||
Verwaltet provider-spezifische Prompts und Background-Last.
|
WP-25b: Implementierung der Lazy-Prompt-Orchestration (Modell-spezifisch).
|
||||||
WP-20: Optimiertes Fallback-Management zum Schutz von Cloud-Quoten.
|
VERSION: 3.5.5 (WP-25b: Prompt Orchestration & Full Resilience)
|
||||||
WP-20 Fix: Bulletproof Prompt-Auflösung für format() Aufrufe.
|
|
||||||
WP-22/JSON: Optionales JSON-Schema + strict (für OpenRouter structured outputs).
|
|
||||||
FIX: Intelligente Rate-Limit Erkennung (429 Handling), v1-API Sync & Timeouts.
|
|
||||||
VERSION: 3.3.7
|
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
FIX:
|
FIX:
|
||||||
- Implementiert striktes max_retries Handling für alle Provider (v.a. für Chat-Stabilität).
|
- WP-25b: get_prompt() unterstützt Hierarchie: Model-ID -> Provider -> Default.
|
||||||
- Synchronisiert Rate-Limit Retries mit dem max_retries Parameter.
|
- WP-25b: generate_raw_response() unterstützt prompt_key + variables für Lazy-Formatting.
|
||||||
- Optimiert Logging für sofortige Fehlererkennung.
|
- WP-25a: Voller Erhalt der rekursiven Fallback-Kaskade und visited_profiles Schutz.
|
||||||
|
- WP-20: Restaurierung des internen Ollama-Retry-Loops für Hardware-Stabilität.
|
||||||
"""
|
"""
|
||||||
import httpx
|
import httpx
|
||||||
import yaml
|
import yaml
|
||||||
@@ -20,23 +17,25 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
from google import genai
|
from google import genai
|
||||||
from google.genai import types
|
from google.genai import types
|
||||||
from openai import AsyncOpenAI # Für OpenRouter (OpenAI-kompatibel)
|
from openai import AsyncOpenAI
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional, Dict, Any, Literal
|
from typing import Optional, Dict, Any, Literal
|
||||||
from app.config import get_settings
|
from app.config import get_settings
|
||||||
|
|
||||||
|
# Import der neutralen Bereinigungs-Logik
|
||||||
|
from app.core.registry import clean_llm_text
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class LLMService:
|
class LLMService:
|
||||||
# GLOBALER SEMAPHOR für Hintergrund-Last Steuerung (WP-06)
|
|
||||||
_background_semaphore = None
|
_background_semaphore = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.settings = get_settings()
|
self.settings = get_settings()
|
||||||
self.prompts = self._load_prompts()
|
self.prompts = self._load_prompts()
|
||||||
|
self.profiles = self._load_llm_profiles()
|
||||||
|
self._decision_engine = None
|
||||||
|
|
||||||
# Initialisiere Semaphore einmalig auf Klassen-Ebene
|
|
||||||
if LLMService._background_semaphore is None:
|
if LLMService._background_semaphore is None:
|
||||||
limit = getattr(self.settings, "BACKGROUND_LIMIT", 2)
|
limit = getattr(self.settings, "BACKGROUND_LIMIT", 2)
|
||||||
logger.info(f"🚦 LLMService: Initializing Background Semaphore with limit: {limit}")
|
logger.info(f"🚦 LLMService: Initializing Background Semaphore with limit: {limit}")
|
||||||
@@ -48,10 +47,9 @@ class LLMService:
|
|||||||
timeout=httpx.Timeout(self.settings.LLM_TIMEOUT)
|
timeout=httpx.Timeout(self.settings.LLM_TIMEOUT)
|
||||||
)
|
)
|
||||||
|
|
||||||
# 2. Google GenAI Client (Modern SDK)
|
# 2. Google GenAI Client
|
||||||
self.google_client = None
|
self.google_client = None
|
||||||
if self.settings.GOOGLE_API_KEY:
|
if self.settings.GOOGLE_API_KEY:
|
||||||
# FIX: Wir erzwingen api_version 'v1' für höhere Stabilität bei 2.5er Modellen.
|
|
||||||
self.google_client = genai.Client(
|
self.google_client = genai.Client(
|
||||||
api_key=self.settings.GOOGLE_API_KEY,
|
api_key=self.settings.GOOGLE_API_KEY,
|
||||||
http_options={'api_version': 'v1'}
|
http_options={'api_version': 'v1'}
|
||||||
@@ -64,16 +62,20 @@ class LLMService:
|
|||||||
self.openrouter_client = AsyncOpenAI(
|
self.openrouter_client = AsyncOpenAI(
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="https://openrouter.ai/api/v1",
|
||||||
api_key=self.settings.OPENROUTER_API_KEY,
|
api_key=self.settings.OPENROUTER_API_KEY,
|
||||||
# Strikter Timeout für OpenRouter Free-Tier zur Vermeidung von Hangs.
|
|
||||||
timeout=45.0
|
timeout=45.0
|
||||||
)
|
)
|
||||||
logger.info("🛰️ LLMService: OpenRouter Integration active.")
|
logger.info("🛰️ LLMService: OpenRouter Integration active.")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def decision_engine(self):
|
||||||
|
if self._decision_engine is None:
|
||||||
|
from app.core.retrieval.decision_engine import DecisionEngine
|
||||||
|
self._decision_engine = DecisionEngine()
|
||||||
|
return self._decision_engine
|
||||||
|
|
||||||
def _load_prompts(self) -> dict:
|
def _load_prompts(self) -> dict:
|
||||||
"""Lädt die Prompt-Konfiguration aus der YAML-Datei."""
|
|
||||||
path = Path(self.settings.PROMPTS_PATH)
|
path = Path(self.settings.PROMPTS_PATH)
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
logger.error(f"❌ Prompts file not found at {path}")
|
|
||||||
return {}
|
return {}
|
||||||
try:
|
try:
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
@@ -82,30 +84,49 @@ class LLMService:
|
|||||||
logger.error(f"❌ Failed to load prompts: {e}")
|
logger.error(f"❌ Failed to load prompts: {e}")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def get_prompt(self, key: str, provider: str = None) -> str:
|
def _load_llm_profiles(self) -> dict:
|
||||||
|
"""WP-25a: Lädt die zentralen MoE-Profile aus der llm_profiles.yaml."""
|
||||||
|
path_str = getattr(self.settings, "LLM_PROFILES_PATH", "config/llm_profiles.yaml")
|
||||||
|
path = Path(path_str)
|
||||||
|
if not path.exists():
|
||||||
|
logger.warning(f"⚠️ LLM Profiles file not found at {path}.")
|
||||||
|
return {}
|
||||||
|
try:
|
||||||
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
data = yaml.safe_load(f) or {}
|
||||||
|
return data.get("profiles", {})
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Failed to load llm_profiles.yaml: {e}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_prompt(self, key: str, model_id: str = None, provider: str = None) -> str:
|
||||||
"""
|
"""
|
||||||
Hole provider-spezifisches Template mit intelligenter Text-Kaskade.
|
WP-25b: Hochpräziser Prompt-Lookup mit detailliertem Trace-Logging.
|
||||||
HINWEIS: Dies ist nur ein Text-Lookup und verbraucht kein API-Kontingent.
|
|
||||||
Kaskade: Gewählter Provider -> Gemini (Cloud-Stil) -> Ollama (Basis-Stil).
|
|
||||||
"""
|
"""
|
||||||
active_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
|
||||||
data = self.prompts.get(key, "")
|
data = self.prompts.get(key, "")
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return str(data)
|
||||||
|
|
||||||
if isinstance(data, dict):
|
# 1. Spezifischstes Match: Exakte Modell-ID
|
||||||
# Wir versuchen erst den Provider, dann Gemini, dann Ollama
|
if model_id and model_id in data:
|
||||||
val = data.get(active_provider, data.get("gemini", data.get("ollama", "")))
|
logger.info(f"🎯 [PROMPT-TRACE] Level 1 Match: Model-specific ('{model_id}') for key '{key}'")
|
||||||
|
return str(data[model_id])
|
||||||
|
|
||||||
# Falls val durch YAML-Fehler immer noch ein Dict ist, extrahiere ersten String
|
# 2. Mittlere Ebene: Provider
|
||||||
if isinstance(val, dict):
|
if provider and provider in data:
|
||||||
logger.warning(f"⚠️ [LLMService] Nested dictionary detected for key '{key}'. Using first entry.")
|
logger.info(f"📡 [PROMPT-TRACE] Level 2 Match: Provider-fallback ('{provider}') for key '{key}'")
|
||||||
val = next(iter(val.values()), "") if val else ""
|
return str(data[provider])
|
||||||
return str(val)
|
|
||||||
|
|
||||||
return str(data)
|
# 3. Globaler Fallback
|
||||||
|
default_val = data.get("default", data.get("gemini", data.get("ollama", "")))
|
||||||
|
logger.info(f"⚓ [PROMPT-TRACE] Level 3 Match: Global Default for key '{key}'")
|
||||||
|
return str(default_val)
|
||||||
|
|
||||||
async def generate_raw_response(
|
async def generate_raw_response(
|
||||||
self,
|
self,
|
||||||
prompt: str,
|
prompt: str = None,
|
||||||
|
prompt_key: str = None, # WP-25b: Lazy Loading Key
|
||||||
|
variables: dict = None, # WP-25b: Daten für Formatierung
|
||||||
system: str = None,
|
system: str = None,
|
||||||
force_json: bool = False,
|
force_json: bool = False,
|
||||||
max_retries: int = 2,
|
max_retries: int = 2,
|
||||||
@@ -115,46 +136,110 @@ class LLMService:
|
|||||||
model_override: Optional[str] = None,
|
model_override: Optional[str] = None,
|
||||||
json_schema: Optional[Dict[str, Any]] = None,
|
json_schema: Optional[Dict[str, Any]] = None,
|
||||||
json_schema_name: str = "mindnet_json",
|
json_schema_name: str = "mindnet_json",
|
||||||
strict_json_schema: bool = True
|
strict_json_schema: bool = True,
|
||||||
|
profile_name: Optional[str] = None,
|
||||||
|
visited_profiles: Optional[list] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""Haupteinstiegspunkt für LLM-Anfragen mit Lazy-Prompt Orchestrierung."""
|
||||||
Haupteinstiegspunkt für LLM-Anfragen mit Priorisierung.
|
visited_profiles = visited_profiles or []
|
||||||
"""
|
target_provider = provider
|
||||||
target_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
target_model = model_override
|
||||||
|
target_temp = None
|
||||||
|
fallback_profile = None
|
||||||
|
|
||||||
if priority == "background":
|
# 1. Profil-Auflösung (Mixture of Experts)
|
||||||
async with LLMService._background_semaphore:
|
if profile_name and self.profiles:
|
||||||
return await self._dispatch(
|
profile = self.profiles.get(profile_name)
|
||||||
target_provider, prompt, system, force_json,
|
if profile:
|
||||||
max_retries, base_delay, model_override,
|
target_provider = profile.get("provider", target_provider)
|
||||||
json_schema, json_schema_name, strict_json_schema
|
target_model = profile.get("model", target_model)
|
||||||
|
target_temp = profile.get("temperature")
|
||||||
|
fallback_profile = profile.get("fallback_profile")
|
||||||
|
visited_profiles.append(profile_name)
|
||||||
|
logger.info(f"🎭 MoE Dispatch: Profil='{profile_name}' -> Provider='{target_provider}' | Model='{target_model}'")
|
||||||
|
else:
|
||||||
|
logger.warning(f"⚠️ Profil '{profile_name}' nicht in llm_profiles.yaml gefunden!")
|
||||||
|
|
||||||
|
if not target_provider:
|
||||||
|
target_provider = self.settings.MINDNET_LLM_PROVIDER
|
||||||
|
|
||||||
|
# 2. WP-25b: Lazy Prompt Resolving
|
||||||
|
# Wir laden den Prompt erst JETZT, basierend auf dem gerade aktiven Modell.
|
||||||
|
current_prompt = prompt
|
||||||
|
if prompt_key:
|
||||||
|
template = self.get_prompt(prompt_key, model_id=target_model, provider=target_provider)
|
||||||
|
# WP-25b FIX: Validierung des geladenen Prompts
|
||||||
|
if not template or not template.strip():
|
||||||
|
available_keys = list(self.prompts.keys())
|
||||||
|
logger.error(f"❌ Prompt key '{prompt_key}' not found or empty. Available keys: {available_keys[:10]}...")
|
||||||
|
raise ValueError(f"Invalid prompt_key: '{prompt_key}' (not found in prompts.yaml)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Formatierung mit den übergebenen Variablen
|
||||||
|
current_prompt = template.format(**(variables or {}))
|
||||||
|
except KeyError as e:
|
||||||
|
logger.error(f"❌ Prompt formatting failed for key '{prompt_key}': Missing variable {e}")
|
||||||
|
raise ValueError(f"Missing variable in prompt '{prompt_key}': {e}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Prompt formatting failed for key '{prompt_key}': {e}")
|
||||||
|
current_prompt = template # Sicherheits-Fallback
|
||||||
|
|
||||||
|
# 3. Ausführung mit Fehler-Handling für Kaskade
|
||||||
|
try:
|
||||||
|
if priority == "background":
|
||||||
|
async with LLMService._background_semaphore:
|
||||||
|
res = await self._dispatch(
|
||||||
|
target_provider, current_prompt, system, force_json,
|
||||||
|
max_retries, base_delay, target_model,
|
||||||
|
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
res = await self._dispatch(
|
||||||
|
target_provider, current_prompt, system, force_json,
|
||||||
|
max_retries, base_delay, target_model,
|
||||||
|
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self._dispatch(
|
# Check auf leere Cloud-Antworten (WP-25 Stability)
|
||||||
target_provider, prompt, system, force_json,
|
if not res and target_provider != "ollama":
|
||||||
max_retries, base_delay, model_override,
|
logger.warning(f"⚠️ Empty response from {target_provider}. Triggering fallback.")
|
||||||
json_schema, json_schema_name, strict_json_schema
|
raise ValueError(f"Empty response from {target_provider}")
|
||||||
)
|
|
||||||
|
return clean_llm_text(res) if not force_json else res
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"❌ Error during execution of profile '{profile_name}' ({target_provider}): {e}")
|
||||||
|
|
||||||
|
# 4. WP-25b Kaskaden-Logik (Rekursiv mit Modell-spezifischem Re-Loading)
|
||||||
|
if fallback_profile and fallback_profile not in visited_profiles:
|
||||||
|
logger.info(f"🔄 Switching to fallback profile: '{fallback_profile}'")
|
||||||
|
return await self.generate_raw_response(
|
||||||
|
prompt=prompt,
|
||||||
|
prompt_key=prompt_key,
|
||||||
|
variables=variables, # Ermöglicht neues Formatting für Fallback-Modell
|
||||||
|
system=system, force_json=force_json,
|
||||||
|
max_retries=max_retries, base_delay=base_delay,
|
||||||
|
priority=priority, provider=None, model_override=None,
|
||||||
|
json_schema=json_schema, json_schema_name=json_schema_name,
|
||||||
|
strict_json_schema=strict_json_schema,
|
||||||
|
profile_name=fallback_profile,
|
||||||
|
visited_profiles=visited_profiles
|
||||||
|
)
|
||||||
|
|
||||||
|
# 5. Ultimativer Notanker: Falls alles fehlschlägt, direkt zu Ollama
|
||||||
|
if target_provider != "ollama" and self.settings.LLM_FALLBACK_ENABLED:
|
||||||
|
logger.warning(f"🚨 Kaskade erschöpft. Nutze finalen Ollama-Notanker.")
|
||||||
|
res = await self._execute_ollama(current_prompt, system, force_json, max_retries, base_delay, target_temp, target_model)
|
||||||
|
return clean_llm_text(res) if not force_json else res
|
||||||
|
|
||||||
|
raise e
|
||||||
|
|
||||||
async def _dispatch(
|
async def _dispatch(
|
||||||
self,
|
self, provider, prompt, system, force_json, max_retries, base_delay,
|
||||||
provider: str,
|
model_override, json_schema, json_schema_name, strict_json_schema, temperature
|
||||||
prompt: str,
|
|
||||||
system: Optional[str],
|
|
||||||
force_json: bool,
|
|
||||||
max_retries: int,
|
|
||||||
base_delay: float,
|
|
||||||
model_override: Optional[str],
|
|
||||||
json_schema: Optional[Dict[str, Any]],
|
|
||||||
json_schema_name: str,
|
|
||||||
strict_json_schema: bool
|
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""Routet die Anfrage an den spezifischen Provider-Executor."""
|
||||||
Routet die Anfrage mit intelligenter Rate-Limit Erkennung.
|
|
||||||
Nutzt max_retries um die Rate-Limit Schleife zu begrenzen.
|
|
||||||
"""
|
|
||||||
rate_limit_attempts = 0
|
rate_limit_attempts = 0
|
||||||
# FIX: Wir nutzen max_retries als Limit für Rate-Limit Versuche, wenn explizit klein gewählt (z.B. Chat)
|
|
||||||
max_rate_retries = min(max_retries, getattr(self.settings, "LLM_RATE_LIMIT_RETRIES", 3))
|
max_rate_retries = min(max_retries, getattr(self.settings, "LLM_RATE_LIMIT_RETRIES", 3))
|
||||||
wait_time = getattr(self.settings, "LLM_RATE_LIMIT_WAIT", 60.0)
|
wait_time = getattr(self.settings, "LLM_RATE_LIMIT_WAIT", 60.0)
|
||||||
|
|
||||||
@@ -162,114 +247,74 @@ class LLMService:
|
|||||||
try:
|
try:
|
||||||
if provider == "openrouter" and self.openrouter_client:
|
if provider == "openrouter" and self.openrouter_client:
|
||||||
return await self._execute_openrouter(
|
return await self._execute_openrouter(
|
||||||
prompt=prompt,
|
prompt=prompt, system=system, force_json=force_json,
|
||||||
system=system,
|
model_override=model_override, json_schema=json_schema,
|
||||||
force_json=force_json,
|
json_schema_name=json_schema_name, strict_json_schema=strict_json_schema,
|
||||||
model_override=model_override,
|
temperature=temperature
|
||||||
json_schema=json_schema,
|
|
||||||
json_schema_name=json_schema_name,
|
|
||||||
strict_json_schema=strict_json_schema
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if provider == "gemini" and self.google_client:
|
if provider == "gemini" and self.google_client:
|
||||||
return await self._execute_google(prompt, system, force_json, model_override)
|
return await self._execute_google(prompt, system, force_json, model_override, temperature)
|
||||||
|
|
||||||
# Default/Fallback zu Ollama
|
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay, temperature, model_override)
|
||||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err_str = str(e)
|
err_str = str(e)
|
||||||
# Intelligente 429 Erkennung
|
if any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited"]):
|
||||||
is_rate_limit = any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited", "Too Many Requests"])
|
|
||||||
|
|
||||||
if is_rate_limit and rate_limit_attempts < max_rate_retries:
|
|
||||||
rate_limit_attempts += 1
|
rate_limit_attempts += 1
|
||||||
logger.warning(
|
logger.warning(f"⏳ Rate Limit {provider}. Attempt {rate_limit_attempts}. Wait {wait_time}s.")
|
||||||
f"⏳ [LLMService] Rate Limit detected from {provider}. "
|
|
||||||
f"Attempt {rate_limit_attempts}/{max_rate_retries}. Waiting {wait_time}s..."
|
|
||||||
)
|
|
||||||
await asyncio.sleep(wait_time)
|
await asyncio.sleep(wait_time)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Wenn kein Rate-Limit oder Retries erschöpft -> Fallback zu Ollama (falls aktiviert)
|
|
||||||
if self.settings.LLM_FALLBACK_ENABLED and provider != "ollama":
|
|
||||||
logger.warning(
|
|
||||||
f"🔄 Provider {provider} failed ({err_str}). Falling back to LOCAL OLLAMA."
|
|
||||||
)
|
|
||||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
async def _execute_google(self, prompt, system, force_json, model_override):
|
async def _execute_google(self, prompt, system, force_json, model_override, temperature):
|
||||||
"""Native Google SDK Integration (Gemini) mit v1 Fix."""
|
model = (model_override or self.settings.GEMINI_MODEL).replace("models/", "")
|
||||||
model = model_override or self.settings.GEMINI_MODEL
|
config_kwargs = {
|
||||||
clean_model = model.replace("models/", "")
|
"system_instruction": system,
|
||||||
|
"response_mime_type": "application/json" if force_json else "text/plain"
|
||||||
|
}
|
||||||
|
if temperature is not None:
|
||||||
|
config_kwargs["temperature"] = temperature
|
||||||
|
|
||||||
config = types.GenerateContentConfig(
|
config = types.GenerateContentConfig(**config_kwargs)
|
||||||
system_instruction=system,
|
|
||||||
response_mime_type="application/json" if force_json else "text/plain"
|
|
||||||
)
|
|
||||||
response = await asyncio.wait_for(
|
response = await asyncio.wait_for(
|
||||||
asyncio.to_thread(
|
asyncio.to_thread(self.google_client.models.generate_content, model=model, contents=prompt, config=config),
|
||||||
self.google_client.models.generate_content,
|
|
||||||
model=clean_model, contents=prompt, config=config
|
|
||||||
),
|
|
||||||
timeout=45.0
|
timeout=45.0
|
||||||
)
|
)
|
||||||
return response.text.strip()
|
return response.text.strip()
|
||||||
|
|
||||||
async def _execute_openrouter(
|
async def _execute_openrouter(self, prompt, system, force_json, model_override, json_schema, json_schema_name, strict_json_schema, temperature) -> str:
|
||||||
self,
|
|
||||||
prompt: str,
|
|
||||||
system: Optional[str],
|
|
||||||
force_json: bool,
|
|
||||||
model_override: Optional[str],
|
|
||||||
json_schema: Optional[Dict[str, Any]] = None,
|
|
||||||
json_schema_name: str = "mindnet_json",
|
|
||||||
strict_json_schema: bool = True
|
|
||||||
) -> str:
|
|
||||||
"""OpenRouter API Integration (OpenAI-kompatibel)."""
|
|
||||||
model = model_override or self.settings.OPENROUTER_MODEL
|
model = model_override or self.settings.OPENROUTER_MODEL
|
||||||
|
logger.info(f"🛰️ OpenRouter Call: Model='{model}' | Temp={temperature}")
|
||||||
messages = []
|
messages = []
|
||||||
if system:
|
if system: messages.append({"role": "system", "content": system})
|
||||||
messages.append({"role": "system", "content": system})
|
|
||||||
messages.append({"role": "user", "content": prompt})
|
messages.append({"role": "user", "content": prompt})
|
||||||
|
|
||||||
kwargs: Dict[str, Any] = {}
|
kwargs: Dict[str, Any] = {}
|
||||||
|
if temperature is not None: kwargs["temperature"] = temperature
|
||||||
|
|
||||||
if force_json:
|
if force_json:
|
||||||
if json_schema:
|
if json_schema:
|
||||||
kwargs["response_format"] = {
|
kwargs["response_format"] = {"type": "json_schema", "json_schema": {"name": json_schema_name, "strict": strict_json_schema, "schema": json_schema}}
|
||||||
"type": "json_schema",
|
|
||||||
"json_schema": {
|
|
||||||
"name": json_schema_name,
|
|
||||||
"strict": strict_json_schema,
|
|
||||||
"schema": json_schema
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
kwargs["response_format"] = {"type": "json_object"}
|
kwargs["response_format"] = {"type": "json_object"}
|
||||||
|
|
||||||
response = await self.openrouter_client.chat.completions.create(
|
response = await self.openrouter_client.chat.completions.create(model=model, messages=messages, **kwargs)
|
||||||
model=model,
|
if not response.choices: return ""
|
||||||
messages=messages,
|
return response.choices[0].message.content.strip() if response.choices[0].message.content else ""
|
||||||
**kwargs
|
|
||||||
)
|
async def _execute_ollama(self, prompt, system, force_json, max_retries, base_delay, temperature=None, model_override=None):
|
||||||
return response.choices[0].message.content.strip()
|
# WP-20: Restaurierter Retry-Loop für lokale Hardware-Resilienz
|
||||||
|
effective_model = model_override or self.settings.LLM_MODEL
|
||||||
|
effective_temp = temperature if temperature is not None else (0.1 if force_json else 0.7)
|
||||||
|
|
||||||
async def _execute_ollama(self, prompt, system, force_json, max_retries, base_delay):
|
|
||||||
"""Lokaler Ollama Call mit striktem Retry-Limit."""
|
|
||||||
payload = {
|
payload = {
|
||||||
"model": self.settings.LLM_MODEL,
|
"model": effective_model,
|
||||||
"prompt": prompt,
|
"prompt": prompt, "stream": False,
|
||||||
"stream": False,
|
"options": {"temperature": effective_temp, "num_ctx": 8192}
|
||||||
"options": {
|
|
||||||
"temperature": 0.1 if force_json else 0.7,
|
|
||||||
"num_ctx": 8192 # Begrenzung für Stabilität (WP-20)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if force_json:
|
if force_json: payload["format"] = "json"
|
||||||
payload["format"] = "json"
|
if system: payload["system"] = system
|
||||||
if system:
|
|
||||||
payload["system"] = system
|
|
||||||
|
|
||||||
attempt = 0
|
attempt = 0
|
||||||
while True:
|
while True:
|
||||||
@@ -279,31 +324,14 @@ class LLMService:
|
|||||||
return res.json().get("response", "").strip()
|
return res.json().get("response", "").strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
attempt += 1
|
attempt += 1
|
||||||
# WICHTIG: Wenn max_retries=0 (Chat), bricht dies nach dem 1. Versuch (attempt=1) sofort ab.
|
|
||||||
if attempt > max_retries:
|
if attempt > max_retries:
|
||||||
logger.error(f"❌ Ollama request failed after {attempt} attempt(s): {e}")
|
logger.error(f"❌ Ollama failure after {attempt} attempts: {e}")
|
||||||
raise e
|
raise e
|
||||||
|
await asyncio.sleep(base_delay * (2 ** (attempt - 1)))
|
||||||
|
|
||||||
wait_time = base_delay * (2 ** (attempt - 1))
|
async def generate_rag_response(self, query: str, context_str: Optional[str] = None) -> str:
|
||||||
logger.warning(f"⚠️ Ollama attempt {attempt} failed. Retrying in {wait_time}s...")
|
return await self.decision_engine.ask(query)
|
||||||
await asyncio.sleep(wait_time)
|
|
||||||
|
|
||||||
async def generate_rag_response(self, query: str, context_str: str) -> str:
|
|
||||||
"""Vollständiges RAG Chat-Interface."""
|
|
||||||
provider = self.settings.MINDNET_LLM_PROVIDER
|
|
||||||
system_prompt = self.get_prompt("system_prompt", provider)
|
|
||||||
rag_template = self.get_prompt("rag_template", provider)
|
|
||||||
|
|
||||||
final_prompt = rag_template.format(context_str=context_str, query=query)
|
|
||||||
|
|
||||||
# RAG Aufrufe im Chat nutzen nun standardmäßig max_retries=2 (überschreibbar)
|
|
||||||
return await self.generate_raw_response(
|
|
||||||
final_prompt,
|
|
||||||
system=system_prompt,
|
|
||||||
priority="realtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""Schließt die HTTP-Verbindungen."""
|
|
||||||
if self.ollama_client:
|
if self.ollama_client:
|
||||||
await self.ollama_client.aclose()
|
await self.ollama_client.aclose()
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
"""
|
|
||||||
FILE: app/services/semantic_analyzer.py
|
|
||||||
DESCRIPTION: KI-gestützte Kanten-Validierung. Nutzt LLM (Background-Priority), um Kanten präzise einem Chunk zuzuordnen.
|
|
||||||
WP-20 Fix: Volle Kompatibilität mit der provider-basierten Routing-Logik (OpenRouter Primary).
|
|
||||||
WP-22: Integration von valid_types zur Halluzinations-Vermeidung.
|
|
||||||
FIX: Mistral-sicheres JSON-Parsing (<s> & [OUT] Handling) und 100% Logik-Erhalt.
|
|
||||||
VERSION: 2.2.6
|
|
||||||
STATUS: Active
|
|
||||||
DEPENDENCIES: app.services.llm_service, app.services.edge_registry, json, logging, re
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
from typing import List, Optional, Any
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
# Importe
|
|
||||||
from app.services.llm_service import LLMService
|
|
||||||
# WP-22: Registry für Vokabular-Erzwingung
|
|
||||||
from app.services.edge_registry import registry as edge_registry
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
class SemanticAnalyzer:
|
|
||||||
def __init__(self):
|
|
||||||
self.llm = LLMService()
|
|
||||||
|
|
||||||
def _is_valid_edge_string(self, edge_str: str) -> bool:
|
|
||||||
"""
|
|
||||||
Prüft, ob ein String eine valide Kante im Format 'kind:target' ist.
|
|
||||||
Verhindert, dass LLM-Geschwätz als Kante durchrutscht.
|
|
||||||
"""
|
|
||||||
if not isinstance(edge_str, str) or ":" not in edge_str:
|
|
||||||
return False
|
|
||||||
|
|
||||||
parts = edge_str.split(":", 1)
|
|
||||||
kind = parts[0].strip()
|
|
||||||
target = parts[1].strip()
|
|
||||||
|
|
||||||
# Regel 1: Ein 'kind' (Beziehungstyp) darf keine Leerzeichen enthalten.
|
|
||||||
if " " in kind:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Regel 2: Plausible Länge für den Typ (Vermeidet Sätze als Typ)
|
|
||||||
if len(kind) > 40 or len(kind) < 2:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Regel 3: Target darf nicht leer sein
|
|
||||||
if not target:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def _extract_json_safely(self, text: str) -> Any:
|
|
||||||
"""
|
|
||||||
Extrahiert JSON-Daten und bereinigt LLM-Steuerzeichen (Mistral/Llama).
|
|
||||||
Implementiert robuste Recovery-Logik für Cloud-Provider.
|
|
||||||
"""
|
|
||||||
if not text:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# 1. Entferne Mistral/Llama Steuerzeichen und Tags
|
|
||||||
clean = text.replace("<s>", "").replace("</s>", "")
|
|
||||||
clean = clean.replace("[OUT]", "").replace("[/OUT]", "")
|
|
||||||
clean = clean.strip()
|
|
||||||
|
|
||||||
# 2. Suche nach Markdown JSON-Blöcken
|
|
||||||
match = re.search(r"```(?:json)?\s*(.*?)\s*```", clean, re.DOTALL)
|
|
||||||
payload = match.group(1) if match else clean
|
|
||||||
|
|
||||||
try:
|
|
||||||
return json.loads(payload.strip())
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# 3. Recovery: Suche nach der ersten [ und letzten ]
|
|
||||||
start = payload.find('[')
|
|
||||||
end = payload.rfind(']') + 1
|
|
||||||
if start != -1 and end > start:
|
|
||||||
try:
|
|
||||||
return json.loads(payload[start:end])
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
# 4. Zweite Recovery: Suche nach der ersten { und letzten }
|
|
||||||
start_obj = payload.find('{')
|
|
||||||
end_obj = payload.rfind('}') + 1
|
|
||||||
if start_obj != -1 and end_obj > start_obj:
|
|
||||||
try:
|
|
||||||
return json.loads(payload[start_obj:end_obj])
|
|
||||||
except: pass
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def assign_edges_to_chunk(self, chunk_text: str, all_edges: List[str], note_type: str) -> List[str]:
|
|
||||||
"""
|
|
||||||
Sendet einen Chunk und eine Liste potenzieller Kanten an das LLM.
|
|
||||||
Das LLM filtert heraus, welche Kanten für diesen Chunk relevant sind.
|
|
||||||
WP-20: Nutzt primär den konfigurierten Provider (z.B. OpenRouter).
|
|
||||||
"""
|
|
||||||
if not all_edges:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# 1. Bestimmung des Providers und Modells (Dynamisch über Settings)
|
|
||||||
provider = self.llm.settings.MINDNET_LLM_PROVIDER
|
|
||||||
model = self.llm.settings.OPENROUTER_MODEL if provider == "openrouter" else self.llm.settings.GEMINI_MODEL
|
|
||||||
|
|
||||||
# 2. Prompt laden (Provider-spezifisch via get_prompt)
|
|
||||||
prompt_template = self.llm.get_prompt("edge_allocation_template", provider)
|
|
||||||
|
|
||||||
if not prompt_template or not isinstance(prompt_template, str):
|
|
||||||
logger.warning("⚠️ [SemanticAnalyzer] Prompt 'edge_allocation_template' ungültig. Nutze Recovery-Template.")
|
|
||||||
prompt_template = (
|
|
||||||
"TASK: Wähle aus den Kandidaten die relevanten Kanten für den Text.\n"
|
|
||||||
"TEXT: {chunk_text}\n"
|
|
||||||
"KANDIDATEN: {edge_list}\n"
|
|
||||||
"OUTPUT: JSON Liste von Strings [\"kind:target\"]."
|
|
||||||
)
|
|
||||||
|
|
||||||
# 3. Daten für Template vorbereiten (Vokabular-Check)
|
|
||||||
edge_registry.ensure_latest()
|
|
||||||
valid_types_str = ", ".join(sorted(list(edge_registry.valid_types)))
|
|
||||||
edges_str = "\n".join([f"- {e}" for e in all_edges])
|
|
||||||
|
|
||||||
logger.debug(f"🔍 [SemanticAnalyzer] Request: {len(chunk_text)} chars Text, {len(all_edges)} Candidates.")
|
|
||||||
|
|
||||||
# 4. Prompt füllen mit Format-Check (Kein Shortcut)
|
|
||||||
try:
|
|
||||||
# Wir begrenzen den Text auf eine vernünftige Länge für das Kontextfenster
|
|
||||||
final_prompt = prompt_template.format(
|
|
||||||
chunk_text=chunk_text[:6000],
|
|
||||||
edge_list=edges_str,
|
|
||||||
valid_types=valid_types_str
|
|
||||||
)
|
|
||||||
except Exception as format_err:
|
|
||||||
logger.error(f"❌ [SemanticAnalyzer] Prompt Formatting failed: {format_err}")
|
|
||||||
return []
|
|
||||||
|
|
||||||
try:
|
|
||||||
# 5. LLM Call mit Background Priority & Semaphore Control
|
|
||||||
response_json = await self.llm.generate_raw_response(
|
|
||||||
prompt=final_prompt,
|
|
||||||
force_json=True,
|
|
||||||
max_retries=3,
|
|
||||||
base_delay=2.0,
|
|
||||||
priority="background",
|
|
||||||
provider=provider,
|
|
||||||
model_override=model
|
|
||||||
)
|
|
||||||
|
|
||||||
# 6. Mistral-sicheres JSON Parsing via Helper
|
|
||||||
data = self._extract_json_safely(response_json)
|
|
||||||
|
|
||||||
if not data:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# 7. Robuste Normalisierung (List vs Dict Recovery)
|
|
||||||
raw_candidates = []
|
|
||||||
if isinstance(data, list):
|
|
||||||
raw_candidates = data
|
|
||||||
elif isinstance(data, dict):
|
|
||||||
logger.info(f"ℹ️ [SemanticAnalyzer] LLM returned dict, trying recovery.")
|
|
||||||
for key in ["edges", "results", "kanten", "matches"]:
|
|
||||||
if key in data and isinstance(data[key], list):
|
|
||||||
raw_candidates.extend(data[key])
|
|
||||||
break
|
|
||||||
# Falls immer noch leer, nutze Schlüssel-Wert Paare als Behelf
|
|
||||||
if not raw_candidates:
|
|
||||||
for k, v in data.items():
|
|
||||||
if isinstance(v, str): raw_candidates.append(f"{k}:{v}")
|
|
||||||
elif isinstance(v, list):
|
|
||||||
for target in v:
|
|
||||||
if isinstance(target, str): raw_candidates.append(f"{k}:{target}")
|
|
||||||
|
|
||||||
# 8. Strikte Validierung gegen Kanten-Format
|
|
||||||
valid_edges = []
|
|
||||||
for e in raw_candidates:
|
|
||||||
e_str = str(e).strip()
|
|
||||||
if self._is_valid_edge_string(e_str):
|
|
||||||
valid_edges.append(e_str)
|
|
||||||
else:
|
|
||||||
logger.debug(f" [SemanticAnalyzer] Rejected invalid edge format: '{e_str}'")
|
|
||||||
|
|
||||||
if valid_edges:
|
|
||||||
logger.info(f"✅ [SemanticAnalyzer] Assigned {len(valid_edges)} edges to chunk.")
|
|
||||||
return valid_edges
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"💥 [SemanticAnalyzer] Critical error during analysis: {e}", exc_info=True)
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def close(self):
|
|
||||||
if self.llm:
|
|
||||||
await self.llm.close()
|
|
||||||
|
|
||||||
# Singleton Instanziierung
|
|
||||||
_analyzer_instance = None
|
|
||||||
def get_semantic_analyzer():
|
|
||||||
global _analyzer_instance
|
|
||||||
if _analyzer_instance is None:
|
|
||||||
_analyzer_instance = SemanticAnalyzer()
|
|
||||||
return _analyzer_instance
|
|
||||||
+120
-124
@@ -1,145 +1,141 @@
|
|||||||
# config/decision_engine.yaml
|
# config/decision_engine.yaml
|
||||||
# Steuerung der Decision Engine (Intent Recognition & Graph Routing)
|
# VERSION: 3.2.2 (WP-25a: Decoupled MoE Logic)
|
||||||
# VERSION: 2.6.1 (WP-20: Hybrid LLM & WP-22: Semantic Graph Routing)
|
|
||||||
# STATUS: Active
|
# STATUS: Active
|
||||||
# DoD: Keine Hardcoded Modelle, volle Integration der strategischen Boosts.
|
# DESCRIPTION: Zentrale Orchestrierung der Multi-Stream-Engine.
|
||||||
|
# FIX:
|
||||||
|
# - Auslagerung der LLM-Profile in llm_profiles.yaml zur zentralen Wartbarkeit.
|
||||||
|
# - Integration von compression_thresholds zur Inhaltsverdichtung (WP-25a).
|
||||||
|
# - 100% Erhalt aller WP-25 Edge-Boosts und Filter-Typen (v3.1.6).
|
||||||
|
|
||||||
version: 2.6
|
version: 3.2
|
||||||
|
|
||||||
settings:
|
settings:
|
||||||
llm_fallback_enabled: true
|
llm_fallback_enabled: true
|
||||||
|
# "auto" nutzt den globalen Default-Provider aus der .env
|
||||||
# Strategie für den Router selbst (Welches Modell erkennt den Intent?)
|
|
||||||
# "auto" nutzt den in MINDNET_LLM_PROVIDER gesetzten Standard (z.B. openrouter).
|
|
||||||
router_provider: "auto"
|
router_provider: "auto"
|
||||||
|
# Verweis auf den Intent-Klassifizierer in der prompts.yaml
|
||||||
|
router_prompt_key: "intent_router_v1"
|
||||||
|
# Pfad zur neuen Experten-Konfiguration (WP-25a Architektur-Cleanliness)
|
||||||
|
profiles_config_path: "config/llm_profiles.yaml"
|
||||||
|
router_profile: "compression_fast"
|
||||||
|
|
||||||
# Few-Shot Prompting für den LLM-Router
|
# --- EBENE 1: STREAM-LIBRARY (Bausteine basierend auf types.yaml v2.7.0) ---
|
||||||
llm_router_prompt: |
|
streams_library:
|
||||||
Du bist der zentrale Intent-Klassifikator für Mindnet, einen digitalen Zwilling.
|
values_stream:
|
||||||
Analysiere die Nachricht und wähle die passende Strategie.
|
name: "Identität & Ethik"
|
||||||
Antworte NUR mit dem Namen der Strategie.
|
# Referenz auf Experten-Profil (z.B. lokal via Ollama für Privacy)
|
||||||
|
llm_profile: "identity_safe"
|
||||||
STRATEGIEN:
|
compression_profile: "identity_safe"
|
||||||
- INTERVIEW: User will Wissen erfassen, Notizen anlegen oder Dinge festhalten.
|
compression_threshold: 2500
|
||||||
- DECISION: Rat, Strategie, Abwägung von Werten, "Soll ich tun X?".
|
query_template: "Welche meiner Werte und Prinzipien betreffen: {query}"
|
||||||
- EMPATHY: Gefühle, Reflexion der eigenen Verfassung, Frust, Freude.
|
filter_types: ["value", "principle", "belief", "trait", "boundary", "need", "motivation"]
|
||||||
- CODING: Code-Erstellung, Debugging, technische Dokumentation.
|
top_k: 5
|
||||||
- FACT: Reine Wissensabfrage, Definitionen, Suchen von Informationen.
|
edge_boosts:
|
||||||
|
guides: 3.0
|
||||||
BEISPIELE:
|
depends_on: 2.5
|
||||||
User: "Wie funktioniert die Qdrant-Vektor-DB?" -> FACT
|
based_on: 2.0
|
||||||
User: "Soll ich mein Startup jetzt verkaufen?" -> DECISION
|
upholds: 2.5
|
||||||
User: "Notiere mir kurz meine Gedanken zum Meeting." -> INTERVIEW
|
violates: 2.5
|
||||||
User: "Ich fühle mich heute sehr erschöpft." -> EMPATHY
|
aligned_with: 2.0
|
||||||
User: "Schreibe eine FastAPI-Route für den Ingest." -> CODING
|
conflicts_with: 2.0
|
||||||
|
supports: 1.5
|
||||||
NACHRICHT: "{query}"
|
contradicts: 1.5
|
||||||
|
facts_stream:
|
||||||
STRATEGIE:
|
name: "Operative Realität"
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
strategies:
|
compression_profile: "compression_fast"
|
||||||
# 1. Fakten-Abfrage (Turbo-Modus via OpenRouter / Primary)
|
compression_threshold: 3500
|
||||||
FACT:
|
query_template: "Status, Ressourcen und Fakten zu: {query}"
|
||||||
description: "Reine Wissensabfrage."
|
filter_types: ["project", "decision", "task", "goal", "event", "state"]
|
||||||
preferred_provider: "openrouter"
|
top_k: 5
|
||||||
trigger_keywords: []
|
|
||||||
inject_types: []
|
|
||||||
# WP-22: Definitionen & Hierarchien im Graphen bevorzugen
|
|
||||||
edge_boosts:
|
edge_boosts:
|
||||||
part_of: 2.0
|
part_of: 2.0
|
||||||
composed_of: 2.0
|
depends_on: 1.5
|
||||||
similar_to: 1.5
|
implemented_in: 1.5
|
||||||
caused_by: 0.5
|
|
||||||
prompt_template: "rag_template"
|
|
||||||
prepend_instruction: null
|
|
||||||
|
|
||||||
# 2. Entscheidungs-Frage (Power-Strategie via Gemini)
|
biography_stream:
|
||||||
DECISION:
|
name: "Persönliche Erfahrung"
|
||||||
description: "Der User sucht Rat, Strategie oder Abwägung."
|
llm_profile: "synthesis_pro"
|
||||||
preferred_provider: "gemini"
|
compression_profile: "compression_fast"
|
||||||
trigger_keywords:
|
compression_threshold: 3000
|
||||||
- "soll ich"
|
query_template: "Welche Erlebnisse habe ich im Kontext von {query} gemacht?"
|
||||||
- "meinung"
|
filter_types: ["experience", "journal", "profile", "person"]
|
||||||
- "besser"
|
top_k: 3
|
||||||
- "empfehlung"
|
edge_boosts:
|
||||||
- "strategie"
|
related_to: 1.5
|
||||||
- "entscheidung"
|
experienced_in: 2.0
|
||||||
- "abwägung"
|
expert_for: 2.5
|
||||||
- "vergleich"
|
followed_by: 2.0
|
||||||
inject_types: ["value", "principle", "goal", "risk"]
|
preceded_by: 2.0
|
||||||
# WP-22: Risiken und Konsequenzen im Graphen priorisieren
|
|
||||||
|
risk_stream:
|
||||||
|
name: "Risiko-Radar"
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
|
compression_profile: "compression_fast"
|
||||||
|
compression_threshold: 2500
|
||||||
|
query_template: "Gefahren, Hindernisse oder Risiken bei: {query}"
|
||||||
|
filter_types: ["risk", "obstacle", "bias"]
|
||||||
|
top_k: 3
|
||||||
edge_boosts:
|
edge_boosts:
|
||||||
blocks: 2.5
|
blocks: 2.5
|
||||||
solves: 2.0
|
|
||||||
depends_on: 1.5
|
|
||||||
risk_of: 2.5
|
|
||||||
impacts: 2.0
|
impacts: 2.0
|
||||||
prompt_template: "decision_template"
|
risk_of: 2.5
|
||||||
prepend_instruction: |
|
|
||||||
!!! ENTSCHEIDUNGS-MODUS (HYBRID AI) !!!
|
|
||||||
BITTE WÄGE FAKTEN GEGEN FOLGENDE WERTE, PRINZIPIEN UND ZIELE AB:
|
|
||||||
|
|
||||||
# 3. Empathie / "Ich"-Modus (Lokal & Privat via Ollama)
|
tech_stream:
|
||||||
EMPATHY:
|
name: "Wissen & Technik"
|
||||||
description: "Reaktion auf emotionale Zustände."
|
llm_profile: "tech_expert"
|
||||||
preferred_provider: "openrouter"
|
compression_profile: "compression_fast"
|
||||||
trigger_keywords:
|
compression_threshold: 4500
|
||||||
- "ich fühle"
|
query_template: "Inhaltliche Details und Definitionen zu: {query}"
|
||||||
- "traurig"
|
filter_types: ["concept", "source", "glossary", "idea", "insight", "skill", "habit"]
|
||||||
- "glücklich"
|
top_k: 5
|
||||||
- "gestresst"
|
|
||||||
- "angst"
|
|
||||||
- "nervt"
|
|
||||||
- "überfordert"
|
|
||||||
- "müde"
|
|
||||||
inject_types: ["experience", "belief", "profile"]
|
|
||||||
edge_boosts:
|
|
||||||
based_on: 2.0
|
|
||||||
related_to: 2.0
|
|
||||||
experienced_in: 2.5
|
|
||||||
blocks: 0.1
|
|
||||||
prompt_template: "empathy_template"
|
|
||||||
prepend_instruction: null
|
|
||||||
|
|
||||||
# 4. Coding / Technical (Gemini Power)
|
|
||||||
CODING:
|
|
||||||
description: "Technische Anfragen und Programmierung."
|
|
||||||
preferred_provider: "gemini"
|
|
||||||
trigger_keywords:
|
|
||||||
- "code"
|
|
||||||
- "python"
|
|
||||||
- "script"
|
|
||||||
- "funktion"
|
|
||||||
- "bug"
|
|
||||||
- "syntax"
|
|
||||||
- "json"
|
|
||||||
- "yaml"
|
|
||||||
- "bash"
|
|
||||||
inject_types: ["snippet", "reference", "source"]
|
|
||||||
# WP-22: Technische Abhängigkeiten priorisieren
|
|
||||||
edge_boosts:
|
edge_boosts:
|
||||||
uses: 2.5
|
uses: 2.5
|
||||||
depends_on: 2.0
|
|
||||||
implemented_in: 3.0
|
implemented_in: 3.0
|
||||||
prompt_template: "technical_template"
|
|
||||||
prepend_instruction: null
|
|
||||||
|
|
||||||
# 5. Interview / Datenerfassung (Lokal)
|
# --- EBENE 2: STRATEGIEN (Finale Komposition via MoE-Profile) ---
|
||||||
|
strategies:
|
||||||
|
FACT_WHEN:
|
||||||
|
description: "Abfrage von exakten Zeitpunkten und Terminen."
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
|
trigger_keywords: ["wann", "datum", "uhrzeit", "zeitpunkt"]
|
||||||
|
use_streams: ["facts_stream", "biography_stream", "tech_stream"]
|
||||||
|
prompt_template: "fact_synthesis_v1"
|
||||||
|
|
||||||
|
FACT_WHAT:
|
||||||
|
description: "Abfrage von Definitionen, Listen und Inhalten."
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
|
trigger_keywords: ["was ist", "welche sind", "liste", "übersicht", "zusammenfassung"]
|
||||||
|
use_streams: ["facts_stream", "tech_stream", "biography_stream"]
|
||||||
|
prompt_template: "fact_synthesis_v1"
|
||||||
|
|
||||||
|
DECISION:
|
||||||
|
description: "Der User sucht Rat, Strategie oder Abwägung."
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
|
trigger_keywords: ["soll ich", "sollte ich", "entscheidung", "abwägen", "priorität", "empfehlung"]
|
||||||
|
use_streams: ["values_stream", "facts_stream", "risk_stream"]
|
||||||
|
prompt_template: "decision_synthesis_v1"
|
||||||
|
prepend_instruction: |
|
||||||
|
!!! ENTSCHEIDUNGS-MODUS (AGENTIC MULTI-STREAM) !!!
|
||||||
|
Analysiere die Fakten vor dem Hintergrund meiner Werte und evaluiere die Risiken.
|
||||||
|
Wäge ab, ob das Vorhaben mit meiner langfristigen Identität kompatibel ist.
|
||||||
|
|
||||||
|
EMPATHY:
|
||||||
|
description: "Reaktion auf emotionale Zustände."
|
||||||
|
llm_profile: "synthesis_pro"
|
||||||
|
trigger_keywords: ["fühle", "traurig", "glücklich", "stress", "angst"]
|
||||||
|
use_streams: ["biography_stream", "values_stream"]
|
||||||
|
prompt_template: "empathy_template"
|
||||||
|
|
||||||
|
CODING:
|
||||||
|
description: "Technische Anfragen und Programmierung."
|
||||||
|
llm_profile: "tech_expert"
|
||||||
|
trigger_keywords: ["code", "python", "script", "bug", "syntax"]
|
||||||
|
use_streams: ["tech_stream", "facts_stream"]
|
||||||
|
prompt_template: "technical_template"
|
||||||
|
|
||||||
INTERVIEW:
|
INTERVIEW:
|
||||||
description: "Der User möchte Wissen erfassen."
|
description: "Der User möchte Wissen erfassen (Eingabemodus)."
|
||||||
preferred_provider: "openrouter"
|
llm_profile: "compression_fast"
|
||||||
trigger_keywords:
|
use_streams: []
|
||||||
- "neue notiz"
|
|
||||||
- "etwas notieren"
|
|
||||||
- "festhalten"
|
|
||||||
- "erstellen"
|
|
||||||
- "dokumentieren"
|
|
||||||
- "anlegen"
|
|
||||||
- "interview"
|
|
||||||
- "erfassen"
|
|
||||||
- "idee speichern"
|
|
||||||
- "draft"
|
|
||||||
inject_types: []
|
|
||||||
edge_boosts: {}
|
|
||||||
prompt_template: "interview_template"
|
prompt_template: "interview_template"
|
||||||
prepend_instruction: null
|
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
# config/llm_profiles.yaml
|
||||||
|
# VERSION: 1.3.0 (WP-25a: Global MoE & Fallback Cascade)
|
||||||
|
# STATUS: Active
|
||||||
|
# DESCRIPTION: Zentrale Definition der LLM-Rollen inkl. Ausfall-Logik (Kaskade).
|
||||||
|
|
||||||
|
profiles:
|
||||||
|
# --- CHAT & SYNTHESE ---
|
||||||
|
# Der "Architekt": Hochwertige Synthese. Fällt bei Fehlern auf den Backup-Cloud-Experten zurück.
|
||||||
|
synthesis_pro:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "google/gemini-2.0-flash-exp:free"
|
||||||
|
temperature: 0.7
|
||||||
|
fallback_profile: "synthesis_backup"
|
||||||
|
|
||||||
|
# Der "Vize": Leistungsstarkes Modell bei einem anderen Provider (Resilienz).
|
||||||
|
synthesis_backup:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "meta-llama/llama-3.3-70b-instruct:free"
|
||||||
|
temperature: 0.5
|
||||||
|
fallback_profile: "identity_safe" # Letzte Instanz: Lokal
|
||||||
|
|
||||||
|
# Der "Ingenieur": Fachspezialist für Code. Nutzt bei Ausfall den Generalisten.
|
||||||
|
tech_expert:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "qwen/qwen-2.5-vl-7b-instruct:free"
|
||||||
|
temperature: 0.3
|
||||||
|
fallback_profile: "synthesis_pro"
|
||||||
|
|
||||||
|
# Der "Dampfhammer": Schnell für Routing und Zusammenfassungen.
|
||||||
|
compression_fast:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "mistralai/mistral-7b-instruct:free"
|
||||||
|
temperature: 0.1
|
||||||
|
fallback_profile: "identity_safe"
|
||||||
|
|
||||||
|
# --- INGESTION EXPERTEN ---
|
||||||
|
# Spezialist für die Extraktion komplexer Datenstrukturen aus Dokumenten.
|
||||||
|
ingest_extractor:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "mistralai/mistral-7b-instruct:free"
|
||||||
|
temperature: 0.2
|
||||||
|
fallback_profile: "synthesis_backup"
|
||||||
|
|
||||||
|
# Spezialist für binäre Prüfungen (YES/NO). Muss extrem deterministisch sein.
|
||||||
|
ingest_validator:
|
||||||
|
provider: "openrouter"
|
||||||
|
model: "mistralai/mistral-7b-instruct:free"
|
||||||
|
temperature: 0.0
|
||||||
|
fallback_profile: "compression_fast"
|
||||||
|
|
||||||
|
# --- LOKALER ANKER & PRIVACY ---
|
||||||
|
# Der "Wächter": Lokales Modell für maximale Privatsphäre. Ende der Kaskade.
|
||||||
|
identity_safe:
|
||||||
|
provider: "ollama"
|
||||||
|
model: "phi3:mini"
|
||||||
|
temperature: 0.2
|
||||||
|
# Kein fallback_profile definiert = Terminaler Endpunkt
|
||||||
|
|
||||||
|
# --- EMBEDDING EXPERTE ---
|
||||||
|
# Zentralisierung des Embedding-Modells zur Entfernung aus der .env.
|
||||||
|
embedding_expert:
|
||||||
|
provider: "ollama"
|
||||||
|
model: "nomic-embed-text"
|
||||||
|
dimensions: 768
|
||||||
@@ -46,3 +46,18 @@ MINDNET_VOCAB_PATH=/mindnet/vault/mindnet/_system/dictionary/edge_vocabulary.md
|
|||||||
|
|
||||||
# Change Detection für effiziente Re-Imports
|
# Change Detection für effiziente Re-Imports
|
||||||
MINDNET_CHANGE_DETECTION_MODE=full
|
MINDNET_CHANGE_DETECTION_MODE=full
|
||||||
|
|
||||||
|
# --- WP-24c v4.2.0: Konfigurierbare Markdown-Header für Edge-Zonen ---
|
||||||
|
# Komma-separierte Liste von Headern für LLM-Validierung
|
||||||
|
# Format: Header1,Header2,Header3
|
||||||
|
MINDNET_LLM_VALIDATION_HEADERS=Unzugeordnete Kanten,Edge Pool,Candidates
|
||||||
|
|
||||||
|
# Header-Ebene für LLM-Validierung (1-6, Default: 3 für ###)
|
||||||
|
MINDNET_LLM_VALIDATION_HEADER_LEVEL=3
|
||||||
|
|
||||||
|
# Komma-separierte Liste von Headern für Note-Scope Zonen
|
||||||
|
# Format: Header1,Header2,Header3
|
||||||
|
MINDNET_NOTE_SCOPE_ZONE_HEADERS=Smart Edges,Relationen,Global Links,Note-Level Relations,Globale Verbindungen
|
||||||
|
|
||||||
|
# Header-Ebene für Note-Scope Zonen (1-6, Default: 2 für ##)
|
||||||
|
MINDNET_NOTE_SCOPE_HEADER_LEVEL=2
|
||||||
@@ -0,0 +1,337 @@
|
|||||||
|
# config/prompts.yaml — VERSION 3.1.2 (WP-25 Cleanup: Multi-Stream Sync)
|
||||||
|
# STATUS: Active
|
||||||
|
# FIX:
|
||||||
|
# - 100% Wiederherstellung der Ingest- & Validierungslogik (Sektion 5-8).
|
||||||
|
# - Überführung der Kategorien 1-4 in die Multi-Stream Struktur unter Beibehaltung des Inhalts.
|
||||||
|
# - Konsolidierung: Sektion 9 (v3.0.0) wurde in Sektion 1 & 2 integriert (keine Redundanz).
|
||||||
|
|
||||||
|
system_prompt: |
|
||||||
|
Du bist 'mindnet', mein digitaler Zwilling und strategischer Partner.
|
||||||
|
|
||||||
|
DEINE IDENTITÄT:
|
||||||
|
- Du bist nicht nur eine Datenbank, sondern handelst nach MEINEN Werten und Zielen.
|
||||||
|
- Du passt deinen Stil dynamisch an die Situation an (Analytisch, Empathisch oder Technisch).
|
||||||
|
|
||||||
|
DEINE REGELN:
|
||||||
|
1. Deine Antwort muss zu 100% auf dem bereitgestellten KONTEXT basieren.
|
||||||
|
2. Halluziniere keine Fakten, die nicht in den Quellen stehen.
|
||||||
|
3. Antworte auf Deutsch (außer bei Code/Fachbegriffen).
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 1. STANDARD: Fakten & Wissen (Intent: FACT_WHAT / FACT_WHEN)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# Ersetzt das alte 'rag_template'. Nutzt jetzt parallele Streams.
|
||||||
|
fact_synthesis_v1:
|
||||||
|
ollama: |
|
||||||
|
WISSENS-STREAMS:
|
||||||
|
=========================================
|
||||||
|
FAKTEN & STATUS:
|
||||||
|
{facts_stream}
|
||||||
|
|
||||||
|
ERFAHRUNG & BIOGRAFIE:
|
||||||
|
{biography_stream}
|
||||||
|
|
||||||
|
WISSEN & TECHNIK:
|
||||||
|
{tech_stream}
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
FRAGE:
|
||||||
|
{query}
|
||||||
|
|
||||||
|
ANWEISUNG:
|
||||||
|
Beantworte die Frage präzise basierend auf den Quellen.
|
||||||
|
Kombiniere harte Fakten mit persönlichen Erfahrungen, falls vorhanden.
|
||||||
|
Fasse die Informationen zusammen. Sei objektiv und neutral.
|
||||||
|
gemini: |
|
||||||
|
Beantworte die Wissensabfrage "{query}" basierend auf diesen Streams:
|
||||||
|
FAKTEN: {facts_stream}
|
||||||
|
BIOGRAFIE/ERFAHRUNG: {biography_stream}
|
||||||
|
TECHNIK: {tech_stream}
|
||||||
|
Kombiniere harte Fakten mit persönlichen Erfahrungen, falls vorhanden. Antworte strukturiert und präzise.
|
||||||
|
openrouter: |
|
||||||
|
Synthese der Wissens-Streams für: {query}
|
||||||
|
Inhalt: {facts_stream} | {biography_stream} | {tech_stream}
|
||||||
|
Antworte basierend auf dem bereitgestellten Kontext.
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 2. DECISION: Strategie & Abwägung (Intent: DECISION)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# Ersetzt das alte 'decision_template'. Nutzt jetzt parallele Streams.
|
||||||
|
decision_synthesis_v1:
|
||||||
|
ollama: |
|
||||||
|
ENTSCHEIDUNGS-STREAMS:
|
||||||
|
=========================================
|
||||||
|
WERTE & PRINZIPIEN (Identität):
|
||||||
|
{values_stream}
|
||||||
|
|
||||||
|
OPERATIVE FAKTEN (Realität):
|
||||||
|
{facts_stream}
|
||||||
|
|
||||||
|
RISIKO-RADAR (Konsequenzen):
|
||||||
|
{risk_stream}
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
ENTSCHEIDUNGSFRAGE:
|
||||||
|
{query}
|
||||||
|
|
||||||
|
ANWEISUNG:
|
||||||
|
Du agierst als mein Entscheidungs-Partner.
|
||||||
|
1. Analysiere die Faktenlage aus den Quellen.
|
||||||
|
2. Prüfe dies hart gegen meine strategischen Notizen (Werte & Prinzipien).
|
||||||
|
3. Wäge ab: Passt die technische/faktische Lösung zu meinen Werten?
|
||||||
|
|
||||||
|
FORMAT:
|
||||||
|
- **Analyse:** (Kurze Zusammenfassung der Fakten)
|
||||||
|
- **Abgleich:** (Gibt es Konflikte mit Werten/Zielen? Nenne die Quelle!)
|
||||||
|
- **Empfehlung:** (Klare Meinung: Ja/No/Vielleicht mit Begründung)
|
||||||
|
gemini: |
|
||||||
|
Agiere als mein strategischer Partner. Analysiere die Frage: {query}
|
||||||
|
Werte: {values_stream} | Fakten: {facts_stream} | Risiken: {risk_stream}.
|
||||||
|
Wäge ab und gib eine klare strategische Empfehlung ab.
|
||||||
|
openrouter: |
|
||||||
|
Strategische Multi-Stream Analyse für: {query}
|
||||||
|
Werte-Basis: {values_stream} | Fakten: {facts_stream} | Risiken: {risk_stream}
|
||||||
|
Bitte wäge ab und gib eine Empfehlung.
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 3. EMPATHY: Der Spiegel / "Ich"-Modus (Intent: EMPATHY)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
empathy_template:
|
||||||
|
ollama: |
|
||||||
|
KONTEXT (ERFAHRUNGEN & WERTE):
|
||||||
|
=========================================
|
||||||
|
ERLEBNISSE & BIOGRAFIE:
|
||||||
|
{biography_stream}
|
||||||
|
|
||||||
|
WERTE & BEDÜRFNISSE:
|
||||||
|
{values_stream}
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
SITUATION:
|
||||||
|
{query}
|
||||||
|
|
||||||
|
ANWEISUNG:
|
||||||
|
Du agierst jetzt als mein empathischer Spiegel.
|
||||||
|
1. Versuche nicht sofort, das Problem technisch zu lösen.
|
||||||
|
2. Zeige Verständnis für die Situation basierend auf meinen eigenen Erfahrungen ([EXPERIENCE]) oder Werten, falls im Kontext vorhanden.
|
||||||
|
3. Antworte in der "Ich"-Form oder "Wir"-Form. Sei unterstützend.
|
||||||
|
|
||||||
|
TONFALL:
|
||||||
|
Ruhig, verständnisvoll, reflektiert. Keine Aufzählungszeichen, sondern fließender Text.
|
||||||
|
gemini: "Sei mein digitaler Spiegel für {query}. Kontext: {biography_stream}, {values_stream}"
|
||||||
|
openrouter: "Empathische Reflexion der Situation {query}. Persönlicher Kontext: {biography_stream}, {values_stream}"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 4. TECHNICAL: Der Coder (Intent: CODING)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
technical_template:
|
||||||
|
ollama: |
|
||||||
|
KONTEXT (WISSEN & PROJEKTE):
|
||||||
|
=========================================
|
||||||
|
TECHNIK & SNIPPETS:
|
||||||
|
{tech_stream}
|
||||||
|
|
||||||
|
PROJEKT-STATUS:
|
||||||
|
{facts_stream}
|
||||||
|
=========================================
|
||||||
|
|
||||||
|
TASK:
|
||||||
|
{query}
|
||||||
|
|
||||||
|
ANWEISUNG:
|
||||||
|
Du bist Senior Developer.
|
||||||
|
1. Ignoriere Smalltalk. Komm sofort zum Punkt.
|
||||||
|
2. Generiere validen, performanten Code basierend auf den Quellen.
|
||||||
|
3. Wenn Quellen fehlen, nutze dein allgemeines Programmierwissen, aber weise darauf hin.
|
||||||
|
|
||||||
|
FORMAT:
|
||||||
|
- Kurze Erklärung des Ansatzes.
|
||||||
|
- Markdown Code-Block (Copy-Paste fertig).
|
||||||
|
- Wichtige Edge-Cases.
|
||||||
|
gemini: "Generiere Code für {query} unter Berücksichtigung von {tech_stream} und {facts_stream}."
|
||||||
|
openrouter: "Technischer Support für {query}. Referenzen: {tech_stream}, Projekt-Kontext: {facts_stream}"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 5. INTERVIEW: Der "One-Shot Extractor" (WP-07)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
interview_template:
|
||||||
|
ollama: |
|
||||||
|
TASK:
|
||||||
|
Du bist ein professioneller Ghostwriter. Verwandle den "USER INPUT" in eine strukturierte Notiz vom Typ '{target_type}'.
|
||||||
|
|
||||||
|
STRUKTUR (Nutze EXAKT diese Überschriften):
|
||||||
|
{schema_fields}
|
||||||
|
|
||||||
|
USER INPUT:
|
||||||
|
"{query}"
|
||||||
|
|
||||||
|
ANWEISUNG ZUM INHALT:
|
||||||
|
1. Analysiere den Input genau.
|
||||||
|
2. Schreibe die Inhalte unter die passenden Überschriften aus der STRUKTUR-Liste oben.
|
||||||
|
3. STIL: Schreibe flüssig, professionell und in der Ich-Perspektive. Korrigiere Grammatikfehler, aber behalte den persönlichen Ton bei.
|
||||||
|
4. Wenn Informationen für einen Abschnitt fehlen, schreibe nur: "[TODO: Ergänzen]". Erfinde nichts dazu.
|
||||||
|
|
||||||
|
OUTPUT FORMAT (YAML + MARKDOWN):
|
||||||
|
---
|
||||||
|
type: {target_type}
|
||||||
|
status: draft
|
||||||
|
title: (Erstelle einen treffenden, kurzen Titel für den Inhalt)
|
||||||
|
tags: [Tag1, Tag2]
|
||||||
|
---
|
||||||
|
|
||||||
|
# (Wiederhole den Titel hier)
|
||||||
|
|
||||||
|
## (Erster Begriff aus STRUKTUR)
|
||||||
|
(Text...)
|
||||||
|
|
||||||
|
## (Zweiter Begriff aus STRUKTUR)
|
||||||
|
(Text...)
|
||||||
|
gemini: "Extrahiere Daten für {target_type} aus {query}."
|
||||||
|
openrouter: "Strukturiere den Input {query} nach dem Schema {schema_fields} für Typ {target_type}."
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 6. EDGE_ALLOCATION: Kantenfilter (Ingest)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
edge_allocation_template:
|
||||||
|
ollama: |
|
||||||
|
TASK:
|
||||||
|
Du bist ein strikter Selektor. Du erhältst eine Liste von "Kandidaten-Kanten" (Strings).
|
||||||
|
Wähle jene aus, die inhaltlich im "Textabschnitt" vorkommen oder relevant sind.
|
||||||
|
|
||||||
|
TEXTABSCHNITT:
|
||||||
|
"""
|
||||||
|
{chunk_text}
|
||||||
|
"""
|
||||||
|
|
||||||
|
KANDIDATEN (Auswahl-Pool):
|
||||||
|
{edge_list}
|
||||||
|
|
||||||
|
REGELN:
|
||||||
|
1. Die Kanten haben das Format "typ:ziel". Der "typ" ist variabel und kann ALLES sein.
|
||||||
|
2. Gib NUR die Strings aus der Kandidaten-Liste zurück, die zum Text passen.
|
||||||
|
3. Erfinde KEINE neuen Kanten.
|
||||||
|
4. Antworte als flache JSON-Liste.
|
||||||
|
|
||||||
|
DEIN OUTPUT (JSON):
|
||||||
|
gemini: |
|
||||||
|
TASK: Ordne Kanten einem Textabschnitt zu.
|
||||||
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
|
TEXT: {chunk_text}
|
||||||
|
KANDIDATEN: {edge_list}
|
||||||
|
OUTPUT: STRIKT eine flache JSON-Liste ["typ:ziel"]. Kein Text davor/danach. Wenn nichts: []. Keine Objekte!
|
||||||
|
openrouter: |
|
||||||
|
TASK: Filtere relevante Kanten aus dem Pool.
|
||||||
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
|
TEXT: {chunk_text}
|
||||||
|
POOL: {edge_list}
|
||||||
|
ANWEISUNG: Gib NUR eine flache JSON-Liste von Strings zurück.
|
||||||
|
BEISPIEL: ["kind:target", "kind:target"]
|
||||||
|
REGEL: Kein Text, keine Analyse, keine Kommentare. Wenn nichts passt, gib [] zurück.
|
||||||
|
OUTPUT:
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 7. SMART EDGE ALLOCATION: Extraktion (Ingest)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
edge_extraction:
|
||||||
|
ollama: |
|
||||||
|
TASK:
|
||||||
|
Du bist ein Wissens-Ingenieur für den digitalen Zwilling 'mindnet'.
|
||||||
|
Deine Aufgabe ist es, semantische Relationen (Kanten) aus dem Text zu extrahieren,
|
||||||
|
die die Hauptnotiz '{note_id}' mit anderen Konzepten verbinden.
|
||||||
|
|
||||||
|
ANWEISUNGEN:
|
||||||
|
1. Identifiziere wichtige Entitäten, Konzepte oder Ereignisse im Text.
|
||||||
|
2. Bestimme die Art der Beziehung (z.B. part_of, uses, related_to, blocks, caused_by).
|
||||||
|
3. Das Ziel (target) muss ein prägnanter Begriff sein.
|
||||||
|
4. Antworte AUSSCHLIESSLICH in validem JSON als Liste von Objekten.
|
||||||
|
|
||||||
|
BEISPIEL:
|
||||||
|
[[ {{"to": "Ziel-Konzept", \"kind\": \"beziehungs_typ\"}} ]]
|
||||||
|
|
||||||
|
TEXT:
|
||||||
|
"""
|
||||||
|
{text}
|
||||||
|
"""
|
||||||
|
|
||||||
|
DEIN OUTPUT (JSON):
|
||||||
|
gemini: |
|
||||||
|
Analysiere '{note_id}'. Extrahiere semantische Beziehungen.
|
||||||
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
|
TEXT: {text}
|
||||||
|
OUTPUT: STRIKT JSON-Array von Objekten: [[{{"to\":\"Ziel\",\"kind\":\"typ\"}}]]. Kein Text davor/danach. Wenn nichts: [].
|
||||||
|
openrouter: |
|
||||||
|
TASK: Extrahiere semantische Relationen für '{note_id}'.
|
||||||
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
|
TEXT: {text}
|
||||||
|
ANWEISUNG: Antworte AUSSCHLIESSLICH mit einem JSON-Array von Objekten.
|
||||||
|
FORMAT: [[{{"to\":\"Ziel-Begriff\",\"kind\":\"typ\"}}]]
|
||||||
|
STRIKTES VERBOT: Schreibe keine Einleitung, keine Analyse und keine Erklärungen.
|
||||||
|
Wenn keine Relationen existieren, antworte NUR mit: []
|
||||||
|
OUTPUT:
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 8. WP-15b: EDGE VALIDATION (Ingest/Validate)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
edge_validation:
|
||||||
|
gemini: |
|
||||||
|
Bewerte die semantische Validität dieser Verbindung im Wissensgraph.
|
||||||
|
|
||||||
|
KONTEXT DER QUELLE (Chunk):
|
||||||
|
"{chunk_text}"
|
||||||
|
|
||||||
|
ZIEL-NOTIZ: "{target_title}"
|
||||||
|
ZIEL-BESCHREIBUNG (Zusammenfassung):
|
||||||
|
"{target_summary}"
|
||||||
|
|
||||||
|
GEPLANTE RELATION: "{edge_kind}"
|
||||||
|
|
||||||
|
FRAGE: Bestätigt der Kontext der Quelle die Beziehung '{edge_kind}' zum Ziel?
|
||||||
|
REGEL: Antworte NUR mit 'YES' oder 'NO'. Keine Erklärungen oder Smalltalk.
|
||||||
|
openrouter: |
|
||||||
|
Verify semantic relation for graph construction.
|
||||||
|
Source Context: {chunk_text}
|
||||||
|
Target Note: {target_title}
|
||||||
|
Target Summary: {target_summary}
|
||||||
|
Proposed Relation: {edge_kind}
|
||||||
|
Instruction: Does the source context support this relation to the target?
|
||||||
|
Result: Respond ONLY with 'YES' or 'NO'.
|
||||||
|
ollama: |
|
||||||
|
Bewerte die semantische Korrektheit dieser Verbindung.
|
||||||
|
QUELLE: {chunk_text}
|
||||||
|
ZIEL: {target_title} ({target_summary})
|
||||||
|
BEZIEHUNG: {edge_kind}
|
||||||
|
Ist diese Verbindung valide? Antworte NUR mit YES oder NO.
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 10. WP-25: INTENT ROUTING (Intent: CLASSIFY)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
intent_router_v1:
|
||||||
|
ollama: |
|
||||||
|
Analysiere die Nutzeranfrage und wähle die passende Strategie.
|
||||||
|
Antworte NUR mit dem Namen der Strategie.
|
||||||
|
|
||||||
|
STRATEGIEN:
|
||||||
|
- FACT_WHEN: Nur für explizite Fragen nach einem exakten Datum, Uhrzeit oder dem "Wann" eines Ereignisses.
|
||||||
|
- FACT_WHAT: Fragen nach Inhalten, Listen von Objekten/Projekten, Definitionen oder "Was/Welche" Anfragen (auch bei Zeiträumen).
|
||||||
|
- DECISION: Rat, Meinung, "Soll ich?", Abwägung gegen Werte.
|
||||||
|
- EMPATHY: Emotionen, Reflexion, Befindlichkeit.
|
||||||
|
- CODING: Programmierung, Skripte, technische Syntax.
|
||||||
|
- INTERVIEW: Dokumentation neuer Informationen, Notizen anlegen.
|
||||||
|
|
||||||
|
NACHRICHT: "{query}"
|
||||||
|
STRATEGIE:
|
||||||
|
gemini: |
|
||||||
|
Classify intent:
|
||||||
|
- FACT_WHEN: Exact dates/times only.
|
||||||
|
- FACT_WHAT: Content, lists of entities (projects, etc.), definitions, "What/Which" queries.
|
||||||
|
- DECISION: Strategic advice/values.
|
||||||
|
- EMPATHY: Emotions.
|
||||||
|
- CODING: Tech/Code.
|
||||||
|
- INTERVIEW: Data entry.
|
||||||
|
Query: "{query}"
|
||||||
|
Result (One word only):
|
||||||
|
openrouter: |
|
||||||
|
Select strategy for Mindnet:
|
||||||
|
FACT_WHEN (timing/dates), FACT_WHAT (entities/lists/what/which), DECISION, EMPATHY, CODING, INTERVIEW.
|
||||||
|
Query: "{query}"
|
||||||
|
Response:
|
||||||
+217
-37
@@ -1,8 +1,9 @@
|
|||||||
# config/prompts.yaml — Final V2.6.0 (WP-15b Candidate-Validation)
|
# config/prompts.yaml — VERSION 3.2.2 (WP-25b: Hierarchical Model Sync)
|
||||||
# WP-20: Optimierte Cloud-Templates zur Unterdrückung von Modell-Geschwätz.
|
# STATUS: Active
|
||||||
# FIX: Explizite Verbote für Einleitungstexte zur Vermeidung von JSON-Parsing-Fehlern.
|
# FIX:
|
||||||
# WP-15b: Integration der binären edge_validation für den Two-Pass Workflow.
|
# - 100% Erhalt der Original-Prompts aus v3.1.2 für die Provider-Ebene (ollama, gemini, openrouter).
|
||||||
# OLLAMA: UNVERÄNDERT laut Benutzeranweisung.
|
# - Integration der Modell-spezifischen Overrides für Gemini 2.0, Llama 3.3 und Qwen 2.5.
|
||||||
|
# - Hinzufügen des notwendigen 'compression_template' für die DecisionEngine v1.3.0.
|
||||||
|
|
||||||
system_prompt: |
|
system_prompt: |
|
||||||
Du bist 'mindnet', mein digitaler Zwilling und strategischer Partner.
|
Du bist 'mindnet', mein digitaler Zwilling und strategischer Partner.
|
||||||
@@ -17,13 +18,32 @@ system_prompt: |
|
|||||||
3. Antworte auf Deutsch (außer bei Code/Fachbegriffen).
|
3. Antworte auf Deutsch (außer bei Code/Fachbegriffen).
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 1. STANDARD: Fakten & Wissen (Intent: FACT)
|
# 1. STANDARD: Fakten & Wissen (Intent: FACT_WHAT / FACT_WHEN)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
rag_template:
|
fact_synthesis_v1:
|
||||||
|
# --- Modell-spezifisch (WP-25b Optimierung) ---
|
||||||
|
"google/gemini-2.0-flash-exp:free": |
|
||||||
|
Analysiere die Wissens-Streams für: {query}
|
||||||
|
FAKTEN: {facts_stream} | BIOGRAFIE: {biography_stream} | TECHNIK: {tech_stream}
|
||||||
|
Nutze deine hohe Reasoning-Kapazität für eine tiefe Synthese. Antworte präzise auf Deutsch.
|
||||||
|
|
||||||
|
"meta-llama/llama-3.3-70b-instruct:free": |
|
||||||
|
Erstelle eine fundierte Synthese für die Frage: "{query}"
|
||||||
|
Nutze die Daten: {facts_stream}, {biography_stream} und {tech_stream}.
|
||||||
|
Trenne klare Fakten von Erfahrungen. Bleibe strikt beim bereitgestellten Kontext.
|
||||||
|
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
ollama: |
|
ollama: |
|
||||||
QUELLEN (WISSEN):
|
WISSENS-STREAMS:
|
||||||
=========================================
|
=========================================
|
||||||
{context_str}
|
FAKTEN & STATUS:
|
||||||
|
{facts_stream}
|
||||||
|
|
||||||
|
ERFAHRUNG & BIOGRAFIE:
|
||||||
|
{biography_stream}
|
||||||
|
|
||||||
|
WISSEN & TECHNIK:
|
||||||
|
{tech_stream}
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
FRAGE:
|
FRAGE:
|
||||||
@@ -31,25 +51,45 @@ rag_template:
|
|||||||
|
|
||||||
ANWEISUNG:
|
ANWEISUNG:
|
||||||
Beantworte die Frage präzise basierend auf den Quellen.
|
Beantworte die Frage präzise basierend auf den Quellen.
|
||||||
|
Kombiniere harte Fakten mit persönlichen Erfahrungen, falls vorhanden.
|
||||||
Fasse die Informationen zusammen. Sei objektiv und neutral.
|
Fasse die Informationen zusammen. Sei objektiv und neutral.
|
||||||
gemini: |
|
|
||||||
Kontext meines digitalen Zwillings: {context_str}
|
|
||||||
Beantworte strukturiert und präzise: {query}
|
|
||||||
openrouter: |
|
|
||||||
Kontext-Analyse für den digitalen Zwilling:
|
|
||||||
{context_str}
|
|
||||||
|
|
||||||
Anfrage: {query}
|
gemini: |
|
||||||
Antworte basierend auf dem Kontext.
|
Beantworte die Wissensabfrage "{query}" basierend auf diesen Streams:
|
||||||
|
FAKTEN: {facts_stream}
|
||||||
|
BIOGRAFIE/ERFAHRUNG: {biography_stream}
|
||||||
|
TECHNIK: {tech_stream}
|
||||||
|
Kombiniere harte Fakten mit persönlichen Erfahrungen, falls vorhanden. Antworte strukturiert und präzise.
|
||||||
|
|
||||||
|
openrouter: |
|
||||||
|
Synthese der Wissens-Streams für: {query}
|
||||||
|
Inhalt: {facts_stream} | {biography_stream} | {tech_stream}
|
||||||
|
Antworte basierend auf dem bereitgestellten Kontext.
|
||||||
|
|
||||||
|
default: "Beantworte {query} basierend auf dem Kontext: {facts_stream} {biography_stream} {tech_stream}."
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 2. DECISION: Strategie & Abwägung (Intent: DECISION)
|
# 2. DECISION: Strategie & Abwägung (Intent: DECISION)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
decision_template:
|
decision_synthesis_v1:
|
||||||
|
# --- Modell-spezifisch (WP-25b Optimierung) ---
|
||||||
|
"google/gemini-2.0-flash-exp:free": |
|
||||||
|
Agiere als strategischer Partner für: {query}
|
||||||
|
WERTE: {values_stream} | FAKTEN: {facts_stream} | RISIKEN: {risk_stream}
|
||||||
|
Prüfe die Fakten gegen meine Werte. Zeige Zielkonflikte auf. Gib eine klare Empfehlung.
|
||||||
|
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
ollama: |
|
ollama: |
|
||||||
KONTEXT (FAKTEN & STRATEGIE):
|
ENTSCHEIDUNGS-STREAMS:
|
||||||
=========================================
|
=========================================
|
||||||
{context_str}
|
WERTE & PRINZIPIEN (Identität):
|
||||||
|
{values_stream}
|
||||||
|
|
||||||
|
OPERATIVE FAKTEN (Realität):
|
||||||
|
{facts_stream}
|
||||||
|
|
||||||
|
RISIKO-RADAR (Konsequenzen):
|
||||||
|
{risk_stream}
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
ENTSCHEIDUNGSFRAGE:
|
ENTSCHEIDUNGSFRAGE:
|
||||||
@@ -58,27 +98,39 @@ decision_template:
|
|||||||
ANWEISUNG:
|
ANWEISUNG:
|
||||||
Du agierst als mein Entscheidungs-Partner.
|
Du agierst als mein Entscheidungs-Partner.
|
||||||
1. Analysiere die Faktenlage aus den Quellen.
|
1. Analysiere die Faktenlage aus den Quellen.
|
||||||
2. Prüfe dies hart gegen meine strategischen Notizen (Typ [VALUE], [PRINCIPLE], [GOAL]).
|
2. Prüfe dies hart gegen meine strategischen Notizen (Werte & Prinzipien).
|
||||||
3. Wäge ab: Passt die technische/faktische Lösung zu meinen Werten?
|
3. Wäge ab: Passt die technische/faktische Lösung zu meinen Werten?
|
||||||
|
|
||||||
FORMAT:
|
FORMAT:
|
||||||
- **Analyse:** (Kurze Zusammenfassung der Fakten)
|
- **Analyse:** (Kurze Zusammenfassung der Fakten)
|
||||||
- **Abgleich:** (Gibt es Konflikte mit Werten/Zielen? Nenne die Quelle!)
|
- **Abgleich:** (Gibt es Konflikte mit Werten/Zielen? Nenne die Quelle!)
|
||||||
- **Empfehlung:** (Klare Meinung: Ja/No/Vielleicht mit Begründung)
|
- **Empfehlung:** (Klare Meinung: Ja/No/Vielleicht mit Begründung)
|
||||||
|
|
||||||
gemini: |
|
gemini: |
|
||||||
Agiere als strategischer Partner. Analysiere die Frage {query} basierend auf meinen Werten im Kontext {context_str}.
|
Agiere als mein strategischer Partner. Analysiere die Frage: {query}
|
||||||
|
Werte: {values_stream} | Fakten: {facts_stream} | Risiken: {risk_stream}.
|
||||||
|
Wäge ab und gib eine klare strategische Empfehlung ab.
|
||||||
|
|
||||||
openrouter: |
|
openrouter: |
|
||||||
Strategische Entscheidungsanalyse: {query}
|
Strategische Multi-Stream Analyse für: {query}
|
||||||
Wertebasis aus dem Graphen: {context_str}
|
Werte-Basis: {values_stream} | Fakten: {facts_stream} | Risiken: {risk_stream}
|
||||||
|
Bitte wäge ab und gib eine Empfehlung.
|
||||||
|
|
||||||
|
default: "Prüfe {query} gegen Werte {values_stream} und Fakten {facts_stream}."
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 3. EMPATHY: Der Spiegel / "Ich"-Modus (Intent: EMPATHY)
|
# 3. EMPATHY: Der Spiegel / "Ich"-Modus (Intent: EMPATHY)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
empathy_template:
|
empathy_template:
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
ollama: |
|
ollama: |
|
||||||
KONTEXT (ERFAHRUNGEN & GLAUBENSSÄTZE):
|
KONTEXT (ERFAHRUNGEN & WERTE):
|
||||||
=========================================
|
=========================================
|
||||||
{context_str}
|
ERLEBNISSE & BIOGRAFIE:
|
||||||
|
{biography_stream}
|
||||||
|
|
||||||
|
WERTE & BEDÜRFNISSE:
|
||||||
|
{values_stream}
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
SITUATION:
|
SITUATION:
|
||||||
@@ -87,22 +139,36 @@ empathy_template:
|
|||||||
ANWEISUNG:
|
ANWEISUNG:
|
||||||
Du agierst jetzt als mein empathischer Spiegel.
|
Du agierst jetzt als mein empathischer Spiegel.
|
||||||
1. Versuche nicht sofort, das Problem technisch zu lösen.
|
1. Versuche nicht sofort, das Problem technisch zu lösen.
|
||||||
2. Zeige Verständnis für die Situation basierend auf meinen eigenen Erfahrungen ([EXPERIENCE]) oder Glaubenssätzen ([BELIEF]), falls im Kontext vorhanden.
|
2. Zeige Verständnis für die Situation basierend auf meinen eigenen Erfahrungen ([EXPERIENCE]) oder Werten, falls im Kontext vorhanden.
|
||||||
3. Antworte in der "Ich"-Form oder "Wir"-Form. Sei unterstützend.
|
3. Antworte in der "Ich"-Form oder "Wir"-Form. Sei unterstützend.
|
||||||
|
|
||||||
TONFALL:
|
TONFALL:
|
||||||
Ruhig, verständnisvoll, reflektiert. Keine Aufzählungszeichen, sondern fließender Text.
|
Ruhig, verständnisvoll, reflektiert. Keine Aufzählungszeichen, sondern fließender Text.
|
||||||
gemini: "Sei mein digitaler Spiegel für {query}. Kontext: {context_str}"
|
|
||||||
openrouter: "Empathische Reflexion der Situation {query}. Persönlicher Kontext: {context_str}"
|
gemini: "Sei mein digitaler Spiegel für {query}. Kontext: {biography_stream}, {values_stream}"
|
||||||
|
openrouter: "Empathische Reflexion der Situation {query}. Persönlicher Kontext: {biography_stream}, {values_stream}"
|
||||||
|
|
||||||
|
default: "Reflektiere empathisch über {query} basierend auf {biography_stream}."
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 4. TECHNICAL: Der Coder (Intent: CODING)
|
# 4. TECHNICAL: Der Coder (Intent: CODING)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
technical_template:
|
technical_template:
|
||||||
|
# --- Modell-spezifisch (WP-25b Optimierung) ---
|
||||||
|
"qwen/qwen-2.5-vl-7b-instruct:free": |
|
||||||
|
Du bist Senior Software Engineer. TASK: {query}
|
||||||
|
REFERENZEN: {tech_stream} | KONTEXT: {facts_stream}
|
||||||
|
Generiere validen, performanten Code. Nutze die Snippets aus dem Kontext.
|
||||||
|
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
ollama: |
|
ollama: |
|
||||||
KONTEXT (DOCS & SNIPPETS):
|
KONTEXT (WISSEN & PROJEKTE):
|
||||||
=========================================
|
=========================================
|
||||||
{context_str}
|
TECHNIK & SNIPPETS:
|
||||||
|
{tech_stream}
|
||||||
|
|
||||||
|
PROJEKT-STATUS:
|
||||||
|
{facts_stream}
|
||||||
=========================================
|
=========================================
|
||||||
|
|
||||||
TASK:
|
TASK:
|
||||||
@@ -118,13 +184,17 @@ technical_template:
|
|||||||
- Kurze Erklärung des Ansatzes.
|
- Kurze Erklärung des Ansatzes.
|
||||||
- Markdown Code-Block (Copy-Paste fertig).
|
- Markdown Code-Block (Copy-Paste fertig).
|
||||||
- Wichtige Edge-Cases.
|
- Wichtige Edge-Cases.
|
||||||
gemini: "Generiere Code für {query} unter Berücksichtigung von {context_str}."
|
|
||||||
openrouter: "Technischer Support für {query}. Code-Referenzen: {context_str}"
|
gemini: "Generiere Code für {query} unter Berücksichtigung von {tech_stream} und {facts_stream}."
|
||||||
|
openrouter: "Technischer Support für {query}. Referenzen: {tech_stream}, Projekt-Kontext: {facts_stream}"
|
||||||
|
|
||||||
|
default: "Erstelle eine technische Lösung für {query}."
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 5. INTERVIEW: Der "One-Shot Extractor" (Performance Mode)
|
# 5. INTERVIEW: Der "One-Shot Extractor" (WP-07)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
interview_template:
|
interview_template:
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
ollama: |
|
ollama: |
|
||||||
TASK:
|
TASK:
|
||||||
Du bist ein professioneller Ghostwriter. Verwandle den "USER INPUT" in eine strukturierte Notiz vom Typ '{target_type}'.
|
Du bist ein professioneller Ghostwriter. Verwandle den "USER INPUT" in eine strukturierte Notiz vom Typ '{target_type}'.
|
||||||
@@ -156,11 +226,30 @@ interview_template:
|
|||||||
|
|
||||||
## (Zweiter Begriff aus STRUKTUR)
|
## (Zweiter Begriff aus STRUKTUR)
|
||||||
(Text...)
|
(Text...)
|
||||||
|
|
||||||
gemini: "Extrahiere Daten für {target_type} aus {query}."
|
gemini: "Extrahiere Daten für {target_type} aus {query}."
|
||||||
openrouter: "Strukturiere den Input {query} nach dem Schema {schema_fields} für Typ {target_type}."
|
openrouter: "Strukturiere den Input {query} nach dem Schema {schema_fields} für Typ {target_type}."
|
||||||
|
|
||||||
|
default: "Extrahiere Informationen für {target_type} aus dem Input: {query}"
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 6. EDGE_ALLOCATION: Kantenfilter (Intent: OFFLINE_FILTER)
|
# 6. WP-25b: PRE-SYNTHESIS COMPRESSION (Neu!)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
compression_template:
|
||||||
|
"mistralai/mistral-7b-instruct:free": |
|
||||||
|
Reduziere den Stream '{stream_name}' auf die Informationen, die für die Beantwortung der Frage '{query}' absolut notwendig sind.
|
||||||
|
BEHALTE: Harte Fakten, Projektnamen, konkrete Werte und Quellenangaben.
|
||||||
|
ENTFERNE: Redundante Einleitungen, Füllwörter und irrelevante Details.
|
||||||
|
|
||||||
|
INHALT:
|
||||||
|
{content}
|
||||||
|
|
||||||
|
KOMPRIMIERTE ANALYSE:
|
||||||
|
|
||||||
|
default: "Fasse das Wichtigste aus {stream_name} für die Frage {query} kurz zusammen: {content}"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 7. EDGE_ALLOCATION: Kantenfilter (Ingest)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
edge_allocation_template:
|
edge_allocation_template:
|
||||||
ollama: |
|
ollama: |
|
||||||
@@ -183,12 +272,14 @@ edge_allocation_template:
|
|||||||
4. Antworte als flache JSON-Liste.
|
4. Antworte als flache JSON-Liste.
|
||||||
|
|
||||||
DEIN OUTPUT (JSON):
|
DEIN OUTPUT (JSON):
|
||||||
|
|
||||||
gemini: |
|
gemini: |
|
||||||
TASK: Ordne Kanten einem Textabschnitt zu.
|
TASK: Ordne Kanten einem Textabschnitt zu.
|
||||||
ERLAUBTE TYPEN: {valid_types}
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
TEXT: {chunk_text}
|
TEXT: {chunk_text}
|
||||||
KANDIDATEN: {edge_list}
|
KANDIDATEN: {edge_list}
|
||||||
OUTPUT: STRIKT eine flache JSON-Liste ["typ:ziel"]. Kein Text davor/danach. Wenn nichts: []. Keine Objekte!
|
OUTPUT: STRIKT eine flache JSON-Liste ["typ:ziel"]. Kein Text davor/danach. Wenn nichts: []. Keine Objekte!
|
||||||
|
|
||||||
openrouter: |
|
openrouter: |
|
||||||
TASK: Filtere relevante Kanten aus dem Pool.
|
TASK: Filtere relevante Kanten aus dem Pool.
|
||||||
ERLAUBTE TYPEN: {valid_types}
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
@@ -199,8 +290,10 @@ edge_allocation_template:
|
|||||||
REGEL: Kein Text, keine Analyse, keine Kommentare. Wenn nichts passt, gib [] zurück.
|
REGEL: Kein Text, keine Analyse, keine Kommentare. Wenn nichts passt, gib [] zurück.
|
||||||
OUTPUT:
|
OUTPUT:
|
||||||
|
|
||||||
|
default: "[]"
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 7. SMART EDGE ALLOCATION: Extraktion (Intent: INGEST)
|
# 8. SMART EDGE ALLOCATION: Extraktion (Ingest)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
edge_extraction:
|
edge_extraction:
|
||||||
ollama: |
|
ollama: |
|
||||||
@@ -224,11 +317,13 @@ edge_extraction:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
DEIN OUTPUT (JSON):
|
DEIN OUTPUT (JSON):
|
||||||
|
|
||||||
gemini: |
|
gemini: |
|
||||||
Analysiere '{note_id}'. Extrahiere semantische Beziehungen.
|
Analysiere '{note_id}'. Extrahiere semantische Beziehungen.
|
||||||
ERLAUBTE TYPEN: {valid_types}
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
TEXT: {text}
|
TEXT: {text}
|
||||||
OUTPUT: STRIKT JSON-Array von Objekten: [[{{"to\":\"Ziel\",\"kind\":\"typ\"}}]]. Kein Text davor/danach. Wenn nichts: [].
|
OUTPUT: STRIKT JSON-Array von Objekten: [[{{"to\":\"Ziel\",\"kind\":\"typ\"}}]]. Kein Text davor/danach. Wenn nichts: [].
|
||||||
|
|
||||||
openrouter: |
|
openrouter: |
|
||||||
TASK: Extrahiere semantische Relationen für '{note_id}'.
|
TASK: Extrahiere semantische Relationen für '{note_id}'.
|
||||||
ERLAUBTE TYPEN: {valid_types}
|
ERLAUBTE TYPEN: {valid_types}
|
||||||
@@ -239,10 +334,20 @@ edge_extraction:
|
|||||||
Wenn keine Relationen existieren, antworte NUR mit: []
|
Wenn keine Relationen existieren, antworte NUR mit: []
|
||||||
OUTPUT:
|
OUTPUT:
|
||||||
|
|
||||||
|
default: "[]"
|
||||||
|
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
# 8. WP-15b: EDGE VALIDATION (Intent: VALIDATE)
|
# 9. INGESTION: EDGE VALIDATION (Ingest/Validate)
|
||||||
# ---------------------------------------------------------
|
# ---------------------------------------------------------
|
||||||
edge_validation:
|
edge_validation:
|
||||||
|
# --- Modell-spezifisch (WP-25b Optimierung) ---
|
||||||
|
"mistralai/mistral-7b-instruct:free": |
|
||||||
|
Verify relation '{edge_kind}' for graph integrity.
|
||||||
|
Chunk: "{chunk_text}"
|
||||||
|
Target: "{target_title}" ({target_summary})
|
||||||
|
Respond ONLY with 'YES' or 'NO'.
|
||||||
|
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
gemini: |
|
gemini: |
|
||||||
Bewerte die semantische Validität dieser Verbindung im Wissensgraph.
|
Bewerte die semantische Validität dieser Verbindung im Wissensgraph.
|
||||||
|
|
||||||
@@ -257,6 +362,7 @@ edge_validation:
|
|||||||
|
|
||||||
FRAGE: Bestätigt der Kontext der Quelle die Beziehung '{edge_kind}' zum Ziel?
|
FRAGE: Bestätigt der Kontext der Quelle die Beziehung '{edge_kind}' zum Ziel?
|
||||||
REGEL: Antworte NUR mit 'YES' oder 'NO'. Keine Erklärungen oder Smalltalk.
|
REGEL: Antworte NUR mit 'YES' oder 'NO'. Keine Erklärungen oder Smalltalk.
|
||||||
|
|
||||||
openrouter: |
|
openrouter: |
|
||||||
Verify semantic relation for graph construction.
|
Verify semantic relation for graph construction.
|
||||||
Source Context: {chunk_text}
|
Source Context: {chunk_text}
|
||||||
@@ -265,9 +371,83 @@ edge_validation:
|
|||||||
Proposed Relation: {edge_kind}
|
Proposed Relation: {edge_kind}
|
||||||
Instruction: Does the source context support this relation to the target?
|
Instruction: Does the source context support this relation to the target?
|
||||||
Result: Respond ONLY with 'YES' or 'NO'.
|
Result: Respond ONLY with 'YES' or 'NO'.
|
||||||
|
|
||||||
ollama: |
|
ollama: |
|
||||||
Bewerte die semantische Korrektheit dieser Verbindung.
|
Bewerte die semantische Korrektheit dieser Verbindung.
|
||||||
QUELLE: {chunk_text}
|
QUELLE: {chunk_text}
|
||||||
ZIEL: {target_title} ({target_summary})
|
ZIEL: {target_title} ({target_summary})
|
||||||
BEZIEHUNG: {edge_kind}
|
BEZIEHUNG: {edge_kind}
|
||||||
Ist diese Verbindung valide? Antworte NUR mit YES oder NO.
|
Ist diese Verbindung valide? Antworte NUR mit YES oder NO.
|
||||||
|
|
||||||
|
default: "YES"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 10. WP-25: INTENT ROUTING (Intent: CLASSIFY)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
intent_router_v1:
|
||||||
|
# --- Modell-spezifisch (WP-25b Optimierung) ---
|
||||||
|
"mistralai/mistral-7b-instruct:free": |
|
||||||
|
Classify query "{query}" into exactly one of these categories:
|
||||||
|
FACT_WHEN, FACT_WHAT, DECISION, EMPATHY, CODING, INTERVIEW.
|
||||||
|
Respond with the category name only.
|
||||||
|
|
||||||
|
# --- EXAKTE Provider-Fallbacks aus v3.1.2 ---
|
||||||
|
ollama: |
|
||||||
|
Analysiere die Nutzeranfrage und wähle die passende Strategie.
|
||||||
|
Antworte NUR mit dem Namen der Strategie.
|
||||||
|
|
||||||
|
STRATEGIEN:
|
||||||
|
- FACT_WHEN: Nur für explizite Fragen nach einem exakten Datum, Uhrzeit oder dem "Wann" eines Ereignisses.
|
||||||
|
- FACT_WHAT: Fragen nach Inhalten, Listen von Objekten/Projekten, Definitionen oder "Was/Welche" Anfragen (auch bei Zeiträumen).
|
||||||
|
- DECISION: Rat, Meinung, "Soll ich?", Abwägung gegen Werte.
|
||||||
|
- EMPATHY: Emotionen, Reflexion, Befindlichkeit.
|
||||||
|
- CODING: Programmierung, Skripte, technische Syntax.
|
||||||
|
- INTERVIEW: Dokumentation neuer Informationen, Notizen anlegen.
|
||||||
|
|
||||||
|
NACHRICHT: "{query}"
|
||||||
|
STRATEGIE:
|
||||||
|
|
||||||
|
gemini: |
|
||||||
|
Classify intent:
|
||||||
|
- FACT_WHEN: Exact dates/times only.
|
||||||
|
- FACT_WHAT: Content, lists of entities (projects, etc.), definitions, "What/Which" queries.
|
||||||
|
- DECISION: Strategic advice/values.
|
||||||
|
- EMPATHY: Emotions.
|
||||||
|
- CODING: Tech/Code.
|
||||||
|
- INTERVIEW: Data entry.
|
||||||
|
Query: "{query}"
|
||||||
|
Result (One word only):
|
||||||
|
|
||||||
|
openrouter: |
|
||||||
|
Select strategy for Mindnet:
|
||||||
|
FACT_WHEN (timing/dates), FACT_WHAT (entities/lists/what/which), DECISION, EMPATHY, CODING, INTERVIEW.
|
||||||
|
Query: "{query}"
|
||||||
|
Response:
|
||||||
|
|
||||||
|
default: "FACT_WHAT"
|
||||||
|
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
# 11. WP-25b: FALLBACK SYNTHESIS (Error Recovery)
|
||||||
|
# ---------------------------------------------------------
|
||||||
|
fallback_synthesis:
|
||||||
|
ollama: |
|
||||||
|
Beantworte die folgende Frage basierend auf dem bereitgestellten Kontext.
|
||||||
|
|
||||||
|
FRAGE:
|
||||||
|
{query}
|
||||||
|
|
||||||
|
KONTEXT:
|
||||||
|
{context}
|
||||||
|
|
||||||
|
ANWEISUNG:
|
||||||
|
Nutze den Kontext, um eine präzise Antwort zu geben. Falls der Kontext unvollständig ist, weise darauf hin.
|
||||||
|
|
||||||
|
gemini: |
|
||||||
|
Frage: {query}
|
||||||
|
Kontext: {context}
|
||||||
|
Antworte basierend auf dem Kontext.
|
||||||
|
|
||||||
|
openrouter: |
|
||||||
|
Answer the question "{query}" using the provided context: {context}
|
||||||
|
|
||||||
|
default: "Answer: {query}\n\nContext: {context}"
|
||||||
+50
-65
@@ -1,4 +1,4 @@
|
|||||||
version: 2.6.0 # Final WP-15 Config: Smart Edges & Strict/Soft Chunking
|
version: 2.7.0 # WP-14 Update: Dynamisierung der Ingestion-Pipeline
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 1. CHUNKING PROFILES
|
# 1. CHUNKING PROFILES
|
||||||
@@ -23,7 +23,6 @@ chunking_profiles:
|
|||||||
overlap: [50, 100]
|
overlap: [50, 100]
|
||||||
|
|
||||||
# C. SMART FLOW (Text-Fluss)
|
# C. SMART FLOW (Text-Fluss)
|
||||||
# Nutzt Sliding Window, aber mit LLM-Kanten-Analyse.
|
|
||||||
sliding_smart_edges:
|
sliding_smart_edges:
|
||||||
strategy: sliding_window
|
strategy: sliding_window
|
||||||
enable_smart_edge_allocation: true
|
enable_smart_edge_allocation: true
|
||||||
@@ -32,7 +31,6 @@ chunking_profiles:
|
|||||||
overlap: [50, 80]
|
overlap: [50, 80]
|
||||||
|
|
||||||
# D. SMART STRUCTURE (Soft Split)
|
# D. SMART STRUCTURE (Soft Split)
|
||||||
# Trennt bevorzugt an H2, fasst aber kleine Abschnitte zusammen ("Soft Mode").
|
|
||||||
structured_smart_edges:
|
structured_smart_edges:
|
||||||
strategy: by_heading
|
strategy: by_heading
|
||||||
enable_smart_edge_allocation: true
|
enable_smart_edge_allocation: true
|
||||||
@@ -43,8 +41,6 @@ chunking_profiles:
|
|||||||
overlap: [50, 80]
|
overlap: [50, 80]
|
||||||
|
|
||||||
# E. SMART STRUCTURE STRICT (H2 Hard Split)
|
# E. SMART STRUCTURE STRICT (H2 Hard Split)
|
||||||
# Trennt ZWINGEND an jeder H2.
|
|
||||||
# Verhindert, dass "Vater" und "Partner" (Profile) oder Werte verschmelzen.
|
|
||||||
structured_smart_edges_strict:
|
structured_smart_edges_strict:
|
||||||
strategy: by_heading
|
strategy: by_heading
|
||||||
enable_smart_edge_allocation: true
|
enable_smart_edge_allocation: true
|
||||||
@@ -55,9 +51,6 @@ chunking_profiles:
|
|||||||
overlap: [50, 80]
|
overlap: [50, 80]
|
||||||
|
|
||||||
# F. SMART STRUCTURE DEEP (H3 Hard Split + Merge-Check)
|
# F. SMART STRUCTURE DEEP (H3 Hard Split + Merge-Check)
|
||||||
# Spezialfall für "Leitbild Prinzipien":
|
|
||||||
# - Trennt H1, H2, H3 hart.
|
|
||||||
# - Aber: Merged "leere" H2 (Tier 2) mit der folgenden H3 (MP1).
|
|
||||||
structured_smart_edges_strict_L3:
|
structured_smart_edges_strict_L3:
|
||||||
strategy: by_heading
|
strategy: by_heading
|
||||||
enable_smart_edge_allocation: true
|
enable_smart_edge_allocation: true
|
||||||
@@ -73,18 +66,36 @@ chunking_profiles:
|
|||||||
defaults:
|
defaults:
|
||||||
retriever_weight: 1.0
|
retriever_weight: 1.0
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
edge_defaults: []
|
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 3. TYPE DEFINITIONS
|
# 3. INGESTION SETTINGS (WP-14 Dynamization)
|
||||||
|
# ==============================================================================
|
||||||
|
ingestion_settings:
|
||||||
|
ignore_statuses: ["system", "template", "archive", "hidden"]
|
||||||
|
default_note_type: "concept"
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 4. SUMMARY & SCAN SETTINGS
|
||||||
|
# ==============================================================================
|
||||||
|
summary_settings:
|
||||||
|
max_summary_length: 500
|
||||||
|
pre_scan_depth: 600
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 5. LLM SETTINGS
|
||||||
|
# ==============================================================================
|
||||||
|
llm_settings:
|
||||||
|
cleanup_patterns: ["<s>", "</s>", "[OUT]", "[/OUT]", "```json", "```"]
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 6. TYPE DEFINITIONS
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
types:
|
types:
|
||||||
|
|
||||||
experience:
|
experience:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 1.10 # Erhöht für biografische Relevanz
|
retriever_weight: 1.10
|
||||||
edge_defaults: ["derived_from", "references"]
|
|
||||||
detection_keywords: ["erleben", "reagieren", "handeln", "prägen", "reflektieren"]
|
detection_keywords: ["erleben", "reagieren", "handeln", "prägen", "reflektieren"]
|
||||||
schema:
|
schema:
|
||||||
- "Situation (Was ist passiert?)"
|
- "Situation (Was ist passiert?)"
|
||||||
@@ -93,9 +104,8 @@ types:
|
|||||||
- "Reflexion & Learning (Was lerne ich daraus?)"
|
- "Reflexion & Learning (Was lerne ich daraus?)"
|
||||||
|
|
||||||
insight:
|
insight:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 1.20 # Hoch gewichtet für aktuelle Steuerung
|
retriever_weight: 1.20
|
||||||
edge_defaults: ["references", "based_on"]
|
|
||||||
detection_keywords: ["beobachten", "erkennen", "verstehen", "analysieren", "schlussfolgern"]
|
detection_keywords: ["beobachten", "erkennen", "verstehen", "analysieren", "schlussfolgern"]
|
||||||
schema:
|
schema:
|
||||||
- "Beobachtung (Was sehe ich?)"
|
- "Beobachtung (Was sehe ich?)"
|
||||||
@@ -104,9 +114,8 @@ types:
|
|||||||
- "Handlungsempfehlung"
|
- "Handlungsempfehlung"
|
||||||
|
|
||||||
project:
|
project:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 0.97
|
retriever_weight: 0.97
|
||||||
edge_defaults: ["references", "depends_on"]
|
|
||||||
detection_keywords: ["umsetzen", "planen", "starten", "bauen", "abschließen"]
|
detection_keywords: ["umsetzen", "planen", "starten", "bauen", "abschließen"]
|
||||||
schema:
|
schema:
|
||||||
- "Mission & Zielsetzung"
|
- "Mission & Zielsetzung"
|
||||||
@@ -116,7 +125,6 @@ types:
|
|||||||
decision:
|
decision:
|
||||||
chunking_profile: structured_smart_edges_strict
|
chunking_profile: structured_smart_edges_strict
|
||||||
retriever_weight: 1.00
|
retriever_weight: 1.00
|
||||||
edge_defaults: ["caused_by", "references"]
|
|
||||||
detection_keywords: ["entscheiden", "wählen", "abwägen", "priorisieren", "festlegen"]
|
detection_keywords: ["entscheiden", "wählen", "abwägen", "priorisieren", "festlegen"]
|
||||||
schema:
|
schema:
|
||||||
- "Kontext & Problemstellung"
|
- "Kontext & Problemstellung"
|
||||||
@@ -124,12 +132,9 @@ types:
|
|||||||
- "Die Entscheidung"
|
- "Die Entscheidung"
|
||||||
- "Begründung"
|
- "Begründung"
|
||||||
|
|
||||||
# --- PERSÖNLICHKEIT & IDENTITÄT ---
|
|
||||||
|
|
||||||
value:
|
value:
|
||||||
chunking_profile: structured_smart_edges_strict
|
chunking_profile: structured_smart_edges_strict
|
||||||
retriever_weight: 1.00
|
retriever_weight: 1.00
|
||||||
edge_defaults: ["related_to"]
|
|
||||||
detection_keywords: ["werten", "achten", "verpflichten", "bedeuten"]
|
detection_keywords: ["werten", "achten", "verpflichten", "bedeuten"]
|
||||||
schema:
|
schema:
|
||||||
- "Definition"
|
- "Definition"
|
||||||
@@ -139,7 +144,6 @@ types:
|
|||||||
principle:
|
principle:
|
||||||
chunking_profile: structured_smart_edges_strict_L3
|
chunking_profile: structured_smart_edges_strict_L3
|
||||||
retriever_weight: 0.95
|
retriever_weight: 0.95
|
||||||
edge_defaults: ["derived_from", "references"]
|
|
||||||
detection_keywords: ["leiten", "steuern", "ausrichten", "handhaben"]
|
detection_keywords: ["leiten", "steuern", "ausrichten", "handhaben"]
|
||||||
schema:
|
schema:
|
||||||
- "Das Prinzip"
|
- "Das Prinzip"
|
||||||
@@ -148,7 +152,6 @@ types:
|
|||||||
trait:
|
trait:
|
||||||
chunking_profile: structured_smart_edges_strict
|
chunking_profile: structured_smart_edges_strict
|
||||||
retriever_weight: 1.10
|
retriever_weight: 1.10
|
||||||
edge_defaults: ["related_to"]
|
|
||||||
detection_keywords: ["begeistern", "können", "auszeichnen", "befähigen", "stärken"]
|
detection_keywords: ["begeistern", "können", "auszeichnen", "befähigen", "stärken"]
|
||||||
schema:
|
schema:
|
||||||
- "Eigenschaft / Talent"
|
- "Eigenschaft / Talent"
|
||||||
@@ -158,7 +161,6 @@ types:
|
|||||||
obstacle:
|
obstacle:
|
||||||
chunking_profile: structured_smart_edges_strict
|
chunking_profile: structured_smart_edges_strict
|
||||||
retriever_weight: 1.00
|
retriever_weight: 1.00
|
||||||
edge_defaults: ["blocks", "related_to"]
|
|
||||||
detection_keywords: ["blockieren", "fürchten", "vermeiden", "hindern", "zweifeln"]
|
detection_keywords: ["blockieren", "fürchten", "vermeiden", "hindern", "zweifeln"]
|
||||||
schema:
|
schema:
|
||||||
- "Beschreibung der Hürde"
|
- "Beschreibung der Hürde"
|
||||||
@@ -169,7 +171,6 @@ types:
|
|||||||
belief:
|
belief:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.90
|
retriever_weight: 0.90
|
||||||
edge_defaults: ["related_to"]
|
|
||||||
detection_keywords: ["glauben", "meinen", "annehmen", "überzeugen"]
|
detection_keywords: ["glauben", "meinen", "annehmen", "überzeugen"]
|
||||||
schema:
|
schema:
|
||||||
- "Der Glaubenssatz"
|
- "Der Glaubenssatz"
|
||||||
@@ -178,18 +179,15 @@ types:
|
|||||||
profile:
|
profile:
|
||||||
chunking_profile: structured_smart_edges_strict
|
chunking_profile: structured_smart_edges_strict
|
||||||
retriever_weight: 0.70
|
retriever_weight: 0.70
|
||||||
edge_defaults: ["references", "related_to"]
|
|
||||||
detection_keywords: ["verkörpern", "verantworten", "agieren", "repräsentieren"]
|
detection_keywords: ["verkörpern", "verantworten", "agieren", "repräsentieren"]
|
||||||
schema:
|
schema:
|
||||||
- "Rolle / Identität"
|
- "Rolle / Identität"
|
||||||
- "Fakten & Daten"
|
- "Fakten & Daten"
|
||||||
- "Historie"
|
- "Historie"
|
||||||
|
|
||||||
|
|
||||||
idea:
|
idea:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.70
|
retriever_weight: 0.70
|
||||||
edge_defaults: ["leads_to", "references"]
|
|
||||||
detection_keywords: ["einfall", "gedanke", "potenzial", "möglichkeit"]
|
detection_keywords: ["einfall", "gedanke", "potenzial", "möglichkeit"]
|
||||||
schema:
|
schema:
|
||||||
- "Der Kerngedanke"
|
- "Der Kerngedanke"
|
||||||
@@ -199,7 +197,6 @@ types:
|
|||||||
skill:
|
skill:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: sliding_smart_edges
|
||||||
retriever_weight: 0.90
|
retriever_weight: 0.90
|
||||||
edge_defaults: ["references", "related_to"]
|
|
||||||
detection_keywords: ["lernen", "beherrschen", "üben", "fertigkeit", "kompetenz"]
|
detection_keywords: ["lernen", "beherrschen", "üben", "fertigkeit", "kompetenz"]
|
||||||
schema:
|
schema:
|
||||||
- "Definition der Fähigkeit"
|
- "Definition der Fähigkeit"
|
||||||
@@ -209,7 +206,6 @@ types:
|
|||||||
habit:
|
habit:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.85
|
retriever_weight: 0.85
|
||||||
edge_defaults: ["related_to", "triggered_by"]
|
|
||||||
detection_keywords: ["gewohnheit", "routine", "automatismus", "immer wenn"]
|
detection_keywords: ["gewohnheit", "routine", "automatismus", "immer wenn"]
|
||||||
schema:
|
schema:
|
||||||
- "Auslöser (Trigger)"
|
- "Auslöser (Trigger)"
|
||||||
@@ -218,9 +214,8 @@ types:
|
|||||||
- "Strategie"
|
- "Strategie"
|
||||||
|
|
||||||
need:
|
need:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 1.05
|
retriever_weight: 1.05
|
||||||
edge_defaults: ["related_to", "impacts"]
|
|
||||||
detection_keywords: ["bedürfnis", "brauchen", "mangel", "erfüllung"]
|
detection_keywords: ["bedürfnis", "brauchen", "mangel", "erfüllung"]
|
||||||
schema:
|
schema:
|
||||||
- "Das Bedürfnis"
|
- "Das Bedürfnis"
|
||||||
@@ -228,9 +223,8 @@ types:
|
|||||||
- "Bezug zu Werten"
|
- "Bezug zu Werten"
|
||||||
|
|
||||||
motivation:
|
motivation:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 0.95
|
retriever_weight: 0.95
|
||||||
edge_defaults: ["drives", "references"]
|
|
||||||
detection_keywords: ["motivation", "antrieb", "warum", "energie"]
|
detection_keywords: ["motivation", "antrieb", "warum", "energie"]
|
||||||
schema:
|
schema:
|
||||||
- "Der Antrieb"
|
- "Der Antrieb"
|
||||||
@@ -240,86 +234,77 @@ types:
|
|||||||
bias:
|
bias:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.80
|
retriever_weight: 0.80
|
||||||
edge_defaults: ["affects", "related_to"]
|
|
||||||
detection_keywords: ["denkfehler", "verzerrung", "vorurteil", "falle"]
|
detection_keywords: ["denkfehler", "verzerrung", "vorurteil", "falle"]
|
||||||
schema: ["Beschreibung der Verzerrung", "Typische Situationen", "Gegenstrategie"]
|
schema: ["Beschreibung der Verzerrung", "Typische Situationen", "Gegenstrategie"]
|
||||||
|
|
||||||
state:
|
state:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.60
|
retriever_weight: 0.60
|
||||||
edge_defaults: ["impacts"]
|
|
||||||
detection_keywords: ["stimmung", "energie", "gefühl", "verfassung"]
|
detection_keywords: ["stimmung", "energie", "gefühl", "verfassung"]
|
||||||
schema: ["Aktueller Zustand", "Auslöser", "Auswirkung auf den Tag"]
|
schema: ["Aktueller Zustand", "Auslöser", "Auswirkung auf den Tag"]
|
||||||
|
|
||||||
boundary:
|
boundary:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 0.90
|
retriever_weight: 0.90
|
||||||
edge_defaults: ["protects", "related_to"]
|
|
||||||
detection_keywords: ["grenze", "nein sagen", "limit", "schutz"]
|
detection_keywords: ["grenze", "nein sagen", "limit", "schutz"]
|
||||||
schema: ["Die Grenze", "Warum sie wichtig ist", "Konsequenz bei Verletzung"]
|
schema: ["Die Grenze", "Warum sie wichtig ist", "Konsequenz bei Verletzung"]
|
||||||
# --- STRATEGIE & RISIKO ---
|
|
||||||
|
|
||||||
goal:
|
goal:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 0.95
|
retriever_weight: 0.95
|
||||||
edge_defaults: ["depends_on", "related_to"]
|
detection_keywords: ["ziel", "zielzustand", "kpi", "zeitrahmen", "deadline", "meilenstein"]
|
||||||
schema: ["Zielzustand", "Zeitrahmen & KPIs", "Motivation"]
|
schema: ["Zielzustand", "Zeitrahmen & KPIs", "Motivation"]
|
||||||
|
|
||||||
risk:
|
risk:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.85
|
retriever_weight: 0.85
|
||||||
edge_defaults: ["related_to", "blocks"]
|
|
||||||
detection_keywords: ["risiko", "gefahr", "bedrohung"]
|
detection_keywords: ["risiko", "gefahr", "bedrohung"]
|
||||||
schema: ["Beschreibung des Risikos", "Auswirkungen", "Gegenmaßnahmen"]
|
schema: ["Beschreibung des Risikos", "Auswirkungen", "Gegenmaßnahmen"]
|
||||||
|
|
||||||
# --- BASIS & WISSEN ---
|
|
||||||
|
|
||||||
concept:
|
concept:
|
||||||
chunking_profile: sliding_smart_edges
|
chunking_profile: structured_smart_edges
|
||||||
retriever_weight: 0.60
|
retriever_weight: 0.6
|
||||||
edge_defaults: ["references", "related_to"]
|
detection_keywords: ["definition", "konzept", "begriff", "modell", "rahmen", "theorie"]
|
||||||
schema: ["Definition", "Kontext", "Verwandte Konzepte"]
|
schema: ["Definition", "Kontext", "Verwandte Konzepte"]
|
||||||
|
|
||||||
task:
|
task:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.80
|
retriever_weight: 0.8
|
||||||
edge_defaults: ["depends_on", "part_of"]
|
detection_keywords: ["aufgabe", "todo", "next_action", "erledigen", "definition_of_done", "checkliste"]
|
||||||
schema: ["Aufgabe", "Kontext", "Definition of Done"]
|
schema: ["Aufgabe", "Kontext", "Definition of Done"]
|
||||||
|
|
||||||
journal:
|
journal:
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
retriever_weight: 0.80
|
retriever_weight: 0.8
|
||||||
edge_defaults: ["references", "related_to"]
|
detection_keywords: ["journal", "tagebuch", "log", "eintrag", "reflexion", "heute"]
|
||||||
schema: ["Log-Eintrag", "Gedanken"]
|
schema: ["Log-Eintrag", "Gedanken"]
|
||||||
|
|
||||||
source:
|
source:
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
retriever_weight: 0.50
|
retriever_weight: 0.5
|
||||||
edge_defaults: []
|
detection_keywords: ["quelle", "paper", "buch", "artikel", "link", "zitat", "studie"]
|
||||||
schema: ["Metadaten", "Zusammenfassung", "Zitate"]
|
schema: ["Metadaten", "Zusammenfassung", "Zitate"]
|
||||||
|
|
||||||
glossary:
|
glossary:
|
||||||
chunking_profile: sliding_short
|
chunking_profile: sliding_short
|
||||||
retriever_weight: 0.40
|
retriever_weight: 0.4
|
||||||
edge_defaults: ["related_to"]
|
detection_keywords: ["glossar", "begriff", "definition", "terminologie"]
|
||||||
schema: ["Begriff", "Definition"]
|
schema: ["Begriff", "Definition"]
|
||||||
|
|
||||||
person:
|
person:
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
retriever_weight: 0.50
|
retriever_weight: 0.5
|
||||||
edge_defaults: ["related_to"]
|
detection_keywords: ["person", "mensch", "kontakt", "name", "beziehung", "stakeholder"]
|
||||||
schema: ["Rolle", "Beziehung", "Kontext"]
|
schema: ["Profile", "Beziehung", "Kontext"]
|
||||||
|
|
||||||
event:
|
event:
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
retriever_weight: 0.60
|
retriever_weight: 0.6
|
||||||
edge_defaults: ["related_to"]
|
detection_keywords: ["ereignis", "termin", "datum", "ort", "teilnehmer", "meeting"]
|
||||||
schema: ["Datum & Ort", "Teilnehmer", "Ergebnisse"]
|
schema: ["Datum & Ort", "Teilnehmer", "Ergebnisse"]
|
||||||
|
|
||||||
# --- FALLBACK ---
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
chunking_profile: sliding_standard
|
chunking_profile: sliding_standard
|
||||||
retriever_weight: 1.00
|
retriever_weight: 1.0
|
||||||
edge_defaults: ["references"]
|
detection_keywords: []
|
||||||
schema: ["Inhalt"]
|
schema: ["Inhalt"]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
[0114/152756.633:ERROR:third_party\crashpad\crashpad\util\win\registration_protocol_win.cc:108] CreateFile: Das System kann die angegebene Datei nicht finden. (0x2)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
__pycache__/
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
# syntax=docker/dockerfile:1.7
|
||||||
|
#
|
||||||
|
# Mindnet-Image für API und Streamlit-UI (ein Image, Modus per CMD: api | ui | preflight).
|
||||||
|
# Build-Kontext ist das Repository-Root; die Allowlist steht in /.dockerignore.
|
||||||
|
#
|
||||||
|
# docker build -f deploy/Dockerfile -t mindnet:<git-sha> .
|
||||||
|
#
|
||||||
|
# Reproduzierbarkeit: Basis-Image per Digest, Abhängigkeiten per Hash-Lock
|
||||||
|
# (deploy/requirements.lock). Kein torch / sentence-transformers: Embeddings
|
||||||
|
# kommen ausschließlich von Ollama (nomic-embed-text, 768 Dimensionen).
|
||||||
|
|
||||||
|
ARG PYTHON_IMAGE=python:3.12-slim-bookworm@sha256:e697c81897302a4c4333bade000d98038fbb21eb064f8ee1fd42d02ebcd9d337
|
||||||
|
|
||||||
|
# --- Abhängigkeiten in ein isoliertes venv -----------------------------------
|
||||||
|
FROM ${PYTHON_IMAGE} AS deps
|
||||||
|
ENV PIP_NO_CACHE_DIR=1 \
|
||||||
|
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||||||
|
PYTHONDONTWRITEBYTECODE=1
|
||||||
|
COPY deploy/requirements.lock /tmp/requirements.lock
|
||||||
|
RUN python -m venv /opt/venv \
|
||||||
|
&& /opt/venv/bin/pip install --require-hashes --no-deps -r /tmp/requirements.lock
|
||||||
|
|
||||||
|
# --- Laufzeit-Image -------------------------------------------------------------
|
||||||
|
FROM ${PYTHON_IMAGE} AS runtime
|
||||||
|
ARG MINDNET_UID=10001
|
||||||
|
ENV PATH=/opt/venv/bin:${PATH} \
|
||||||
|
PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
PYTHONPATH=/app \
|
||||||
|
HOME=/tmp \
|
||||||
|
STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
|
||||||
|
|
||||||
|
RUN groupadd --system --gid ${MINDNET_UID} mindnet \
|
||||||
|
&& useradd --system --uid ${MINDNET_UID} --gid mindnet --no-create-home --shell /usr/sbin/nologin mindnet
|
||||||
|
|
||||||
|
COPY --from=deps /opt/venv /opt/venv
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
# Code und Konfiguration gehören root und sind für den Laufzeitnutzer nur lesbar.
|
||||||
|
COPY app/ app/
|
||||||
|
COPY config/ config/
|
||||||
|
COPY scripts/ scripts/
|
||||||
|
COPY deploy/entrypoint.py deploy/healthcheck.py /opt/mindnet/
|
||||||
|
|
||||||
|
# Einzige beschreibbare Pfade (als Volumes gemountet): Logs und Laufzeitdaten.
|
||||||
|
RUN install -d -o mindnet -g mindnet -m 0750 /app/logs /app/data /app/data/logs
|
||||||
|
|
||||||
|
USER mindnet:mindnet
|
||||||
|
EXPOSE 8000 8501
|
||||||
|
ENTRYPOINT ["python", "/opt/mindnet/entrypoint.py"]
|
||||||
|
CMD ["api"]
|
||||||
|
|
||||||
|
# --- Test-Image (nur für nichtproduktive Läufe) ---------------------------------
|
||||||
|
FROM runtime AS test
|
||||||
|
USER root
|
||||||
|
COPY deploy/requirements-test.lock /tmp/requirements-test.lock
|
||||||
|
RUN pip install --no-cache-dir --require-hashes --no-deps -r /tmp/requirements-test.lock
|
||||||
|
COPY tests/ tests/
|
||||||
|
COPY deploy/ deploy/
|
||||||
|
COPY .dockerignore ./
|
||||||
|
COPY .gitea/workflows/ .gitea/workflows/
|
||||||
|
USER mindnet:mindnet
|
||||||
|
ENTRYPOINT []
|
||||||
|
CMD ["python", "-m", "pytest", "-q", "-p", "no:cacheprovider", \
|
||||||
|
"tests/test_container_preflight.py", "tests/test_container_connectivity.py", \
|
||||||
|
"tests/test_container_appenv.py"]
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
"""
|
||||||
|
FILE: deploy/appenv.py
|
||||||
|
DESCRIPTION: Liest die unveränderte .env der systemd-Instanz und leitet daraus ab, was
|
||||||
|
Compose für denselben Betrieb im Container braucht:
|
||||||
|
- Vault: Host-Pfad und Einhängepunkt so, dass MINDNET_VAULT_ROOT der .env im
|
||||||
|
Container auf dasselbe Verzeichnis zeigt (relativ -> unter /app, wie unter
|
||||||
|
dem WorkingDirectory der systemd-Unit)
|
||||||
|
- erwartetes Collection-Präfix für den Smoke-Test
|
||||||
|
Prüft außerdem, dass die Container-Datei nur Container-Angaben enthält und
|
||||||
|
damit keine Anwendungskonfiguration überschreibt.
|
||||||
|
Nur Standardbibliothek (läuft direkt auf dem Runner).
|
||||||
|
VERSION: 1.0.0
|
||||||
|
STATUS: Active
|
||||||
|
USAGE: appenv.py <systemd.env> <container.env> -> Shell-Zuweisungen auf stdout
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import posixpath
|
||||||
|
import shlex
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Einzige Schlüssel, die die Container-Datei setzen darf.
|
||||||
|
CONTAINER_KEYS = {
|
||||||
|
"MINDNET_API_PORT", "MINDNET_API_BIND", "MINDNET_UI_PORT", "MINDNET_UI_BIND",
|
||||||
|
"MINDNET_API_MEM_LIMIT", "MINDNET_UI_MEM_LIMIT", "MINDNET_ALLOW_CLOUD_EGRESS",
|
||||||
|
"MINDNET_VAULT_SENTINEL",
|
||||||
|
}
|
||||||
|
APP_WORKDIR = "/app"
|
||||||
|
DEFAULT_VAULT_ROOT = "./vault_master" # Default aus app/config.py
|
||||||
|
DEFAULT_PREFIX = "mindnet" # Default aus app/config.py
|
||||||
|
|
||||||
|
|
||||||
|
class AppEnvError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def parse(path: str) -> dict[str, str]:
|
||||||
|
"""Einfacher dotenv-Parser (KEY=VALUE, optional 'export', Quotes, Kommentare)."""
|
||||||
|
values = {}
|
||||||
|
for raw in Path(path).read_text(encoding="utf-8").splitlines():
|
||||||
|
line = raw.strip()
|
||||||
|
if not line or line.startswith("#") or "=" not in line:
|
||||||
|
continue
|
||||||
|
if line.startswith("export "):
|
||||||
|
line = line[len("export "):].lstrip()
|
||||||
|
key, value = (part.strip() for part in line.split("=", 1))
|
||||||
|
if len(value) >= 2 and value[0] in "\"'" and value[-1] == value[0]:
|
||||||
|
value = value[1:-1]
|
||||||
|
elif " #" in value:
|
||||||
|
value = value.split(" #", 1)[0].rstrip()
|
||||||
|
values[key] = value
|
||||||
|
return values
|
||||||
|
|
||||||
|
|
||||||
|
def vault_mount(app_env_path: str, values: dict[str, str]) -> tuple[str, str]:
|
||||||
|
root = values.get("MINDNET_VAULT_ROOT") or DEFAULT_VAULT_ROOT
|
||||||
|
if posixpath.isabs(root):
|
||||||
|
return posixpath.normpath(root), posixpath.normpath(root)
|
||||||
|
# systemd startet im Ordner der .env (WorkingDirectory), der Container in /app.
|
||||||
|
host_dir = posixpath.dirname(Path(app_env_path).as_posix())
|
||||||
|
return posixpath.normpath(posixpath.join(host_dir, root)), posixpath.normpath(posixpath.join(APP_WORKDIR, root))
|
||||||
|
|
||||||
|
|
||||||
|
def derive(app_env_path: str, container_env_path: str) -> dict[str, str]:
|
||||||
|
extra = sorted(set(parse(container_env_path)) - CONTAINER_KEYS)
|
||||||
|
if extra:
|
||||||
|
raise AppEnvError(
|
||||||
|
f"{container_env_path} darf nur Container-Angaben enthalten; "
|
||||||
|
f"Anwendungswerte gehören in {app_env_path}: {', '.join(extra)}"
|
||||||
|
)
|
||||||
|
values = parse(app_env_path)
|
||||||
|
host, target = vault_mount(app_env_path, values)
|
||||||
|
return {
|
||||||
|
"MINDNET_VAULT_HOST_PATH": host,
|
||||||
|
"MINDNET_VAULT_TARGET": target,
|
||||||
|
"MINDNET_EXPECTED_PREFIX": values.get("COLLECTION_PREFIX") or DEFAULT_PREFIX,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]) -> int:
|
||||||
|
if len(argv) != 3:
|
||||||
|
print("Aufruf: appenv.py <systemd.env> <container.env>", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
try:
|
||||||
|
derived = derive(argv[1], argv[2])
|
||||||
|
except (OSError, AppEnvError) as e:
|
||||||
|
print(f"appenv: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
for key, value in derived.items():
|
||||||
|
print(f"{key}={shlex.quote(value)}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main(sys.argv))
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
# Mindnet ENTWICKLUNG als Container: dieselbe Anwendung mit derselben Konfiguration
|
||||||
|
# wie die systemd-Dev-Instanz (8002/8502), parallel auf 8012/8512 zum Vergleich.
|
||||||
|
#
|
||||||
|
# Gestartet über deploy/deploy.sh dev. Zwei Dateien, beide nur auf dem Host:
|
||||||
|
# - MINDNET_APP_ENV_FILE: die unveränderte .env der systemd-Dev-Instanz
|
||||||
|
# - MINDNET_ENV_FILE: nur Container-Angaben (Ports, Bind-Adressen, Freigaben)
|
||||||
|
# Fest überschrieben wird nur, was im Container anders lauten muss: die Adressen
|
||||||
|
# der Host-Dienste (statt 127.0.0.1). Vault-Pfad und Präfix kommen aus der .env.
|
||||||
|
|
||||||
|
name: mindnet-dev
|
||||||
|
|
||||||
|
x-hardening: &hardening
|
||||||
|
image: mindnet:${MINDNET_IMAGE_TAG:-dev}
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: deploy/Dockerfile
|
||||||
|
target: runtime
|
||||||
|
env_file:
|
||||||
|
- ${MINDNET_APP_ENV_FILE:?MINDNET_APP_ENV_FILE auf die .env der systemd-Dev-Instanz setzen}
|
||||||
|
- ${MINDNET_ENV_FILE:-env/dev.env}
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:size=64m,mode=1777"]
|
||||||
|
cap_drop: [ALL]
|
||||||
|
security_opt: ["no-new-privileges:true"]
|
||||||
|
init: true
|
||||||
|
pids_limit: 256
|
||||||
|
restart: unless-stopped
|
||||||
|
extra_hosts: ["host.docker.internal:host-gateway"]
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
options: {max-size: "10m", max-file: "3"}
|
||||||
|
networks: [mindnet-dev]
|
||||||
|
|
||||||
|
# Container-bedingte Werte; haben Vorrang vor beiden env-Dateien.
|
||||||
|
x-container-env: &container-env
|
||||||
|
MINDNET_ENV: dev
|
||||||
|
QDRANT_URL: http://host.docker.internal:6333
|
||||||
|
MINDNET_OLLAMA_URL: http://host.docker.internal:11434
|
||||||
|
VECTOR_DIM: "768"
|
||||||
|
MINDNET_EXPECTED_VECTOR_DIM: "768"
|
||||||
|
MINDNET_EXPECTED_EMBEDDING_MODEL: nomic-embed-text
|
||||||
|
MINDNET_VAULT_READONLY: "true"
|
||||||
|
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
<<: *hardening
|
||||||
|
command: ["api"]
|
||||||
|
environment:
|
||||||
|
<<: *container-env
|
||||||
|
volumes:
|
||||||
|
# Derselbe Vault wie systemd-Dev, eingehängt dort, wo MINDNET_VAULT_ROOT hinzeigt.
|
||||||
|
# Nur lesend, solange beide Instanzen parallel laufen.
|
||||||
|
- type: bind
|
||||||
|
source: ${MINDNET_VAULT_HOST_PATH:?wird von deploy/deploy.sh aus der .env abgeleitet}
|
||||||
|
target: ${MINDNET_VAULT_TARGET:?wird von deploy/deploy.sh aus der .env abgeleitet}
|
||||||
|
read_only: true
|
||||||
|
bind:
|
||||||
|
create_host_path: false
|
||||||
|
propagation: rslave
|
||||||
|
- logs:/app/logs
|
||||||
|
- data:/app/data
|
||||||
|
ports: ["${MINDNET_API_BIND:-127.0.0.1}:${MINDNET_API_PORT:-8012}:8000"]
|
||||||
|
mem_limit: ${MINDNET_API_MEM_LIMIT:-2g}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "/opt/mindnet/healthcheck.py", "api"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
ui:
|
||||||
|
<<: *hardening
|
||||||
|
command: ["ui"]
|
||||||
|
environment:
|
||||||
|
<<: *container-env
|
||||||
|
MINDNET_VAULT_REQUIRED: "false"
|
||||||
|
MINDNET_API_URL: http://api:8000
|
||||||
|
volumes:
|
||||||
|
- data:/app/data:ro
|
||||||
|
# Wie systemd (STREAMLIT_SERVER_ADDRESS=0.0.0.0): im LAN erreichbar.
|
||||||
|
ports: ["${MINDNET_UI_BIND:-0.0.0.0}:${MINDNET_UI_PORT:-8512}:8501"]
|
||||||
|
mem_limit: ${MINDNET_UI_MEM_LIMIT:-1g}
|
||||||
|
depends_on:
|
||||||
|
api: {condition: service_healthy}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "/opt/mindnet/healthcheck.py", "ui"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 30s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
logs:
|
||||||
|
data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
mindnet-dev:
|
||||||
|
driver: bridge
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
# Mindnet PRODUKTION als Container (Vorlage, noch NICHT aktiv): dieselbe Anwendung
|
||||||
|
# mit derselben Konfiguration wie die systemd-Prod-Instanz (8001/8501), parallel auf
|
||||||
|
# 8011/8511 zum Vergleich. Umschaltung erst mit Freigabe (Gate G5).
|
||||||
|
#
|
||||||
|
# Gestartet über deploy/deploy.sh prod. Zwei Dateien, beide nur auf dem Host:
|
||||||
|
# - MINDNET_APP_ENV_FILE: die unveränderte .env der systemd-Prod-Instanz
|
||||||
|
# - MINDNET_ENV_FILE: nur Container-Angaben (Ports, Bind-Adressen, Freigaben)
|
||||||
|
# Fest überschrieben wird nur, was im Container anders lauten muss: die Adressen
|
||||||
|
# der Host-Dienste (statt 127.0.0.1). Vault-Pfad und Präfix kommen aus der .env.
|
||||||
|
# Vault nur lesend, bis R2 (Pfadsicherheit beim Speichern) behoben ist.
|
||||||
|
|
||||||
|
name: mindnet-prod
|
||||||
|
|
||||||
|
x-hardening: &hardening
|
||||||
|
image: mindnet:${MINDNET_IMAGE_TAG:?MINDNET_IMAGE_TAG setzen (z. B. prod-<commit>)}
|
||||||
|
build:
|
||||||
|
context: ..
|
||||||
|
dockerfile: deploy/Dockerfile
|
||||||
|
target: runtime
|
||||||
|
env_file:
|
||||||
|
- ${MINDNET_APP_ENV_FILE:?MINDNET_APP_ENV_FILE auf die .env der systemd-Prod-Instanz setzen}
|
||||||
|
- ${MINDNET_ENV_FILE:-env/prod.env}
|
||||||
|
read_only: true
|
||||||
|
tmpfs: ["/tmp:size=64m,mode=1777"]
|
||||||
|
cap_drop: [ALL]
|
||||||
|
security_opt: ["no-new-privileges:true"]
|
||||||
|
init: true
|
||||||
|
pids_limit: 256
|
||||||
|
restart: unless-stopped
|
||||||
|
extra_hosts: ["host.docker.internal:host-gateway"]
|
||||||
|
logging:
|
||||||
|
driver: json-file
|
||||||
|
options: {max-size: "10m", max-file: "3"}
|
||||||
|
networks: [mindnet-prod]
|
||||||
|
|
||||||
|
# Container-bedingte Werte; haben Vorrang vor beiden env-Dateien.
|
||||||
|
x-container-env: &container-env
|
||||||
|
MINDNET_ENV: prod
|
||||||
|
QDRANT_URL: http://host.docker.internal:6333
|
||||||
|
MINDNET_OLLAMA_URL: http://host.docker.internal:11434
|
||||||
|
VECTOR_DIM: "768"
|
||||||
|
MINDNET_EXPECTED_VECTOR_DIM: "768"
|
||||||
|
MINDNET_EXPECTED_EMBEDDING_MODEL: nomic-embed-text
|
||||||
|
MINDNET_VAULT_READONLY: "true"
|
||||||
|
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
<<: *hardening
|
||||||
|
command: ["api"]
|
||||||
|
environment:
|
||||||
|
<<: *container-env
|
||||||
|
volumes:
|
||||||
|
# Derselbe Vault wie systemd-Prod, eingehängt dort, wo MINDNET_VAULT_ROOT hinzeigt.
|
||||||
|
# Nur lesend, solange beide Instanzen parallel laufen.
|
||||||
|
- type: bind
|
||||||
|
source: ${MINDNET_VAULT_HOST_PATH:?wird von deploy/deploy.sh aus der .env abgeleitet}
|
||||||
|
target: ${MINDNET_VAULT_TARGET:?wird von deploy/deploy.sh aus der .env abgeleitet}
|
||||||
|
read_only: true
|
||||||
|
bind:
|
||||||
|
create_host_path: false
|
||||||
|
propagation: rslave
|
||||||
|
- logs:/app/logs
|
||||||
|
- data:/app/data
|
||||||
|
ports: ["${MINDNET_API_BIND:-127.0.0.1}:${MINDNET_API_PORT:-8011}:8000"]
|
||||||
|
mem_limit: ${MINDNET_API_MEM_LIMIT:-2g}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "/opt/mindnet/healthcheck.py", "api"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 40s
|
||||||
|
|
||||||
|
ui:
|
||||||
|
<<: *hardening
|
||||||
|
command: ["ui"]
|
||||||
|
environment:
|
||||||
|
<<: *container-env
|
||||||
|
MINDNET_VAULT_REQUIRED: "false"
|
||||||
|
MINDNET_API_URL: http://api:8000
|
||||||
|
volumes:
|
||||||
|
- data:/app/data:ro
|
||||||
|
# Wie systemd (STREAMLIT_SERVER_ADDRESS=0.0.0.0): im LAN erreichbar.
|
||||||
|
ports: ["${MINDNET_UI_BIND:-0.0.0.0}:${MINDNET_UI_PORT:-8511}:8501"]
|
||||||
|
mem_limit: ${MINDNET_UI_MEM_LIMIT:-1g}
|
||||||
|
depends_on:
|
||||||
|
api: {condition: service_healthy}
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python", "/opt/mindnet/healthcheck.py", "ui"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 30s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
logs:
|
||||||
|
data:
|
||||||
|
|
||||||
|
networks:
|
||||||
|
mindnet-prod:
|
||||||
|
driver: bridge
|
||||||
Executable
+92
@@ -0,0 +1,92 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Baut, prüft und startet einen Mindnet-Container-Stack. Genutzt von den
|
||||||
|
# Gitea-Workflows (develop -> dev, main -> prod) und für manuelle Deploys.
|
||||||
|
#
|
||||||
|
# MINDNET_APP_ENV_FILE=/home/llmadmin/mindnet_dev/.env # MINDNET_ENV_FILE=/home/llmadmin/docker/mindnet-dev/container.env deploy/deploy.sh dev
|
||||||
|
#
|
||||||
|
# MINDNET_APP_ENV_FILE ist die unveränderte .env der systemd-Instanz (gleiche Konfiguration),
|
||||||
|
# MINDNET_ENV_FILE enthält nur Container-Angaben (deploy/env/<env>.env.example).
|
||||||
|
#
|
||||||
|
# Ablauf (jeder Schritt bricht ab, bevor der laufende Stack angefasst wird):
|
||||||
|
# 1. Container-Tests im Test-Image, ohne Netzwerk (blockierend)
|
||||||
|
# 2. volle Testsuite ohne Netzwerk (informativ, bekannte Fehler siehe Migrationsplan)
|
||||||
|
# 3. Image bauen, Tag = Umgebung + Commit
|
||||||
|
# 4. preflight --live: Ollama/Qdrant erreichbar, Modelle, 768 Dimensionen, Vault (nur lesend)
|
||||||
|
# 5. up --wait (Healthchecks), danach smoke.py über die veröffentlichten Ports
|
||||||
|
# 6. Prod: bei Fehler Rückkehr zum letzten erfolgreichen Tag
|
||||||
|
#
|
||||||
|
# Beide Dateien liegen auf dem Host außerhalb von Git; im Verzeichnis der Container-Datei
|
||||||
|
# merkt sich das Skript den letzten erfolgreichen Image-Tag (last_good_tag).
|
||||||
|
set -euo pipefail
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
ENV="${1:-}"
|
||||||
|
case "$ENV" in
|
||||||
|
dev|prod) ;;
|
||||||
|
*) echo "Aufruf: deploy/deploy.sh dev|prod" >&2; exit 2 ;;
|
||||||
|
esac
|
||||||
|
: "${MINDNET_APP_ENV_FILE:?MINDNET_APP_ENV_FILE auf die .env der systemd-Instanz setzen}"
|
||||||
|
: "${MINDNET_ENV_FILE:?MINDNET_ENV_FILE auf die Container-Datei des Hosts setzen}"
|
||||||
|
for f in "$MINDNET_APP_ENV_FILE" "$MINDNET_ENV_FILE"; do
|
||||||
|
[ -f "$f" ] || { echo "env-Datei fehlt: $f" >&2; exit 1; }
|
||||||
|
done
|
||||||
|
export MINDNET_APP_ENV_FILE MINDNET_ENV_FILE
|
||||||
|
|
||||||
|
# Vault-Mount und Präfix aus der systemd-.env ableiten (gleiches Verzeichnis wie heute).
|
||||||
|
DERIVED="$(python3 deploy/appenv.py "$MINDNET_APP_ENV_FILE" "$MINDNET_ENV_FILE")"
|
||||||
|
eval "$DERIVED"
|
||||||
|
export MINDNET_VAULT_HOST_PATH MINDNET_VAULT_TARGET
|
||||||
|
PREFIX="$MINDNET_EXPECTED_PREFIX"
|
||||||
|
echo "$DERIVED"
|
||||||
|
|
||||||
|
SHA="$(git rev-parse --short=12 HEAD)"
|
||||||
|
export MINDNET_IMAGE_TAG="$ENV-$SHA"
|
||||||
|
LAST_GOOD="$(dirname "$MINDNET_ENV_FILE")/last_good_tag"
|
||||||
|
|
||||||
|
dc() { docker compose -f "deploy/compose.$ENV.yml" --env-file "$MINDNET_ENV_FILE" "$@"; }
|
||||||
|
step() { printf '\n=== %s\n' "$*"; }
|
||||||
|
show_logs() { dc ps || true; dc logs --tail 150 api ui || true; }
|
||||||
|
published() { dc port "$1" "$2" | head -n 1 | sed 's/^0\.0\.0\.0:/127.0.0.1:/'; }
|
||||||
|
|
||||||
|
smoke() {
|
||||||
|
python3 deploy/smoke.py --api "http://$(published api 8000)" --ui "http://$(published ui 8501)" \
|
||||||
|
--prefix "$PREFIX" --require-data
|
||||||
|
}
|
||||||
|
|
||||||
|
step "1/5 Container-Tests (ohne Netzwerk)"
|
||||||
|
docker build -f deploy/Dockerfile --target test -t "mindnet:test-$SHA" .
|
||||||
|
docker run --rm --network none "mindnet:test-$SHA"
|
||||||
|
|
||||||
|
step "2/5 Volle Testsuite (informativ, ohne Netzwerk)"
|
||||||
|
if ! docker run --rm --network none "mindnet:test-$SHA" \
|
||||||
|
python -m pytest tests -q -p no:cacheprovider --continue-on-collection-errors --basetemp=/tmp/pytest; then
|
||||||
|
echo "WARNUNG: Testsuite nicht grün. Bekannte Fehler: docs/05_Development/05_docker_migration_plan.md"
|
||||||
|
fi
|
||||||
|
docker image rm "mindnet:test-$SHA" >/dev/null || true
|
||||||
|
|
||||||
|
step "3/5 Image bauen ($MINDNET_IMAGE_TAG)"
|
||||||
|
dc build api ui
|
||||||
|
|
||||||
|
step "4/5 Preflight mit Erreichbarkeit (nur lesend)"
|
||||||
|
dc run --rm --no-deps api preflight --live
|
||||||
|
|
||||||
|
step "5/5 Start ($MINDNET_IMAGE_TAG) und Smoke"
|
||||||
|
if dc up -d --wait --no-build --remove-orphans && smoke; then
|
||||||
|
echo "$MINDNET_IMAGE_TAG" > "$LAST_GOOD"
|
||||||
|
echo "=== mindnet-$ENV läuft mit $MINDNET_IMAGE_TAG"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
show_logs
|
||||||
|
PREV="$(cat "$LAST_GOOD" 2>/dev/null || true)"
|
||||||
|
if [ "$ENV" = prod ] && [ -n "$PREV" ] && [ "$PREV" != "$MINDNET_IMAGE_TAG" ]; then
|
||||||
|
step "Rollback auf $PREV"
|
||||||
|
export MINDNET_IMAGE_TAG="$PREV"
|
||||||
|
if dc up -d --wait --no-build --remove-orphans && smoke; then
|
||||||
|
echo "Rollback erfolgreich; Deploy von $ENV-$SHA ist trotzdem fehlgeschlagen."
|
||||||
|
else
|
||||||
|
show_logs
|
||||||
|
echo "ROLLBACK FEHLGESCHLAGEN, manueller Eingriff nötig." >&2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
@@ -0,0 +1,258 @@
|
|||||||
|
"""
|
||||||
|
FILE: deploy/entrypoint.py
|
||||||
|
DESCRIPTION: Container-Entrypoint für Mindnet (API, UI, Preflight).
|
||||||
|
Prüft vor dem Start alle Annahmen, deren Verletzung still Daten
|
||||||
|
verfälschen oder Inhalte nach außen geben würde, und bricht sonst ab:
|
||||||
|
- Vektor-Dimension und Embedding-Modell (768 / nomic-embed-text)
|
||||||
|
- explizite Umgebung und Collection-Präfix (Dev nie auf Prod-Präfix)
|
||||||
|
- Vault: vorhanden, Sentinel lesbar (NAS gemountet), schreibgeschützt
|
||||||
|
- kein Cloud-LLM-Key ohne ausdrückliche Freigabe
|
||||||
|
- Dienst-URLs gesetzt und nicht auf den Container selbst (localhost)
|
||||||
|
Mit 'preflight --live' zusätzlich (nur lesend): Ollama und Qdrant
|
||||||
|
erreichbar, benötigte Modelle vorhanden, Dimensionen stimmen.
|
||||||
|
VERSION: 1.1.0
|
||||||
|
STATUS: Active
|
||||||
|
USAGE: entrypoint.py api | ui | preflight [--live]
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import urllib.error
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
CLOUD_KEYS = ("OPENROUTER_API_KEY", "GOOGLE_API_KEY")
|
||||||
|
CLOUD_EGRESS_ACK = "MINDNET_ALLOW_CLOUD_EGRESS"
|
||||||
|
SERVICE_URLS = ("MINDNET_OLLAMA_URL", "QDRANT_URL")
|
||||||
|
LOOPBACK_HOSTS = {"localhost", "127.0.0.1", "::1", "0.0.0.0"}
|
||||||
|
|
||||||
|
|
||||||
|
class PreflightError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _flag(name: str, default: str) -> bool:
|
||||||
|
return os.getenv(name, default).strip().lower() == "true"
|
||||||
|
|
||||||
|
|
||||||
|
def check_environment() -> str:
|
||||||
|
env = os.getenv("MINDNET_ENV", "").strip()
|
||||||
|
if env not in {"dev", "prod"}:
|
||||||
|
raise PreflightError("MINDNET_ENV muss 'dev' oder 'prod' sein.")
|
||||||
|
prefix = os.getenv("COLLECTION_PREFIX", "").strip()
|
||||||
|
if not prefix:
|
||||||
|
# Ohne Präfix fällt die App still auf 'mindnet' (= Produktion) zurück.
|
||||||
|
raise PreflightError("COLLECTION_PREFIX muss explizit gesetzt sein.")
|
||||||
|
if env == "dev" and not prefix.endswith("_dev"):
|
||||||
|
raise PreflightError(f"Dev-Umgebung verlangt ein Präfix mit Suffix '_dev' (ist: '{prefix}').")
|
||||||
|
if env == "prod" and prefix.endswith("_dev"):
|
||||||
|
raise PreflightError(f"Prod-Umgebung mit Dev-Präfix '{prefix}'.")
|
||||||
|
return env
|
||||||
|
|
||||||
|
|
||||||
|
def _load_profiles(profiles_path: str | None = None) -> dict:
|
||||||
|
# Gleicher Pfad wie in der App (Settings kennt keinen Override dafür).
|
||||||
|
path = Path(profiles_path or "config/llm_profiles.yaml")
|
||||||
|
try:
|
||||||
|
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
|
||||||
|
except (OSError, yaml.YAMLError) as e:
|
||||||
|
raise PreflightError(f"LLM-Profile nicht lesbar: {path} ({type(e).__name__}).") from None
|
||||||
|
return data.get("profiles") or {}
|
||||||
|
|
||||||
|
|
||||||
|
def check_embeddings(profiles_path: str | None = None) -> None:
|
||||||
|
expected_dim = int(os.getenv("MINDNET_EXPECTED_VECTOR_DIM", "768"))
|
||||||
|
expected_model = os.getenv("MINDNET_EXPECTED_EMBEDDING_MODEL", "nomic-embed-text")
|
||||||
|
|
||||||
|
# qdrant.py fällt ohne VECTOR_DIM auf 384 (MiniLM) zurück.
|
||||||
|
dim = os.getenv("VECTOR_DIM", "").strip()
|
||||||
|
if dim != str(expected_dim):
|
||||||
|
raise PreflightError(f"VECTOR_DIM muss {expected_dim} sein (ist: '{dim or 'nicht gesetzt'}').")
|
||||||
|
|
||||||
|
env_model = os.getenv("MINDNET_EMBEDDING_MODEL", "").strip()
|
||||||
|
if env_model and env_model != expected_model:
|
||||||
|
raise PreflightError(f"MINDNET_EMBEDDING_MODEL='{env_model}' weicht von '{expected_model}' ab.")
|
||||||
|
|
||||||
|
# Das Profil hat Vorrang vor der Umgebung; fehlt es, fällt der Client auf das Chat-Modell zurück.
|
||||||
|
profile = _load_profiles(profiles_path).get("embedding_expert") or {}
|
||||||
|
if profile.get("provider", "ollama") != "ollama" or profile.get("model") != expected_model:
|
||||||
|
raise PreflightError(f"Profil embedding_expert muss ollama/{expected_model} sein.")
|
||||||
|
if int(profile.get("dimensions") or 0) != expected_dim:
|
||||||
|
raise PreflightError(f"Profil embedding_expert muss dimensions={expected_dim} haben.")
|
||||||
|
|
||||||
|
|
||||||
|
def check_cloud_egress() -> None:
|
||||||
|
configured = [k for k in CLOUD_KEYS if os.getenv(k, "").strip()]
|
||||||
|
if configured and not _flag(CLOUD_EGRESS_ACK, "false"):
|
||||||
|
# Ohne Privacy Gateway würden Vault-Inhalte unmaskiert an Cloud-Provider gehen.
|
||||||
|
raise PreflightError(
|
||||||
|
f"Cloud-Keys gesetzt ({', '.join(configured)}), aber {CLOUD_EGRESS_ACK}=true fehlt. "
|
||||||
|
"Ohne Privacy Gateway gehen Vault-Inhalte unmaskiert an den Provider."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_read_only_mount(path: Path) -> bool:
|
||||||
|
if not hasattr(os, "statvfs"):
|
||||||
|
return False # nicht prüfbar -> als beschreibbar behandeln (fail closed)
|
||||||
|
return bool(os.statvfs(path).f_flag & os.ST_RDONLY)
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_VAULT_ROOT = "./vault_master" # Default aus app/config.py
|
||||||
|
|
||||||
|
|
||||||
|
def check_vault() -> None:
|
||||||
|
if not _flag("MINDNET_VAULT_REQUIRED", "true"):
|
||||||
|
return
|
||||||
|
root = Path(os.getenv("MINDNET_VAULT_ROOT") or DEFAULT_VAULT_ROOT)
|
||||||
|
if not root.is_dir():
|
||||||
|
raise PreflightError(f"Vault-Verzeichnis fehlt: '{root}'.")
|
||||||
|
# Ein ausgefallenes NAS hinterlässt einen leeren Mountpunkt.
|
||||||
|
try:
|
||||||
|
empty = next(root.iterdir(), None) is None
|
||||||
|
except OSError:
|
||||||
|
raise PreflightError(f"Vault '{root}' nicht lesbar (NAS nicht gemountet?).") from None
|
||||||
|
if empty:
|
||||||
|
raise PreflightError(f"Vault '{root}' ist leer (NAS nicht gemountet?).")
|
||||||
|
# Optional: zusätzlich eine bestimmte Datei verlangen (z. B. auf dem NAS-Vault).
|
||||||
|
sentinel_name = os.getenv("MINDNET_VAULT_SENTINEL", "").strip()
|
||||||
|
if sentinel_name:
|
||||||
|
sentinel = root / sentinel_name
|
||||||
|
try:
|
||||||
|
with sentinel.open("rb") as f:
|
||||||
|
f.read(1)
|
||||||
|
except OSError:
|
||||||
|
raise PreflightError(f"Vault-Sentinel nicht lesbar: '{sentinel}' (NAS nicht gemountet?).") from None
|
||||||
|
if _flag("MINDNET_VAULT_READONLY", "true") and not _is_read_only_mount(root):
|
||||||
|
raise PreflightError(f"Vault '{root}' ist nicht schreibgeschützt gemountet (':ro' erforderlich).")
|
||||||
|
|
||||||
|
|
||||||
|
def check_service_urls() -> None:
|
||||||
|
for name in SERVICE_URLS:
|
||||||
|
url = os.getenv(name, "").strip()
|
||||||
|
if not url:
|
||||||
|
# Die App fiele sonst still auf http://127.0.0.1:... zurück, also auf den Container selbst.
|
||||||
|
raise PreflightError(f"{name} muss explizit gesetzt sein.")
|
||||||
|
if urllib.parse.urlsplit(url).hostname in LOOPBACK_HOSTS:
|
||||||
|
raise PreflightError(
|
||||||
|
f"{name}='{url}' zeigt im Container auf den Container selbst. "
|
||||||
|
"Host-Dienste über host.docker.internal ansprechen."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _fetch_json(url: str, payload: dict | None = None, headers: dict | None = None, timeout: float = 10):
|
||||||
|
"""GET (oder POST mit payload). Netzwerkfehler werden zu PreflightError, HTTPError bleibt erhalten."""
|
||||||
|
req = urllib.request.Request(
|
||||||
|
url, method="POST" if payload is not None else "GET",
|
||||||
|
data=json.dumps(payload).encode() if payload is not None else None,
|
||||||
|
headers={"Content-Type": "application/json", **(headers or {})},
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req, timeout=timeout) as res:
|
||||||
|
return json.load(res)
|
||||||
|
except urllib.error.HTTPError:
|
||||||
|
raise
|
||||||
|
except (urllib.error.URLError, OSError) as e:
|
||||||
|
hint = ""
|
||||||
|
if urllib.parse.urlsplit(url).hostname == "host.docker.internal":
|
||||||
|
hint = " Lauscht der Dienst auf dem Host nur an 127.0.0.1?"
|
||||||
|
raise PreflightError(f"{url} nicht erreichbar ({getattr(e, 'reason', e)}).{hint}") from None
|
||||||
|
except ValueError:
|
||||||
|
raise PreflightError(f"{url} liefert kein JSON.") from None
|
||||||
|
|
||||||
|
|
||||||
|
def required_ollama_models(profiles_path: str | None = None) -> set[str]:
|
||||||
|
models = {p["model"] for p in _load_profiles(profiles_path).values()
|
||||||
|
if isinstance(p, dict) and p.get("provider") == "ollama" and p.get("model")}
|
||||||
|
# Lokales Chat-Modell: Endpunkt der Fallback-Kette ohne Cloud-Keys (Review R4).
|
||||||
|
if os.getenv("MINDNET_LLM_MODEL", "").strip():
|
||||||
|
models.add(os.environ["MINDNET_LLM_MODEL"].strip())
|
||||||
|
return models
|
||||||
|
|
||||||
|
|
||||||
|
def _has_model(required: str, available: set[str]) -> bool:
|
||||||
|
return required in available or (":" not in required and f"{required}:latest" in available)
|
||||||
|
|
||||||
|
|
||||||
|
def check_live(profiles_path: str | None = None) -> None:
|
||||||
|
"""Laufzeitprüfung (nur lesend): Erreichbarkeit, Modelle, Embedding- und Collection-Dimension."""
|
||||||
|
expected_dim = int(os.getenv("MINDNET_EXPECTED_VECTOR_DIM", "768"))
|
||||||
|
ollama = os.getenv("MINDNET_OLLAMA_URL", "").rstrip("/")
|
||||||
|
model = os.getenv("MINDNET_EXPECTED_EMBEDDING_MODEL", "nomic-embed-text")
|
||||||
|
|
||||||
|
tags = _fetch_json(f"{ollama}/api/tags")
|
||||||
|
available = {m.get("name") for m in tags.get("models") or []}
|
||||||
|
missing = sorted(m for m in required_ollama_models(profiles_path) if not _has_model(m, available))
|
||||||
|
if missing:
|
||||||
|
raise PreflightError(f"Ollama ({ollama}) fehlen die Modelle: {', '.join(missing)}.")
|
||||||
|
|
||||||
|
embedding = _fetch_json(f"{ollama}/api/embeddings", {"model": model, "prompt": "preflight"}, timeout=30)
|
||||||
|
got = len(embedding.get("embedding") or [])
|
||||||
|
if got != expected_dim:
|
||||||
|
raise PreflightError(f"Ollama liefert {got} Dimensionen für '{model}', erwartet {expected_dim}.")
|
||||||
|
print(f"preflight: Ollama erreichbar ({ollama}), Modelle vorhanden, '{model}' = {got} Dimensionen.")
|
||||||
|
|
||||||
|
qdrant = os.getenv("QDRANT_URL", "").rstrip("/")
|
||||||
|
headers = {"api-key": os.environ["QDRANT_API_KEY"]} if os.getenv("QDRANT_API_KEY") else {}
|
||||||
|
try:
|
||||||
|
_fetch_json(f"{qdrant}/collections", headers=headers)
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
raise PreflightError(f"Qdrant ({qdrant}) antwortet mit HTTP {e.code}.") from None
|
||||||
|
prefix = os.getenv("COLLECTION_PREFIX", "")
|
||||||
|
for suffix in ("notes", "chunks"):
|
||||||
|
name = f"{prefix}_{suffix}"
|
||||||
|
try:
|
||||||
|
info = _fetch_json(f"{qdrant}/collections/{name}", headers=headers)
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
if e.code == 404:
|
||||||
|
print(f"preflight: Collection '{name}' existiert noch nicht.")
|
||||||
|
continue
|
||||||
|
raise PreflightError(f"Qdrant-Collection '{name}': HTTP {e.code}.") from None
|
||||||
|
vectors = info["result"]["config"]["params"]["vectors"]
|
||||||
|
sizes = [vectors["size"]] if "size" in vectors else [v["size"] for v in vectors.values()]
|
||||||
|
if any(s != expected_dim for s in sizes):
|
||||||
|
raise PreflightError(f"Collection '{name}' hat Dimension {sizes}, erwartet {expected_dim}.")
|
||||||
|
print(f"preflight: Qdrant erreichbar ({qdrant}), Collections '{prefix}_*' passen.")
|
||||||
|
|
||||||
|
|
||||||
|
def run_checks(mode: str) -> None:
|
||||||
|
check_environment()
|
||||||
|
check_embeddings()
|
||||||
|
check_cloud_egress()
|
||||||
|
if mode in {"api", "preflight"}:
|
||||||
|
check_service_urls()
|
||||||
|
check_vault()
|
||||||
|
|
||||||
|
|
||||||
|
COMMANDS = {
|
||||||
|
"api": ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"],
|
||||||
|
"ui": ["streamlit", "run", "app/frontend/ui.py", "--server.address", "0.0.0.0",
|
||||||
|
"--server.port", "8501", "--server.headless", "true", "--browser.gatherUsageStats", "false"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]) -> int:
|
||||||
|
mode = argv[1] if len(argv) > 1 else "api"
|
||||||
|
if mode not in {*COMMANDS, "preflight"}:
|
||||||
|
print(f"Unbekannter Modus '{mode}'. Erlaubt: api, ui, preflight [--live]", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
try:
|
||||||
|
run_checks(mode)
|
||||||
|
if mode == "preflight" and "--live" in argv:
|
||||||
|
check_live()
|
||||||
|
except PreflightError as e:
|
||||||
|
print(f"preflight: ABBRUCH: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
if mode == "preflight":
|
||||||
|
print("preflight: OK")
|
||||||
|
return 0
|
||||||
|
os.execvp(COMMANDS[mode][0], COMMANDS[mode])
|
||||||
|
return 0 # nicht erreichbar
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main(sys.argv))
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
*.env
|
||||||
|
!*.env.example
|
||||||
Vendored
+15
@@ -0,0 +1,15 @@
|
|||||||
|
# Mindnet DEV (Container): NUR Container-Angaben. Liegt auf dem Host außerhalb von Git.
|
||||||
|
# Die Anwendungskonfiguration kommt unverändert aus der .env der systemd-Instanz
|
||||||
|
# (MINDNET_APP_ENV_FILE im Workflow). deploy/appenv.py lehnt hier andere Schlüssel ab,
|
||||||
|
# damit diese Datei die Anwendungskonfiguration nicht überschreibt.
|
||||||
|
|
||||||
|
MINDNET_API_PORT=8012
|
||||||
|
# API ohne Authentisierung; die UI erreicht sie intern. Für Zugriff von außen auf 0.0.0.0 setzen.
|
||||||
|
MINDNET_API_BIND=127.0.0.1
|
||||||
|
MINDNET_UI_PORT=8512
|
||||||
|
MINDNET_UI_BIND=0.0.0.0
|
||||||
|
|
||||||
|
# Enthält die systemd-.env Cloud-Keys (OpenRouter/Gemini), startet der Container nur mit
|
||||||
|
# ausdrücklicher Bestätigung. true = gleiches Verhalten wie heute: Vault-Inhalte gehen
|
||||||
|
# ohne Privacy Gateway an den Cloud-Provider.
|
||||||
|
MINDNET_ALLOW_CLOUD_EGRESS=false
|
||||||
Vendored
+15
@@ -0,0 +1,15 @@
|
|||||||
|
# Mindnet PROD (Container): NUR Container-Angaben. Liegt auf dem Host außerhalb von Git.
|
||||||
|
# Die Anwendungskonfiguration kommt unverändert aus der .env der systemd-Instanz
|
||||||
|
# (MINDNET_APP_ENV_FILE im Workflow). deploy/appenv.py lehnt hier andere Schlüssel ab,
|
||||||
|
# damit diese Datei die Anwendungskonfiguration nicht überschreibt.
|
||||||
|
|
||||||
|
MINDNET_API_PORT=8011
|
||||||
|
# API ohne Authentisierung; die UI erreicht sie intern. Für Zugriff von außen auf 0.0.0.0 setzen.
|
||||||
|
MINDNET_API_BIND=127.0.0.1
|
||||||
|
MINDNET_UI_PORT=8511
|
||||||
|
MINDNET_UI_BIND=0.0.0.0
|
||||||
|
|
||||||
|
# Enthält die systemd-.env Cloud-Keys (OpenRouter/Gemini), startet der Container nur mit
|
||||||
|
# ausdrücklicher Bestätigung. true = gleiches Verhalten wie heute: Vault-Inhalte gehen
|
||||||
|
# ohne Privacy Gateway an den Cloud-Provider.
|
||||||
|
MINDNET_ALLOW_CLOUD_EGRESS=false
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
# VORLAGE, NICHT AKTIV: liegt absichtlich nicht unter .gitea/workflows/.
|
||||||
|
# Aktivierung erst bei der Umschaltung (Gate G5, eigene Freigabe), und zwar im
|
||||||
|
# selben Commit, der .gitea/workflows/deploy.yml (systemd) entfernt:
|
||||||
|
# git rm .gitea/workflows/deploy.yml
|
||||||
|
# git mv deploy/gitea-workflows/deploy-prod.yml .gitea/workflows/deploy-prod.yml
|
||||||
|
# Sonst deployen bei jedem Push auf main beide Wege.
|
||||||
|
#
|
||||||
|
# Prod nur über Merge-Commit develop -> main (git merge --no-ff develop), wie bei Kansho.
|
||||||
|
# Ablauf, Abbruchbedingungen und Rollback: deploy/deploy.sh.
|
||||||
|
|
||||||
|
name: Deploy mindnet PROD (Container)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
# Gleiche Gruppe wie der systemd-Workflow: nie zwei Prod-Deploys gleichzeitig.
|
||||||
|
group: deploy-mindnet
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
# Runner auf dem Zielhost (heute llm-node). Bei einem Umzug auf einen anderen
|
||||||
|
# Mini-PC nur das Label und die beiden Pfade anpassen.
|
||||||
|
runs-on: linux_host
|
||||||
|
env:
|
||||||
|
# Unveränderte Konfiguration der systemd-Prod-Instanz.
|
||||||
|
MINDNET_APP_ENV_FILE: /home/llmadmin/mindnet/.env
|
||||||
|
# Nur Container-Angaben (Vorlage: deploy/env/prod.env.example).
|
||||||
|
MINDNET_ENV_FILE: /home/llmadmin/docker/mindnet/container.env
|
||||||
|
steps:
|
||||||
|
# Ohne actions/checkout: kein Abruf von github.com (DNS/Internet auf dem Host
|
||||||
|
# nicht nötig). Holt genau den Commit des Pushes vom eigenen Gitea, mit Job-Token.
|
||||||
|
- name: Checkout
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
git init -q .
|
||||||
|
git -c http.extraHeader="Authorization: token ${{ github.token }}" \
|
||||||
|
fetch -q --depth 1 "${{ github.server_url }}/${{ github.repository }}.git" "${{ github.sha }}"
|
||||||
|
git checkout -q --force FETCH_HEAD
|
||||||
|
git clean -q -ffdx
|
||||||
|
git log -1 --format='Checkout %H %s'
|
||||||
|
|
||||||
|
- name: Container-Datei beim ersten Lauf anlegen
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
install -d -m 0750 "$(dirname "$MINDNET_ENV_FILE")"
|
||||||
|
if [ ! -f "$MINDNET_ENV_FILE" ]; then
|
||||||
|
install -m 0600 deploy/env/prod.env.example "$MINDNET_ENV_FILE"
|
||||||
|
echo "Container-Datei aus Vorlage angelegt: $MINDNET_ENV_FILE"
|
||||||
|
fi
|
||||||
|
docker version --format 'Docker {{.Server.Version}}'
|
||||||
|
docker compose version
|
||||||
|
|
||||||
|
- name: Test, Build, Preflight, Start, Smoke (Rollback bei Fehler)
|
||||||
|
run: bash deploy/deploy.sh prod
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
"""
|
||||||
|
FILE: deploy/healthcheck.py
|
||||||
|
DESCRIPTION: Docker-Healthcheck für API und UI.
|
||||||
|
API ist nur gesund, wenn /healthz antwortet UND der Vault-Sentinel
|
||||||
|
lesbar ist. Ein zur Laufzeit ausgefallenes NAS wird so als
|
||||||
|
'unhealthy' sichtbar, statt still auf einem leeren Mountpunkt zu laufen.
|
||||||
|
USAGE: healthcheck.py api | ui
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import urllib.request
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
URLS = {
|
||||||
|
"api": "http://127.0.0.1:8000/healthz",
|
||||||
|
"ui": "http://127.0.0.1:8501/_stcore/health",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main(mode: str) -> int:
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(URLS[mode], timeout=5) as res:
|
||||||
|
if res.status != 200:
|
||||||
|
return 1
|
||||||
|
except Exception as e:
|
||||||
|
print(f"health: {mode} nicht erreichbar ({type(e).__name__})")
|
||||||
|
return 1
|
||||||
|
if mode == "api" and os.getenv("MINDNET_VAULT_REQUIRED", "true").lower() == "true":
|
||||||
|
root = Path(os.getenv("MINDNET_VAULT_ROOT") or "./vault_master")
|
||||||
|
sentinel_name = os.getenv("MINDNET_VAULT_SENTINEL", "").strip()
|
||||||
|
try:
|
||||||
|
ok = (root / sentinel_name).is_file() if sentinel_name else next(root.iterdir(), None) is not None
|
||||||
|
except OSError:
|
||||||
|
ok = False
|
||||||
|
if not ok:
|
||||||
|
print(f"health: Vault leer oder nicht lesbar ({root})")
|
||||||
|
return 1
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main(sys.argv[1] if len(sys.argv) > 1 else "api"))
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-c requirements.lock
|
||||||
|
pytest>=8.4.2
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
colorama==0.4.6 ; sys_platform == 'win32' \
|
||||||
|
--hash=sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44 \
|
||||||
|
--hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
|
||||||
|
# via
|
||||||
|
# -c requirements.lock
|
||||||
|
# pytest
|
||||||
|
iniconfig==2.3.0 \
|
||||||
|
--hash=sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730 \
|
||||||
|
--hash=sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12
|
||||||
|
# via pytest
|
||||||
|
packaging==26.0 \
|
||||||
|
--hash=sha256:00243ae351a257117b6a241061796684b084ed1c516a08c48a3f7e147a9d80b4 \
|
||||||
|
--hash=sha256:b36f1fef9334a5588b4166f8bcd26a14e521f2b55e6b9de3aaa80d3ff7a37529
|
||||||
|
# via
|
||||||
|
# -c requirements.lock
|
||||||
|
# pytest
|
||||||
|
pluggy==1.6.0 \
|
||||||
|
--hash=sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3 \
|
||||||
|
--hash=sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746
|
||||||
|
# via pytest
|
||||||
|
pygments==2.19.2 \
|
||||||
|
--hash=sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887 \
|
||||||
|
--hash=sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b
|
||||||
|
# via pytest
|
||||||
|
pytest==9.0.2 \
|
||||||
|
--hash=sha256:711ffd45bf766d5264d487b917733b453d917afd2b0ad65223959f59089f875b \
|
||||||
|
--hash=sha256:75186651a92bd89611d1d9fc20f0b4345fd827c41ccd5c299a868a05d70edf11
|
||||||
|
# via -r requirements-test.in
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
# Laufzeit-Abhängigkeiten für die Container-Images (API + UI).
|
||||||
|
# Abgeleitet aus requirements.txt OHNE torch, sentence-transformers und pytest:
|
||||||
|
# - app/embeddings.py (MiniLM) wird von keinem eingebundenen Router importiert;
|
||||||
|
# Embeddings laufen über Ollama (embedding_expert, nomic-embed-text, 768 Dim.).
|
||||||
|
# - pytest steckt nur im Test-Target (requirements-test.in).
|
||||||
|
# Lock neu erzeugen: siehe docs/04_Operations/04_docker_deployment.md, Abschnitt "Lock".
|
||||||
|
fastapi>=0.116.1
|
||||||
|
uvicorn[standard]>=0.35.0
|
||||||
|
httpx>=0.28.1
|
||||||
|
qdrant-client>=1.15.1
|
||||||
|
pydantic>=2.11.7
|
||||||
|
numpy>=2.3.2
|
||||||
|
python-frontmatter>=1.1.0
|
||||||
|
markdown-it-py>=3.0.0
|
||||||
|
PyYAML>=6.0.2
|
||||||
|
python-slugify>=8.0.4
|
||||||
|
python-dotenv>=1.1.1
|
||||||
|
requests>=2.32.5
|
||||||
|
tqdm>=4.67.1
|
||||||
|
streamlit>=1.39.0
|
||||||
|
streamlit-agraph>=0.0.45
|
||||||
|
st-cytoscape
|
||||||
|
google-genai>=0.3.0
|
||||||
|
openai>=1.50.0
|
||||||
File diff suppressed because it is too large.
Load diff
+113
@@ -0,0 +1,113 @@
|
|||||||
|
"""
|
||||||
|
FILE: deploy/smoke.py
|
||||||
|
DESCRIPTION: Nicht-schreibender Smoke-Test gegen einen laufenden Mindnet-Stack,
|
||||||
|
aus Sicht des Hosts über die veröffentlichten Ports. Nur Standardbibliothek,
|
||||||
|
läuft daher direkt auf dem Runner (python3), ohne venv.
|
||||||
|
- API /healthz: Status ok, erwartetes Collection-Präfix
|
||||||
|
- API /admin/stats: Vektorgröße 768; API erreicht Qdrant (Collection-Counts)
|
||||||
|
- UI /_stcore/health
|
||||||
|
--require-data (Prod): alle Collections vorhanden und Notes nicht leer.
|
||||||
|
VERSION: 1.0.0
|
||||||
|
STATUS: Active
|
||||||
|
USAGE: smoke.py --api http://127.0.0.1:8012 --ui http://127.0.0.1:8512 --prefix mindnet_dev [--require-data]
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
EXPECTED_VECTOR_SIZE = 768
|
||||||
|
|
||||||
|
|
||||||
|
class SmokeError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _get(url: str, timeout: float = 15) -> bytes:
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(url, timeout=timeout) as res:
|
||||||
|
return res.read()
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
raise SmokeError(f"{url}: HTTP {e.code}") from None
|
||||||
|
except (urllib.error.URLError, OSError) as e:
|
||||||
|
raise SmokeError(f"{url} nicht erreichbar ({getattr(e, 'reason', e)})") from None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_json(url: str) -> dict:
|
||||||
|
try:
|
||||||
|
return json.loads(_get(url))
|
||||||
|
except ValueError:
|
||||||
|
raise SmokeError(f"{url} liefert kein JSON") from None
|
||||||
|
|
||||||
|
|
||||||
|
def check_api(api: str, prefix: str, require_data: bool) -> list[str]:
|
||||||
|
"""Gibt Warnungen zurück; harte Fehler als SmokeError."""
|
||||||
|
health = _get_json(f"{api}/healthz")
|
||||||
|
if health.get("status") != "ok":
|
||||||
|
raise SmokeError(f"/healthz meldet {health.get('status')!r}")
|
||||||
|
if health.get("prefix") != prefix:
|
||||||
|
raise SmokeError(f"API nutzt Präfix {health.get('prefix')!r}, erwartet {prefix!r}")
|
||||||
|
|
||||||
|
stats = _get_json(f"{api}/admin/stats")
|
||||||
|
size = (stats.get("config") or {}).get("vector_size")
|
||||||
|
if size != EXPECTED_VECTOR_SIZE:
|
||||||
|
raise SmokeError(f"API-Vektorgröße {size}, erwartet {EXPECTED_VECTOR_SIZE}")
|
||||||
|
|
||||||
|
warnings = []
|
||||||
|
collections = stats.get("collections") or {}
|
||||||
|
for kind in ("notes", "chunks", "edges"):
|
||||||
|
entry = collections.get(kind) or {}
|
||||||
|
count = entry.get("count", -1)
|
||||||
|
if count < 0:
|
||||||
|
# -1: Collection fehlt oder Qdrant ist aus dem API-Container nicht erreichbar.
|
||||||
|
msg = f"Collection {entry.get('name', kind)!r} nicht lesbar (fehlt oder Qdrant nicht erreichbar)"
|
||||||
|
if require_data:
|
||||||
|
raise SmokeError(msg)
|
||||||
|
warnings.append(msg)
|
||||||
|
if require_data and (collections.get("notes") or {}).get("count", 0) <= 0:
|
||||||
|
raise SmokeError("Notes-Collection ist leer")
|
||||||
|
return warnings
|
||||||
|
|
||||||
|
|
||||||
|
def check_ui(ui: str) -> None:
|
||||||
|
body = _get(f"{ui}/_stcore/health").decode("utf-8", "replace").strip()
|
||||||
|
if body != "ok":
|
||||||
|
raise SmokeError(f"UI-Health meldet {body!r}")
|
||||||
|
|
||||||
|
|
||||||
|
def run(api: str, ui: str, prefix: str, require_data: bool = False, attempts: int = 1, delay: float = 5) -> int:
|
||||||
|
api, ui = api.rstrip("/"), ui.rstrip("/")
|
||||||
|
for attempt in range(1, attempts + 1):
|
||||||
|
try:
|
||||||
|
warnings = check_api(api, prefix, require_data)
|
||||||
|
check_ui(ui)
|
||||||
|
except SmokeError as e:
|
||||||
|
if attempt == attempts:
|
||||||
|
print(f"smoke: FEHLER: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
time.sleep(delay)
|
||||||
|
continue
|
||||||
|
for w in warnings:
|
||||||
|
print(f"smoke: WARNUNG: {w}")
|
||||||
|
print(f"smoke: OK (api={api}, ui={ui}, prefix={prefix})")
|
||||||
|
return 0
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] | None = None) -> int:
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__.split("USAGE:")[0])
|
||||||
|
parser.add_argument("--api", required=True)
|
||||||
|
parser.add_argument("--ui", required=True)
|
||||||
|
parser.add_argument("--prefix", required=True)
|
||||||
|
parser.add_argument("--require-data", action="store_true")
|
||||||
|
parser.add_argument("--attempts", type=int, default=3)
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
return run(args.api, args.ui, args.prefix, args.require_data, args.attempts)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
# Mindnet V3.0: Der Aufstieg des Digitalen Zwillings
|
||||||
|
## Von der Wissensdatenbank zum strategischen Partner – Ein Paradigmenwechsel
|
||||||
|
|
||||||
|
### Einleitung: Die Vision von Version 3.0
|
||||||
|
Mit der Vollendung des Meilensteins WP25 (inklusive der Architektur-Erweiterungen 25a und 25b) transformiert sich Mindnet von einem reinen Retrieval-System (V2) zu einem autonomen, agentischen Ökosystem (V3.0). Mindnet V3.0 ist nicht länger nur ein Werkzeug zur Informationswiedergabe; es ist ein **Digitaler Zwilling**, der in der Lage ist, komplexe Realitäten durch Multi-Stream-Analysen zu erfassen, strategische Empfehlungen auf Basis individueller Werte zu geben und eine bisher unerreichte Ausfallsicherheit zu garantieren.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Die 6 Säulen der Mindnet V3.0 Architektur
|
||||||
|
|
||||||
|
#### 1. Agentic Multi-Stream Retrieval (WP-25)
|
||||||
|
Das Herzstück von V3.0 ist die neue `DecisionEngine`. Während herkömmliche Systeme lediglich eine einfache Vektorsuche durchführen, orchestriert die DecisionEngine parallele Wissens-Streams:
|
||||||
|
* **Werte-Stream:** Abgleich von Anfragen mit Ihrer ethischen und strategischen Identität.
|
||||||
|
* **Fakten-Stream:** Analyse der operativen Realität und aktueller Projektdaten.
|
||||||
|
* **Biografie-Stream:** Integration persönlicher Erfahrungen und historischer Kontexte.
|
||||||
|
* **Risiko-Radar:** Proaktive Identifikation von Hindernissen und Zielkonflikten.
|
||||||
|
* **Technik-Wissen:** Tiefgreifende fachliche Expertise für spezialisierte Aufgaben.
|
||||||
|
|
||||||
|
Dieses System erlaubt es Mindnet, eine Anfrage aus fünf verschiedenen Perspektiven gleichzeitig zu beleuchten, bevor eine finale Synthese erfolgt.
|
||||||
|
|
||||||
|
#### 2. Mixture of Experts (MoE) & Dynamic Profiling (WP-25a)
|
||||||
|
Mindnet V3.0 nutzt nicht mehr nur "ein" Modell. Über die zentrale Steuerung in der `llm_profiles.yaml` wird für jede Teilaufgabe der ideale "Experte" gerufen:
|
||||||
|
* **Der Architekt (Gemini 2.0 Flash):** Für hochkomplexe reasoning-intensive Synthesen.
|
||||||
|
* **Der Ingenieur (Qwen 2.5):** Spezialisiert auf präzise Code-Generierung und technische Problemlösung.
|
||||||
|
* **Der Dampfhammer (Mistral 7B):** Optimiert für blitzschnelles Routing und asynchrone Inhaltskompression.
|
||||||
|
* **Der Wächter (Phi-3 Mini):** Ein lokales Modell via Ollama, das maximale Privatsphäre für sensible Identitätsdaten garantiert.
|
||||||
|
|
||||||
|
#### 3. Hierarchische Lazy-Prompt-Orchestration (WP-25b)
|
||||||
|
Ein technologisches Highlight ist die Einführung des **Lazy-Promptings**. Prompts werden nicht mehr statisch im Code verwaltet, sondern erst im Moment der Modellauswahl hierarchisch aufgelöst:
|
||||||
|
1. **Modell-Ebene:** Spezifisch für die jeweilige Modell-ID optimierte Instruktionen.
|
||||||
|
2. **Provider-Ebene:** Fallback-Anweisungen für OpenRouter oder Ollama.
|
||||||
|
3. **Global-Ebene:** Sicherheits-Instruktionen als ultimativer Anker.
|
||||||
|
|
||||||
|
Dies garantiert, dass jedes Modell in seiner "Muttersprache" angesprochen wird, was die Antwortqualität drastisch erhöht.
|
||||||
|
|
||||||
|
#### 4. Die unzerstörbare Fallback-Kaskade
|
||||||
|
Resilienz ist in V3.0 kein Schlagwort, sondern ein Algorithmus. Sollte ein Cloud-Anbieter (wie OpenRouter) ausfallen oder in ein Rate-Limit laufen, reagiert das System autonom:
|
||||||
|
* Automatischer Wechsel auf das Backup-Profil (z.B. von Gemini auf Llama).
|
||||||
|
* In letzter Instanz: Rückzug auf die lokale Hardware (Ollama/Phi-3), sodass Mindnet auch offline voll einsatzfähig bleibt.
|
||||||
|
* **Lazy-Re-Formatting:** Beim Wechsel des Modells wird auch der Prompt sofort neu geladen und für das neue Modell optimiert.
|
||||||
|
|
||||||
|
#### 5. Hochpräzises Intent-Routing mit Regex-Cleaning
|
||||||
|
Durch den neuen ultra-robusten Router in der `DecisionEngine` v1.3.2 erkennt Mindnet Nutzerintentionen mit chirurgischer Präzision. Modell-Artefakte (wie Stop-Marker oder überflüssige Tags freier Modelle) werden durch aggressive Regex-Filter eliminiert, bevor sie das System-Routing stören können. Dies stellt sicher, dass eine Coding-Frage niemals fälschlicherweise im Fakten-Modus landet.
|
||||||
|
|
||||||
|
#### 6. Semantische Ingestion-Validierung v2.14.0
|
||||||
|
Die Qualität des Wissensgraphen wird durch eine neue Validierungsebene geschützt. Während des Imports prüft Mindnet semantisch, ob vorgeschlagene Verknüpfungen (Edges) zwischen Informationen wirklich sinnvoll sind. Dabei unterscheidet das System zwischen temporären Netzwerkfehlern und dauerhaften Logikfehlern, um die Integrität Ihres digitalen Gedächtnisses zu wahren.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Technische Highlights für Power-User
|
||||||
|
|
||||||
|
| Feature | Technologie | Nutzen |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| **Orchestrator** | `DecisionEngine v1.3.2` | Agentische Steuerung & Multi-Stream Retrieval |
|
||||||
|
| **Hybrid Cloud** | OpenRouter & Ollama | Maximale Flexibilität zwischen Leistung und Datenschutz |
|
||||||
|
| **Traceability** | `[PROMPT-TRACE]` Logs | Volle Transparenz über die genutzten KI-Instruktionen |
|
||||||
|
| **Context Guard** | Asynchrone Kompression | Optimierung der Kontextfenster für maximale Kosten-Effizienz |
|
||||||
|
| **Resilienz** | Rekursive Fallback-Kaskade | 100% Verfügbarkeit durch Cloud-to-Local Automatisierung |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Fazit: Ihr Gehirn, erweitert durch Mindnet V3.0
|
||||||
|
Mindnet V3.0 ist das Ergebnis einer konsequenten Weiterentwicklung hin zu einer **Zero-Failure-Architektur**. Durch die Kombination aus agentischer Intelligenz, hybrider Modellnutzung und der neuen Lazy-Prompt-Infrastruktur bietet es eine Basis, die nicht nur mit Ihrem Wissen wächst, sondern aktiv dabei hilft, dieses Wissen in strategisches Handeln zu übersetzen.
|
||||||
|
|
||||||
|
**Willkommen in der Ära von Mindnet V3.0 – Ihr strategischer Partner ist bereit.**
|
||||||
|
|
||||||
|
---
|
||||||
|
*Dokumentations-Identifikator: `mindnet_v3_core_release`*
|
||||||
|
*Synchronisations-Stand: WP-25b Final*
|
||||||
@@ -18,9 +18,12 @@ Das Repository ist in **logische Domänen** unterteilt.
|
|||||||
*Zielgruppe: Alle*
|
*Zielgruppe: Alle*
|
||||||
| Datei | Inhalt & Zweck |
|
| Datei | Inhalt & Zweck |
|
||||||
| :--- | :--- |
|
| :--- | :--- |
|
||||||
|
| `README.md` | **Einstiegspunkt.** Übersicht über die Dokumentationsstruktur, Schnellzugriff nach Rollen und Navigation. |
|
||||||
|
| `00_quickstart.md` | **Schnellstart.** Installation und erste Schritte in 15 Minuten. Ideal für neue Benutzer. |
|
||||||
| `00_vision_and_strategy.md` | **Strategie.** Warum bauen wir das? Prinzipien (Privacy, Local-First), High-Level Architektur. |
|
| `00_vision_and_strategy.md` | **Strategie.** Warum bauen wir das? Prinzipien (Privacy, Local-First), High-Level Architektur. |
|
||||||
| `00_glossary.md` | **Definitionen.** Was bedeutet "Smart Edge", "Traffic Control", "Chunk"? Verhindert Begriffsverwirrung. |
|
| `00_glossary.md` | **Definitionen.** Was bedeutet "Smart Edge", "Traffic Control", "Chunk"? Verhindert Begriffsverwirrung. |
|
||||||
| `00_documentation_map.md` | **Dieser Index.** Navigationshilfe. |
|
| `00_documentation_map.md` | **Dieser Index.** Navigationshilfe. |
|
||||||
|
| `00_quality_checklist.md` | **Qualitätsprüfung.** Systematische Checkliste zur Vollständigkeitsprüfung für alle Rollen. |
|
||||||
|
|
||||||
### 📂 01_User_Manual (Anwendung)
|
### 📂 01_User_Manual (Anwendung)
|
||||||
*Zielgruppe: Mindmaster, Autoren, Power-User*
|
*Zielgruppe: Mindmaster, Autoren, Power-User*
|
||||||
@@ -28,7 +31,8 @@ Das Repository ist in **logische Domänen** unterteilt.
|
|||||||
| :--- | :--- |
|
| :--- | :--- |
|
||||||
| `01_chat_usage_guide.md` | **Bedienung.** Wie steuere ich die Personas (Berater, Spiegel)? Wie nutze ich das Feedback? |
|
| `01_chat_usage_guide.md` | **Bedienung.** Wie steuere ich die Personas (Berater, Spiegel)? Wie nutze ich das Feedback? |
|
||||||
| `01_knowledge_design.md` | **Content-Regeln.** Die "Bibel" für den Vault. Erklärt Note-Typen, Matrix-Logik und Markdown-Syntax. |
|
| `01_knowledge_design.md` | **Content-Regeln.** Die "Bibel" für den Vault. Erklärt Note-Typen, Matrix-Logik und Markdown-Syntax. |
|
||||||
| `01-authoring-guidelines` | **Content strukturieren-.** primäres Werkzeug, um Wissen so zu strukturieren, so dass Mindnet die Persönlichkeit spiegelt, empathisch reagiert und strategisch berät |
|
| `01_authoring_guidelines.md` | **Content strukturieren.** Primäres Werkzeug, um Wissen so zu strukturieren, dass Mindnet die Persönlichkeit spiegelt, empathisch reagiert und strategisch berät. |
|
||||||
|
| `01_obsidian_integration_guide.md` | **Obsidian Setup.** Technische Anleitung für die Integration von Mindnet mit Obsidian (Templater, Skripte, Workflows). |
|
||||||
|
|
||||||
### 📂 02_Concepts (Fachliche Logik)
|
### 📂 02_Concepts (Fachliche Logik)
|
||||||
*Zielgruppe: Architekten, Product Owner*
|
*Zielgruppe: Architekten, Product Owner*
|
||||||
@@ -36,6 +40,7 @@ Das Repository ist in **logische Domänen** unterteilt.
|
|||||||
| :--- | :--- |
|
| :--- | :--- |
|
||||||
| `02_concept_graph_logic.md` | **Graph-Theorie.** Abstrakte Erklärung von Knoten, Kanten, Provenance und Idempotenz. |
|
| `02_concept_graph_logic.md` | **Graph-Theorie.** Abstrakte Erklärung von Knoten, Kanten, Provenance und Idempotenz. |
|
||||||
| `02_concept_ai_personality.md`| **KI-Verhalten.** Konzepte hinter dem Hybrid Router, Empathie-Modell und "Teach-the-AI". |
|
| `02_concept_ai_personality.md`| **KI-Verhalten.** Konzepte hinter dem Hybrid Router, Empathie-Modell und "Teach-the-AI". |
|
||||||
|
| `02_concept_architecture_patterns.md` | **Architektur-Patterns.** Design-Entscheidungen, modulare Struktur (WP-14), Resilienz-Patterns und Erweiterbarkeit. |
|
||||||
|
|
||||||
### 📂 03_Technical_Reference (Technik & Code)
|
### 📂 03_Technical_Reference (Technik & Code)
|
||||||
*Zielgruppe: Entwickler, DevOps. (Enthält JSON/YAML Beispiele)*
|
*Zielgruppe: Entwickler, DevOps. (Enthält JSON/YAML Beispiele)*
|
||||||
@@ -46,13 +51,16 @@ Das Repository ist in **logische Domänen** unterteilt.
|
|||||||
| `03_tech_retrieval_scoring.md` | **Suche.** Die mathematischen Formeln für Scoring, Hybrid Search und Explanation Layer. |
|
| `03_tech_retrieval_scoring.md` | **Suche.** Die mathematischen Formeln für Scoring, Hybrid Search und Explanation Layer. |
|
||||||
| `03_tech_chat_backend.md` | **API & LLM.** Implementation des Routers, Traffic Control (Semaphore) und Feedback-Traceability. |
|
| `03_tech_chat_backend.md` | **API & LLM.** Implementation des Routers, Traffic Control (Semaphore) und Feedback-Traceability. |
|
||||||
| `03_tech_frontend.md` | **UI & Graph.** Architektur des Streamlit-Frontends, State-Management, Cytoscape-Integration und Editor-Logik. |
|
| `03_tech_frontend.md` | **UI & Graph.** Architektur des Streamlit-Frontends, State-Management, Cytoscape-Integration und Editor-Logik. |
|
||||||
| `03_tech_configuration.md` | **Config.** Referenztabellen für `.env`, `types.yaml` und `retriever.yaml`. |
|
| `03_tech_configuration.md` | **Config.** Referenztabellen für `.env`, `types.yaml`, `decision_engine.yaml`, `llm_profiles.yaml`, `prompts.yaml`. **Neu:** Verbindungen zwischen Config-Dateien, Praxisbeispiel und Mermaid-Grafik. |
|
||||||
|
| `03_tech_api_reference.md` | **API-Referenz.** Vollständige Dokumentation aller Endpunkte (`/query`, `/chat`, `/ingest`, `/graph`, etc.). |
|
||||||
|
|
||||||
### 📂 04_Operations (Betrieb)
|
### 📂 04_Operations (Betrieb)
|
||||||
*Zielgruppe: Administratoren*
|
*Zielgruppe: Administratoren*
|
||||||
| Datei | Inhalt & Zweck |
|
| Datei | Inhalt & Zweck |
|
||||||
| :--- | :--- |
|
| :--- | :--- |
|
||||||
| `04_admin_operations.md` | **Runbook.** Installation, Docker-Setup, Backup/Restore, Troubleshooting Guide. |
|
| `04_admin_operations.md` | **Runbook.** Installation, Docker-Setup, Backup/Restore, Troubleshooting Guide. |
|
||||||
|
| `04_server_operation_manual.md` | **Server-Betrieb.** Detaillierte Dokumentation für den Betrieb auf llm-node (Systemd, Borgmatic, Disaster Recovery). |
|
||||||
|
| `04_deployment_guide.md` | **Deployment.** CI/CD-Pipelines, Rollout-Strategien, Versionierung, Rollback und Pre/Post-Deployment-Checklisten. |
|
||||||
|
|
||||||
### 📂 05_Development (Code)
|
### 📂 05_Development (Code)
|
||||||
*Zielgruppe: Entwickler*
|
*Zielgruppe: Entwickler*
|
||||||
@@ -60,6 +68,7 @@ Das Repository ist in **logische Domänen** unterteilt.
|
|||||||
| :--- | :--- |
|
| :--- | :--- |
|
||||||
| `05_developer_guide.md` | **Workflow.** Hardware-Setup (Win/Pi/Beelink), Git-Flow, Test-Befehle, Modul-Interna. |
|
| `05_developer_guide.md` | **Workflow.** Hardware-Setup (Win/Pi/Beelink), Git-Flow, Test-Befehle, Modul-Interna. |
|
||||||
| `05_genai_best_practices.md` | **AI Workflow.** Prompt-Library, Templates und Best Practices für die Entwicklung mit LLMs. |
|
| `05_genai_best_practices.md` | **AI Workflow.** Prompt-Library, Templates und Best Practices für die Entwicklung mit LLMs. |
|
||||||
|
| `05_testing_guide.md` | **Testing.** Umfassender Test-Guide: Strategien, Frameworks, Test-Daten, Best Practices. |
|
||||||
|
|
||||||
### 📂 06_Roadmap & 99_Archive
|
### 📂 06_Roadmap & 99_Archive
|
||||||
*Zielgruppe: Projektleitung*
|
*Zielgruppe: Projektleitung*
|
||||||
@@ -82,7 +91,9 @@ Nutze diese Matrix, wenn du ein Workpackage bearbeitest, um die Dokumentation ko
|
|||||||
| **Retrieval / Scoring** | `03_tech_retrieval_scoring.md` (Formeln anpassen) |
|
| **Retrieval / Scoring** | `03_tech_retrieval_scoring.md` (Formeln anpassen) |
|
||||||
| **Frontend / Visualisierung** | 1. `03_tech_frontend.md` (Technische Details)<br>2. `01_chat_usage_guide.md` (Bedienung) |
|
| **Frontend / Visualisierung** | 1. `03_tech_frontend.md` (Technische Details)<br>2. `01_chat_usage_guide.md` (Bedienung) |
|
||||||
| **Chat-Logik / Prompts**| 1. `02_concept_ai_personality.md` (Konzept)<br>2. `03_tech_chat_backend.md` (Tech)<br>3. `01_chat_usage_guide.md` (User-Sicht) |
|
| **Chat-Logik / Prompts**| 1. `02_concept_ai_personality.md` (Konzept)<br>2. `03_tech_chat_backend.md` (Tech)<br>3. `01_chat_usage_guide.md` (User-Sicht) |
|
||||||
| **Deployment / Server** | `04_admin_operations.md` |
|
| **Architektur / Design-Patterns** | 1. `02_concept_architecture_patterns.md` (Patterns & Entscheidungen)<br>2. `02_concept_graph_logic.md` (Graph-Theorie)<br>3. `05_developer_guide.md` (Modulare Struktur) |
|
||||||
|
| **Deployment / Server** | 1. `04_deployment_guide.md` (CI/CD, Rollout)<br>2. `04_admin_operations.md` (Installation, Wartung)<br>3. `04_server_operation_manual.md` (Server-Betrieb) |
|
||||||
|
| **Testing / QA** | 1. `05_testing_guide.md` (Test-Strategien & Frameworks)<br>2. `05_developer_guide.md` (Test-Befehle) |
|
||||||
| **Neuen Features (Allg.)**| `06_active_roadmap.md` (Status Update) |
|
| **Neuen Features (Allg.)**| `06_active_roadmap.md` (Status Update) |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -111,3 +122,37 @@ Damit dieses System wartbar bleibt (auch für KI-Agenten wie NotebookLM), gelten
|
|||||||
context: "Beschreibung der Scoring-Formel."
|
context: "Beschreibung der Scoring-Formel."
|
||||||
---
|
---
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Schnellzugriff & Empfehlungen
|
||||||
|
|
||||||
|
### Für neue Benutzer
|
||||||
|
1. Starte mit **[Schnellstart](00_quickstart.md)** für die Installation
|
||||||
|
2. Lese **[Vision & Strategie](00_vision_and_strategy.md)** für das große Bild
|
||||||
|
3. Nutze **[Chat Usage Guide](../01_User_Manual/01_chat_usage_guide.md)** für die ersten Schritte
|
||||||
|
|
||||||
|
### Für Entwickler
|
||||||
|
1. **[Developer Guide](../05_Development/05_developer_guide.md)** - Umfassender technischer Guide
|
||||||
|
2. **[Technical References](../03_Technical_References/)** - Detaillierte API- und Architektur-Dokumentation
|
||||||
|
3. **[GenAI Best Practices](../05_Development/05_genai_best_practices.md)** - Workflow mit LLMs
|
||||||
|
|
||||||
|
### Für Administratoren
|
||||||
|
1. **[Admin Operations](../04_Operations/04_admin_operations.md)** - Installation und Wartung
|
||||||
|
2. **[Server Operations Manual](../04_Operations/04_server_operation_manual.md)** - Server-Betrieb und Disaster Recovery
|
||||||
|
3. **[Troubleshooting Guide](../04_Operations/04_admin_operations.md#33-troubleshooting-guide)** - Häufige Probleme und Lösungen
|
||||||
|
|
||||||
|
### Für Autoren
|
||||||
|
1. **[Knowledge Design](../01_User_Manual/01_knowledge_design.md)** - Content-Regeln und Best Practices
|
||||||
|
2. **[Authoring Guidelines](../01_User_Manual/01_authoring_guidelines.md)** - Strukturierung für den Digitalen Zwilling
|
||||||
|
3. **[Obsidian Integration](../01_User_Manual/01_obsidian_integration_guide.md)** - Workflow-Optimierung
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Dokumentations-Status
|
||||||
|
|
||||||
|
**Aktuelle Version:** 3.1.1
|
||||||
|
**Letzte Aktualisierung:** 2026-01-02
|
||||||
|
**Status:** ✅ Vollständig und aktiv gepflegt
|
||||||
|
|
||||||
|
**Hinweis:** Diese Dokumentation wird kontinuierlich aktualisiert. Bei Fragen oder Verbesserungsvorschlägen bitte im Repository melden.
|
||||||
@@ -2,30 +2,36 @@
|
|||||||
doc_type: glossary
|
doc_type: glossary
|
||||||
audience: all
|
audience: all
|
||||||
status: active
|
status: active
|
||||||
version: 2.8.0
|
version: 4.5.8
|
||||||
context: "Zentrales Glossar für Mindnet v2.8. Enthält Definitionen zu Hybrid-Cloud Resilienz, WP-76 Quoten-Steuerung und Mistral-safe Parsing."
|
context: "Zentrales Glossar für Mindnet v4.5.8. Enthält Definitionen zu Hybrid-Cloud Resilienz, WP-14 Modularisierung, WP-15b Two-Pass Ingestion, WP-15c Multigraph-Support, WP-25 Agentic Multi-Stream RAG, WP-25a Mixture of Experts (MoE), WP-25b Lazy-Prompt-Orchestration, WP-24c Phase 3 Agentic Edge Validation und Mistral-safe Parsing."
|
||||||
---
|
---
|
||||||
|
|
||||||
# Mindnet Glossar
|
# Mindnet Glossar
|
||||||
|
|
||||||
**Quellen:** `01_edge_vocabulary.md`, `llm_service.py`, `ingestion.py`, `edge_registry.py`
|
**Quellen:** `01_edge_vocabulary.md`, `llm_service.py`, `ingestion.py`, `edge_registry.py`, `registry.py`, `qdrant.py`
|
||||||
|
|
||||||
## Kern-Entitäten
|
## Kern-Entitäten
|
||||||
|
|
||||||
* **Note:** Repräsentiert eine Markdown-Datei. Die fachliche Haupteinheit. Verfügt über einen **Status** (stable, draft, system), der das Scoring beeinflusst.
|
* **Note:** Repräsentiert eine Markdown-Datei. Die fachliche Haupteinheit. Verfügt über einen **Status** (stable, draft, system), der das Scoring beeinflusst.
|
||||||
* **Chunk:** Ein Textabschnitt einer Note. Die technische Sucheinheit (Vektor).
|
* **Chunk:** Ein Textabschnitt einer Note. Die technische Sucheinheit (Vektor).
|
||||||
* **Edge:** Eine gerichtete Verbindung zwischen zwei Knoten. Wird in WP-22 durch die Registry validiert.
|
* **Edge:** Eine gerichtete Verbindung zwischen zwei Knoten. Wird in WP-22 durch die Registry validiert. Seit v2.9.1 unterstützt Edges **Section-basierte Links** (`target_section`), sodass mehrere Kanten zwischen denselben Knoten existieren können, wenn sie auf verschiedene Abschnitte zeigen.
|
||||||
* **Vault:** Der lokale Ordner mit den Markdown-Dateien (Source of Truth).
|
* **Vault:** Der lokale Ordner mit den Markdown-Dateien (Source of Truth).
|
||||||
* **Frontmatter:** Der YAML-Header am Anfang einer Notiz (enthält `id`, `type`, `title`, `status`).
|
* **Frontmatter:** Der YAML-Header am Anfang einer Notiz (enthält `id`, `type`, `title`, `status`).
|
||||||
|
|
||||||
## Komponenten
|
## Komponenten
|
||||||
|
|
||||||
* **Edge Registry:** Der zentrale Dienst (SSOT), der Kanten-Typen validiert und Aliase in kanonische Typen auflöst. Nutzt `01_edge_vocabulary.md` als Basis.
|
* **Edge Registry:** Der zentrale Dienst (SSOT), der Kanten-Typen validiert und Aliase in kanonische Typen auflöst. Nutzt `01_edge_vocabulary.md` als Basis.
|
||||||
* **LLM Service:** Der Hybrid-Client (v3.3.6), der Anfragen zwischen OpenRouter, Google Gemini und lokalem Ollama routet. Verwaltet Cloud-Timeouts und Quoten-Management.
|
* **LLM Service:** Der Hybrid-Client (v3.3.6), der Anfragen zwischen OpenRouter, Google Gemini und lokalem Ollama routet. Verwaltet Cloud-Timeouts und Quoten-Management. Nutzt zur Text-Bereinigung nun die neutrale `registry.py`, um Circular Imports zu vermeiden.
|
||||||
* **Retriever:** Besteht in v2.7+ aus der Orchestrierung (`retriever.py`) und der mathematischen Scoring-Engine (`retriever_scoring.py`).
|
* **Retriever:** Besteht in v2.7+ aus der Orchestrierung (`retriever.py`) und der mathematischen Scoring-Engine (`retriever_scoring.py`). Seit WP-14 im Paket `app.core.retrieval` gekapselt.
|
||||||
* **Decision Engine:** Teil des Routers, der Intents erkennt und entsprechende **Boost-Faktoren** für das Retrieval injiziert.
|
* **Decision Engine (WP-25):** Der zentrale **Agentic Orchestrator**, der Intents erkennt, parallele Wissens-Streams orchestriert und die Ergebnisse synthetisiert. Implementiert Multi-Stream Retrieval und Intent-basiertes Routing.
|
||||||
|
* **Agentic Multi-Stream RAG (WP-25):** Architektur-Paradigma, bei dem Nutzeranfragen in parallele, spezialisierte Wissens-Streams aufgeteilt werden (Values, Facts, Biography, Risk, Tech), die gleichzeitig abgefragt und zu einer kontextreichen Antwort synthetisiert werden.
|
||||||
|
* **Stream-Tracing (WP-25):** Kennzeichnung jedes Treffers mit seinem Ursprungs-Stream (`stream_origin`), um Feedback-Optimierung pro Wissensbereich zu ermöglichen.
|
||||||
|
* **Intent-basiertes Routing (WP-25):** Hybrid-Modus zur Intent-Erkennung mit Keyword Fast-Path (sofortige Erkennung von Triggern) und LLM Slow-Path (semantische Analyse für unklare Anfragen).
|
||||||
|
* **Wissens-Synthese (WP-25):** Template-basierte Zusammenführung der Ergebnisse aus parallelen Streams mit expliziten Stream-Variablen (z.B. `{values_stream}`, `{risk_stream}`), um dem LLM eine differenzierte Abwägung zu ermöglichen.
|
||||||
* **Traffic Control:** Verwaltet Prioritäten und drosselt Hintergrund-Tasks (z.B. Smart Edges) mittels Semaphoren und Timeouts (45s) zur Vermeidung von System-Hangs.
|
* **Traffic Control:** Verwaltet Prioritäten und drosselt Hintergrund-Tasks (z.B. Smart Edges) mittels Semaphoren und Timeouts (45s) zur Vermeidung von System-Hangs.
|
||||||
* **Unknown Edges Log:** Die Datei `unknown_edges.jsonl`, in der das System Kanten-Typen protokolliert, die nicht im Dictionary gefunden wurden.
|
* **Unknown Edges Log:** Die Datei `unknown_edges.jsonl`, in der das System Kanten-Typen protokolliert, die nicht im Dictionary gefunden wurden.
|
||||||
|
* **Database Package (WP-14):** Zentralisiertes Infrastruktur-Paket (`app.core.database`), das den Qdrant-Client (`qdrant.py`) und das Point-Mapping (`qdrant_points.py`) verwaltet.
|
||||||
|
* **LocalBatchCache (WP-15b):** Ein globaler In-Memory-Index, der während des Pass 1 Scans aufgebaut wird und Metadaten (IDs, Titel, Summaries) aller Notizen für die Kantenvalidierung bereithält.
|
||||||
|
|
||||||
## Konzepte & Features
|
## Konzepte & Features
|
||||||
|
|
||||||
@@ -40,5 +46,29 @@ context: "Zentrales Glossar für Mindnet v2.8. Enthält Definitionen zu Hybrid-C
|
|||||||
* `explicit`: Vom Mensch gesetzt (Prio 1).
|
* `explicit`: Vom Mensch gesetzt (Prio 1).
|
||||||
* `semantic_ai`: Von der KI im Turbo-Mode extrahiert und validiert (Prio 2).
|
* `semantic_ai`: Von der KI im Turbo-Mode extrahiert und validiert (Prio 2).
|
||||||
* `structure`: Durch System-Regeln/Matrix erzeugt (Prio 3).
|
* `structure`: Durch System-Regeln/Matrix erzeugt (Prio 3).
|
||||||
* **Smart Edge Allocation:** KI-Verfahren zur Relevanzprüfung von Links für spezifische Textabschnitte.
|
* **Smart Edge Allocation (WP-15b):** KI-Verfahren zur Relevanzprüfung von Links für spezifische Textabschnitte. Validiert Kandidaten semantisch gegen das Ziel im LocalBatchCache.
|
||||||
* **Matrix Logic:** Bestimmung des Kanten-Typs basierend auf Quell- und Ziel-Entität (z.B. Erfahrung -> Wert = `based_on`).
|
* **Matrix Logic:** Bestimmung des Kanten-Typs basierend auf Quell- und Ziel-Entität (z.B. Erfahrung -> Wert = `based_on`).
|
||||||
|
* **Two-Pass Workflow (WP-15b):** Optimiertes Ingestion-Verfahren:
|
||||||
|
* **Pass 1 (Pre-Scan):** Schnelles Scannen aller Dateien zur Befüllung des LocalBatchCache.
|
||||||
|
* **Pass 2 (Semantic Processing):** Tiefenverarbeitung (Chunking, Embedding, Validierung) nur für geänderte Dateien.
|
||||||
|
* **Circular Import Registry (WP-14):** Entkopplung von Kern-Logik (wie Textbereinigung) in eine neutrale `registry.py`, um Abhängigkeitsschleifen zwischen Diensten und Ingestion-Utilities zu verhindern.
|
||||||
|
* **Deep-Link / Section-basierter Link:** Ein Link wie `[[Note#Section]]`, der auf einen spezifischen Abschnitt innerhalb einer Note verweist. Seit v2.9.1 wird dieser in `target_id="Note"` und `target_section="Section"` aufgeteilt, um "Phantom-Knoten" zu vermeiden und Multigraph-Support zu ermöglichen.
|
||||||
|
* **Atomic Section Logic (v3.9.9):** Chunking-Verfahren, das Sektions-Überschriften und deren Inhalte atomar in Chunks hält (Pack-and-Carry-Over). Verhindert, dass Überschriften über Chunk-Grenzen hinweg getrennt werden.
|
||||||
|
* **Registry-First Profiling (v2.13.12):** Hierarchische Auflösung des Chunking-Profils: Frontmatter > types.yaml Typ-Config > Global Defaults. Stellt sicher, dass Note-Typen automatisch das korrekte Profil erhalten.
|
||||||
|
* **Mixture of Experts (MoE) - WP-25a:** Profilbasierte Experten-Architektur, bei der jede Systemaufgabe (Synthese, Ingestion-Validierung, Routing, Kompression) einem dedizierten Profil zugewiesen wird, das Modell, Provider und Parameter unabhängig von der globalen Konfiguration definiert.
|
||||||
|
* **LLM-Profil:** Zentrale Definition in `llm_profiles.yaml`, die Provider, Modell, Temperature und Fallback-Profil für eine spezifische Aufgabe festlegt (z.B. `synthesis_pro`, `tech_expert`, `ingest_validator`).
|
||||||
|
* **Fallback-Kaskade (WP-25a):** Rekursive Fallback-Logik, bei der bei Fehlern automatisch auf das `fallback_profile` umgeschaltet wird, bis der terminale Endpunkt (`identity_safe`) erreicht wird. Schutz gegen Zirkel-Referenzen via `visited_profiles`-Tracking.
|
||||||
|
* **Pre-Synthesis Kompression (WP-25a):** Asynchrone Verdichtung überlanger Wissens-Streams vor der Synthese, um Token-Verbrauch zu reduzieren und die Synthese zu beschleunigen. Nutzt `compression_profile` (z.B. `compression_fast`).
|
||||||
|
* **Profilgesteuerte Validierung (WP-25a):** Semantische Kanten-Validierung in der Ingestion erfolgt zwingend über das MoE-Profil `ingest_validator` (Temperature 0.0 für Determinismus), unabhängig von der globalen Provider-Konfiguration.
|
||||||
|
* **Lazy-Prompt-Orchestration (WP-25b):** Hierarchisches Prompt-Resolution-System, das Prompts erst im Moment des Modellaustauschs lädt, basierend auf dem exakt aktiven Modell. Ermöglicht modell-spezifisches Tuning und maximale Resilienz bei Modell-Fallbacks.
|
||||||
|
* **Hierarchische Prompt-Resolution (WP-25b):** Dreistufige Auflösungs-Logik: Level 1 (Modell-ID) → Level 2 (Provider) → Level 3 (Default). Gewährleistet, dass jedes Modell das optimale Template erhält.
|
||||||
|
* **PROMPT-TRACE (WP-25b):** Logging-Mechanismus, der die genutzte Prompt-Auflösungs-Ebene protokolliert (`🎯 Level 1`, `📡 Level 2`, `⚓ Level 3`). Bietet vollständige Transparenz über die genutzten Instruktionen.
|
||||||
|
* **Ultra-robustes Intent-Parsing (WP-25b):** Regex-basierter Intent-Parser in der DecisionEngine, der Modell-Artefakte wie `[/S]`, `</s>` oder Newlines zuverlässig bereinigt, um präzises Strategie-Routing zu gewährleisten.
|
||||||
|
* **Differenzierte Ingestion-Validierung (WP-25b):** Unterscheidung zwischen transienten Fehlern (Netzwerk, Timeout) und permanenten Fehlern (Config, Validation). Transiente Fehler erlauben die Kante (Datenverlust vermeiden), permanente Fehler lehnen sie ab (Graph-Qualität schützen).
|
||||||
|
* **Phase 3 Agentic Edge Validation (WP-24c v4.5.8):** Finales Validierungs-Gate für alle Kanten mit `candidate:` Präfix. Nutzt LLM-basierte semantische Prüfung zur Verifizierung von Wissensverknüpfungen. Verhindert "Geister-Verknüpfungen" und sichert die Graph-Qualität gegen Fehlinterpretationen ab.
|
||||||
|
* **candidate: Präfix (WP-24c v4.5.8):** Markierung für unbestätigte Kanten in `rule_id` oder `provenance`. Alle Kanten mit diesem Präfix werden in Phase 3 dem LLM-Validator vorgelegt. Nach erfolgreicher Validierung wird das Präfix entfernt.
|
||||||
|
* **verified Status (WP-24c v4.5.8):** Impliziter Status für Kanten nach erfolgreicher Phase 3 Validierung. Kanten ohne `candidate:` Präfix gelten als verifiziert und werden in die Datenbank geschrieben.
|
||||||
|
* **Note-Scope (WP-24c v4.2.0):** Globale Verbindungen, die der gesamten Note zugeordnet werden (nicht nur einem spezifischen Chunk). Wird durch spezielle Header-Zonen (z.B. `## Smart Edges`) definiert. In Phase 3 Validierung wird `note_summary` oder `note_text` als Kontext verwendet.
|
||||||
|
* **Chunk-Scope (WP-24c v4.2.0):** Lokale Verbindungen, die einem spezifischen Textabschnitt (Chunk) zugeordnet werden. In Phase 3 Validierung wird der spezifische Chunk-Text als Kontext verwendet, falls verfügbar.
|
||||||
|
* **Kontext-Optimierung (WP-24c v4.5.8):** Dynamische Kontext-Auswahl in Phase 3 Validierung basierend auf `scope`. Note-Scope nutzt aggregierten Note-Text, Chunk-Scope nutzt spezifischen Chunk-Text. Optimiert die Validierungs-Genauigkeit durch passenden Kontext.
|
||||||
|
* **rejected_edges (WP-24c v4.5.8):** Liste von Kanten, die in Phase 3 Validierung abgelehnt wurden. Diese Kanten werden **nicht** in die Datenbank geschrieben und vollständig ignoriert. Verhindert persistente "Geister-Verknüpfungen" im Wissensgraphen.
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
---
|
||||||
|
doc_type: quality_assurance
|
||||||
|
audience: all
|
||||||
|
status: active
|
||||||
|
version: 4.5.8
|
||||||
|
context: "Qualitätsprüfung der Dokumentation für alle Rollen: Vollständigkeit, Korrektheit und Anwendbarkeit. Inkludiert WP-24c Phase 3 Agentic Edge Validation, automatische Spiegelkanten und Note-Scope Zonen."
|
||||||
|
---
|
||||||
|
|
||||||
|
# Dokumentations-Qualitätsprüfung
|
||||||
|
|
||||||
|
Diese Checkliste dient zur systematischen Prüfung, ob die Dokumentation alle Fragen jeder Rolle vollständig beantwortet.
|
||||||
|
|
||||||
|
## ✅ Entwickler
|
||||||
|
|
||||||
|
### Setup & Installation
|
||||||
|
- [x] **Lokales Setup:** [Developer Guide](../05_Development/05_developer_guide.md#6-lokales-setup-development)
|
||||||
|
- [x] **Schnellstart:** [Quickstart](00_quickstart.md)
|
||||||
|
- [x] **Hardware-Anforderungen:** [Admin Operations](../04_Operations/04_admin_operations.md#11-voraussetzungen)
|
||||||
|
|
||||||
|
### Architektur & Code
|
||||||
|
- [x] **Modulare Struktur:** [Developer Guide - Architektur](../05_Development/05_developer_guide.md#4-projektstruktur--modul-referenz-deep-dive)
|
||||||
|
- [x] **Design-Patterns:** [Architektur-Patterns](../02_concepts/02_concept_architecture_patterns.md)
|
||||||
|
- [x] **API-Referenz:** [API Reference](../03_Technical_References/03_tech_api_reference.md)
|
||||||
|
- [x] **Datenmodell:** [Data Model](../03_Technical_References/03_tech_data_model.md)
|
||||||
|
|
||||||
|
### Entwicklung & Erweiterung
|
||||||
|
- [x] **Workflow:** [Developer Guide - Workflow](../05_Development/05_developer_guide.md#7-der-entwicklungs-zyklus-workflow)
|
||||||
|
- [x] **Erweiterungs-Guide:** [Teach-the-AI](../05_Development/05_developer_guide.md#8-erweiterungs-guide-teach-the-ai)
|
||||||
|
- [x] **GenAI Best Practices:** [GenAI Best Practices](../05_Development/05_genai_best_practices.md)
|
||||||
|
|
||||||
|
### Testing
|
||||||
|
- [x] **Test-Strategien:** [Testing Guide](../05_Development/05_testing_guide.md)
|
||||||
|
- [x] **Test-Frameworks:** [Testing Guide - Frameworks](../05_Development/05_testing_guide.md#3-test-frameworks--tools)
|
||||||
|
- [x] **Test-Daten:** [Testing Guide - Test-Daten](../05_Development/05_testing_guide.md#2-test-daten--vaults)
|
||||||
|
|
||||||
|
### Debugging & Troubleshooting
|
||||||
|
- [x] **Troubleshooting:** [Developer Guide - Troubleshooting](../05_Development/05_developer_guide.md#10-troubleshooting--one-liners)
|
||||||
|
- [x] **Debug-Tools:** [Testing Guide - Debugging](../05_Development/05_testing_guide.md#7-debugging--diagnose)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Administratoren
|
||||||
|
|
||||||
|
### Installation & Setup
|
||||||
|
- [x] **Installation:** [Admin Operations](../04_Operations/04_admin_operations.md#1-installation--setup)
|
||||||
|
- [x] **Docker Setup:** [Admin Operations - Qdrant](../04_Operations/04_admin_operations.md#12-qdrant-docker)
|
||||||
|
- [x] **Systemd Services:** [Admin Operations - Deployment](../04_Operations/04_admin_operations.md#2-deployment-systemd-services)
|
||||||
|
|
||||||
|
### Betrieb & Wartung
|
||||||
|
- [x] **Monitoring:** [Admin Operations - Wartung](../04_Operations/04_admin_operations.md#3-wartung--monitoring)
|
||||||
|
- [x] **Backup & Restore:** [Admin Operations - Backup](../04_Operations/04_admin_operations.md#4-backup--restore)
|
||||||
|
- [x] **Troubleshooting:** [Admin Operations - Troubleshooting](../04_Operations/04_admin_operations.md#33-troubleshooting-guide)
|
||||||
|
|
||||||
|
### Server-Betrieb
|
||||||
|
- [x] **Server-Konfiguration:** [Server Operations Manual](../04_Operations/04_server_operation_manual.md)
|
||||||
|
- [x] **Disaster Recovery:** [Server Operations - DR](../04_Operations/04_server_operation_manual.md#5-disaster-recovery-wiederherstellung-two-stage-dr)
|
||||||
|
- [x] **Backup-Strategie:** [Server Operations - Backup](../04_Operations/04_server_operation_manual.md#4-backup-strategie-borgmatic)
|
||||||
|
|
||||||
|
### Konfiguration
|
||||||
|
- [x] **ENV-Variablen:** [Configuration Reference](../03_Technical_References/03_tech_configuration.md#1-environment-variablen-env)
|
||||||
|
- [x] **YAML-Configs:** [Configuration Reference - YAML](../03_Technical_References/03_tech_configuration.md#2-typ-registry-typesyaml)
|
||||||
|
- [x] **Phase 3 Validierung:** [Configuration Reference - ENV](../03_Technical_References/03_tech_configuration.md#1-environment-variablen-env) (MINDNET_LLM_VALIDATION_HEADERS, MINDNET_NOTE_SCOPE_ZONE_HEADERS)
|
||||||
|
- [x] **LLM-Profile:** [Configuration Reference - LLM Profiles](../03_Technical_References/03_tech_configuration.md#6-llm-profile-registry-llm_profilesyaml-v130)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Anwender
|
||||||
|
|
||||||
|
### Erste Schritte
|
||||||
|
- [x] **Schnellstart:** [Quickstart](00_quickstart.md)
|
||||||
|
- [x] **Was ist Mindnet:** [Vision & Strategie](00_vision_and_strategy.md)
|
||||||
|
- [x] **Grundlagen:** [Glossar](00_glossary.md)
|
||||||
|
|
||||||
|
### Nutzung
|
||||||
|
- [x] **Chat-Bedienung:** [Chat Usage Guide](../01_User_Manual/01_chat_usage_guide.md)
|
||||||
|
- [x] **Graph Explorer:** [Chat Usage Guide - Graph](../01_User_Manual/01_chat_usage_guide.md#22-modus--graph-explorer-cytoscape)
|
||||||
|
- [x] **Editor:** [Chat Usage Guide - Editor](../01_User_Manual/01_chat_usage_guide.md#23-modus--manueller-editor)
|
||||||
|
|
||||||
|
### Content-Erstellung
|
||||||
|
- [x] **Knowledge Design:** [Knowledge Design Manual](../01_User_Manual/01_knowledge_design.md)
|
||||||
|
- [x] **Authoring Guidelines:** [Authoring Guidelines](../01_User_Manual/01_authoring_guidelines.md)
|
||||||
|
- [x] **Obsidian-Integration:** [Obsidian Integration](../01_User_Manual/01_obsidian_integration_guide.md)
|
||||||
|
- [x] **Note-Scope Zonen:** [Note-Scope Zonen](../01_User_Manual/NOTE_SCOPE_ZONEN.md) (WP-24c v4.2.0)
|
||||||
|
- [x] **LLM-Validierung:** [LLM-Validierung von Links](../01_User_Manual/LLM_VALIDIERUNG_VON_LINKS.md) (WP-24c v4.5.8)
|
||||||
|
|
||||||
|
### Häufige Fragen
|
||||||
|
- [x] **Wie strukturiere ich Notizen?** → [Knowledge Design](../01_User_Manual/01_knowledge_design.md)
|
||||||
|
- [x] **Welche Note-Typen gibt es?** → [Knowledge Design - Typ-Referenz](../01_User_Manual/01_knowledge_design.md#31-typ-referenz--stream-logik)
|
||||||
|
- [x] **Wie verknüpfe ich Notizen?** → [Knowledge Design - Edges](../01_User_Manual/01_knowledge_design.md#4-edges--verlinkung)
|
||||||
|
- [x] **Wie nutze ich den Chat?** → [Chat Usage Guide](../01_User_Manual/01_chat_usage_guide.md)
|
||||||
|
- [x] **Was sind automatische Spiegelkanten?** → [Knowledge Design - Spiegelkanten](../01_User_Manual/01_knowledge_design.md#43-automatische-spiegelkanten-invers-logik---wp-24c-v458)
|
||||||
|
- [x] **Was ist Phase 3 Validierung?** → [Knowledge Design - Phase 3](../01_User_Manual/01_knowledge_design.md#44-explizite-vs-validierte-kanten-phase-3-validierung---wp-24c-v458)
|
||||||
|
- [x] **Was sind Note-Scope Zonen?** → [Note-Scope Zonen](../01_User_Manual/NOTE_SCOPE_ZONEN.md)
|
||||||
|
- [x] **Wann nutze ich explizite vs. validierte Links?** → [Knowledge Design - Explizite vs. Validierte](../01_User_Manual/01_knowledge_design.md#44-explizite-vs-validierte-kanten-phase-3-validierung---wp-24c-v458)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Tester
|
||||||
|
|
||||||
|
### Test-Strategien
|
||||||
|
- [x] **Test-Pyramide:** [Testing Guide - Strategien](../05_Development/05_testing_guide.md#1-test-strategie--ebenen)
|
||||||
|
- [x] **Unit Tests:** [Testing Guide - Unit Tests](../05_Development/05_testing_guide.md#11-unit-tests-pytest)
|
||||||
|
- [x] **Integration Tests:** [Testing Guide - Integration](../05_Development/05_testing_guide.md#12-integration-tests)
|
||||||
|
- [x] **E2E Tests:** [Testing Guide - E2E](../05_Development/05_testing_guide.md#13-e2e--smoke-tests)
|
||||||
|
|
||||||
|
### Test-Frameworks
|
||||||
|
- [x] **Pytest:** [Testing Guide - Frameworks](../05_Development/05_testing_guide.md#31-pytest-unit-tests)
|
||||||
|
- [x] **Unittest:** [Testing Guide - Unittest](../05_Development/05_testing_guide.md#32-unittest-e2e-tests)
|
||||||
|
- [x] **Shell-Skripte:** [Testing Guide - Shell](../05_Development/05_testing_guide.md#33-shell-skripte-e2e-roundtrip)
|
||||||
|
|
||||||
|
### Test-Daten & Tools
|
||||||
|
- [x] **Test-Vault erstellen:** [Testing Guide - Test-Daten](../05_Development/05_testing_guide.md#21-test-vault-erstellen)
|
||||||
|
- [x] **Test-Skripte:** [Developer Guide - Scripts](../05_Development/05_developer_guide.md#44-scripts--tooling-die-admin-toolbox)
|
||||||
|
- [x] **Test-Checkliste:** [Testing Guide - Checkliste](../05_Development/05_testing_guide.md#8-test-checkliste-für-pull-requests)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## ✅ Deployment
|
||||||
|
|
||||||
|
### Deployment-Prozesse
|
||||||
|
- [x] **Deployment-Guide:** [Deployment Guide](../04_Operations/04_deployment_guide.md)
|
||||||
|
- [x] **CI/CD Pipeline:** [Deployment Guide - CI/CD](../04_Operations/04_deployment_guide.md#9-cicd-pipeline-details)
|
||||||
|
- [x] **Rollout-Strategien:** [Deployment Guide - Rollout](../04_Operations/04_deployment_guide.md#4-rollout-strategien)
|
||||||
|
|
||||||
|
### Versionierung & Releases
|
||||||
|
- [x] **Version-Schema:** [Deployment Guide - Versionierung](../04_Operations/04_deployment_guide.md#51-version-schema)
|
||||||
|
- [x] **Release-Prozess:** [Deployment Guide - Release](../04_Operations/04_deployment_guide.md#52-release-prozess)
|
||||||
|
|
||||||
|
### Rollback & Recovery
|
||||||
|
- [x] **Rollback-Strategien:** [Deployment Guide - Rollback](../04_Operations/04_deployment_guide.md#6-rollback-strategien)
|
||||||
|
- [x] **Disaster Recovery:** [Server Operations - DR](../04_Operations/04_server_operation_manual.md#5-disaster-recovery-wiederherstellung-two-stage-dr)
|
||||||
|
|
||||||
|
### Pre/Post-Deployment
|
||||||
|
- [x] **Pre-Deployment Checkliste:** [Deployment Guide - Checkliste](../04_Operations/04_deployment_guide.md#7-pre-deployment-checkliste)
|
||||||
|
- [x] **Post-Deployment Validierung:** [Deployment Guide - Validierung](../04_Operations/04_deployment_guide.md#8-post-deployment-validierung)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 📊 Zusammenfassung
|
||||||
|
|
||||||
|
### Vollständigkeit nach Rolle
|
||||||
|
|
||||||
|
| Rolle | Abgedeckte Themen | Status |
|
||||||
|
| :--- | :--- | :--- |
|
||||||
|
| **Entwickler** | Setup, Architektur, Code, Testing, Debugging | ✅ Vollständig |
|
||||||
|
| **Administratoren** | Installation, Betrieb, Wartung, Backup, DR | ✅ Vollständig |
|
||||||
|
| **Anwender** | Nutzung, Content-Erstellung, Workflows | ✅ Vollständig |
|
||||||
|
| **Tester** | Test-Strategien, Frameworks, Tools | ✅ Vollständig |
|
||||||
|
| **Deployment** | CI/CD, Rollout, Versionierung, Rollback | ✅ Vollständig |
|
||||||
|
|
||||||
|
### Neue Dokumente
|
||||||
|
|
||||||
|
1. ✅ `05_testing_guide.md` - Umfassender Test-Guide
|
||||||
|
2. ✅ `04_deployment_guide.md` - Vollständiger Deployment-Guide
|
||||||
|
3. ✅ `02_concept_architecture_patterns.md` - Architektur-Patterns
|
||||||
|
4. ✅ `03_tech_api_reference.md` - API-Referenz
|
||||||
|
5. ✅ `00_quickstart.md` - Schnellstart-Anleitung
|
||||||
|
6. ✅ `README.md` - Dokumentations-Einstiegspunkt
|
||||||
|
|
||||||
|
### Aktualisierte Dokumente
|
||||||
|
|
||||||
|
1. ✅ `00_documentation_map.md` - Alle neuen Dokumente aufgenommen
|
||||||
|
2. ✅ `04_admin_operations.md` - Troubleshooting erweitert, Phase 3 Validierung dokumentiert
|
||||||
|
3. ✅ `05_developer_guide.md` - Modulare Struktur ergänzt, WP-24c Phase 3 dokumentiert
|
||||||
|
4. ✅ `03_tech_ingestion_pipeline.md` - Background Tasks dokumentiert, Phase 3 Agentic Validation hinzugefügt
|
||||||
|
5. ✅ `03_tech_configuration.md` - Fehlende ENV-Variablen ergänzt, WP-24c Konfiguration dokumentiert
|
||||||
|
6. ✅ `00_vision_and_strategy.md` - Design-Entscheidungen ergänzt
|
||||||
|
7. ✅ `01_knowledge_design.md` - Automatische Spiegelkanten, Phase 3 Validierung, Note-Scope Zonen dokumentiert
|
||||||
|
8. ✅ `02_concept_graph_logic.md` - Phase 3 Validierung, automatische Spiegelkanten, Note-Scope vs. Chunk-Scope dokumentiert
|
||||||
|
9. ✅ `03_tech_data_model.md` - candidate: Präfix, verified Status, virtual Flag dokumentiert
|
||||||
|
10. ✅ `NOTE_SCOPE_ZONEN.md` - Phase 3 Validierung integriert
|
||||||
|
11. ✅ `LLM_VALIDIERUNG_VON_LINKS.md` - Phase 3 statt global_pool, Kontext-Optimierung dokumentiert
|
||||||
|
12. ✅ `05_testing_guide.md` - WP-24c Test-Szenarien hinzugefügt
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Status:** ✅ Alle Rollen vollständig abgedeckt
|
||||||
|
**Letzte Prüfung:** 2025-01-XX
|
||||||
|
**Version:** 2.9.1
|
||||||
|
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
---
|
||||||
|
doc_type: quickstart_guide
|
||||||
|
audience: user, developer, admin
|
||||||
|
status: active
|
||||||
|
version: 2.9.1
|
||||||
|
context: "Schnellstart-Anleitung für neue Benutzer von Mindnet"
|
||||||
|
---
|
||||||
|
|
||||||
|
# Mindnet Schnellstart
|
||||||
|
|
||||||
|
Diese Anleitung hilft dir, in 15 Minuten mit Mindnet loszulegen.
|
||||||
|
|
||||||
|
## 🎯 Was ist Mindnet?
|
||||||
|
|
||||||
|
Mindnet ist ein **persönliches KI-Gedächtnis**, das:
|
||||||
|
- Dein Wissen in Markdown-Notizen speichert
|
||||||
|
- Semantisch verknüpft (Wissensgraph)
|
||||||
|
- Als intelligenter Dialogpartner agiert (RAG-Chat)
|
||||||
|
- **Lokal und privat** läuft (Privacy First)
|
||||||
|
|
||||||
|
## 📋 Voraussetzungen
|
||||||
|
|
||||||
|
- **Python 3.10+** installiert
|
||||||
|
- **Docker** installiert (für Qdrant)
|
||||||
|
- **Ollama** installiert (für lokale LLMs)
|
||||||
|
- Optional: **Obsidian** (für komfortables Schreiben)
|
||||||
|
|
||||||
|
## ⚡ Installation (5 Minuten)
|
||||||
|
|
||||||
|
### Schritt 1: Repository klonen
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone <repository-url> mindnet
|
||||||
|
cd mindnet
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 2: Virtuelle Umgebung erstellen
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m venv .venv
|
||||||
|
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 3: Abhängigkeiten installieren
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 4: Qdrant starten (Docker)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose up -d qdrant
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 5: Ollama-Modelle laden
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ollama pull phi3:mini
|
||||||
|
ollama pull nomic-embed-text
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 6: Konfiguration anpassen
|
||||||
|
|
||||||
|
Erstelle eine `.env`-Datei im Projektroot:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
QDRANT_URL=http://localhost:6333
|
||||||
|
MINDNET_OLLAMA_URL=http://localhost:11434
|
||||||
|
MINDNET_LLM_MODEL=phi3:mini
|
||||||
|
MINDNET_EMBEDDING_MODEL=nomic-embed-text
|
||||||
|
COLLECTION_PREFIX=mindnet
|
||||||
|
MINDNET_VAULT_ROOT=./vault
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🚀 Erste Schritte (5 Minuten)
|
||||||
|
|
||||||
|
### Schritt 1: Backend starten
|
||||||
|
|
||||||
|
```bash
|
||||||
|
uvicorn app.main:app --reload --port 8001
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 2: Frontend starten (neues Terminal)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
streamlit run app/frontend/ui.py --server.port 8501
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 3: Browser öffnen
|
||||||
|
|
||||||
|
Öffne `http://localhost:8501` im Browser.
|
||||||
|
|
||||||
|
### Schritt 4: Erste Notiz importieren
|
||||||
|
|
||||||
|
Erstelle eine Test-Notiz im `vault/` Ordner:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
id: 20250101-test
|
||||||
|
title: Meine erste Notiz
|
||||||
|
type: concept
|
||||||
|
status: active
|
||||||
|
---
|
||||||
|
|
||||||
|
# Meine erste Notiz
|
||||||
|
|
||||||
|
Dies ist eine Test-Notiz für Mindnet.
|
||||||
|
|
||||||
|
[[rel:related_to Mindnet]]
|
||||||
|
```
|
||||||
|
|
||||||
|
Dann importiere sie:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m scripts.import_markdown --vault ./vault --prefix mindnet --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 5: Erste Chat-Anfrage
|
||||||
|
|
||||||
|
Im Browser-Chat eingeben:
|
||||||
|
```
|
||||||
|
Was ist Mindnet?
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📚 Nächste Schritte
|
||||||
|
|
||||||
|
Nach dem Schnellstart empfehle ich:
|
||||||
|
|
||||||
|
1. **[Chat Usage Guide](../01_User_Manual/01_chat_usage_guide.md)** - Lerne die Chat-Funktionen kennen
|
||||||
|
2. **[Knowledge Design](../01_User_Manual/01_knowledge_design.md)** - Verstehe, wie du Notizen strukturierst
|
||||||
|
3. **[Authoring Guidelines](../01_User_Manual/01_authoring_guidelines.md)** - Lerne Best Practices für das Schreiben
|
||||||
|
|
||||||
|
## 🆘 Hilfe & Troubleshooting
|
||||||
|
|
||||||
|
**Problem:** Qdrant startet nicht
|
||||||
|
- **Lösung:** Prüfe, ob Docker läuft: `docker ps`
|
||||||
|
|
||||||
|
**Problem:** Ollama-Modell nicht gefunden
|
||||||
|
- **Lösung:** Prüfe mit `ollama list`, ob die Modelle geladen sind
|
||||||
|
|
||||||
|
**Problem:** Import schlägt fehl
|
||||||
|
- **Lösung:** Prüfe die Logs und stelle sicher, dass Qdrant läuft
|
||||||
|
|
||||||
|
Für detaillierte Troubleshooting-Informationen siehe [Admin Operations](../04_Operations/04_admin_operations.md#33-troubleshooting-guide).
|
||||||
|
|
||||||
|
## 🔗 Weitere Ressourcen
|
||||||
|
|
||||||
|
- **[Dokumentationskarte](00_documentation_map.md)** - Übersicht aller Dokumente
|
||||||
|
- **[Glossar](00_glossary.md)** - Wichtige Begriffe erklärt
|
||||||
|
- **[Vision & Strategie](00_vision_and_strategy.md)** - Die Philosophie hinter Mindnet
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Viel Erfolg mit Mindnet!** 🚀
|
||||||
|
|
||||||
@@ -65,3 +65,41 @@ Die folgenden Prinzipien steuern jede technische Entscheidung:
|
|||||||
|
|
||||||
10. **Local First & Privacy:**
|
10. **Local First & Privacy:**
|
||||||
Nutzung lokaler LLMs (Ollama) für Inference. Keine Daten verlassen den Server.
|
Nutzung lokaler LLMs (Ollama) für Inference. Keine Daten verlassen den Server.
|
||||||
|
|
||||||
|
11. **Modulare Architektur (WP-14):**
|
||||||
|
Core-Logik ist in spezialisierte Pakete unterteilt (`chunking/`, `database/`, `graph/`, `ingestion/`, `parser/`, `retrieval/`). Dies ermöglicht unabhängige Entwicklung, Testbarkeit und Wartbarkeit.
|
||||||
|
|
||||||
|
12. **Resilienz durch Kaskaden:**
|
||||||
|
System nutzt Provider-Kaskaden (Cloud → Rate-Limit-Handling → Lokaler Fallback) für hohe Verfügbarkeit. Deep Fallback erkennt auch kognitive Blockaden (Silent Refusals).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Design-Entscheidungen & Trade-offs
|
||||||
|
|
||||||
|
### 4.1 Qdrant als Vektor-DB
|
||||||
|
**Entscheidung:** Self-hosted Qdrant statt Managed Service (Pinecone/Weaviate)
|
||||||
|
**Gründe:** Privacy First, Open Source, lokale Kontrolle
|
||||||
|
**Trade-off:** Mehr Wartungsaufwand, aber vollständige Datenhoheit
|
||||||
|
|
||||||
|
### 4.2 Hybrid Retrieval
|
||||||
|
**Entscheidung:** Kombination von Vektor-Suche (Semantik) und Graph-Expansion
|
||||||
|
**Gründe:** Bessere Relevanz durch strukturelle Verbindungen
|
||||||
|
**Trade-off:** Höhere Komplexität, aber deutlich bessere Ergebnisse
|
||||||
|
|
||||||
|
### 4.3 Background Tasks statt Queue-System
|
||||||
|
**Entscheidung:** FastAPI Background Tasks statt Redis/RabbitMQ
|
||||||
|
**Gründe:** Einfacher Setup, ausreichend für Single-User
|
||||||
|
**Trade-off:** Keine Persistenz bei Server-Neustart, aber weniger Infrastruktur
|
||||||
|
|
||||||
|
### 4.4 Markdown als Source of Truth
|
||||||
|
**Entscheidung:** Markdown-Dateien sind primär, Datenbank ist Cache
|
||||||
|
**Gründe:** Versionierbarkeit, Editierbarkeit, Unabhängigkeit
|
||||||
|
**Trade-off:** Zwei Datenquellen, aber maximale Flexibilität
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Weitere Informationen
|
||||||
|
|
||||||
|
- **Architektur-Patterns:** Siehe [Architektur-Patterns](../02_concepts/02_concept_architecture_patterns.md)
|
||||||
|
- **Graph-Logik:** Siehe [Graph-Logik](../02_concepts/02_concept_graph_logic.md)
|
||||||
|
- **KI-Persönlichkeit:** Siehe [KI-Persönlichkeit](../02_concepts/02_concept_ai_personality.md)
|
||||||
@@ -3,7 +3,7 @@ id: 01-authoring-guidelines
|
|||||||
title: Authoring Guidelines – Handbuch für den Digitalen Zwilling
|
title: Authoring Guidelines – Handbuch für den Digitalen Zwilling
|
||||||
type: principle
|
type: principle
|
||||||
status: stable
|
status: stable
|
||||||
version: 1.1.0
|
version: 1.3.0
|
||||||
area: system_documentation
|
area: system_documentation
|
||||||
tags: [handbuch, authoring, methodik, obsidian, mindnet, best-practice]
|
tags: [handbuch, authoring, methodik, obsidian, mindnet, best-practice]
|
||||||
retriever_weight: 2.0
|
retriever_weight: 2.0
|
||||||
@@ -11,145 +11,121 @@ retriever_weight: 2.0
|
|||||||
|
|
||||||
# Authoring Guidelines: Dein Werkzeug für den Digitalen Zwilling
|
# Authoring Guidelines: Dein Werkzeug für den Digitalen Zwilling
|
||||||
|
|
||||||
|
Dieses Handbuch ist dein primäres Werkzeug, um Wissen so zu strukturieren, dass Mindnet deine Persönlichkeit spiegelt, empathisch reagiert und dich sowie deine Nachkommen strategisch berät. Es dient als Brücke zwischen deiner menschlichen Navigation in Obsidian und der technischen Logik der Mindnet-Engine.
|
||||||
Dieses Handbuch ist dein primäres Werkzeug, um Wissen so zu strukturieren, dass Mindnet deine Persönlichkeit spiegelt, empathisch reagiert und dich strategisch berät.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## ⚡ Die 6 Goldenen Regeln des Knowledge Designs
|
## ⚡ Die 6 Goldenen Regeln (TL;DR)
|
||||||
|
|
||||||
1. **Atomare Gedanken:** Eine Notiz = Ein Thema. Trenne z. B. „Meditation“ von „Mobility“, auch wenn beides Ich-Pflege ist.
|
1. **Atomare Gedanken:** Eine Notiz = Ein Thema. Trenne z. B. „Meditation“ von „Mobility“.
|
||||||
2. **Explizite Typen:** Nutze den `type`, um der KI zu sagen, wie sie den Text verarbeiten soll (z. B. `insight` für Beobachtungen, `experience` für Erlebnisse).
|
2. **Explizite Typen:** Nutze den `type` im Frontmatter (z. B. `insight`, `experience`, `value`), um die mathematische Gewichtung zu steuern.
|
||||||
3. **Semantische Links:** Verknüpfe aktiv mit `[[rel:depends_on ...]]` oder `[[rel:based_on ...]]`. Sag dem System, *warum* Dinge zusammenhängen.
|
3. **H3-Hub-Pairing (NEU):** Nutze H3-Überschriften in Hubs, um spezifische Links und ihre Bedeutung (Edges) in isolierten Chunks für die KI zu fixieren, ohne die Obsidian-Graphen-Logik zu brechen.
|
||||||
4. **Werte & Ziele definieren:** Erstelle für jeden Nordstern und jeden Kernwert eine eigene Notiz. Ohne Maßstäbe kann der „Berater“ nicht entscheiden.
|
4. **Werte & Ziele definieren:** Erstelle für jeden Kernwert eine eigene Notiz (`type: value`). Ohne explizite Maßstäbe kann die Decision Engine nicht in deinem Sinne abwägen.
|
||||||
5. **Emotionales Bridging:** Nutze Begriffe wie „Druck“, „Stolz“, „Euphorie“ oder „Hilflosigkeit“, um die Empathie-Ebene der KI zu aktivieren.
|
5. **Emotionales Bridging:** Nutze Begriffe wie „Druck“, „Faszination“ oder „Angst“, um die Empathie-Ebene der KI zu aktivieren.
|
||||||
6. **Narrative Tiefe (Fleisch am Knochen):** Dokumentiere das „Warum“ hinter einer Entscheidung. Fakten informieren, aber Erzählungen prägen den Charakter.
|
6. **Narrative Tiefe (Fleisch am Knochen):** Dokumentiere das „Warum“ hinter einer Entscheidung. Erzählungen prägen deinen Charakter für die Nachwelt mehr als reine Fakten.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 1. Strategische Steuerung (Status & Gewicht)
|
## 1. Die Vault-Architektur (Stream-Mapping)
|
||||||
|
|
||||||
Du entscheidest über das Frontmatter, wie präsent eine Information im „Gedächtnis“ ist.
|
Der Vault ist in acht funktionale Domänen unterteilt, die direkt mit den internen Wissens-Streams korrespondieren.
|
||||||
|
|
||||||
### 1.1 Status-Logik
|
| Ordner | Domäne | Stream-Logik | Zweck |
|
||||||
* **`stable`**: Gold-Standard. Für finale Leitbild-Texte und Nordsterne. Erhält +20% Relevanz-Bonus.
|
| :--- | :--- | :--- | :--- |
|
||||||
* **`active`**: Standard für laufende Projekte und aktuelle Beobachtungen.
|
| **00_Leitbild** | Verfassung | **Identity** | Chronik deiner Werte-Evolution über die Jahre. |
|
||||||
* **`draft`**: Brainstorming oder rohe Tages-Logs. Die KI nutzt diese nur nachrangig (50% Malus), um Rauschen zu vermeiden.
|
| **01_Identify** | Kern-Identität | **Identity** | SSOT für Werte, Prinzipien, Rollen, Bedürfnisse und Glaubenssätze. |
|
||||||
* **`system`**: Rein technische Dateien (Templates, Guides). Werden im Chat ignoriert.
|
| **02_Projects** | Dynamik | **Action** | Aktive Vorhaben, Missionen und operative Aufgaben (Tasks). |
|
||||||
|
| **03_Experiences** | Biografie | **History** | Speicherort für Erlebnisse (Experiences), Ereignisse (Events) und Zustände (States). |
|
||||||
### 1.2 Manuelle Gewichtung (`retriever_weight`)
|
| **04_Insights** | Erkenntnisse | **History/Basis** | Fachwissen, Konzepte, Ideen und tiefe Musteranalysen. |
|
||||||
* **Boost (1.20):** Für hochrelevante Erkenntnisse. **Beispiel:** Beobachtungen zum Verhalten von Rohan werden als `insight` mit `1.20` markiert, damit sie bei Erziehungsfragen sofort präsent sind.
|
| **05_Decisions** | Steuerung | **Action** | Dokumentation getroffener Entscheidungen (ADR-Logik). |
|
||||||
* **Deboost (0.50 - 0.80):** Für tägliche Routine-Einträge („Heute 10 Min. meditiert“). Sie dienen der Chronik, sollen aber keine tiefen Analysen verfälschen.
|
| **06_Skills** | Kompetenz | **Action** | Fertigkeiten, Lernpfade und Meisterschaftsnachweise. |
|
||||||
|
| **07_People** | Soziales Netz | **History/Basis** | Kontaktprofile, Rollen und soziale Vernetzung. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 2. Das Kochbuch: Praktische Use-Cases
|
## 2. Das Schicht-Modell & Hub-Design
|
||||||
|
|
||||||
### 2.1 Ein Erlebnis aufschreiben (`type: experience`)
|
In Hub-Notizen (z. B. „Wendepunkte“) nutzen wir eine hierarchische Schichtung, um Präzision für die KI und Übersicht für den Menschen zu garantieren.
|
||||||
**Ziel:** Den „Spiegel“ (Empathy) mit deiner Biografie kalibrieren.
|
|
||||||
|
|
||||||
**Struktur & Leitfragen:**
|
### 2.1 Die H3-Regel für präzises Pairing
|
||||||
- **Kontext:** Was ist passiert? (Sachlich kurz).
|
Um Kausalitäten (z. B. Ereignis A führte zu Gefühl B) ohne proprietäre Syntax abzubilden, wird jedes Hauptelement eines Hubs in eine **H3-Sektion** gefasst.
|
||||||
- **Emotions-Check:** Wie habe ich mich in der Situation gefühlt? (Wichtig für Regel 5).
|
* **Technischer Effekt:** Das System nutzt für Hubs das Profil `structured_smart_edges_strict_L3` und schneidet an jeder H3-Ebene einen sauberen Chunk.
|
||||||
- **Die Lektion:** Was habe ich über mich gelernt? (z. B. „Ich reagiere allergisch auf Ungerechtigkeit“).
|
* **Vorteil:** Callouts innerhalb dieser Sektion beziehen sich ausschließlich auf diesen Kontext, was die Antwortqualität massiv erhöht.
|
||||||
- **Deep-Edge:** Mit welcher Rolle ist das verknüpft? `[[rel:supports Meine Rollenlandkarte 2025#Vater]]`.
|
|
||||||
|
|
||||||
### 2.2 Eine Beobachtung festhalten (`type: insight`)
|
### 2.2 Die 3 Schichten im Detail
|
||||||
**Ziel:** Den „Berater“ (Decision) mit Mustern versorgen.
|
* **Ebene 1: Cluster (Hub)**: Der Navigator (`type: insight`, `status: stable`). Fasst Lebensphasen oder Themenwelten zusammen.
|
||||||
|
* **Ebene 2: Reflexion (Erlebnis)**: Die `experience`-Notiz. Die bewusste Aufarbeitung mit „Emotions-Check“ und „Lektion“.
|
||||||
|
* **Ebene 3: Evidenz (Faktum)**: `event` oder `state`-Notizen. Die atomaren Rohdaten und Gefühle des Augenblicks (Momentaufnahmen).
|
||||||
|
|
||||||
**Beispiel Rohan-Beobachtung:**
|
|
||||||
- **Beobachtung:** Rohan reagiert positiv auf „leise“ Impulse und 90/10 Coaching.
|
|
||||||
- **Interpretation:** Direkte Konfrontation erzeugt Gegendruck; Fragen erzeugen Ownership.
|
|
||||||
- **Konsequenz:** Prinzip P3a (Familienregeln) muss immer einen Bedürfnischeck vorausschicken.
|
|
||||||
|
|
||||||
### 2.3 Ein Review durchführen (`type: journal` / `insight`)
|
|
||||||
**Ziel:** Den Fortschritt steuern.
|
|
||||||
- **Daily:** Rohes Log (Status `draft`).
|
|
||||||
- **Weekly/Monthly:** Verdichtung der Erkenntnisse in eine `stable` Notiz. Frage: „Was war der größte Hebel diese Woche?“.
|
|
||||||
|
|
||||||
### 2.4 Das Handlungsprinzip (`type: principle`)
|
|
||||||
**Ziel:** Testbare Regeln für schwierige Entscheidungssituationen.
|
|
||||||
|
|
||||||
**Struktur-Vorgabe:**---
|
|
||||||
id: 01-authoring-guidelines
|
|
||||||
title: Authoring Guidelines – Das Handbuch für den Digitalen Zwilling
|
|
||||||
type: principle
|
|
||||||
status: stable
|
|
||||||
version: 1.2.0
|
|
||||||
area: system_documentation
|
|
||||||
tags: [handbuch, authoring, methodik, obsidian, mindnet, best-practice]
|
|
||||||
retriever_weight: 2.0
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🕸️ Teil 3: Netzdesign (Hubs, Edges & Lücken)
|
## 3. Strategische Steuerung (Status & Gewicht)
|
||||||
|
|
||||||
|
Du entscheidest über das Frontmatter, wie präsent eine Information im „Gedächtnis“ des Systems ist.
|
||||||
|
|
||||||
|
* **`status: stable`**: Gold-Standard (+20% Relevanz-Bonus). Für finales Leitbild-Wissen und Kernwerte.
|
||||||
|
* **`status: active`**: Standard für laufende Projekte und verifizierte Erlebnisse.
|
||||||
|
* **`status: draft`**: Brainstorming oder rohe Tages-Logs. Erhält einen Malus (50%), um Rauschen zu vermeiden.
|
||||||
|
* **`retriever_weight`**: Nutze `1.2` für Hubs und `1.1` für prägende Erlebnisse.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Netzdesign & Semantic Mapping
|
||||||
|
|
||||||
Ein intelligentes Netz wächst durch strategische Verknüpfungen, nicht durch Textmenge.
|
Ein intelligentes Netz wächst durch strategische Verknüpfungen, nicht durch Textmenge.
|
||||||
|
|
||||||
### 3.1 Wissens-Hubs (Zentralnotizen)
|
### 4.1 Zentrale Kanten (Edges)
|
||||||
Hubs fungieren als Verteilerzentren im Graphen.
|
Nutze das kanonische Vokabular in `[!edge]` Callouts innerhalb der H3-Sektionen:
|
||||||
* **Struktur:** Nutze Überschriften (H2, H3) und verlinke von dort auf Detailnotizen.
|
* **`resulted_in` / `erzeugt`**: Verbindung zu einem daraus entstandenen Wert oder Gefühl.
|
||||||
* **Deep-Edges:** Verlinke präzise Abschnitte: `[[Rollenlandkarte#Vater]]`. Dies ermöglicht der KI, nur den relevanten Kontext zu laden.
|
* **`caused_by` / `wegen`**: Dokumentiert die Ursache einer emotionalen Prägung oder Entscheidung.
|
||||||
|
* **`part_of` / `gehört_zu`**: Bindet Details an einen übergeordneten Cluster oder Hub.
|
||||||
|
* **`guides` / `steuert`**: Prinzipien oder Werte, die eine Sektion oder ein Vorhaben leiten.
|
||||||
|
|
||||||
### 3.2 Forward-Mapping (Strategische Lücken)
|
### 4.2 Forward-Mapping (Strategische Lücken)
|
||||||
Setze bewusst Links auf Dateien, die noch nicht existieren (z. B. `[[Die beste Version meiner selbst]]`).
|
Setze bewusst Links auf Dateien, die noch nicht existieren (z. B. `[[Die beste Version meiner selbst]]`). Die KI erkennt diese Lücken und stellt proaktiv Fragen, um diese Felder gemeinsam mit dir zu füllen.
|
||||||
* **Zweck:** Du signalisierst dem System: „Hier entsteht ein wichtiges Konzept“.
|
|
||||||
* **Effekt:** Die KI erkennt diese Lücken und kann im Chat proaktiv Fragen stellen, um diese Felder mit dir gemeinsam zu füllen.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 4. Obsidian Usability & Automatisierung
|
## 5. Das Kochbuch: Praktische Use-Cases
|
||||||
|
|
||||||
### 4.1 Templater-Integration
|
### 5.1 Ein Erlebnis aufschreiben (`type: experience`)
|
||||||
Nutze Vorlagen, die bereits die **Leitfragen** und das richtige **Retriever-Weight** enthalten. Ein Klick auf „Neues Erlebnis“ sollte automatisch das Frontmatter mit `type: experience` und `weight: 1.1` füllen.
|
**Ziel:** Den „Spiegel“ (Empathy) mit deiner Biografie kalibrieren.
|
||||||
|
* **Struktur:** Kontext (Was ist passiert?), Emotions-Check (Gefühle?), Lektion (Was gelernt?).
|
||||||
|
* **Deep-Edge:** Verknüpfe es immer mit einer Rolle: `[[rel:supports Meine Rollenlandkarte 2025#Vater]]`.
|
||||||
|
|
||||||
### 4.2 Meta Bind Dashboards
|
### 5.2 Eine Beobachtung festhalten (`type: insight`)
|
||||||
Nutze **Meta Bind**, um Felder wie `status` oder `retriever_weight` über Schieberegler direkt in der Notiz zu steuern. Das macht die Gewichtung deines Wissens intuitiv und spielerisch.
|
**Ziel:** Den „Berater“ (Decision) mit Mustern versorgen.
|
||||||
|
* **Beispiel:** "Beobachtung: Rohan reagiert positiv auf leise Impulse" -> Konsequenz: Prinzip Bedürfnischeck.
|
||||||
|
|
||||||
|
### 5.3 Das Handlungsprinzip (`type: principle`)
|
||||||
|
**Ziel:** Testbare Regeln für schwierige Situationen.
|
||||||
|
* **Struktur:** Das Prinzip, Anwendung & Beispiele, Wächterfrage (Was frage ich mich im Moment der Entscheidung?).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 5. Vernetzung für die Mindnet-Personas
|
## 6. Obsidian Usability & Automatisierung
|
||||||
|
|
||||||
|
### 6.1 Templater & Meta Bind
|
||||||
|
* **Automatisierung:** Nutze Vorlagen, die bereits die Leitfragen und das richtige `retriever_weight` enthalten.
|
||||||
|
* **Interaktive Steuerung:** Nutze Meta Bind, um Felder wie `status` oder `retriever_weight` über Schieberegler direkt in der Notiz zu steuern.
|
||||||
|
|
||||||
|
### 6.2 Deep-Edges & Verknüpfung
|
||||||
|
* Verlinke Erlebnisse konsequent mit Rollen, Personen oder Clustern.
|
||||||
|
* Nutze `derived_from`, um Prinzipien an ihre Ursprungssituation zu binden.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Vernetzung für die Mindnet-Personas
|
||||||
|
|
||||||
| Persona | Notiz-Fokus | Effekt im Dialog |
|
| Persona | Notiz-Fokus | Effekt im Dialog |
|
||||||
| :--- | :--- | :--- |
|
| :--- | :--- | :--- |
|
||||||
| **🪞 Spiegel (Empathy)** | Gefühle & Biografisches | „Ich verstehe deinen Frust, das war bei Projekt X ähnlich...“. |
|
| **🪞 Spiegel (Empathy)** | `states`, `journal`, `experience` | Erzeugt Resonanz durch Nachempfinden deiner Gefühle. |
|
||||||
| **⚖️ Berater (Decision)** | Werte & Wächterfragen | „Option A ist nicht ratsam, da sie gegen dein Prinzip der Integrität verstößt“. |
|
| **⚖️ Berater (Decision)** | `values`, `principles`, `decisions` | Wägt aktuelle Fragen gegen deine lebenslangen Maßstäbe ab. |
|
||||||
| **📚 Bibliothekar (Facts)** | Struktur & Definitionen | „Deine Mission als Vater ist es, verlässliche Präsenz zu zeigen...“. |
|
| **📚 Bibliothekar (Facts)** | `events`, `concepts`, `sources`, `person` | Liefert präzise Fakten, historische Daten und soziale Kontexte. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
> [!abstract] Fazit für den Autor
|
## 8. Verbindungen von Notiztypen (Graphen-Logik)
|
||||||
> Mindnet ist ein **Persönlichkeitsspiegel**. Dokumentiere weniger „Technik“ und mehr „Mensch“. Jede Notiz sollte die Frage beantworten: „Was sagt das über mich und meine Werte aus?“.
|
|
||||||
- **3 Signale:** Woran merke ich im Alltag, dass ich das Prinzip lebe?.
|
|
||||||
- **Wächterfrage:** Welche Frage stelle ich mir im Moment der Entscheidung?.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 6. Obsidian Workflow-Hacks
|
|
||||||
|
|
||||||
### 6.1 Templater & Meta Bind
|
|
||||||
- **Automatisierung:** Nutze das **Templater-Plugin**, um beim Erstellen einer Notiz sofort die passenden Leitfragen und das richtige `retriever_weight` einzufügen.
|
|
||||||
- **Interaktive Steuerung:** Nutze **Meta Bind**, um Felder wie `status` oder `retriever_weight` über Buttons oder Slider direkt im Lesemodus zu ändern, ohne im YAML-Code zu tippen.
|
|
||||||
|
|
||||||
### 6.2 Deep-Edges & Verknüpfung
|
|
||||||
- Verknüpfe Erlebnisse immer mit der entsprechenden Rolle: `[[rel:supports Meine Rollenlandkarte 2025#Vater]]`.
|
|
||||||
- Nutze `derived_from`, um Prinzipien an ihre Ursprungs-Session zu binden.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 7. Vernetzung für Mindnet Personas
|
|
||||||
|
|
||||||
| Persona | Notiz-Fokus | Beispiel |
|
|
||||||
| :--- | :--- | :--- |
|
|
||||||
| **🪞 Spiegel (Empathy)** | Emotionen & Prägungen | „Ich fühlte mich hilflos, als...“. |
|
|
||||||
| **⚖️ Berater (Decision)** | Kriterien & Abwägungen | „Option A verletzt Wert X, weil...“. |
|
|
||||||
| **📚 Bibliothekar (Facts)** | Struktur & Definition | „Rollenmission Vater bedeutet...“. |
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
> [!tip] Best Practice
|
|
||||||
> Wenn du merkst, dass eine Notiz zu technisch wird: Halte inne und frage dich: „Was macht das mit mir als Mensch?“ Schreibe *das* auf. Das ist die Essenz für deinen KI-Zwilling.
|
|
||||||
|
|
||||||
## 8. Verbindungen von Notiztypen
|
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
graph TD
|
graph TD
|
||||||
@@ -186,4 +162,3 @@ graph TD
|
|||||||
style V fill:#f9f,stroke:#333,stroke-width:2px
|
style V fill:#f9f,stroke:#333,stroke-width:2px
|
||||||
style EX fill:#bbf,stroke:#333,stroke-width:2px
|
style EX fill:#bbf,stroke:#333,stroke-width:2px
|
||||||
style P fill:#bfb,stroke:#333,stroke-width:2px
|
style P fill:#bfb,stroke:#333,stroke-width:2px
|
||||||
|
|
||||||
@@ -1,25 +1,29 @@
|
|||||||
---
|
---
|
||||||
doc_type: user_manual
|
doc_type: user_manual
|
||||||
audience: user, mindmaster
|
audience: user, mindmaster
|
||||||
scope: chat, ui, feedback, graph
|
scope: chat, ui, feedback, graph, agentic_validation
|
||||||
status: active
|
status: active
|
||||||
version: 2.6
|
version: 4.5.8
|
||||||
context: "Anleitung zur Nutzung der Web-Oberfläche, der Chat-Personas und des Graph Explorers."
|
context: "Anleitung zur Nutzung der Web-Oberfläche, der Chat-Personas, Multi-Stream RAG und des Graph Explorers. Inkludiert WP-24c Chunk-Aware Multigraph-System und automatische Spiegelkanten."
|
||||||
---
|
---
|
||||||
|
|
||||||
# Chat & Graph Usage Guide
|
# Chat & Graph Usage Guide
|
||||||
|
|
||||||
**Quellen:** `user_guide.md`
|
**Quellen:** `user_guide.md`
|
||||||
|
|
||||||
|
> 💡 **Tipp für Einsteiger:** Falls du noch nie mit Mindnet gearbeitet hast, starte mit dem [Schnellstart](../00_General/00_quickstart.md) und dem [Knowledge Design Manual](01_knowledge_design.md), um die Grundlagen zu verstehen.
|
||||||
|
|
||||||
## 1. Was Mindnet für dich tut
|
## 1. Was Mindnet für dich tut
|
||||||
|
|
||||||
Mindnet ist ein **assoziatives Gedächtnis** mit Persönlichkeit. Es unterscheidet sich von einer reinen Suche dadurch, dass es **kontextsensitiv** agiert.
|
Mindnet ist ein **assoziatives Gedächtnis** mit Persönlichkeit. Es unterscheidet sich von einer reinen Suche dadurch, dass es **kontextsensitiv** agiert.
|
||||||
|
|
||||||
**Das Gedächtnis (Der Graph):**
|
**Das Gedächtnis (Der Graph - Chunk-Aware Multigraph):**
|
||||||
Wenn du nach "Projekt Alpha" suchst, findet Mindnet auch:
|
Wenn du nach "Projekt Alpha" suchst, findet Mindnet auch:
|
||||||
* **Abhängigkeiten:** "Technologie X wird benötigt".
|
* **Abhängigkeiten:** "Technologie X wird benötigt".
|
||||||
* **Entscheidungen:** "Warum nutzen wir X?".
|
* **Entscheidungen:** "Warum nutzen wir X?".
|
||||||
* **Ähnliches:** "Projekt Beta war ähnlich".
|
* **Ähnliches:** "Projekt Beta war ähnlich".
|
||||||
|
* **Beide Richtungen:** Dank automatischer Spiegelkanten findest du auch Notizen, die auf "Projekt Alpha" verweisen (z.B. "Projekt Beta enforced_by: Projekt Alpha").
|
||||||
|
* **Präzise Abschnitte:** Deep-Links zu spezifischen Abschnitten (`[[Note#Section]]`) ermöglichen präzise Verknüpfungen innerhalb langer Dokumente.
|
||||||
|
|
||||||
**Der Zwilling (Die Personas):**
|
**Der Zwilling (Die Personas):**
|
||||||
Mindnet passt seinen Charakter an: Mal ist es der neutrale Bibliothekar, mal der strategische Berater, mal der empathische Spiegel.
|
Mindnet passt seinen Charakter an: Mal ist es der neutrale Bibliothekar, mal der strategische Berater, mal der empathische Spiegel.
|
||||||
@@ -58,21 +62,49 @@ Ein Editor mit **"File System First"** Garantie.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 3. Den Chat steuern (Intents)
|
## 3. Den Chat steuern (Intents & Multi-Stream RAG)
|
||||||
|
|
||||||
Du steuerst die Persönlichkeit von Mindnet durch deine Wortwahl. Der **Hybrid Router v5** unterscheidet intelligent:
|
Du steuerst die Persönlichkeit von Mindnet durch deine Wortwahl. Seit WP-25 nutzt Mindnet **Agentic Multi-Stream RAG**, das deine Anfrage in parallele Wissens-Streams aufteilt:
|
||||||
|
|
||||||
### 3.1 Frage-Modus (Wissen abrufen)
|
### 3.1 Intent-Erkennung (Hybrid-Router)
|
||||||
Ausgelöst durch `?` oder W-Wörter.
|
|
||||||
|
Der Router erkennt deine Absicht auf zwei Wegen:
|
||||||
|
|
||||||
|
**Schnelle Erkennung (Keyword Fast-Path):**
|
||||||
|
* **"Soll ich..."** → Sofortige Erkennung als `DECISION` (Berater)
|
||||||
|
* **"Wann..."** → Sofortige Erkennung als `FACT_WHEN` (Zeitpunkte)
|
||||||
|
* **"Was ist..."** → Sofortige Erkennung als `FACT_WHAT` (Wissen)
|
||||||
|
* **"Ich fühle..."** → Sofortige Erkennung als `EMPATHY` (Spiegel)
|
||||||
|
|
||||||
|
**Intelligente Analyse (LLM Slow-Path):**
|
||||||
|
* Bei unklaren Anfragen analysiert die KI semantisch deine Absicht
|
||||||
|
|
||||||
|
### 3.2 Multi-Stream Retrieval (WP-25)
|
||||||
|
|
||||||
|
Anstelle einer einzelnen Suche führt Mindnet nun **parallele Abfragen** in spezialisierten Wissens-Streams aus:
|
||||||
|
|
||||||
|
**Die Streams:**
|
||||||
|
* **Values Stream:** Deine Identität, Ethik und Prinzipien (`value`, `principle`, `belief`)
|
||||||
|
* **Facts Stream:** Operative Daten zu Projekten, Tasks und Status (`project`, `decision`, `task`)
|
||||||
|
* **Biography Stream:** Persönliche Erfahrungen und Journal-Einträge (`experience`, `journal`)
|
||||||
|
* **Risk Stream:** Hindernisse und potenzielle Gefahren (`risk`, `obstacle`)
|
||||||
|
* **Tech Stream:** Technisches Wissen, Code und Dokumentation (`concept`, `source`, `glossary`)
|
||||||
|
|
||||||
|
**Vorteil:** Jeder Stream fokussiert auf spezifische Wissensbereiche, was zu präziseren und kontextreicheren Antworten führt.
|
||||||
|
|
||||||
|
### 3.3 Frage-Modi (Strategien)
|
||||||
|
|
||||||
* **Entscheidung ("Soll ich?"):** Der **Berater**.
|
* **Entscheidung ("Soll ich?"):** Der **Berater**.
|
||||||
* Mindnet lädt deine Werte (`type: value`) und Ziele (`type: goal`).
|
* Nutzt: Values Stream, Facts Stream, Risk Stream
|
||||||
|
* Wägt Fakten gegen deine Werte ab und evaluiert Risiken
|
||||||
* *Beispiel:* "Soll ich Tool X nutzen?" -> "Nein, Tool X speichert Daten in den USA. Das verstößt gegen dein Prinzip 'Privacy First'."
|
* *Beispiel:* "Soll ich Tool X nutzen?" -> "Nein, Tool X speichert Daten in den USA. Das verstößt gegen dein Prinzip 'Privacy First'."
|
||||||
* **Empathie ("Ich fühle..."):** Der **Spiegel**.
|
* **Empathie ("Ich fühle..."):** Der **Spiegel**.
|
||||||
* Mindnet lädt deine Erfahrungen (`type: experience`).
|
* Nutzt: Biography Stream, Values Stream
|
||||||
|
* Greift auf deine Erfahrungen und Werte zurück
|
||||||
* *Beispiel:* "Ich bin frustriert." -> "Das erinnert mich an Projekt Y, da ging es uns ähnlich..."
|
* *Beispiel:* "Ich bin frustriert." -> "Das erinnert mich an Projekt Y, da ging es uns ähnlich..."
|
||||||
* **Fakten ("Was ist?"):** Der **Bibliothekar**.
|
* **Fakten ("Was ist?", "Wann..."):** Der **Bibliothekar**.
|
||||||
* Liefert präzise Definitionen.
|
* Nutzt: Facts Stream, Tech Stream, Biography Stream
|
||||||
|
* Liefert präzise Definitionen und zeitliche Informationen
|
||||||
|
|
||||||
### 3.2 Befehls-Modus (Interview)
|
### 3.2 Befehls-Modus (Interview)
|
||||||
Ausgelöst durch Aussagen wie "Neues Projekt", "Ich will festhalten".
|
Ausgelöst durch Aussagen wie "Neues Projekt", "Ich will festhalten".
|
||||||
@@ -82,13 +114,17 @@ Ausgelöst durch Aussagen wie "Neues Projekt", "Ich will festhalten".
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 4. Ergebnisse interpretieren (Explanation Layer)
|
## 4. Ergebnisse interpretieren (Explanation Layer & Stream-Tracing)
|
||||||
|
|
||||||
Mindnet liefert eine **Begründung** ("Reasons"), warum es etwas gefunden hat.
|
Mindnet liefert eine **Begründung** ("Reasons"), warum es etwas gefunden hat.
|
||||||
Öffne eine Quellen-Karte, um zu sehen:
|
Öffne eine Quellen-Karte, um zu sehen:
|
||||||
* *"Hohe textuelle Übereinstimmung."* (Semantik)
|
* *"Hohe textuelle Übereinstimmung."* (Semantik)
|
||||||
* *"Bevorzugt aufgrund des Typs 'decision'."* (Wichtigkeit)
|
* *"Bevorzugt aufgrund des Typs 'decision'."* (Wichtigkeit)
|
||||||
* *"Verweist auf 'Projekt X' via 'depends_on'."* (Graph-Kontext)
|
* *"Verweist auf 'Projekt X' via 'depends_on'."* (Graph-Kontext)
|
||||||
|
* *"Quelle: Values Stream"* (Stream-Tracing - WP-25)
|
||||||
|
|
||||||
|
**Stream-Tracing (WP-25):**
|
||||||
|
Jede Quelle wird mit ihrem Ursprungs-Stream markiert (z.B. "Values Stream", "Facts Stream"). Dies hilft dir zu verstehen, aus welchem Wissensbereich die Information stammt.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -124,3 +160,85 @@ Wenn du im **manuellen Editor** schreibst, unterstützt dich Mindnet aktiv bei d
|
|||||||
* **Semantische Treffer:** Das System findet inhaltlich verwandte Notizen.
|
* **Semantische Treffer:** Das System findet inhaltlich verwandte Notizen.
|
||||||
* **Klick auf "➕ Einfügen":** Fügt den Link (z.B. `[[rel:related_to Mindnet]]`) an der Cursor-Position ein.
|
* **Klick auf "➕ Einfügen":** Fügt den Link (z.B. `[[rel:related_to Mindnet]]`) an der Cursor-Position ein.
|
||||||
4. **Speichern:** Der Text wird sofort in den Vault geschrieben und indiziert.
|
4. **Speichern:** Der Text wird sofort in den Vault geschrieben und indiziert.
|
||||||
|
|
||||||
|
### 6.3 Weg C: Direktes Schreiben in Obsidian
|
||||||
|
Wenn du Obsidian nutzt, kannst du Notizen direkt im Vault erstellen. Nach dem Speichern in Obsidian:
|
||||||
|
|
||||||
|
1. **Automatischer Import (Cron):** Der stündliche Cron-Job importiert neue/geänderte Dateien automatisch.
|
||||||
|
2. **Manueller Import:** Oder führe manuell aus:
|
||||||
|
```bash
|
||||||
|
python3 -m scripts.import_markdown --vault ./vault --prefix mindnet --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteil:** Du arbeitest in deiner gewohnten Obsidian-Umgebung, Mindnet synchronisiert im Hintergrund.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Häufige Anwendungsfälle (Use Cases)
|
||||||
|
|
||||||
|
### 7.1 Entscheidungshilfe suchen
|
||||||
|
**Szenario:** Du stehst vor einer schwierigen Entscheidung.
|
||||||
|
|
||||||
|
**Vorgehen:**
|
||||||
|
1. Stelle eine Frage im Chat: *"Soll ich Tool X nutzen?"*
|
||||||
|
2. Mindnet erkennt `DECISION` Intent
|
||||||
|
3. System lädt deine Werte, Prinzipien und Ziele
|
||||||
|
4. Antwort berücksichtigt deine persönlichen Kriterien
|
||||||
|
|
||||||
|
**Beispiel:** *"Soll ich Notion nutzen?"* → System prüft gegen dein Prinzip "Privacy First" und rät ab.
|
||||||
|
|
||||||
|
### 7.2 Erfahrungen dokumentieren
|
||||||
|
**Szenario:** Du willst ein prägendes Erlebnis festhalten.
|
||||||
|
|
||||||
|
**Vorgehen:**
|
||||||
|
1. Chat: *"Neue Erfahrung: Streit am Recyclinghof"*
|
||||||
|
2. System erstellt strukturierte Notiz mit Feldern:
|
||||||
|
- Situation
|
||||||
|
- Reaktion
|
||||||
|
- Learning
|
||||||
|
3. Verknüpfe mit relevanten Rollen: `[[rel:supports Meine Rollenlandkarte 2025#Vater]]`
|
||||||
|
|
||||||
|
### 7.3 Wissen im Graph erkunden
|
||||||
|
**Szenario:** Du willst verstehen, wie Konzepte zusammenhängen.
|
||||||
|
|
||||||
|
**Vorgehen:**
|
||||||
|
1. Wechsle zum **Graph Explorer**
|
||||||
|
2. Suche nach einem Knoten (z.B. "Projekt Alpha")
|
||||||
|
3. Klicke auf "🎯 Als Zentrum setzen"
|
||||||
|
4. Erkunde die Nachbarschaft (Abhängigkeiten, Entscheidungen, ähnliche Projekte)
|
||||||
|
|
||||||
|
### 7.4 Bestehende Notizen verbessern
|
||||||
|
**Szenario:** Eine Notiz ist unvollständig oder unvernetzt.
|
||||||
|
|
||||||
|
**Vorgehen:**
|
||||||
|
1. Öffne die Notiz im **Manuellen Editor**
|
||||||
|
2. Nutze **Intelligence-Tab** für Link-Vorschläge
|
||||||
|
3. Füge semantische Links hinzu: `[[rel:based_on Wert X]]`
|
||||||
|
4. Speichern → System indiziert neu
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Tipps & Tricks
|
||||||
|
|
||||||
|
### 8.1 Intent-Steuerung
|
||||||
|
- **Frage mit "?":** Löst RAG-Modus aus (Wissen abrufen)
|
||||||
|
- **Aussage ohne "?":** Löst Interview-Modus aus (Wissen speichern)
|
||||||
|
- **Emotionale Wörter:** Aktivieren Empathie-Persona ("Ich fühle...", "Ich bin frustriert...")
|
||||||
|
|
||||||
|
### 8.2 Graph-Navigation
|
||||||
|
- **URL-Persistenz:** Graph-Einstellungen werden in der URL gespeichert → Lesezeichen für spezifische Ansichten
|
||||||
|
- **Deep-Links:** Verlinke präzise Abschnitte: `[[Note#Abschnitt]]` statt nur `[[Note]]`
|
||||||
|
|
||||||
|
### 8.3 Feedback nutzen
|
||||||
|
- **Regelmäßiges Feedback:** Macht das System schlauer
|
||||||
|
- **Granulares Feedback:** Bewerte einzelne Quellen, nicht nur die Gesamtantwort
|
||||||
|
- **Konsequenz:** System lernt deine Präferenzen
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Weitere Informationen
|
||||||
|
|
||||||
|
- **Knowledge Design:** Siehe [Knowledge Design Manual](01_knowledge_design.md)
|
||||||
|
- **Authoring Guidelines:** Siehe [Authoring Guidelines](01_authoring_guidelines.md)
|
||||||
|
- **Obsidian Integration:** Siehe [Obsidian Integration Guide](01_obsidian_integration_guide.md)
|
||||||
|
- **Troubleshooting:** Siehe [Admin Operations - Troubleshooting](../04_Operations/04_admin_operations.md#33-troubleshooting-guide)
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
---
|
---
|
||||||
doc_type: user_manual
|
doc_type: user_manual
|
||||||
audience: user, author
|
audience: user, author
|
||||||
scope: vault, markdown, schema
|
scope: vault, markdown, schema, agentic_validation, note_scope
|
||||||
status: active
|
status: active
|
||||||
version: 2.8.0
|
version: 4.5.8
|
||||||
context: "Regelwerk für das Erstellen von Notizen im Vault. Die 'Source of Truth' für Autoren."
|
context: "Regelwerk für das Erstellen von Notizen im Vault. Die 'Source of Truth' für Autoren. Inkludiert WP-24c Phase 3 Agentic Edge Validation, automatische Spiegelkanten und Note-Scope Zonen."
|
||||||
---
|
---
|
||||||
|
|
||||||
# Knowledge Design Manual
|
# Knowledge Design Manual
|
||||||
@@ -208,6 +208,12 @@ Dies ist die **mächtigste** Methode. Du sagst dem System explizit, **wie** Ding
|
|||||||
> "Daher [[rel:depends_on Qdrant]]."
|
> "Daher [[rel:depends_on Qdrant]]."
|
||||||
> "Dieses Konzept ist [[rel:similar_to Pinecone]]."
|
> "Dieses Konzept ist [[rel:similar_to Pinecone]]."
|
||||||
|
|
||||||
|
**Deep-Links zu Abschnitten (v2.9.1):**
|
||||||
|
Du kannst auch auf spezifische Abschnitte innerhalb einer Note verlinken:
|
||||||
|
> "Siehe [[rel:based_on Mein Leitbild#P3 – Disziplin]]."
|
||||||
|
|
||||||
|
Das System trennt automatisch den Note-Namen (`Mein Leitbild`) vom Abschnitts-Namen (`P3 – Disziplin`), sodass mehrere Links zur gleichen Note möglich sind, wenn sie auf verschiedene Abschnitte zeigen.
|
||||||
|
|
||||||
**Gültige Relationen:**
|
**Gültige Relationen:**
|
||||||
* `depends_on`: Hängt ab von / Benötigt.
|
* `depends_on`: Hängt ab von / Benötigt.
|
||||||
* `blocks`: Blockiert oder gefährdet (z.B. Risiko -> Projekt).
|
* `blocks`: Blockiert oder gefährdet (z.B. Risiko -> Projekt).
|
||||||
@@ -226,8 +232,20 @@ Für Zusammenfassungen am Ende einer Notiz, oder eines Absatzes:
|
|||||||
> [[AI Agents]]
|
> [[AI Agents]]
|
||||||
```
|
```
|
||||||
|
|
||||||
### 4.3 Implizite Bidirektionalität (Edger-Logik) [NEU] [PRÜFEN!]
|
**Multi-Line Support (v2.9.1):**
|
||||||
In Mindnet musst du Kanten **nicht** manuell in beide Richtungen pflegen. Der **Edger** übernimmt die Paarbildung automatisch im Hintergrund.
|
Callout-Blocks mit mehreren Zeilen werden korrekt verarbeitet. Das System erkennt automatisch, wenn mehrere Links im gleichen Callout-Block stehen, und erstellt für jeden Link eine separate Kante (auch bei Deep-Links zu verschiedenen Sections).
|
||||||
|
|
||||||
|
**Format-agnostische De-Duplizierung:**
|
||||||
|
Wenn Kanten bereits via `[!edge]` Callout vorhanden sind, werden sie nicht mehrfach injiziert. Das System erkennt vorhandene Kanten unabhängig vom Format (Inline, Callout, Wikilink).
|
||||||
|
|
||||||
|
### 4.3 Automatische Spiegelkanten (Invers-Logik) - WP-24c v4.5.8
|
||||||
|
|
||||||
|
In Mindnet musst du Kanten **nicht** manuell in beide Richtungen pflegen. Das System erzeugt automatisch **Spiegelkanten** (Invers-Kanten) im Hintergrund.
|
||||||
|
|
||||||
|
**Wie es funktioniert:**
|
||||||
|
1. **Du setzt eine explizite Kante:** Z.B. `[[rel:depends_on Projekt Alpha]]` in Note A
|
||||||
|
2. **System erzeugt automatisch die Spiegelkante:** Note "Projekt Alpha" erhält automatisch `enforced_by: Note A`
|
||||||
|
3. **Vorteil:** Beide Richtungen sind durchsuchbar, ohne dass du beide manuell setzen musst
|
||||||
|
|
||||||
**Deine Aufgabe:** Setze die Kante in der Datei, die du gerade bearbeitest, so wie es der **logische Fluss** vorgibt.
|
**Deine Aufgabe:** Setze die Kante in der Datei, die du gerade bearbeitest, so wie es der **logische Fluss** vorgibt.
|
||||||
|
|
||||||
@@ -235,10 +253,112 @@ In Mindnet musst du Kanten **nicht** manuell in beide Richtungen pflegen. Der **
|
|||||||
* **Blick nach vorn (Vorwärtslink):** Wenn du einen Plan oder ein Protokoll schreibst, nutze `resulted_in`, `supports` oder `next`.
|
* **Blick nach vorn (Vorwärtslink):** Wenn du einen Plan oder ein Protokoll schreibst, nutze `resulted_in`, `supports` oder `next`.
|
||||||
|
|
||||||
**System-Logik (Beispiele):**
|
**System-Logik (Beispiele):**
|
||||||
- Schreibst du in Note A: `next: [[B]]`, weiß das System automatisch: `B prev A`.
|
- Schreibst du in Note A: `[[rel:next Projekt B]]`, erzeugt das System automatisch: `Projekt B prev: Note A`
|
||||||
- Schreibst du in Note B: `derived_from: [[A]]`, weiß das System automatisch: `A resulted_in B`.
|
- Schreibst du in Note B: `[[rel:derived_from Note A]]`, erzeugt das System automatisch: `Note A resulted_in: Note B`
|
||||||
|
- Schreibst du in Note A: `[[rel:impacts Projekt B]]`, erzeugt das System automatisch: `Projekt B impacted_by: Note A`
|
||||||
|
|
||||||
**Vorteil:** Keine redundante Datenpflege, kein "Link-Nightmare", volle Konsistenz im Graphen.
|
**Wichtig:**
|
||||||
|
- **Explizite Kanten haben Vorrang:** Wenn du bereits beide Richtungen explizit gesetzt hast, wird keine automatische Spiegelkante erzeugt (keine Duplikate)
|
||||||
|
- **Höhere Wirksamkeit expliziter Kanten:** Explizit gesetzte Kanten haben höhere Priorität und Confidence-Werte als automatisch generierte Spiegelkanten
|
||||||
|
- **Schutz vor Manipulation:** System-Kanten (`belongs_to`, `next`, `prev`) können nicht manuell überschrieben werden (Provenance Firewall)
|
||||||
|
|
||||||
|
**Vorteil:** Keine redundante Datenpflege, kein "Link-Nightmare", volle Konsistenz im Graphen. Beide Richtungen sind durchsuchbar, was die Auffindbarkeit von Informationen verdoppelt.
|
||||||
|
|
||||||
|
### 4.4 Explizite vs. Validierte Kanten (Phase 3 Validierung) - WP-24c v4.5.8
|
||||||
|
|
||||||
|
Mindnet unterscheidet zwischen **expliziten Kanten** (sofort übernommen) und **validierten Kanten** (Phase 3 LLM-Prüfung).
|
||||||
|
|
||||||
|
#### Explizite Kanten (Höchste Priorität)
|
||||||
|
|
||||||
|
Diese Kanten werden **sofort** in den Graph übernommen, ohne LLM-Validierung:
|
||||||
|
|
||||||
|
1. **Typed Relations im Text:**
|
||||||
|
```markdown
|
||||||
|
Diese Entscheidung [[rel:depends_on Performance-Analyse]] wurde getroffen.
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Callout-Edges:**
|
||||||
|
```markdown
|
||||||
|
> [!edge] depends_on
|
||||||
|
> [[Performance-Analyse]]
|
||||||
|
> [[Projekt Alpha]]
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Note-Scope Zonen:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
[[rel:depends_on|System-Architektur]]
|
||||||
|
[[rel:part_of|Gesamt-System]]
|
||||||
|
```
|
||||||
|
*(Siehe auch: [Note-Scope Zonen](NOTE_SCOPE_ZONEN.md))*
|
||||||
|
|
||||||
|
**Vorteil expliziter Kanten:**
|
||||||
|
- ✅ **Sofortige Übernahme:** Keine Wartezeit auf LLM-Validierung
|
||||||
|
- ✅ **Höchste Priorität:** Werden immer beibehalten, auch bei Duplikaten
|
||||||
|
- ✅ **Höhere Confidence:** Explizite Kanten haben `confidence: 1.0` (maximal)
|
||||||
|
- ✅ **Keine Validierungs-Kosten:** Keine LLM-Aufrufe erforderlich
|
||||||
|
|
||||||
|
#### Validierte Kanten (Phase 3 - candidate: Präfix)
|
||||||
|
|
||||||
|
Kanten, die in speziellen Validierungs-Zonen stehen, erhalten das `candidate:` Präfix und werden in **Phase 3** durch ein LLM semantisch geprüft:
|
||||||
|
|
||||||
|
**Format:**
|
||||||
|
```markdown
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
|
||||||
|
related_to:Mögliche Verbindung
|
||||||
|
depends_on:Unsicherer Link
|
||||||
|
uses:Experimentelle Technologie
|
||||||
|
```
|
||||||
|
|
||||||
|
**Validierungsprozess:**
|
||||||
|
1. **Extraktion:** Links aus `### Unzugeordnete Kanten` erhalten `candidate:` Präfix
|
||||||
|
2. **Phase 3 Validierung:** LLM prüft semantisch: "Passt diese Verbindung zum Kontext?"
|
||||||
|
3. **Erfolg (VERIFIED):** `candidate:` Präfix wird entfernt, Kante wird persistiert
|
||||||
|
4. **Ablehnung (REJECTED):** Kante wird **nicht** in die Datenbank geschrieben
|
||||||
|
|
||||||
|
**Kontext-Optimierung:**
|
||||||
|
- **Note-Scope Kanten:** LLM nutzt Note-Summary oder gesamten Note-Text (besser für globale Verbindungen)
|
||||||
|
- **Chunk-Scope Kanten:** LLM nutzt spezifischen Chunk-Text (besser für lokale Referenzen)
|
||||||
|
|
||||||
|
**Wann nutze ich validierte Kanten?**
|
||||||
|
- ✅ **Explorative Verbindungen:** Du bist unsicher, ob die Verbindung wirklich passt
|
||||||
|
- ✅ **Experimentelle Links:** Du willst testen, ob eine Verbindung semantisch Sinn macht
|
||||||
|
- ✅ **Automatische Vorschläge:** Das System hat Links vorgeschlagen, die du prüfen lassen willst
|
||||||
|
|
||||||
|
**Wann nutze ich explizite Kanten?**
|
||||||
|
- ✅ **Sichere Verbindungen:** Du bist dir sicher, dass die Verbindung korrekt ist
|
||||||
|
- ✅ **Schnelle Übernahme:** Du willst keine Wartezeit auf Validierung
|
||||||
|
- ✅ **Höchste Priorität:** Die Verbindung soll definitiv im Graph sein
|
||||||
|
|
||||||
|
*(Siehe auch: [LLM-Validierung von Links](LLM_VALIDIERUNG_VON_LINKS.md))*
|
||||||
|
|
||||||
|
### 4.5 Note-Scope Zonen (Globale Verbindungen) - WP-24c v4.2.0
|
||||||
|
|
||||||
|
Für Verbindungen, die der **gesamten Note** zugeordnet werden sollen (nicht nur einem spezifischen Chunk), nutze **Note-Scope Zonen**:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
[[rel:depends_on|Projekt-Übersicht]]
|
||||||
|
[[rel:part_of|Größeres System]]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- ✅ **Globale Verbindungen:** Links gelten für die gesamte Note, nicht nur einen Abschnitt
|
||||||
|
- ✅ **Höchste Priorität:** Note-Scope Links haben Vorrang bei Duplikaten
|
||||||
|
- ✅ **Bessere Validierung:** In Phase 3 nutzt das LLM den gesamten Note-Kontext (Note-Summary/Text)
|
||||||
|
|
||||||
|
**Wann nutze ich Note-Scope?**
|
||||||
|
- ✅ **Projekt-Abhängigkeiten:** "Dieses Projekt hängt von X ab" (gilt für die ganze Note)
|
||||||
|
- ✅ **System-Zugehörigkeit:** "Dieses Konzept ist Teil von Y" (gilt für die ganze Note)
|
||||||
|
- ✅ **Globale Prinzipien:** "Diese Entscheidung basiert auf Prinzip Z" (gilt für die ganze Note)
|
||||||
|
|
||||||
|
**Wann nutze ich Chunk-Scope (Standard)?**
|
||||||
|
- ✅ **Lokale Referenzen:** "In diesem Abschnitt nutzen wir Technologie X" (nur für diesen Abschnitt)
|
||||||
|
- ✅ **Spezifische Kontexte:** Links, die nur in einem bestimmten Textabschnitt relevant sind
|
||||||
|
|
||||||
|
*(Siehe auch: [Note-Scope Zonen - Detaillierte Anleitung](NOTE_SCOPE_ZONEN.md))*
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,282 @@
|
|||||||
|
# LLM-Validierung von Links in Notizen (Phase 3 Agentic Edge Validation)
|
||||||
|
|
||||||
|
**Version:** v4.5.8
|
||||||
|
**Status:** Aktiv
|
||||||
|
**Aktualisiert:** WP-24c Phase 3 Agentic Edge Validation mit Kontext-Optimierung
|
||||||
|
|
||||||
|
## Übersicht
|
||||||
|
|
||||||
|
Das Mindnet-System unterstützt zwei Arten von Links:
|
||||||
|
|
||||||
|
1. **Explizite Links** - Werden direkt übernommen (keine Validierung)
|
||||||
|
2. **Global Pool Links** - Werden vom LLM validiert (wenn aktiviert)
|
||||||
|
|
||||||
|
## Explizite Links (keine Validierung)
|
||||||
|
|
||||||
|
Diese Links werden **sofort** in den Graph übernommen, ohne LLM-Validierung:
|
||||||
|
|
||||||
|
### 1. Typed Relations
|
||||||
|
```markdown
|
||||||
|
[[rel:mastered_by|Klaus]]
|
||||||
|
[[rel:depends_on|Projekt Alpha]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Standard Wikilinks
|
||||||
|
```markdown
|
||||||
|
[[Klaus]]
|
||||||
|
[[Projekt Alpha]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Callouts
|
||||||
|
```markdown
|
||||||
|
> [!edge] mastered_by:Klaus
|
||||||
|
> [!edge] depends_on:Projekt Alpha
|
||||||
|
```
|
||||||
|
|
||||||
|
**Hinweis:** Explizite Links haben immer Vorrang und werden nicht validiert.
|
||||||
|
|
||||||
|
## Validierte Links (Phase 3 - candidate: Präfix) - WP-24c v4.5.8
|
||||||
|
|
||||||
|
Links, die vom LLM validiert werden sollen, müssen in einer speziellen Sektion am Ende der Notiz definiert werden. Diese Links erhalten das `candidate:` Präfix und durchlaufen **Phase 3 Agentic Edge Validation**.
|
||||||
|
|
||||||
|
### Format
|
||||||
|
|
||||||
|
Erstellen Sie eine Sektion mit einem der folgenden Titel:
|
||||||
|
- `### Unzugeordnete Kanten`
|
||||||
|
- `### Edge Pool`
|
||||||
|
- `### Candidates`
|
||||||
|
|
||||||
|
In dieser Sektion listen Sie Links im Format `kind:target` auf:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
type: concept
|
||||||
|
title: Meine Notiz
|
||||||
|
---
|
||||||
|
|
||||||
|
# Inhalt der Notiz
|
||||||
|
|
||||||
|
Hier ist der normale Inhalt...
|
||||||
|
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
|
||||||
|
related_to:Klaus
|
||||||
|
mastered_by:Projekt Alpha
|
||||||
|
depends_on:Andere Notiz
|
||||||
|
```
|
||||||
|
|
||||||
|
### Beispiel
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
type: decision
|
||||||
|
title: Entscheidung über Technologie-Stack
|
||||||
|
---
|
||||||
|
|
||||||
|
# Entscheidung über Technologie-Stack
|
||||||
|
|
||||||
|
Wir haben uns für React entschieden, weil...
|
||||||
|
|
||||||
|
## Begründung
|
||||||
|
|
||||||
|
React bietet bessere Performance...
|
||||||
|
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
|
||||||
|
related_to:React-Dokumentation
|
||||||
|
depends_on:Performance-Analyse
|
||||||
|
uses:TypeScript
|
||||||
|
```
|
||||||
|
|
||||||
|
### Validierung
|
||||||
|
|
||||||
|
**Wichtig:** Global Pool Links werden nur validiert, wenn:
|
||||||
|
|
||||||
|
1. Die Chunk-Konfiguration `enable_smart_edge_allocation: true` enthält
|
||||||
|
2. Dies wird normalerweise in `config/types.yaml` pro Note-Typ konfiguriert
|
||||||
|
|
||||||
|
**Beispiel-Konfiguration in `types.yaml`:**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
types:
|
||||||
|
decision:
|
||||||
|
chunking_profile: sliding_smart_edges
|
||||||
|
chunking:
|
||||||
|
sliding_smart_edges:
|
||||||
|
enable_smart_edge_allocation: true # ← Aktiviert LLM-Validierung
|
||||||
|
```
|
||||||
|
|
||||||
|
### Phase 3 Validierungsprozess (WP-24c v4.5.8)
|
||||||
|
|
||||||
|
1. **Extraktion:** Links aus der "Unzugeordnete Kanten" Sektion werden extrahiert
|
||||||
|
2. **candidate: Präfix:** Erhalten `candidate:` Präfix in `rule_id` oder `provenance`
|
||||||
|
3. **Kontext-Optimierung:**
|
||||||
|
- **Note-Scope (`scope: note`):** LLM nutzt `note_summary` (Top 5 Chunks) oder `note_text` (aggregierter Gesamttext)
|
||||||
|
- **Chunk-Scope (`scope: chunk`):** LLM nutzt spezifischen Chunk-Text, falls verfügbar, sonst Note-Text
|
||||||
|
4. **Validierung:** LLM prüft semantisch (via `ingest_validator` Profil, Temperature 0.0):
|
||||||
|
- Ist der Link semantisch relevant für den Kontext?
|
||||||
|
- Passt die Relation (`kind`) zum Ziel?
|
||||||
|
5. **Ergebnis:**
|
||||||
|
- ✅ **VERIFIED:** `candidate:` Präfix wird entfernt, Kante wird in den Graph übernommen
|
||||||
|
- 🚫 **REJECTED:** Kante wird **nicht** in die Datenbank geschrieben (verhindert "Geister-Verknüpfungen")
|
||||||
|
|
||||||
|
### Validierungs-Prompt
|
||||||
|
|
||||||
|
Das System verwendet den Prompt `edge_validation` aus `config/prompts.yaml`:
|
||||||
|
|
||||||
|
```
|
||||||
|
Verify relation '{edge_kind}' for graph integrity.
|
||||||
|
Chunk: "{chunk_text}"
|
||||||
|
Target: "{target_title}" ({target_summary})
|
||||||
|
Respond ONLY with 'YES' or 'NO'.
|
||||||
|
```
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
### ✅ Empfohlen
|
||||||
|
|
||||||
|
1. **Explizite Links für sichere Verbindungen:**
|
||||||
|
```markdown
|
||||||
|
Diese Entscheidung [[rel:depends_on|Performance-Analyse]] wurde getroffen.
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Global Pool für unsichere/explorative Links:**
|
||||||
|
```markdown
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
related_to:Mögliche Verbindung
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Kombination beider Ansätze:**
|
||||||
|
```markdown
|
||||||
|
# Hauptinhalt
|
||||||
|
|
||||||
|
Explizite Verbindung: [[rel:depends_on|Sichere Notiz]]
|
||||||
|
|
||||||
|
## Weitere Überlegungen
|
||||||
|
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
related_to:Unsichere Verbindung
|
||||||
|
explored_in:Experimentelle Notiz
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Vermeiden
|
||||||
|
|
||||||
|
1. **Nicht zu viele Global Pool Links:**
|
||||||
|
- Jeder Link erfordert einen LLM-Aufruf
|
||||||
|
- Kann die Ingestion verlangsamen
|
||||||
|
|
||||||
|
2. **Nicht für offensichtliche Links:**
|
||||||
|
- Nutzen Sie explizite Links für klare Verbindungen
|
||||||
|
- Global Pool ist für explorative/unsichere Links gedacht
|
||||||
|
|
||||||
|
## Aktivierung der Validierung
|
||||||
|
|
||||||
|
### Schritt 1: Chunk-Profile konfigurieren
|
||||||
|
|
||||||
|
In `config/types.yaml`:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
types:
|
||||||
|
your_type:
|
||||||
|
chunking_profile: sliding_smart_edges
|
||||||
|
chunking:
|
||||||
|
sliding_smart_edges:
|
||||||
|
enable_smart_edge_allocation: true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 2: Notiz erstellen
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
type: your_type
|
||||||
|
title: Meine Notiz
|
||||||
|
---
|
||||||
|
|
||||||
|
# Inhalt
|
||||||
|
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
|
||||||
|
related_to:Ziel-Notiz
|
||||||
|
```
|
||||||
|
|
||||||
|
### Schritt 3: Import ausführen
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 -m scripts.import_markdown --vault ./vault --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
## Logging & Debugging (Phase 3)
|
||||||
|
|
||||||
|
Während der Ingestion sehen Sie im Log:
|
||||||
|
|
||||||
|
```
|
||||||
|
🚀 [PHASE 3] Validierung: Note-A -> Ziel-Notiz (related_to) | Scope: chunk | Kontext: Chunk-Scope (c00)
|
||||||
|
⚖️ [VALIDATING] Relation 'related_to' -> 'Ziel-Notiz' (Profile: ingest_validator)...
|
||||||
|
✅ [PHASE 3] VERIFIED: Note-A -> Ziel-Notiz (related_to) | rule_id: explicit
|
||||||
|
```
|
||||||
|
|
||||||
|
oder
|
||||||
|
|
||||||
|
```
|
||||||
|
🚀 [PHASE 3] Validierung: Note-A -> Ziel-Notiz (related_to) | Scope: note | Kontext: Note-Scope (aggregiert)
|
||||||
|
⚖️ [VALIDATING] Relation 'related_to' -> 'Ziel-Notiz' (Profile: ingest_validator)...
|
||||||
|
🚫 [PHASE 3] REJECTED: Note-A -> Ziel-Notiz (related_to)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Hinweis:** Phase 3 Logs zeigen auch die Kontext-Optimierung (Note-Scope vs. Chunk-Scope) und den finalen Status (VERIFIED/REJECTED).
|
||||||
|
|
||||||
|
## Technische Details
|
||||||
|
|
||||||
|
### Provenance-System (WP-24c v4.5.8)
|
||||||
|
|
||||||
|
- `explicit`: Explizite Links (keine Validierung, höchste Priorität)
|
||||||
|
- `explicit:note_zone`: Note-Scope Links aus `## Smart Edges` (keine Validierung)
|
||||||
|
- `candidate:`: Links aus `### Unzugeordnete Kanten` (Phase 3 Validierung erforderlich)
|
||||||
|
- `semantic_ai`: KI-generierte Links
|
||||||
|
- `rule`: Regel-basierte Links (z.B. aus types.yaml)
|
||||||
|
- `structure`: System-generierte Spiegelkanten (automatische Invers-Logik)
|
||||||
|
|
||||||
|
### Code-Referenzen
|
||||||
|
|
||||||
|
- **Extraktion:** `app/core/chunking/chunking_processor.py` (Zeile 66-81)
|
||||||
|
- **Validierung:** `app/core/ingestion/ingestion_validation.py`
|
||||||
|
- **Integration:** `app/core/ingestion/ingestion_processor.py` (Zeile 237-239)
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
**Q: Werden explizite Links auch validiert?**
|
||||||
|
A: Nein, explizite Links werden direkt übernommen.
|
||||||
|
|
||||||
|
**Q: Kann ich die Validierung für bestimmte Links überspringen?**
|
||||||
|
A: Ja, nutzen Sie explizite Links (`[[rel:kind|target]]` oder `> [!edge]`).
|
||||||
|
|
||||||
|
**Q: Was passiert, wenn das LLM nicht verfügbar ist?**
|
||||||
|
A: Das System unterscheidet zwischen:
|
||||||
|
- **Transienten Fehlern (Netzwerk, Timeout):** Kante wird erlaubt (Integrität vor Präzision - verhindert Datenverlust)
|
||||||
|
- **Permanenten Fehlern (Config, Validation):** Kante wird abgelehnt (Graph-Qualität schützen)
|
||||||
|
|
||||||
|
**Q: Was ist der Unterschied zwischen expliziten und validierten Links?**
|
||||||
|
A:
|
||||||
|
- **Explizite Links:** Sofortige Übernahme, höchste Priorität, keine Validierung, `confidence: 1.0`
|
||||||
|
- **Validierte Links:** Phase 3 Prüfung, `candidate:` Präfix, können abgelehnt werden, höhere Graph-Qualität
|
||||||
|
|
||||||
|
**Q: Warum sollte ich explizite Links nutzen statt validierte?**
|
||||||
|
A: Explizite Links haben:
|
||||||
|
- ✅ Sofortige Übernahme (keine Wartezeit)
|
||||||
|
- ✅ Höchste Priorität (werden immer beibehalten)
|
||||||
|
- ✅ Keine Validierungs-Kosten (keine LLM-Aufrufe)
|
||||||
|
- ✅ Höhere Confidence-Werte
|
||||||
|
|
||||||
|
Nutze validierte Links nur, wenn du unsicher bist, ob die Verbindung wirklich passt.
|
||||||
|
|
||||||
|
**Q: Kann ich mehrere Links in einer Zeile angeben?**
|
||||||
|
A: Nein, jeder Link muss in einer eigenen Zeile stehen: `kind:target`.
|
||||||
|
|
||||||
|
## Zusammenfassung (WP-24c v4.5.8)
|
||||||
|
|
||||||
|
- ✅ **Explizite Links:** `[[rel:kind|target]]`, `> [!edge]` oder `## Smart Edges` → Keine Validierung, höchste Priorität
|
||||||
|
- ✅ **Validierte Links:** Sektion `### Unzugeordnete Kanten` → Phase 3 Validierung mit `candidate:` Präfix
|
||||||
|
- ✅ **Phase 3 Validierung:** LLM prüft semantisch mit Kontext-Optimierung (Note-Scope vs. Chunk-Scope)
|
||||||
|
- ✅ **Ergebnis:** VERIFIED (Präfix entfernt, persistiert) oder REJECTED (nicht in DB geschrieben)
|
||||||
|
- ✅ **Format:** `kind:target` (eine pro Zeile in `### Unzugeordnete Kanten`)
|
||||||
|
- ✅ **Automatische Spiegelkanten:** Explizite Kanten erzeugen automatisch Invers-Kanten (beide Richtungen durchsuchbar)
|
||||||
@@ -0,0 +1,275 @@
|
|||||||
|
# Note-Scope Extraktions-Zonen (v4.5.8)
|
||||||
|
|
||||||
|
**Version:** v4.5.8
|
||||||
|
**Status:** Aktiv
|
||||||
|
**Aktualisiert:** WP-24c Phase 3 Agentic Edge Validation
|
||||||
|
|
||||||
|
## Übersicht
|
||||||
|
|
||||||
|
Das Mindnet-System unterstützt nun **Note-Scope Extraktions-Zonen**, die es ermöglichen, Links zu definieren, die der gesamten Note zugeordnet werden (nicht nur einem spezifischen Chunk).
|
||||||
|
|
||||||
|
### Unterschied: Chunk-Scope vs. Note-Scope
|
||||||
|
|
||||||
|
- **Chunk-Scope Links** (`scope: "chunk"`):
|
||||||
|
- Werden aus dem Text-Inhalt extrahiert
|
||||||
|
- Sind lokalem Kontext zugeordnet
|
||||||
|
- `source_id` = `chunk_id`
|
||||||
|
|
||||||
|
- **Note-Scope Links** (`scope: "note"`):
|
||||||
|
- Werden aus speziellen Markdown-Sektionen extrahiert
|
||||||
|
- Sind der gesamten Note zugeordnet
|
||||||
|
- `source_id` = `note_id`
|
||||||
|
- Haben höchste Priorität bei Duplikaten
|
||||||
|
|
||||||
|
## Verwendung
|
||||||
|
|
||||||
|
### Format
|
||||||
|
|
||||||
|
Erstellen Sie eine Sektion mit einem der folgenden Header:
|
||||||
|
|
||||||
|
- `## Smart Edges`
|
||||||
|
- `## Relationen`
|
||||||
|
- `## Global Links`
|
||||||
|
- `## Note-Level Relations`
|
||||||
|
- `## Globale Verbindungen`
|
||||||
|
|
||||||
|
**Wichtig:** Die Header müssen exakt (case-insensitive) übereinstimmen.
|
||||||
|
|
||||||
|
### Beispiel
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
type: decision
|
||||||
|
title: Technologie-Entscheidung
|
||||||
|
---
|
||||||
|
|
||||||
|
# Entscheidung über Technologie-Stack
|
||||||
|
|
||||||
|
Wir haben uns für React entschieden...
|
||||||
|
|
||||||
|
## Begründung
|
||||||
|
|
||||||
|
React bietet bessere Performance...
|
||||||
|
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
[[rel:depends_on|Performance-Analyse]]
|
||||||
|
[[rel:uses|TypeScript]]
|
||||||
|
[[React-Dokumentation]]
|
||||||
|
|
||||||
|
## Weitere Überlegungen
|
||||||
|
|
||||||
|
Hier ist weiterer Inhalt...
|
||||||
|
```
|
||||||
|
|
||||||
|
### Unterstützte Link-Formate
|
||||||
|
|
||||||
|
In Note-Scope Zonen werden folgende Formate unterstützt:
|
||||||
|
|
||||||
|
1. **Typed Relations:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
[[rel:depends_on|Ziel-Notiz]]
|
||||||
|
[[rel:uses|Andere Notiz]]
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Standard Wikilinks:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
[[Ziel-Notiz]]
|
||||||
|
[[Andere Notiz]]
|
||||||
|
```
|
||||||
|
(Werden als `related_to` interpretiert)
|
||||||
|
|
||||||
|
3. **Callouts:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
> [!edge] depends_on:[[Ziel-Notiz]]
|
||||||
|
> [!edge] uses:[[Andere Notiz]]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Technische Details
|
||||||
|
|
||||||
|
### ID-Generierung
|
||||||
|
|
||||||
|
Note-Scope Links verwenden die **exakt gleiche ID-Generierung** wie Symmetrie-Kanten in Phase 2:
|
||||||
|
|
||||||
|
```python
|
||||||
|
_mk_edge_id(kind, note_id, target_id, "note", target_section=sec)
|
||||||
|
```
|
||||||
|
|
||||||
|
Dies stellt sicher, dass:
|
||||||
|
- ✅ Authority-Check in Phase 2 korrekt funktioniert
|
||||||
|
- ✅ Keine Duplikate entstehen
|
||||||
|
- ✅ Symmetrie-Schutz greift
|
||||||
|
|
||||||
|
### Provenance
|
||||||
|
|
||||||
|
Note-Scope Links erhalten:
|
||||||
|
- `provenance: "explicit:note_zone"`
|
||||||
|
- `confidence: 1.0` (höchste Priorität)
|
||||||
|
- `scope: "note"`
|
||||||
|
- `source_id: note_id` (nicht `chunk_id`)
|
||||||
|
|
||||||
|
### Priorisierung
|
||||||
|
|
||||||
|
Bei Duplikaten (gleiche ID):
|
||||||
|
1. **Note-Scope Links** haben **höchste Priorität**
|
||||||
|
2. Dann Confidence-Wert
|
||||||
|
3. Dann Provenance-Priority
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
- Chunk-Link: `related_to:Note-A` (aus Text)
|
||||||
|
- Note-Scope Link: `related_to:Note-A` (aus Zone)
|
||||||
|
- **Ergebnis:** Note-Scope Link wird beibehalten
|
||||||
|
|
||||||
|
## Best Practices
|
||||||
|
|
||||||
|
### ✅ Empfohlen
|
||||||
|
|
||||||
|
1. **Note-Scope für globale Verbindungen:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
[[rel:depends_on|Projekt-Übersicht]]
|
||||||
|
[[rel:part_of|Größeres System]]
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Chunk-Scope für lokale Referenzen:**
|
||||||
|
```markdown
|
||||||
|
In diesem Abschnitt verweisen wir auf [[rel:uses|Spezifische Technologie]].
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Kombination:**
|
||||||
|
```markdown
|
||||||
|
# Hauptinhalt
|
||||||
|
|
||||||
|
Lokale Referenz: [[rel:uses|Lokale Notiz]]
|
||||||
|
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
Globale Verbindung: [[rel:depends_on|Globale Notiz]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Vermeiden
|
||||||
|
|
||||||
|
1. **Nicht für lokale Kontext-Links:**
|
||||||
|
- Nutzen Sie Chunk-Scope Links für lokale Referenzen
|
||||||
|
- Note-Scope ist für Note-weite Verbindungen gedacht
|
||||||
|
|
||||||
|
2. **Nicht zu viele Note-Scope Links:**
|
||||||
|
- Beschränken Sie sich auf wirklich Note-weite Verbindungen
|
||||||
|
- Zu viele Note-Scope Links können die Graph-Struktur verwässern
|
||||||
|
|
||||||
|
## Integration mit Phase 3 Validierung (WP-24c v4.5.8)
|
||||||
|
|
||||||
|
Note-Scope Links können **zwei verschiedene Provenance** haben:
|
||||||
|
|
||||||
|
### Explizite Note-Scope Links (Keine Validierung)
|
||||||
|
|
||||||
|
Links in `## Smart Edges` Zonen werden als `explicit:note_zone` markiert und **direkt übernommen** (keine Phase 3 Validierung):
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
[[rel:depends_on|System-Architektur]]
|
||||||
|
[[rel:part_of|Gesamt-System]]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteil:** Sofortige Übernahme, höchste Priorität, keine Validierungs-Kosten.
|
||||||
|
|
||||||
|
### Validierte Note-Scope Links (Phase 3 Validierung)
|
||||||
|
|
||||||
|
Links in `### Unzugeordnete Kanten` erhalten `candidate:` Präfix und werden in **Phase 3** validiert:
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
### Unzugeordnete Kanten
|
||||||
|
|
||||||
|
related_to:Mögliche Verbindung
|
||||||
|
depends_on:Unsicherer Link
|
||||||
|
```
|
||||||
|
|
||||||
|
**Validierungsprozess:**
|
||||||
|
1. Links erhalten `candidate:` Präfix
|
||||||
|
2. **Phase 3 Validierung:** LLM prüft semantisch gegen Note-Summary oder Note-Text (Note-Scope Kontext-Optimierung)
|
||||||
|
3. **Erfolg (VERIFIED):** `candidate:` Präfix wird entfernt, Kante wird persistiert
|
||||||
|
4. **Ablehnung (REJECTED):** Kante wird **nicht** in die Datenbank geschrieben
|
||||||
|
|
||||||
|
**Wichtig:**
|
||||||
|
- Links in `### Unzugeordnete Kanten` werden als `candidate:` markiert und durchlaufen Phase 3
|
||||||
|
- Links in `## Smart Edges` werden als `explicit:note_zone` markiert und **nicht** validiert (direkt übernommen)
|
||||||
|
- **Note-Scope Kontext-Optimierung:** Bei Note-Scope Kanten nutzt Phase 3 `note_summary` (Top 5 Chunks) oder `note_text` (aggregierter Gesamttext) für bessere Validierungs-Genauigkeit
|
||||||
|
|
||||||
|
## Beispiel: Vollständige Notiz
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
type: decision
|
||||||
|
title: Architektur-Entscheidung
|
||||||
|
---
|
||||||
|
|
||||||
|
# Architektur-Entscheidung
|
||||||
|
|
||||||
|
Wir haben uns für Microservices entschieden...
|
||||||
|
|
||||||
|
## Begründung
|
||||||
|
|
||||||
|
### Performance
|
||||||
|
|
||||||
|
Microservices bieten bessere Skalierbarkeit. Siehe auch [[rel:uses|Kubernetes]] für Orchestrierung.
|
||||||
|
|
||||||
|
### Sicherheit
|
||||||
|
|
||||||
|
Wir nutzen [[rel:enforced_by|OAuth2]] für Authentifizierung.
|
||||||
|
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
[[rel:depends_on|System-Architektur]]
|
||||||
|
[[rel:part_of|Gesamt-System]]
|
||||||
|
[[rel:uses|Cloud-Infrastruktur]]
|
||||||
|
|
||||||
|
## Weitere Details
|
||||||
|
|
||||||
|
Hier ist weiterer Inhalt...
|
||||||
|
```
|
||||||
|
|
||||||
|
**Ergebnis:**
|
||||||
|
- `uses:Kubernetes` → Chunk-Scope (aus Text)
|
||||||
|
- `enforced_by:OAuth2` → Chunk-Scope (aus Text)
|
||||||
|
- `depends_on:System-Architektur` → Note-Scope (aus Zone)
|
||||||
|
- `part_of:Gesamt-System` → Note-Scope (aus Zone)
|
||||||
|
- `uses:Cloud-Infrastruktur` → Note-Scope (aus Zone)
|
||||||
|
|
||||||
|
## Code-Referenzen
|
||||||
|
|
||||||
|
- **Extraktion:** `app/core/graph/graph_derive_edges.py` → `extract_note_scope_zones()`
|
||||||
|
- **Integration:** `app/core/graph/graph_derive_edges.py` → `build_edges_for_note()`
|
||||||
|
- **Header-Liste:** `NOTE_SCOPE_ZONE_HEADERS` in `graph_derive_edges.py`
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
**Q: Können Note-Scope Links auch Section-Links sein?**
|
||||||
|
A: Ja, `[[rel:kind|Target#Section]]` wird unterstützt. `target_section` fließt in die ID ein.
|
||||||
|
|
||||||
|
**Q: Was passiert, wenn ein Link sowohl in Chunk als auch in Note-Scope Zone steht?**
|
||||||
|
A: Der Note-Scope Link hat Vorrang und wird beibehalten.
|
||||||
|
|
||||||
|
**Q: Werden Note-Scope Links validiert?**
|
||||||
|
A: Das hängt von der Zone ab:
|
||||||
|
- **`## Smart Edges`:** Nein, werden direkt übernommen (explizite Links, keine Validierung)
|
||||||
|
- **`### Unzugeordnete Kanten`:** Ja, durchlaufen Phase 3 Validierung (candidate: Präfix)
|
||||||
|
|
||||||
|
**Q: Was ist der Unterschied zwischen Note-Scope in Smart Edges vs. Unzugeordnete Kanten?**
|
||||||
|
A:
|
||||||
|
- **Smart Edges:** Explizite Links, sofortige Übernahme, höchste Priorität
|
||||||
|
- **Unzugeordnete Kanten:** Validierte Links, Phase 3 Prüfung, candidate: Präfix
|
||||||
|
|
||||||
|
**Q: Kann ich eigene Header-Namen verwenden?**
|
||||||
|
A: Aktuell nur die vordefinierten Header. Erweiterung möglich durch Anpassung von `NOTE_SCOPE_ZONE_HEADERS`.
|
||||||
|
|
||||||
|
## Zusammenfassung
|
||||||
|
|
||||||
|
- ✅ **Note-Scope Zonen:** `## Smart Edges` oder ähnliche Header
|
||||||
|
- ✅ **Format:** `[[rel:kind|target]]` oder `[[target]]`
|
||||||
|
- ✅ **Scope:** `scope: "note"`, `source_id: note_id`
|
||||||
|
- ✅ **Priorität:** Höchste Priorität bei Duplikaten
|
||||||
|
- ✅ **ID-Konsistenz:** Exakt wie Symmetrie-Kanten (Phase 2)
|
||||||
@@ -0,0 +1,394 @@
|
|||||||
|
# Mindnet Causal Assistant – Dokumentation der bisher erreichten Resultate (0.4.x) + Architektur, Konfiguration & Strategien
|
||||||
|
|
||||||
|
> Stand: basierend auf den beobachteten Chain-Inspector-Logs und den zuletzt beschriebenen Implementierungen in 0.4.6/0.4.x.
|
||||||
|
> Ziel dieser Doku: Eine **einheitliche, belastbare Basis**, damit Weiterentwicklung (0.5.x/0.6.x) nicht mehr “im Kreis” läuft.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1) Zweck & Gesamtziel von Mindnet
|
||||||
|
|
||||||
|
Mindnet soll in einem Obsidian Vault **kausale/argumentative Zusammenhänge** als Graph abbilden und daraus **nützliche Diagnosen** ableiten:
|
||||||
|
|
||||||
|
- **Graph-Aufbau:** Notes/Sections als Knoten, Links/Kanten als gerichtete Beziehungen (z.B. *wirkt_auf*, *resulted_in*, *depends_on* …).
|
||||||
|
- **Analyse aus einem Kontext:** Nutzer steht in einer Note an einer bestimmten Überschrift/Section → Mindnet analysiert lokale Nachbarschaft + Pfade im Graphen.
|
||||||
|
- **Template Matching:** Einordnen der gefundenen Knoten/Kanten in “Kettenmuster” (Chain Templates) wie z.B. *trigger → transformation → outcome* oder *loop_learning*.
|
||||||
|
- **Findings (Gap-Heuristiken):** Hinweise wie “fehlende Slots”, “fehlende Links”, “unmapped edge types”, “einseitige Konnektivität”, etc.
|
||||||
|
→ Ziel: **Nutzer konkret zum besseren Graphen führen**, ohne “Noisy” zu sein.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2) Begriffe & Datenmodell (so arbeitet der Chain Inspector)
|
||||||
|
|
||||||
|
### 2.1 Kontext (Context)
|
||||||
|
Der Chain Inspector läuft immer gegen einen **aktuellen Kontext**:
|
||||||
|
- `file`: aktuelle Note (z.B. `Tests/03_insight_transformation.md`)
|
||||||
|
- `heading`: aktuelle Section (z.B. `Kern`)
|
||||||
|
- `zoneKind`: i.d.R. `content`
|
||||||
|
|
||||||
|
Das ist wichtig, weil Kanten teils **section-spezifisch** sind und teils (geplant/teilweise offen) **note-weit** gelten könnten.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.2 Knoten (Nodes)
|
||||||
|
Ein Knoten ist im Report meist referenziert als:
|
||||||
|
- `file + heading` (z.B. `Tests/01_experience_trigger.md:Kontext`)
|
||||||
|
- plus abgeleitete Metadaten wie `noteType` (z.B. experience, insight, decision, event)
|
||||||
|
|
||||||
|
**noteType** ist entscheidend fürs Template Matching (Slots).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2.3 Kanten (Edges)
|
||||||
|
Eine Kante hat typischerweise:
|
||||||
|
- `rawEdgeType`: Original-Typ aus Markdown/Notation (z.B. `wirkt_auf`, `resulted_in`, `depends_on`, `derived_from`, …)
|
||||||
|
- `from`: Quelle (file:heading)
|
||||||
|
- `to`: Ziel (file:heading)
|
||||||
|
- `scope`: Gültigkeit / Herkunft
|
||||||
|
- `section`: Edge ist “voll gültig” für die Section
|
||||||
|
- `candidate`: Edge ist nur Kandidat/unsicher, wird optional zugelassen
|
||||||
|
- (geplant/offen) `note`: Edge gilt note-weit (unabhängig von Section)
|
||||||
|
- `evidence`: Fundstelle (file, sectionHeading, lineRange)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3) Erreichte Resultate in 0.4.x (verifiziert)
|
||||||
|
|
||||||
|
### 3.1 includeCandidates – Kandidatenkanten funktionieren wie erwartet
|
||||||
|
**Ergebnis (bereits mehrfach in Logs verifiziert):**
|
||||||
|
- Wenn `includeCandidates=false`, werden Kanten mit `scope: candidate` **im effektiven Graphen ausgefiltert**.
|
||||||
|
- Wenn `includeCandidates=true`, werden Kandidatenkanten **als incoming/outgoing** berücksichtigt und tauchen in `neighbors`/`paths` auf.
|
||||||
|
|
||||||
|
**Implikation:**
|
||||||
|
- Das System kann “unsichere” oder “LLM-vorschlagene” Verbindungen existieren lassen, ohne in jedem Lauf die Analyse zu verfälschen.
|
||||||
|
- In “Discovery” kann man Kandidaten zulassen (mehr Explorationspower).
|
||||||
|
- In “Decisioning” kann man Kandidaten typischerweise sperren (mehr Verlässlichkeit).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.2 required_links: Strict vs Soft Mode – missing_link_constraints Unterdrückung ist umgesetzt
|
||||||
|
**Problem (historisch):**
|
||||||
|
- `missing_link_constraints` wurde teilweise auch dann ausgegeben, wenn `required_links=false` (Soft Mode) aktiv war → unnötig “noisy”.
|
||||||
|
|
||||||
|
**Fix (laut Cursor-Report umgesetzt + Tests):**
|
||||||
|
- `missing_link_constraints` wird nur erzeugt, wenn `effectiveRequiredLinks === true`.
|
||||||
|
- Es gibt eine definierte Auflösungsreihenfolge für `required_links`:
|
||||||
|
|
||||||
|
**Resolution Order (effective required_links):**
|
||||||
|
1. `template.matching?.required_links`
|
||||||
|
2. `profile.required_links`
|
||||||
|
3. `defaults.matching?.required_links`
|
||||||
|
4. Fallback: `false`
|
||||||
|
|
||||||
|
**Transparenz bleibt erhalten:**
|
||||||
|
- `satisfiedLinks` und `requiredLinks` werden weiterhin im Report angezeigt.
|
||||||
|
- `linksComplete` bleibt als technischer Wert im Report bestehen.
|
||||||
|
- **Nur** das Finding `missing_link_constraints` wird unterdrückt, nicht die Fakten.
|
||||||
|
|
||||||
|
**Implikation:**
|
||||||
|
- Soft Mode (= required_links=false) ist jetzt ruhig genug, um “Entdeckung” zu unterstützen.
|
||||||
|
- Strict Mode (= required_links=true) eignet sich für harte Qualitätskontrolle.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.3 “Healthy graph” → Findings leer ([]), Template “confirmed”
|
||||||
|
Wenn Slots **und** geforderte Links erfüllt sind (z.B. `trigger_transformation_outcome` mit 2/2 Links),
|
||||||
|
dann ist `findings: []` das erwartete Ergebnis.
|
||||||
|
|
||||||
|
**Implikation:**
|
||||||
|
- Das ist das zentrale “Green Path”-Signal: Graph ist konsistent für das gewählte Template/Profil.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3.4 Unmapped edges werden erkannt (Diagnose)
|
||||||
|
Wenn ein `rawEdgeType` nicht in die kanonischen Rollen/Edge-Rollen abgebildet werden kann, tauchen typischerweise Diagnosen auf:
|
||||||
|
- `edgesUnmapped > 0`
|
||||||
|
- Findings wie `no_causal_roles` oder link constraints bleiben unerfüllt
|
||||||
|
|
||||||
|
**Implikation:**
|
||||||
|
- Das Rollen-Mapping (chain_roles.yaml) ist “critical path”: wenn ein Edge-Typ nicht gemappt wird, bricht oft der kausale Interpretationspfad.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4) Was ist (noch) offen – echtes nächstes Verifikationsziel
|
||||||
|
|
||||||
|
### 4.1 Note-Level Edges / Note-Scope (“für jede Sektion gültig”)
|
||||||
|
Es existiert ein Konzept/Einbau: In `02_event_trigger_detail` gibt es einen Bereich, der Kanten **auf Note-Ebene** definieren soll (unabhängig von aktueller Section).
|
||||||
|
|
||||||
|
**Offen ist die robuste Verifikation (oder Implementierung), dass:**
|
||||||
|
- diese Edges auch dann gelten, wenn der Cursor in einer anderen Section derselben Note steht,
|
||||||
|
- idealerweise mit klar erkennbarer Kennzeichnung wie `scope: note` im Report.
|
||||||
|
|
||||||
|
**Warum ist das wichtig?**
|
||||||
|
- Das ermöglicht “globaler Kontext” pro Note, ohne alles in jede Section duplizieren zu müssen.
|
||||||
|
- Es ist eine UX-Optimierung: Nutzer kann “Meta-Verbindungen” an einer Stelle pflegen.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5) Konfigurationsdateien – Rolle & Interpretation (Mindnet-Strategie)
|
||||||
|
|
||||||
|
> Die folgenden Bereiche beschreiben eine **saubere, konsistente Interpretation**, wie Mindnet die Configs verwenden sollte.
|
||||||
|
> Konkrete Keys, die in Logs sichtbar waren (z.B. required_links, min_slots_filled_for_gap_findings, min_score_for_gap_findings) sind hier berücksichtigt.
|
||||||
|
|
||||||
|
### 5.1 chain_templates.yaml – “Welche Ketten gibt es, wie werden sie gematcht?”
|
||||||
|
**Zweck:**
|
||||||
|
- Definiert Templates (Muster), z.B.:
|
||||||
|
- `trigger_transformation_outcome`
|
||||||
|
- `loop_learning`
|
||||||
|
- ggf. weitere (constraint_to_adaptation usw.)
|
||||||
|
|
||||||
|
**Template enthält typischerweise:**
|
||||||
|
- Slots (Rollen für Knoten): z.B. `trigger`, `transformation`, `outcome`, `experience`, `learning`, `behavior`, `feedback`
|
||||||
|
- Required Link Constraints (welche Slot-zu-Slot Verbindungen zwingend sind)
|
||||||
|
- Scoring/Matching-Parameter (ggf. weights, thresholds)
|
||||||
|
- Optional: template-level override für `required_links`
|
||||||
|
|
||||||
|
**Matching-Profile (wie in Logs sichtbar):**
|
||||||
|
- z.B. Profile: `discovery`, `decisioning`
|
||||||
|
- Parameter im Profil (sichtbar in Logs):
|
||||||
|
- `required_links` (strict vs soft)
|
||||||
|
- `min_slots_filled_for_gap_findings`
|
||||||
|
- `min_score_for_gap_findings`
|
||||||
|
- ggf. `maxTemplateMatches`
|
||||||
|
|
||||||
|
**Interpretation:**
|
||||||
|
- Templates liefern die “Soll-Struktur”
|
||||||
|
- Profile bestimmen “Wie streng” wir die Soll-Struktur im jeweiligen Workflow bewerten
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.2 chain_roles.yaml – “Welche rawEdgeTypes zählen als welche Rollen?”
|
||||||
|
**Zweck:**
|
||||||
|
- Mappt `rawEdgeType` → kanonische Rollen/EdgeRoles (z.B. `causal`, `influences`, `enables_constraints`, `provenance`).
|
||||||
|
- Diese Rollen sind Grundlage für:
|
||||||
|
- `no_causal_roles` Finding
|
||||||
|
- Link-Constraint-Satisfaction (Template erwartet “causal” zwischen Slots)
|
||||||
|
- Matching Score (welche Edges zählen für welches Template)
|
||||||
|
|
||||||
|
**Interpretation:**
|
||||||
|
- Wenn ein Edge-Typ nicht gemappt ist:
|
||||||
|
- Edge kann trotzdem im Graph auftauchen,
|
||||||
|
- aber Template/Constraint-Logik kann ihn nicht “verstehen” → führt zu Findings.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5.3 analysis_policies.yaml – “Wie noisy dürfen Findings sein?”
|
||||||
|
**Zweck:**
|
||||||
|
- Zentrale Policies für Findings:
|
||||||
|
- welche Finding-Codes existieren
|
||||||
|
- Default-Severity (info/warn/error)
|
||||||
|
- Profilabhängige Overrides
|
||||||
|
- Unterdrückungsregeln (z.B. suppress in soft mode, suppress wenn confirmed, suppress wenn Score hoch…)
|
||||||
|
|
||||||
|
**Interpretation:**
|
||||||
|
- Policies sind “produktseitige UX-Regeln”:
|
||||||
|
- Discovery: eher informativ, weniger warn
|
||||||
|
- Decisioning: klare Warnungen, wenn Qualität fehlt
|
||||||
|
- Der bereits umgesetzte Fix (`missing_link_constraints` nur in strict) ist exakt so eine Policy-Entscheidung (auch wenn technisch im Inspector gelöst).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6) Ablauf des Chain Inspectors (Vorgehensweise in Mindnet)
|
||||||
|
|
||||||
|
Hier ist ein konsistenter “Pipeline”-Ablauf, der zu den Logs passt:
|
||||||
|
|
||||||
|
### Schritt 1: Kontext bestimmen
|
||||||
|
- Aktuelle Datei + aktuelle Section/Heading
|
||||||
|
|
||||||
|
### Schritt 2: Edges aus aktueller Note laden
|
||||||
|
- Outgoing aus der aktuellen Section extrahieren (oder aus einem definierten Block)
|
||||||
|
- (optional/offen) Note-Level Edges ebenfalls laden und für jede Section gültig machen
|
||||||
|
|
||||||
|
### Schritt 3: Nachbarn laden
|
||||||
|
- Backlinks (Notes, die auf die aktuelle Note verlinken) → incoming Kandidatenquellen
|
||||||
|
- Outgoing Neighbor Notes (Notes, auf die aktuelle Note verweist) → Nachbarschaft erweitern
|
||||||
|
|
||||||
|
### Schritt 4: Edges aus Neighbor Notes laden
|
||||||
|
- Aus den verlinkenden Notes die Edges extrahieren, die auf die aktuelle Note/Section zielen
|
||||||
|
- Canonicalization: rawEdgeTypes via chain_roles.yaml in Rollen überführen
|
||||||
|
|
||||||
|
### Schritt 5: Kandidatenfilter / Scopefilter anwenden
|
||||||
|
- Wenn `includeCandidates=false`:
|
||||||
|
- `scope: candidate` aus effective graph entfernen
|
||||||
|
- Optional weitere Filter:
|
||||||
|
- includeNoteLinks / includeSectionLinks
|
||||||
|
- direction (forward/backward/both)
|
||||||
|
- maxDepth (Traversal)
|
||||||
|
|
||||||
|
### Schritt 6: Pfade berechnen (Paths)
|
||||||
|
- Forward/Backward (oder both)
|
||||||
|
- BFS/DFS bis `maxDepth`
|
||||||
|
- Resultat: Pfadlisten mit nodes + edges
|
||||||
|
|
||||||
|
### Schritt 7: Template Matching
|
||||||
|
- Kandidatenknoten für Slots finden (via noteType + Nähe + Pfade)
|
||||||
|
- Links/Constraints prüfen (erwartete slot→slot Beziehungen)
|
||||||
|
- Score berechnen (z.B. per:
|
||||||
|
- Slots erfüllt
|
||||||
|
- Link constraints erfüllt
|
||||||
|
- “RoleEvidence” passend)
|
||||||
|
|
||||||
|
### Schritt 8: Findings berechnen (Gap-Heuristics)
|
||||||
|
Beispiele:
|
||||||
|
- `missing_slot_*` wenn wichtige Slots fehlen (abhängig von Profil-Thresholds)
|
||||||
|
- `one_sided_connectivity` wenn nur incoming oder nur outgoing
|
||||||
|
- `no_causal_roles` wenn Edges da, aber keine causal Rollen im effektiven Graph
|
||||||
|
- `missing_link_constraints` nur wenn effectiveRequiredLinks=true und slotsComplete=true, requiredLinks>0, linksComplete=false
|
||||||
|
|
||||||
|
### Schritt 9: Report ausgeben
|
||||||
|
- context, settings, neighbors, paths, findings, analysisMeta, templateMatches
|
||||||
|
- Transparenz: satisfiedLinks/requiredLinks/linksComplete bleiben sichtbar
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7) Strategien, die Mindnet verfolgen kann (Produkt-/UX-Strategie)
|
||||||
|
|
||||||
|
### Strategie A: Discovery (Exploration)
|
||||||
|
**Ziel:** Möglichst schnell “wo könnte eine sinnvolle Kette entstehen?” finden.
|
||||||
|
- required_links = false (Soft Mode)
|
||||||
|
- includeCandidates = true (optional)
|
||||||
|
- findings eher informativ (info), weniger warn
|
||||||
|
- Templates mehr als Vorschläge (“plausible/weak”), nicht als harte Bewertung
|
||||||
|
|
||||||
|
**Vorteil:** Nutzer bekommt schnell Hypothesen.
|
||||||
|
**Risiko:** Mehr Noise, mehr falsche Kandidaten – muss per Policy gedämpft werden.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Strategie B: Decisioning (Qualitätskontrolle)
|
||||||
|
**Ziel:** Prüfung, ob eine Kette “wirklich steht” und als belastbar gelten kann.
|
||||||
|
- required_links = true (Strict Mode)
|
||||||
|
- includeCandidates = false
|
||||||
|
- findings: warn, wenn Slots/Links fehlen
|
||||||
|
- “confirmed” nur wenn link constraints komplett
|
||||||
|
|
||||||
|
**Vorteil:** Qualitätssicherung & Verlässlichkeit.
|
||||||
|
**Risiko:** Nutzer fühlt sich “blockiert”, wenn Graph noch im Aufbau ist.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Strategie C: Progressive Disclosure (hybrid)
|
||||||
|
**Ziel:** Nutzer nicht überfordern, aber zielgerichtet verbessern.
|
||||||
|
- Soft Mode für Einstieg
|
||||||
|
- Button/Toggle: “Strict prüfen”
|
||||||
|
- Candidate Edges als Vorschlag-Klasse (UI: “proposed edges”)
|
||||||
|
- Findings priorisieren: erst fehlende Slots, dann fehlende Links, dann Detail-Qualität
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8) Wie ein “kausaler Retriever” funktionieren könnte (Causal Retriever)
|
||||||
|
|
||||||
|
Ein kausaler Retriever ist die Komponente, die aus dem Vault/Graphen **relevante Kausalkontexte** für den aktuellen Abschnitt liefert – idealerweise deterministisch, skalierbar und template-aware.
|
||||||
|
|
||||||
|
### 8.1 Retrieval-Ziele
|
||||||
|
- Finde Knoten/Edges, die **kausal relevant** sind zum aktuellen Kontext:
|
||||||
|
- Ursachen (backward)
|
||||||
|
- Wirkungen/Entscheidungen (forward)
|
||||||
|
- Bedingungen/Constraints (seitlich)
|
||||||
|
- Gib nicht nur Knoten zurück, sondern:
|
||||||
|
- Pfade (explainable)
|
||||||
|
- Evidence (wo steht das)
|
||||||
|
- Role-Interpretation (warum ist das causal/influences/etc.)
|
||||||
|
|
||||||
|
### 8.2 Retrieval-Inputs
|
||||||
|
- startNode = current section
|
||||||
|
- direction = forward/backward/both
|
||||||
|
- maxDepth
|
||||||
|
- roleFilter (optional): nur causal/influences/enables_constraints
|
||||||
|
- scopeFilter: includeCandidates, includeNoteLevel
|
||||||
|
- templateBias: bevorzugte Pfadformen (z.B. “experience→insight→decision”)
|
||||||
|
|
||||||
|
### 8.3 Retrieval-Algorithmus (praktisch)
|
||||||
|
**Variante 1: BFS mit Rolle-Gewichtung**
|
||||||
|
- BFS über Kanten
|
||||||
|
- Priorität/Score pro Frontier:
|
||||||
|
- causal > influences > provenance
|
||||||
|
- section-scope > note-scope > candidate (wenn candidates eingeschaltet, sonst candidate=∞)
|
||||||
|
- Stop, wenn:
|
||||||
|
- maxDepth erreicht
|
||||||
|
- genug Top-N Pfade gesammelt (z.B. topNUsed)
|
||||||
|
|
||||||
|
**Variante 2: Template-driven Retrieval**
|
||||||
|
- Wenn ein Template im Fokus ist:
|
||||||
|
- suche explizit nach Slot-Knoten (noteType matching)
|
||||||
|
- suche dann die minimalen Verbindungen, die Constraints erfüllen
|
||||||
|
- Gute Option für “Decisioning”: deterministisch prüfen.
|
||||||
|
|
||||||
|
**Variante 3: Two-phase Retrieval**
|
||||||
|
1) Kandidaten finden (Slots)
|
||||||
|
2) Verbindungen prüfen (Constraints)
|
||||||
|
→ Liefert sehr gut “warum fehlt Link X?” Diagnosen.
|
||||||
|
|
||||||
|
### 8.4 Output-Format
|
||||||
|
- `neighbors` (incoming/outgoing, mit evidence)
|
||||||
|
- `paths` (forward/backward, nodes+edges)
|
||||||
|
- plus “slot candidates” optional (für UI)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9) Empfehlungen für robuste Tests (damit ihr nicht wieder im Kreis lauft)
|
||||||
|
|
||||||
|
### Was ist bereits ausreichend getestet (nicht wiederholen)
|
||||||
|
- includeCandidates Filterverhalten ✅
|
||||||
|
- missing_link_constraints Unterdrückung bei required_links=false ✅
|
||||||
|
- strict/soft required_links via profile/template override ✅
|
||||||
|
- “healthy graph” ergibt findings: [] ✅
|
||||||
|
- unmapped edge type triggert Diagnose ✅
|
||||||
|
|
||||||
|
### Was als einziges “neues” Testziel für Abschluss 0.4.x/Start 0.5.x taugt
|
||||||
|
- **Note-level edges / note-scope**: gelten Kanten “global” pro Note oder nicht?
|
||||||
|
|
||||||
|
**Minimal-Testdefinition (einmalig, reproduzierbar):**
|
||||||
|
1) In `02_event_trigger_detail.md` einen klaren Note-Level Block definieren (z.B. “## Note-Verbindungen”).
|
||||||
|
2) Edge dort definieren, die auf eine andere Note/Section zeigt.
|
||||||
|
3) Cursor in einer anderen Section derselben Note platzieren (z.B. “## Detail” oder “## Extra”).
|
||||||
|
4) Chain Inspector laufen lassen.
|
||||||
|
5) Erwartung:
|
||||||
|
- Edge erscheint trotzdem als outgoing/incoming
|
||||||
|
- evidence zeigt auf den Note-Level Block
|
||||||
|
- ideal: `scope: note`
|
||||||
|
|
||||||
|
Wenn das FAIL ist → klarer 0.5.0 Task.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10) Implikationen für 0.5.x / 0.6.x (wohin sinnvoll weiter)
|
||||||
|
|
||||||
|
### 0.5.x (Stabilisierung)
|
||||||
|
- Note-level edge scope finalisieren (inkl. Report-Transparenz)
|
||||||
|
- policies (analysis_policies) als zentrale Noise-Steuerung weiter ausbauen
|
||||||
|
- Debug/Explainability weiter verbessern (effectiveRequiredLinks pro Match explizit ausgeben)
|
||||||
|
|
||||||
|
### 0.6.x (UX & Workflows)
|
||||||
|
- Actionable Findings: “Was genau soll ich ändern?” inkl. Vorschlagtext oder Snippet
|
||||||
|
- UI-Toggles: Strict/Soft, Candidates on/off
|
||||||
|
- Template Authoring Tools: Linter, “Warum kein Match?”
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11) Kurzes “Was heißt das für Mindnet im Alltag?”
|
||||||
|
- Im Discovery-Modus: Mindnet ist ein **Explorationswerkzeug** (Hypothesen + Hinweise, wenig Warnungen).
|
||||||
|
- Im Decisioning-Modus: Mindnet ist ein **Qualitätsprüfer** (strict, wenige false positives).
|
||||||
|
- Der nächste große Hebel ist Note-scope: Damit wird Pflege einfacher und Ketten werden “wartbarer”.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 12) Appendix: Beispielhafte Report-Signale (Interpretationshilfe)
|
||||||
|
|
||||||
|
- `findings: []` + `confidence: confirmed`
|
||||||
|
→ Template passt sauber (Slots + Links vollständig im gewählten Modus).
|
||||||
|
|
||||||
|
- `linksComplete=false` aber `required_links=false` und **kein** `missing_link_constraints`
|
||||||
|
→ Soft Mode: bewusst kein “Warn-Noise”, aber Transparenz bleibt.
|
||||||
|
|
||||||
|
- `no_causal_roles`
|
||||||
|
→ Edges existieren, aber keine davon wird als “causal” interpretiert (Mapping oder rawEdgeType Problem).
|
||||||
|
|
||||||
|
- `edgesUnmapped > 0`
|
||||||
|
→ chain_roles unvollständig oder Edge-Typ ist neu/fehlerhaft geschrieben.
|
||||||
|
|
||||||
|
- `effectiveIncoming=0` bei includeCandidates=false, aber incoming candidate-edge existiert
|
||||||
|
→ Filter funktioniert wie geplant.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
ENDE
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
---
|
---
|
||||||
doc_type: concept
|
doc_type: concept
|
||||||
audience: architect, product_owner
|
audience: architect, product_owner
|
||||||
scope: ai, router, personas, resilience
|
scope: ai, router, personas, resilience, agentic_rag, moe, lazy_prompts
|
||||||
status: active
|
status: active
|
||||||
version: 2.8.1
|
version: 3.1.1
|
||||||
context: "Fachkonzept der hybriden KI-Persönlichkeit, der Provider-Kaskade und der kognitiven Resilienz (Deep Fallback)."
|
context: "Fachkonzept der hybriden KI-Persönlichkeit, Agentic Multi-Stream RAG, WP-25a Mixture of Experts (MoE), WP-25b Lazy-Prompt-Orchestration, Provider-Kaskade und kognitiven Resilienz (Deep Fallback)."
|
||||||
---
|
---
|
||||||
|
|
||||||
# Konzept: KI-Persönlichkeit & Router
|
# Konzept: KI-Persönlichkeit & Router
|
||||||
@@ -13,13 +13,45 @@ context: "Fachkonzept der hybriden KI-Persönlichkeit, der Provider-Kaskade und
|
|||||||
|
|
||||||
Mindnet soll nicht wie eine Suchmaschine wirken, sondern wie ein **Digitaler Zwilling**. Dazu muss das System erkennen, **was** der Nutzer will, und seine „Persönlichkeit“ sowie seine technische Infrastruktur dynamisch anpassen.
|
Mindnet soll nicht wie eine Suchmaschine wirken, sondern wie ein **Digitaler Zwilling**. Dazu muss das System erkennen, **was** der Nutzer will, und seine „Persönlichkeit“ sowie seine technische Infrastruktur dynamisch anpassen.
|
||||||
|
|
||||||
## 1. Der Hybrid Router (Das Gehirn)
|
## 1. Der Hybrid Router & Agentic Multi-Stream RAG (Das Gehirn)
|
||||||
|
|
||||||
Jede Eingabe durchläuft den **Hybrid Router**. Er entscheidet über die fachliche Strategie und die technische Ausführung.
|
Jede Eingabe durchläuft den **Hybrid Router**. Seit WP-25 agiert das System als **Agentic Orchestrator**, der Nutzeranfragen analysiert, in parallele Wissens-Streams aufteilt und diese zu einer kontextreichen, wertebasierten Antwort synthetisiert.
|
||||||
|
|
||||||
### Modus A: RAG (Retrieval Augmented Generation)
|
### Intent-basiertes Routing (WP-25)
|
||||||
* **Intent:** Der Nutzer hat eine Frage oder ein Problem (`FACT`, `DECISION`, `EMPATHY`).
|
|
||||||
* **Aktion:** Das System sucht im Gedächtnis und generiert eine Antwort.
|
Der Router nutzt einen **Hybrid-Modus** mit Keyword Fast-Path und LLM Slow-Path:
|
||||||
|
|
||||||
|
**Keyword Fast-Path:**
|
||||||
|
* Sofortige Erkennung von Triggern wie "Soll ich", "Wann", "Was ist"
|
||||||
|
* Reduziert Latenz durch schnelle Keyword-Erkennung ohne LLM-Call
|
||||||
|
|
||||||
|
**LLM Slow-Path:**
|
||||||
|
* Komplexe semantische Analyse für unklare Anfragen
|
||||||
|
* Nutzt `intent_router_v1` Prompt zur Klassifizierung
|
||||||
|
|
||||||
|
**Strategien:**
|
||||||
|
* **FACT_WHAT/FACT_WHEN:** Wissensabfrage (Wissen/Listen, Zeitpunkte)
|
||||||
|
* **DECISION:** Beratung (Rat, Strategie, Abwägung)
|
||||||
|
* **EMPATHY:** Reflexion (Emotionale Resonanz)
|
||||||
|
* **CODING:** Technik (Programmierung, Syntax)
|
||||||
|
* **INTERVIEW:** Datenerfassung (Wissen speichern)
|
||||||
|
|
||||||
|
### Modus A: Agentic Multi-Stream RAG (WP-25)
|
||||||
|
|
||||||
|
Anstelle einer einzelnen Suche führt das System **parallele Abfragen** in spezialisierten Wissens-Streams aus:
|
||||||
|
|
||||||
|
**Stream-Library:**
|
||||||
|
* **Values Stream:** Identität, Ethik und Prinzipien (`value`, `principle`, `belief`, `trait`, `boundary`, `need`, `motivation`)
|
||||||
|
* **Facts Stream:** Operative Daten (`project`, `decision`, `task`, `goal`, `event`, `state`)
|
||||||
|
* **Biography Stream:** Persönliche Erfahrungen (`experience`, `journal`, `profile`, `person`)
|
||||||
|
* **Risk Stream:** Hindernisse und Gefahren (`risk`, `obstacle`, `bias`)
|
||||||
|
* **Tech Stream:** Technisches Wissen (`concept`, `source`, `glossary`, `idea`, `insight`, `skill`, `habit`)
|
||||||
|
|
||||||
|
**Wissens-Synthese:**
|
||||||
|
Die Zusammenführung erfolgt über spezialisierte Templates mit expliziten Stream-Variablen (z.B. `{values_stream}`, `{risk_stream}`). Dies ermöglicht dem LLM eine differenzierte Abwägung zwischen Fakten und persönlichen Werten.
|
||||||
|
|
||||||
|
**Stream-Tracing:**
|
||||||
|
Jeder Treffer wird mit `stream_origin` markiert, um Feedback-Optimierung pro Wissensbereich zu ermöglichen.
|
||||||
|
|
||||||
### Modus B: Interview (Knowledge Capture)
|
### Modus B: Interview (Knowledge Capture)
|
||||||
* **Intent:** Der Nutzer will Wissen speichern (`INTERVIEW`).
|
* **Intent:** Der Nutzer will Wissen speichern (`INTERVIEW`).
|
||||||
@@ -27,13 +59,45 @@ Jede Eingabe durchläuft den **Hybrid Router**. Er entscheidet über die fachlic
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 2. Die hybride LLM-Landschaft (Resilienz-Kaskade)
|
## 2. Mixture of Experts (MoE) Architektur (WP-25a)
|
||||||
|
|
||||||
Ein intelligenter Zwilling muss jederzeit verfügbar sein. Mindnet v2.8.1 nutzt eine **dreistufige Kaskade**, um Intelligenz, Kosten und Verfügbarkeit zu optimieren:
|
Seit WP-25a nutzt MindNet eine **profilbasierte Experten-Steuerung** statt einer globalen Provider-Konfiguration. Jede Systemaufgabe wird einem dedizierten Profil zugewiesen, das Modell, Provider und Parameter unabhängig definiert.
|
||||||
|
|
||||||
1. **Stufe 1: Cloud-Speed (Turbo-Mode):** Primäre Wahl für komplexe Extraktionsaufgaben und schnelle RAG-Antworten mittels OpenRouter (Mistral-7B) oder Google Gemini (2.5-flash-lite).
|
### 2.1 Experten-Profile
|
||||||
|
|
||||||
|
**Zentrale Registry (`llm_profiles.yaml`):**
|
||||||
|
* **`synthesis_pro`:** Hochwertige Synthese für Chat-Antworten (Cloud)
|
||||||
|
* **`tech_expert`:** Fachspezialist für Code & Technik (Claude 3.5 Sonnet)
|
||||||
|
* **`compression_fast`:** Schnelle Kompression & Routing (Mistral 7B)
|
||||||
|
* **`ingest_validator`:** Deterministische Validierung (Temperature 0.0)
|
||||||
|
* **`identity_safe`:** Lokaler Anker (Ollama/Phi-3) für maximale Privacy
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
* **Aufgabenspezifische Optimierung:** Jede Aufgabe nutzt das optimale Modell
|
||||||
|
* **Hardware-Optimierung:** Lokaler Anker für kleine Hardware-Umgebungen
|
||||||
|
* **Wartbarkeit:** Zentrale Konfiguration statt verstreuter ENV-Variablen
|
||||||
|
|
||||||
|
### 2.2 Rekursive Fallback-Kaskade
|
||||||
|
|
||||||
|
Die Profile implementieren eine **automatische Fallback-Logik**:
|
||||||
|
|
||||||
|
1. **Primäres Profil:** System versucht das angeforderte Profil (z.B. `synthesis_pro`)
|
||||||
|
2. **Fallback-Level 1:** Bei Fehler → `fallback_profile` (z.B. `synthesis_backup`)
|
||||||
|
3. **Fallback-Level 2:** Bei weiterem Fehler → nächster Fallback (z.B. `identity_safe`)
|
||||||
|
4. **Terminaler Endpunkt:** `identity_safe` hat keinen Fallback (lokales Modell als letzte Instanz)
|
||||||
|
|
||||||
|
**Schutzmechanismen:**
|
||||||
|
* **Zirkuläre Referenzen:** `visited_profiles`-Tracking verhindert Endlosschleifen
|
||||||
|
* **Background-Semaphore:** Parallele Tasks werden gedrosselt
|
||||||
|
|
||||||
|
### 2.3 Die hybride LLM-Landschaft (Legacy & MoE)
|
||||||
|
|
||||||
|
Ein intelligenter Zwilling muss jederzeit verfügbar sein. Seit WP-25a wird die Resilienz durch die **MoE Fallback-Kaskade** gewährleistet:
|
||||||
|
|
||||||
|
1. **Stufe 1: Cloud-Experten:** Spezialisierte Profile für verschiedene Aufgaben (z.B. `synthesis_pro`, `tech_expert`)
|
||||||
2. **Stufe 2: Quoten-Resilienz:** Erkennt das System eine Drosselung durch Cloud-Provider (HTTP 429), pausiert es kontrolliert (`LLM_RATE_LIMIT_WAIT`), führt automatisierte Retries durch und schützt so den laufenden Prozess.
|
2. **Stufe 2: Quoten-Resilienz:** Erkennt das System eine Drosselung durch Cloud-Provider (HTTP 429), pausiert es kontrolliert (`LLM_RATE_LIMIT_WAIT`), führt automatisierte Retries durch und schützt so den laufenden Prozess.
|
||||||
3. **Stufe 3: Deep Fallback & lokale Souveränität (Ollama):** * **Technischer Fallback:** Schlagen alle Cloud-Versuche fehl, übernimmt das lokale Modell (Phi-3).
|
3. **Stufe 3: Lokale Souveränität (Ollama):**
|
||||||
|
* **Technischer Fallback:** Schlagen alle Cloud-Versuche fehl, übernimmt das lokale Modell (Phi-3) via `identity_safe` Profil.
|
||||||
* **Kognitiver Fallback (v2.11.14):** Liefert die Cloud zwar technisch eine Antwort, verweigert aber inhaltlich die Verarbeitung (Silent Refusal/Policy Violation), wird ein **Deep Fallback** erzwungen, um die Datenintegrität lokal zu retten.
|
* **Kognitiver Fallback (v2.11.14):** Liefert die Cloud zwar technisch eine Antwort, verweigert aber inhaltlich die Verarbeitung (Silent Refusal/Policy Violation), wird ein **Deep Fallback** erzwungen, um die Datenintegrität lokal zu retten.
|
||||||
|
|
||||||
|
|
||||||
@@ -45,18 +109,22 @@ Ein intelligenter Zwilling muss jederzeit verfügbar sein. Mindnet v2.8.1 nutzt
|
|||||||
Mindnet wechselt den Hut, je nach Situation.
|
Mindnet wechselt den Hut, je nach Situation.
|
||||||
|
|
||||||
### 3.1 Der Berater (Strategy: DECISION)
|
### 3.1 Der Berater (Strategy: DECISION)
|
||||||
* **Auslöser:** Fragen wie „Soll ich...?“, „Was ist besser?“.
|
* **Auslöser:** Fragen wie „Soll ich...?", „Was ist besser?", „Empfehlung...".
|
||||||
* **Strategic Retrieval:** Lädt aktiv Notizen der Typen `value` (Werte), `goal` (Ziele) und `risk` (Risiken), auch wenn sie im Text nicht direkt vorkommen.
|
* **Multi-Stream Retrieval (WP-25):** Führt parallele Abfragen in Values Stream, Facts Stream und Risk Stream aus.
|
||||||
* **Reasoning:** *„Wäge die Fakten gegen meine Werte ab. Sei strikt bei Risiken.“*
|
* **Wissens-Synthese:** Wägt Fakten gegen Werte ab, evaluiert Risiken und prüft Kompatibilität mit langfristiger Identität.
|
||||||
|
* **Reasoning:** *„Wäge die Fakten gegen meine Werte ab. Sei strikt bei Risiken."*
|
||||||
|
|
||||||
### 3.2 Der Spiegel (Strategy: EMPATHY)
|
### 3.2 Der Spiegel (Strategy: EMPATHY)
|
||||||
* **Auslöser:** Emotionale Aussagen („Ich bin frustriert“).
|
* **Auslöser:** Emotionale Aussagen („Ich bin frustriert", „Ich fühle...", „Stress...").
|
||||||
* **Strategic Retrieval:** Lädt `experience` (Erfahrungen) und `belief` (Glaubenssätze).
|
* **Multi-Stream Retrieval (WP-25):** Führt parallele Abfragen in Biography Stream und Values Stream aus.
|
||||||
* **Reasoning:** *„Nutze meine eigenen Erfahrungen, um die Situation einzuordnen.“*
|
* **Wissens-Synthese:** Greift auf persönliche Erfahrungen und Werte zurück, um emotionale Resonanz zu schaffen.
|
||||||
|
* **Reasoning:** *„Nutze meine eigenen Erfahrungen, um die Situation einzuordnen."*
|
||||||
|
|
||||||
### 3.3 Der Bibliothekar (Strategy: FACT)
|
### 3.3 Der Bibliothekar (Strategy: FACT_WHAT / FACT_WHEN)
|
||||||
* **Auslöser:** Sachfragen („Was ist Qdrant?“).
|
* **Auslöser:** Sachfragen („Was ist...?", „Welche sind...?", „Wann...?", „Datum...").
|
||||||
* **Behavior:** Präzise, neutral, kurz.
|
* **Multi-Stream Retrieval (WP-25):** Führt parallele Abfragen in Facts Stream, Tech Stream und Biography Stream aus.
|
||||||
|
* **Wissens-Synthese:** Kombiniert harte Fakten mit persönlichen Erfahrungen, falls vorhanden.
|
||||||
|
* **Behavior:** Präzise, neutral, strukturiert.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,456 @@
|
|||||||
|
---
|
||||||
|
doc_type: concept
|
||||||
|
audience: architect, developer
|
||||||
|
scope: architecture, design_patterns, modularization
|
||||||
|
status: active
|
||||||
|
version: 2.9.1
|
||||||
|
context: "Architektur-Patterns, Design-Entscheidungen und modulare Struktur von Mindnet. Basis für Verständnis und Erweiterungen."
|
||||||
|
---
|
||||||
|
|
||||||
|
# Architektur-Patterns & Design-Entscheidungen
|
||||||
|
|
||||||
|
Dieses Dokument beschreibt die zentralen Architektur-Patterns und Design-Entscheidungen, die Mindnet prägen. Es dient als Referenz für Entwickler und Architekten, um das System zu verstehen und konsistent zu erweitern.
|
||||||
|
|
||||||
|
## 1. Kern-Paradigmen
|
||||||
|
|
||||||
|
### 1.1 Filesystem First (Source of Truth)
|
||||||
|
|
||||||
|
**Prinzip:** Markdown-Dateien im Vault sind die einzige Quelle der Wahrheit. Die Datenbank (Qdrant) ist ein abgeleiteter Index.
|
||||||
|
|
||||||
|
**Implikationen:**
|
||||||
|
- Dateien werden immer direkt von der Festplatte gelesen (z.B. im Editor)
|
||||||
|
- Datenbank-Inhalte können aus Markdown rekonstruiert werden
|
||||||
|
- Änderungen erfolgen primär im Vault, nicht in der DB
|
||||||
|
- Die Datenbank ist ein Cache, kein primärer Speicher
|
||||||
|
|
||||||
|
**Code-Beispiele:**
|
||||||
|
- `ui_callbacks.py`: Liest Dateien direkt von Disk
|
||||||
|
- `ingestion_processor.py`: Schreibt zuerst auf Disk, dann in DB
|
||||||
|
|
||||||
|
### 1.2 Late Binding (Späte Semantik)
|
||||||
|
|
||||||
|
**Prinzip:** Struktur und Interpretation werden in Konfigurationen definiert, nicht im Code.
|
||||||
|
|
||||||
|
**Implikationen:**
|
||||||
|
- Neue Note-Typen werden in `types.yaml` definiert, nicht im Code
|
||||||
|
- Prompt-Templates sind in `prompts.yaml` konfigurierbar
|
||||||
|
- Edge-Typen werden in `edge_vocabulary.md` verwaltet
|
||||||
|
- Die "Persönlichkeit" ist Config, kein Code
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Erweiterbarkeit ohne Code-Änderungen
|
||||||
|
- A/B-Testing von Prompt-Strategien
|
||||||
|
- Anpassung an verschiedene Use-Cases
|
||||||
|
|
||||||
|
### 1.3 Virtual Schema Layer
|
||||||
|
|
||||||
|
**Prinzip:** Markdown-Dateien benötigen nur minimale Frontmatter-Angaben. Komplexität wird zur Laufzeit injiziert.
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
```yaml
|
||||||
|
# Minimal im Frontmatter
|
||||||
|
---
|
||||||
|
id: 20250101-test
|
||||||
|
title: Test
|
||||||
|
type: project
|
||||||
|
---
|
||||||
|
|
||||||
|
# Im Code wird zur Laufzeit ergänzt:
|
||||||
|
# - chunking_profile aus types.yaml
|
||||||
|
# - retriever_weight aus types.yaml
|
||||||
|
# - edge_defaults aus types.yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Robuste Markdown-Dateien (weniger Breaking Changes)
|
||||||
|
- Zentrale Verwaltung von Logik
|
||||||
|
- Einfache Migration zwischen Versionen
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Modulare Architektur (WP-14)
|
||||||
|
|
||||||
|
### 2.1 Paket-Struktur
|
||||||
|
|
||||||
|
Seit WP-14 ist die Core-Logik in spezialisierte Pakete unterteilt:
|
||||||
|
|
||||||
|
```
|
||||||
|
app/core/
|
||||||
|
├── chunking/ # Text-Segmentierung
|
||||||
|
│ ├── chunking_strategies.py # Sliding/Heading-Strategien
|
||||||
|
│ ├── chunking_processor.py # Orchestrierung
|
||||||
|
│ └── chunking_propagation.py # Edge-Vererbung
|
||||||
|
├── database/ # Qdrant-Infrastruktur
|
||||||
|
│ ├── qdrant.py # Client & Config
|
||||||
|
│ └── qdrant_points.py # Point-Mapping
|
||||||
|
├── graph/ # Graph-Logik
|
||||||
|
│ ├── graph_subgraph.py # Expansion
|
||||||
|
│ └── graph_weights.py # Scoring
|
||||||
|
├── ingestion/ # Import-Pipeline
|
||||||
|
│ ├── ingestion_processor.py # Two-Pass Workflow
|
||||||
|
│ └── ingestion_validation.py # Mistral-safe Parsing
|
||||||
|
├── parser/ # Markdown-Parsing
|
||||||
|
│ ├── parsing_markdown.py # Frontmatter/Body
|
||||||
|
│ └── parsing_scanner.py # File-Scan
|
||||||
|
└── retrieval/ # Suche & Scoring
|
||||||
|
├── retriever.py # Orchestrator
|
||||||
|
└── retriever_scoring.py # Mathematik
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2.2 Design-Pattern: Proxy-Adapter (Abwärtskompatibilität)
|
||||||
|
|
||||||
|
**Problem:** Nach WP-14 Modularisierung müssen alte Import-Pfade weiter funktionieren.
|
||||||
|
|
||||||
|
**Lösung:** Proxy-Module leiten Anfragen an neue Pakete weiter.
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
```python
|
||||||
|
# app/core/retriever.py (Proxy)
|
||||||
|
from .retrieval.retriever import (
|
||||||
|
Retriever, hybrid_retrieve, semantic_retrieve
|
||||||
|
)
|
||||||
|
__all__ = ["Retriever", "hybrid_retrieve", "semantic_retrieve"]
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Keine Breaking Changes für bestehenden Code
|
||||||
|
- Graduelle Migration möglich
|
||||||
|
- Alte Scripts funktionieren weiter
|
||||||
|
|
||||||
|
### 2.3 Design-Pattern: Singleton (Edge Registry)
|
||||||
|
|
||||||
|
**Problem:** Edge Registry muss konsistent über alle Services sein.
|
||||||
|
|
||||||
|
**Lösung:** Singleton-Pattern mit Lazy Loading.
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# app/services/edge_registry.py
|
||||||
|
class EdgeRegistry:
|
||||||
|
_instance = None
|
||||||
|
|
||||||
|
def __new__(cls):
|
||||||
|
if cls._instance is None:
|
||||||
|
cls._instance = super().__new__(cls)
|
||||||
|
return cls._instance
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Einheitliche Validierung
|
||||||
|
- Keine Duplikation von Vokabular-Daten
|
||||||
|
- Thread-safe Initialisierung
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. Asynchrone Verarbeitung
|
||||||
|
|
||||||
|
### 3.1 Background Tasks Pattern
|
||||||
|
|
||||||
|
**Problem:** Ingestion kann lange dauern (LLM-Calls, Embeddings). Blockiert API nicht.
|
||||||
|
|
||||||
|
**Lösung:** FastAPI Background Tasks für non-blocking Verarbeitung.
|
||||||
|
|
||||||
|
**Flow:**
|
||||||
|
1. API empfängt Request (`/ingest/save`)
|
||||||
|
2. Datei wird sofort auf Disk geschrieben
|
||||||
|
3. API antwortet mit `status: "queued"`
|
||||||
|
4. Background Task startet Ingestion asynchron
|
||||||
|
5. User kann weiterarbeiten
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
@router.post("/save")
|
||||||
|
async def save_note(req: SaveRequest, background_tasks: BackgroundTasks):
|
||||||
|
# Sofortige Persistenz
|
||||||
|
write_to_disk(req.markdown_content)
|
||||||
|
|
||||||
|
# Async Processing
|
||||||
|
background_tasks.add_task(run_ingestion_task, ...)
|
||||||
|
|
||||||
|
return SaveResponse(status="queued", ...)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Keine Timeouts bei großen Dateien
|
||||||
|
- Bessere User Experience
|
||||||
|
- System bleibt responsiv
|
||||||
|
|
||||||
|
### 3.2 Traffic Control (Semaphore Pattern)
|
||||||
|
|
||||||
|
**Problem:** Parallele LLM-Calls können System überlasten.
|
||||||
|
|
||||||
|
**Lösung:** Semaphore begrenzt parallele Hintergrund-Tasks.
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# app/services/llm_service.py
|
||||||
|
background_semaphore = asyncio.Semaphore(
|
||||||
|
settings.BACKGROUND_LIMIT # Default: 2
|
||||||
|
)
|
||||||
|
|
||||||
|
async def generate(...):
|
||||||
|
if priority == "background":
|
||||||
|
async with background_semaphore:
|
||||||
|
return await _call_llm(...)
|
||||||
|
else: # realtime
|
||||||
|
return await _call_llm(...) # Keine Limitierung
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Schutz vor API-Quoten-Überschreitung
|
||||||
|
- Stabiler Betrieb bei hoher Last
|
||||||
|
- Priorisierung von Echtzeit-Anfragen
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. Resilienz-Patterns
|
||||||
|
|
||||||
|
### 4.1 Provider-Kaskade (Fallback-Kette)
|
||||||
|
|
||||||
|
**Problem:** Cloud-Provider können ausfallen oder Quoten überschreiten.
|
||||||
|
|
||||||
|
**Lösung:** Dreistufige Kaskade mit intelligentem Fallback.
|
||||||
|
|
||||||
|
**Stufen:**
|
||||||
|
1. **Cloud (OpenRouter/Gemini):** Schnell, aber abhängig von Provider
|
||||||
|
2. **Rate-Limit-Handling:** Automatische Retries bei HTTP 429
|
||||||
|
3. **Lokaler Fallback (Ollama):** Langsam, aber immer verfügbar
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# app/services/llm_service.py
|
||||||
|
async def generate(...):
|
||||||
|
try:
|
||||||
|
return await _call_cloud(...)
|
||||||
|
except RateLimitError:
|
||||||
|
await asyncio.sleep(LLM_RATE_LIMIT_WAIT)
|
||||||
|
return await _call_cloud(...) # Retry
|
||||||
|
except Exception:
|
||||||
|
return await _call_ollama(...) # Fallback
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Hohe Verfügbarkeit
|
||||||
|
- Kostenoptimierung (Cloud für Speed, Lokal für Fallback)
|
||||||
|
- Resilienz gegen Provider-Ausfälle
|
||||||
|
|
||||||
|
### 4.2 Deep Fallback (Kognitiver Fallback)
|
||||||
|
|
||||||
|
**Problem:** Cloud liefert technisch erfolgreiche, aber inhaltlich leere Antworten (Silent Refusal).
|
||||||
|
|
||||||
|
**Lösung:** Validierung der Antwort-Qualität, nicht nur HTTP-Status.
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
response = await _call_cloud(...)
|
||||||
|
if is_valid_json(response):
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
# Deep Fallback: Cloud hat blockiert, lokaler Fallback
|
||||||
|
return await _call_ollama(...)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Erkennung von Policy-Violations
|
||||||
|
- Datenintegrität trotz Cloud-Filter
|
||||||
|
- Lokale Souveränität
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. Datenfluss-Patterns
|
||||||
|
|
||||||
|
### 5.1 Two-Pass Workflow (WP-15b)
|
||||||
|
|
||||||
|
**Problem:** Smart Edge Validation benötigt globalen Kontext (alle Notizen).
|
||||||
|
|
||||||
|
**Lösung:** Zwei-Phasen-Import mit Pre-Scan.
|
||||||
|
|
||||||
|
**Pass 1: Pre-Scan**
|
||||||
|
- Schnelles Scannen aller Dateien
|
||||||
|
- Aufbau von `LocalBatchCache` (IDs, Titel, Summaries)
|
||||||
|
- Keine LLM-Calls
|
||||||
|
|
||||||
|
**Pass 2: Semantic Processing**
|
||||||
|
- Nur für geänderte Dateien
|
||||||
|
- Binäre Validierung gegen Cache
|
||||||
|
- Effiziente Nutzung von LLM-Quoten
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Reduktion von LLM-Calls (nur Validierung, keine Extraktion)
|
||||||
|
- Konsistente Validierung (globaler Kontext)
|
||||||
|
- Schnellerer Import
|
||||||
|
|
||||||
|
### 5.2 Idempotenz-Pattern
|
||||||
|
|
||||||
|
**Prinzip:** Mehrfache Imports derselben Datei führen zum gleichen Ergebnis.
|
||||||
|
|
||||||
|
**Mechanismen:**
|
||||||
|
- **Deterministische IDs:** UUIDv5 basierend auf Datei-Inhalt
|
||||||
|
- **Hash-basierte Change Detection:** Multi-Hash für `body` und `full`
|
||||||
|
- **Deduplizierung:** Kanten werden anhand Identität erkannt
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# Deterministische ID
|
||||||
|
note_id = uuid.uuid5(NAMESPACE_URL, f"{file_path}#{content_hash}")
|
||||||
|
|
||||||
|
# Change Detection
|
||||||
|
if current_hash == stored_hash:
|
||||||
|
skip_processing() # Idempotent
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Sicherheit bei Re-Imports
|
||||||
|
- Keine Duplikate
|
||||||
|
- Konsistente Datenbank
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. Frontend-Patterns
|
||||||
|
|
||||||
|
### 6.1 Active Inspector Pattern
|
||||||
|
|
||||||
|
**Problem:** Graph-Re-Renders bei Knoten-Selektion führen zu Flackern.
|
||||||
|
|
||||||
|
**Lösung:** CSS-Klassen statt State-Änderungen für Selektion.
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# ui_graph_cytoscape.py
|
||||||
|
stylesheet = [
|
||||||
|
{
|
||||||
|
"selector": ".inspected",
|
||||||
|
"style": {"border-width": 6, "border-color": "#FFC300"}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
# Selektion ändert nur CSS-Klasse, nicht React-Key
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Stabiles UI (kein Re-Render)
|
||||||
|
- Bessere Performance
|
||||||
|
- Smooth User Experience
|
||||||
|
|
||||||
|
### 6.2 Resurrection Pattern
|
||||||
|
|
||||||
|
**Problem:** Streamlit vergisst Eingaben bei Re-Runs.
|
||||||
|
|
||||||
|
**Lösung:** Aggressive Synchronisation in `session_state`.
|
||||||
|
|
||||||
|
**Code:**
|
||||||
|
```python
|
||||||
|
# ui_editor.py
|
||||||
|
if widget_key not in st.session_state:
|
||||||
|
st.session_state[widget_key] = restore_from_data_key()
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Texteingaben überleben Tab-Wechsel
|
||||||
|
- Keine Datenverluste
|
||||||
|
- Bessere UX
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Erweiterbarkeit: "Teach-the-AI" Paradigma
|
||||||
|
|
||||||
|
Mindnet lernt durch **Konfiguration**, nicht durch Training.
|
||||||
|
|
||||||
|
### 7.1 Drei-Ebenen-Erweiterung
|
||||||
|
|
||||||
|
**1. Daten-Ebene (`types.yaml`):**
|
||||||
|
```yaml
|
||||||
|
risk:
|
||||||
|
retriever_weight: 0.90
|
||||||
|
edge_defaults: ["blocks"]
|
||||||
|
```
|
||||||
|
|
||||||
|
**2. Strategie-Ebene (`decision_engine.yaml`):**
|
||||||
|
```yaml
|
||||||
|
DECISION:
|
||||||
|
inject_types: ["value", "risk"]
|
||||||
|
```
|
||||||
|
|
||||||
|
**3. Kognitive Ebene (`prompts.yaml`):**
|
||||||
|
```yaml
|
||||||
|
risk_definition:
|
||||||
|
ollama: "Ein Risiko ist eine potenzielle Gefahr..."
|
||||||
|
```
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Keine Code-Änderungen nötig
|
||||||
|
- Schnelle Iteration
|
||||||
|
- A/B-Testing möglich
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Design-Entscheidungen & Trade-offs
|
||||||
|
|
||||||
|
### 8.1 Qdrant als Vektor-DB
|
||||||
|
|
||||||
|
**Entscheidung:** Qdrant statt Pinecone/Weaviate
|
||||||
|
|
||||||
|
**Gründe:**
|
||||||
|
- Self-hosted (Privacy First)
|
||||||
|
- Open Source
|
||||||
|
- Gute Performance bei lokaler Installation
|
||||||
|
|
||||||
|
**Trade-off:** Mehr Wartungsaufwand als Managed Service
|
||||||
|
|
||||||
|
### 8.2 Hybrid Retrieval (Semantik + Graph)
|
||||||
|
|
||||||
|
**Entscheidung:** Kombination von Vektor-Suche und Graph-Expansion
|
||||||
|
|
||||||
|
**Gründe:**
|
||||||
|
- Semantik findet ähnliche Inhalte
|
||||||
|
- Graph findet strukturelle Verbindungen
|
||||||
|
- Kombination liefert bessere Relevanz
|
||||||
|
|
||||||
|
**Trade-off:** Höhere Komplexität, mehr Berechnungsaufwand
|
||||||
|
|
||||||
|
### 8.3 Background Tasks statt Queue-System
|
||||||
|
|
||||||
|
**Entscheidung:** FastAPI Background Tasks statt Redis/RabbitMQ
|
||||||
|
|
||||||
|
**Gründe:**
|
||||||
|
- Einfacher Setup (keine zusätzliche Infrastruktur)
|
||||||
|
- Ausreichend für Single-User-Szenario
|
||||||
|
- Weniger Moving Parts
|
||||||
|
|
||||||
|
**Trade-off:** Keine Persistenz bei Server-Neustart (Tasks gehen verloren)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Konsistenz-Garantien
|
||||||
|
|
||||||
|
### 9.1 Deterministische IDs
|
||||||
|
|
||||||
|
**Prinzip:** UUIDv5 basierend auf Datei-Inhalt
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Reproduzierbare IDs
|
||||||
|
- Keine Duplikate bei Re-Import
|
||||||
|
- Graph ist aus Markdown rekonstruierbar
|
||||||
|
|
||||||
|
### 9.2 Provenance-Hierarchie
|
||||||
|
|
||||||
|
**Prinzip:** Kanten haben Qualitätsstufen (explicit > smart > rule)
|
||||||
|
|
||||||
|
**Vorteile:**
|
||||||
|
- Menschliche Intention hat Vorrang
|
||||||
|
- System-Heuristiken können überschrieben werden
|
||||||
|
- Transparente Gewichtung
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Weitere Informationen
|
||||||
|
|
||||||
|
- **Vision & Strategie:** Siehe [Vision & Strategie](../00_General/00_vision_and_strategy.md)
|
||||||
|
- **Graph-Logik:** Siehe [Graph-Logik](02_concept_graph_logic.md)
|
||||||
|
- **KI-Persönlichkeit:** Siehe [KI-Persönlichkeit](02_concept_ai_personality.md)
|
||||||
|
- **Developer Guide:** Siehe [Developer Guide](../05_Development/05_developer_guide.md)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Letzte Aktualisierung:** 2025-01-XX
|
||||||
|
**Version:** 2.9.1
|
||||||
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
---
|
---
|
||||||
doc_type: concept
|
doc_type: concept
|
||||||
audience: architect, product_owner
|
audience: architect, product_owner
|
||||||
scope: graph, logic, provenance
|
scope: graph, logic, provenance, agentic_validation, note_scope
|
||||||
status: active
|
status: active
|
||||||
version: 2.7.0
|
version: 4.5.8
|
||||||
context: "Fachliche Beschreibung des Wissensgraphen: Knoten, Kanten, Provenance, Matrix-Logik und WP-22 Scoring-Prinzipien."
|
context: "Fachliche Beschreibung des Wissensgraphen: Knoten, Kanten, Provenance, Matrix-Logik, WP-15c Multigraph-Support, WP-22 Scoring-Prinzipien, WP-24c Phase 3 Agentic Edge Validation und automatische Spiegelkanten."
|
||||||
---
|
---
|
||||||
|
|
||||||
# Konzept: Die Graph-Logik
|
# Konzept: Die Graph-Logik
|
||||||
@@ -59,7 +59,7 @@ Um eine konsistente mathematische Gewichtung zu garantieren, werden alle Kanten
|
|||||||
|
|
||||||
### 2.2 Provenance (Herkunft & Vertrauen)
|
### 2.2 Provenance (Herkunft & Vertrauen)
|
||||||
|
|
||||||
Nicht alle Kanten sind gleich viel wert. Mindnet unterscheidet drei Qualitätsstufen (**Provenance**), um bei der Berechnung des Edge-Bonus Prioritäten zu setzen.
|
Nicht alle Kanten sind gleich viel wert. Mindnet unterscheidet mehrere Qualitätsstufen (**Provenance**), um bei der Berechnung des Edge-Bonus Prioritäten zu setzen.
|
||||||
|
|
||||||
**1. Explicit (Der Mensch hat es gesagt)**
|
**1. Explicit (Der Mensch hat es gesagt)**
|
||||||
* *Quelle:* Inline-Links (`[[rel:...]]`) oder Wikilinks im Text.
|
* *Quelle:* Inline-Links (`[[rel:...]]`) oder Wikilinks im Text.
|
||||||
@@ -76,6 +76,19 @@ Nicht alle Kanten sind gleich viel wert. Mindnet unterscheidet drei Qualitätsst
|
|||||||
* *Vertrauen:* **Niedrig (0.7)**.
|
* *Vertrauen:* **Niedrig (0.7)**.
|
||||||
* *Bedeutung:* Systemseitige Heuristik. Diese Verbindungen dienen der Entdeckung neuer Pfade, haben aber weniger Gewicht als explizite Links.
|
* *Bedeutung:* Systemseitige Heuristik. Diese Verbindungen dienen der Entdeckung neuer Pfade, haben aber weniger Gewicht als explizite Links.
|
||||||
|
|
||||||
|
**4. Structure (System-interne Verkettung)**
|
||||||
|
* *Quelle:* Automatische Struktur-Kanten (`belongs_to`, `next`, `prev`).
|
||||||
|
* *Vertrauen:* **Hoch (1.0)**.
|
||||||
|
* *Bedeutung:* Diese Kanten werden ausschließlich durch interne Prozesse erzeugt und sind durch die **Provenance Firewall** geschützt. Sie können weder durch Nutzer noch durch KI manipuliert werden.
|
||||||
|
|
||||||
|
### 2.3 Provenance Firewall (WP-15c)
|
||||||
|
|
||||||
|
Die **Edge Registry** (v0.8.0) implementiert eine strikte Trennung zwischen System-Kanten und Inhalts-Kanten:
|
||||||
|
|
||||||
|
* **Geschützte System-Kanten:** `next`, `prev`, `belongs_to` dürfen nur mit `provenance="structure"` gesetzt werden.
|
||||||
|
* **Blockierung:** Alle anderen Provenienzen (`explicit`, `semantic_ai`, `inherited`, `global_pool`, `rule`) werden bei System-Kanten blockiert und auf `related_to` zurückgesetzt.
|
||||||
|
* **Zweck:** Sichert die Graph-Integrität und verhindert Manipulationen an der internen Struktur.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 3. Matrix-Logik (Kontext-Sensitivität)
|
## 3. Matrix-Logik (Kontext-Sensitivität)
|
||||||
@@ -118,8 +131,152 @@ Der Intent-Router injiziert spezifische Multiplikatoren für kanonische Typen:
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 6. Idempotenz & Konsistenz
|
## 6. Section-basierte Links & Multigraph-Support
|
||||||
|
|
||||||
|
Seit v2.9.1 unterstützt Mindnet **Deep-Links** zu spezifischen Abschnitten innerhalb einer Note.
|
||||||
|
|
||||||
|
### 6.1 Link-Parsing & Self-Links (WP-15c)
|
||||||
|
|
||||||
|
Die Logik erkennt nun präzise **Obsidian-Anker** (`[[Note#Section]]`) und **Self-Links** (`[[#Section]]`):
|
||||||
|
|
||||||
|
* **Obsidian-Anker:** `[[Note#Section]]` wird in `target_id="Note"` und `target_section="Section"` aufgeteilt.
|
||||||
|
* **Self-Links:** `[[#Section]]` wird zu `target_id="current_note_id"` und `target_section="Section"` aufgelöst.
|
||||||
|
* **`target_id`:** Enthält nur den Note-Namen (z.B. "Mein Leitbild")
|
||||||
|
* **`target_section`:** Enthält den Abschnitts-Namen (z.B. "P3 – Disziplin")
|
||||||
|
|
||||||
|
**Vorteil:** Verhindert "Phantom-Knoten", die durch das Einbeziehen des Anchors in die `target_id` entstanden wären. Ermöglicht präzise Verlinkung innerhalb derselben Note.
|
||||||
|
|
||||||
|
### 6.2 Multigraph-Support
|
||||||
|
Die Edge-ID enthält nun einen `variant`-Parameter (die Section), sodass mehrere Kanten zwischen denselben Knoten existieren können, wenn sie auf verschiedene Sections zeigen:
|
||||||
|
* `[[Note#Section1]]` → Edge-ID: `src->tgt:kind@Section1`
|
||||||
|
* `[[Note#Section2]]` → Edge-ID: `src->tgt:kind@Section2`
|
||||||
|
|
||||||
|
### 6.3 Semantische Deduplizierung
|
||||||
|
Die Deduplizierung basiert auf dem `src->tgt:kind@sec` Key, um sicherzustellen, dass identische Links (gleiche Quelle, Ziel, Typ und Section) nicht mehrfach erstellt werden.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. Automatische Spiegelkanten (Invers-Logik) - WP-24c v4.5.8
|
||||||
|
|
||||||
|
Das System erzeugt automatisch **Spiegelkanten** (Invers-Kanten) für explizite Verbindungen, um die Auffindbarkeit von Informationen zu verdoppeln.
|
||||||
|
|
||||||
|
### 7.1 Funktionsweise
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
- **Explizite Kante:** Note A `depends_on: Note B`
|
||||||
|
- **Automatische Spiegelkante:** Note B `enforced_by: Note A`
|
||||||
|
|
||||||
|
**Vorteil:** Beide Richtungen sind durchsuchbar. Wenn du nach "Note B" suchst, findest du auch alle Notizen, die von "Note B" abhängen (via `enforced_by`).
|
||||||
|
|
||||||
|
### 7.2 Invers-Mapping
|
||||||
|
|
||||||
|
Die Edge Registry definiert für jeden Kanten-Typ das symmetrische Gegenstück:
|
||||||
|
- `depends_on` ↔ `enforced_by`
|
||||||
|
- `derived_from` ↔ `resulted_in`
|
||||||
|
- `impacts` ↔ `impacted_by`
|
||||||
|
- `blocks` ↔ `blocked_by`
|
||||||
|
- `next` ↔ `prev`
|
||||||
|
- `related_to` ↔ `related_to` (symmetrisch)
|
||||||
|
|
||||||
|
### 7.3 Priorität & Schutz
|
||||||
|
|
||||||
|
* **Explizite Kanten haben Vorrang:** Wenn du bereits beide Richtungen explizit gesetzt hast, wird keine automatische Spiegelkante erzeugt (keine Duplikate)
|
||||||
|
* **Höhere Wirksamkeit expliziter Kanten:** Explizit gesetzte Kanten haben höhere Confidence-Werte (`confidence: 1.0`) als automatisch generierte Spiegelkanten (`confidence: 0.9 * original`)
|
||||||
|
* **Provenance Firewall:** System-Kanten (`belongs_to`, `next`, `prev`) können nicht manuell überschrieben werden
|
||||||
|
|
||||||
|
### 7.4 Phase 2 Symmetrie-Injektion
|
||||||
|
|
||||||
|
Spiegelkanten werden am Ende des gesamten Imports (Phase 2) in einem Batch-Prozess injiziert:
|
||||||
|
- **Authority-Check:** Nur wenn keine explizite Kante existiert, wird die Spiegelkante erzeugt
|
||||||
|
- **ID-Konsistenz:** Verwendet exakt dieselbe ID-Generierung wie Phase 1 (inkl. `target_section`)
|
||||||
|
- **Logging:** `🔄 [SYMMETRY]` zeigt die erzeugten Spiegelkanten
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. Phase 3 Agentic Edge Validation - WP-24c v4.5.8
|
||||||
|
|
||||||
|
Das System implementiert ein finales Validierungs-Gate für alle Kanten mit `candidate:` Präfix, um "Geister-Verknüpfungen" zu verhindern und die Graph-Qualität zu sichern.
|
||||||
|
|
||||||
|
### 8.1 Trigger-Kriterium
|
||||||
|
|
||||||
|
Kanten erhalten `candidate:` Präfix, wenn sie:
|
||||||
|
- In `### Unzugeordnete Kanten` Sektionen stehen
|
||||||
|
- Von der Smart Edge Allocation als Kandidaten vorgeschlagen wurden
|
||||||
|
- Explizit als `candidate:` markiert wurden
|
||||||
|
|
||||||
|
### 8.2 Validierungsprozess
|
||||||
|
|
||||||
|
1. **Kontext-Optimierung:**
|
||||||
|
- **Note-Scope (`scope: note`):** LLM nutzt `note_summary` (Top 5 Chunks) oder `note_text` (aggregierter Gesamttext)
|
||||||
|
- **Chunk-Scope (`scope: chunk`):** LLM nutzt spezifischen Chunk-Text, falls verfügbar, sonst Note-Text
|
||||||
|
|
||||||
|
2. **LLM-Validierung:**
|
||||||
|
- Nutzt `ingest_validator` Profil (Temperature 0.0 für Determinismus)
|
||||||
|
- Prüft semantisch: "Passt diese Verbindung zum Kontext?"
|
||||||
|
- Binäre Entscheidung: YES (VERIFIED) oder NO (REJECTED)
|
||||||
|
|
||||||
|
3. **Ergebnis:**
|
||||||
|
- **VERIFIED:** `candidate:` Präfix wird entfernt, Kante wird persistiert
|
||||||
|
- **REJECTED:** Kante wird **nicht** in die Datenbank geschrieben (verhindert persistente "Geister-Verknüpfungen")
|
||||||
|
|
||||||
|
### 8.3 Fehlertoleranz
|
||||||
|
|
||||||
|
Das System unterscheidet zwischen:
|
||||||
|
- **Transienten Fehlern (Netzwerk, Timeout):** Kante wird erlaubt (Integrität vor Präzision)
|
||||||
|
- **Permanenten Fehlern (Config, Validation):** Kante wird abgelehnt (Graph-Qualität schützen)
|
||||||
|
|
||||||
|
### 8.4 Provenance nach Validierung
|
||||||
|
|
||||||
|
- **Vor Validierung:** `provenance: "candidate:global_pool"` oder `rule_id: "candidate:..."`
|
||||||
|
- **Nach VERIFIED:** `provenance: "global_pool"` oder `rule_id: "explicit"` (Präfix entfernt)
|
||||||
|
- **Nach REJECTED:** Kante existiert nicht im Graph (wird nicht persistiert)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. Note-Scope vs. Chunk-Scope - WP-24c v4.2.0
|
||||||
|
|
||||||
|
Das System unterscheidet zwischen **Note-Scope** (globale Verbindungen) und **Chunk-Scope** (lokale Referenzen).
|
||||||
|
|
||||||
|
### 9.1 Chunk-Scope (Standard)
|
||||||
|
|
||||||
|
- **Quelle:** `source_id = chunk_id` (z.B. `note-id#c00`)
|
||||||
|
- **Kontext:** Spezifischer Textabschnitt (Chunk)
|
||||||
|
- **Verwendung:** Lokale Referenzen innerhalb eines Abschnitts
|
||||||
|
- **Phase 3 Validierung:** Nutzt spezifischen Chunk-Text
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
```markdown
|
||||||
|
In diesem Abschnitt nutzen wir [[rel:uses|Technologie X]].
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.2 Note-Scope
|
||||||
|
|
||||||
|
- **Quelle:** `source_id = note_id` (nicht `chunk_id`)
|
||||||
|
- **Kontext:** Gesamte Note (Note-Summary oder Note-Text)
|
||||||
|
- **Verwendung:** Globale Verbindungen, die für die ganze Note gelten
|
||||||
|
- **Phase 3 Validierung:** Nutzt `note_summary` (Top 5 Chunks) oder `note_text` (aggregierter Gesamttext)
|
||||||
|
|
||||||
|
**Beispiel:**
|
||||||
|
```markdown
|
||||||
|
## Smart Edges
|
||||||
|
|
||||||
|
[[rel:depends_on|Projekt-Übersicht]]
|
||||||
|
[[rel:part_of|Größeres System]]
|
||||||
|
```
|
||||||
|
|
||||||
|
### 9.3 Priorität
|
||||||
|
|
||||||
|
Bei Duplikaten (gleiche Kante in Chunk-Scope und Note-Scope):
|
||||||
|
1. **Note-Scope Links** haben **höchste Priorität**
|
||||||
|
2. Dann Confidence-Wert
|
||||||
|
3. Dann Provenance-Priority
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10. Idempotenz & Konsistenz
|
||||||
|
|
||||||
Das System garantiert fachliche Konsistenz auch bei mehrfachen Importen.
|
Das System garantiert fachliche Konsistenz auch bei mehrfachen Importen.
|
||||||
* **Stabile IDs:** Deterministische IDs verhindern Duplikate bei Re-Imports.
|
* **Stabile IDs:** Deterministische IDs verhindern Duplikate bei Re-Imports.
|
||||||
* **Deduplizierung:** Kanten werden anhand ihrer Identität erkannt. Die "stärkere" Provenance gewinnt.
|
* **Deduplizierung:** Kanten werden anhand ihrer Identität (inkl. Section) erkannt. Die "stärkere" Provenance gewinnt.
|
||||||
|
* **Format-agnostische Erkennung:** Kanten werden unabhängig vom Format (Inline, Callout, Wikilink) erkannt, um Dopplungen zu vermeiden.
|
||||||
|
* **Phase 3 Validierung:** Verhindert persistente "Geister-Verknüpfungen" durch Ablehnung irrelevanter Kanten.
|
||||||
Loaded 100 of 207 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user