WP19b- chunk_payload an neue Struktur
This commit is contained in:
parent
a6d37c92d2
commit
8ade34af0a
|
|
@ -1,7 +1,9 @@
|
||||||
"""
|
"""
|
||||||
FILE: app/core/ingestion/ingestion_chunk_payload.py
|
FILE: app/core/ingestion/ingestion_chunk_payload.py
|
||||||
DESCRIPTION: Baut das JSON-Objekt für mindnet_chunks.
|
DESCRIPTION: Baut das JSON-Objekt für 'mindnet_chunks'.
|
||||||
VERSION: 2.4.0
|
Fix v2.4.1: Behebt AttributeError bei Zugriff auf Chunk-Objekte.
|
||||||
|
VERSION: 2.4.1
|
||||||
|
STATUS: Active
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
@ -10,10 +12,19 @@ def _as_list(x):
|
||||||
if x is None: return []
|
if x is None: return []
|
||||||
return x if isinstance(x, list) else [x]
|
return x if isinstance(x, list) else [x]
|
||||||
|
|
||||||
def make_chunk_payloads(note: Dict[str, Any], note_path: str, chunks_from_chunker: List[Any], **kwargs) -> List[Dict[str, Any]]:
|
def make_chunk_payloads(note: Dict[str, Any],
|
||||||
"""Erstellt die Payloads für die Chunks eines Dokuments."""
|
note_path: str,
|
||||||
if isinstance(note, dict) and "frontmatter" in note: fm = note["frontmatter"]
|
chunks_from_chunker: List[Any],
|
||||||
else: fm = note or {}
|
**kwargs) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Erstellt die Payloads für die Chunks eines Dokuments.
|
||||||
|
Robust gegenüber Chunk-Objekten (Dataclasses) und Dictionaries.
|
||||||
|
"""
|
||||||
|
# Frontmatter Extraktion
|
||||||
|
if isinstance(note, dict) and "frontmatter" in note:
|
||||||
|
fm = note["frontmatter"]
|
||||||
|
else:
|
||||||
|
fm = note or {}
|
||||||
|
|
||||||
note_type = fm.get("type") or "concept"
|
note_type = fm.get("type") or "concept"
|
||||||
title = fm.get("title") or fm.get("id") or "Untitled"
|
title = fm.get("title") or fm.get("id") or "Untitled"
|
||||||
|
|
@ -23,24 +34,39 @@ def make_chunk_payloads(note: Dict[str, Any], note_path: str, chunks_from_chunke
|
||||||
|
|
||||||
out: List[Dict[str, Any]] = []
|
out: List[Dict[str, Any]] = []
|
||||||
for idx, ch in enumerate(chunks_from_chunker):
|
for idx, ch in enumerate(chunks_from_chunker):
|
||||||
text = getattr(ch, "text", "") or ch.get("text", "")
|
# Dynamische Extraktion basierend auf Typ (Objekt vs Dict)
|
||||||
|
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")
|
||||||
|
|
||||||
|
# Korrektur des AttributeError: Nutzt getattr für Objekte, .get für Dicts
|
||||||
|
section = getattr(ch, "section_title", "") if not is_dict else ch.get("section", "")
|
||||||
|
|
||||||
pl: Dict[str, Any] = {
|
pl: Dict[str, Any] = {
|
||||||
"note_id": getattr(ch, "note_id", None) or fm.get("id"),
|
"note_id": nid or fm.get("id"),
|
||||||
"chunk_id": getattr(ch, "id", None),
|
"chunk_id": cid,
|
||||||
"title": title,
|
"title": title,
|
||||||
"index": int(getattr(ch, "index", idx)),
|
"index": int(index),
|
||||||
"ord": int(getattr(ch, "index", idx)) + 1,
|
"ord": int(index) + 1,
|
||||||
"type": note_type,
|
"type": note_type,
|
||||||
"tags": tags,
|
"tags": tags,
|
||||||
"text": text,
|
"text": text,
|
||||||
"window": getattr(ch, "window", text),
|
"window": window,
|
||||||
"neighbors_prev": _as_list(getattr(ch, "neighbors_prev", None)),
|
"neighbors_prev": _as_list(prev_id),
|
||||||
"neighbors_next": _as_list(getattr(ch, "neighbors_next", None)),
|
"neighbors_next": _as_list(next_id),
|
||||||
"section": getattr(ch, "section_title", "") or ch.get("section", ""),
|
"section": section,
|
||||||
"path": note_path,
|
"path": note_path,
|
||||||
"source_path": kwargs.get("file_path") or note_path,
|
"source_path": kwargs.get("file_path") or note_path,
|
||||||
"retriever_weight": rw,
|
"retriever_weight": rw,
|
||||||
"chunk_profile": cp
|
"chunk_profile": cp
|
||||||
}
|
}
|
||||||
out.append(pl)
|
out.append(pl)
|
||||||
|
|
||||||
return out
|
return out
|
||||||
Loading…
Reference in New Issue
Block a user