mindnet/app/core/qdrant_points.py

71 lines
2.4 KiB
Python
Raw Normal View History

2025-09-03 07:57:31 +02:00
from __future__ import annotations
2025-09-04 08:15:35 +02:00
import uuid
2025-09-04 08:08:33 +02:00
from typing import List, Tuple
2025-09-03 07:57:31 +02:00
from qdrant_client.http import models as rest
2025-09-04 08:08:33 +02:00
def _names(prefix: str) -> Tuple[str, str, str]:
return f"{prefix}_notes", f"{prefix}_chunks", f"{prefix}_edges"
2025-09-04 08:15:35 +02:00
def _to_uuid(stable_key: str) -> str:
2025-09-04 08:08:33 +02:00
"""
2025-09-04 08:15:35 +02:00
Erzeuge eine stabile UUIDv5 aus einem stabilen String-Key (z. B. note_id, chunk_id, edge_id).
Wir verwenden NAMESPACE_URL, damit die UUIDs deterministisch sind.
"""
return str(uuid.uuid5(uuid.NAMESPACE_URL, stable_key))
def points_for_note(
prefix: str,
note_payload: dict,
note_vec: List[float] | None,
dim: int,
) -> Tuple[str, List[rest.PointStruct]]:
"""
(collection_name, [PointStruct]) für die Notes-Collection.
Falls kein Note-Embedding vorhanden -> Nullvektor der Länge `dim`.
2025-09-04 08:08:33 +02:00
"""
notes_col, _, _ = _names(prefix)
vector = note_vec if note_vec is not None else [0.0] * int(dim)
2025-09-04 08:15:35 +02:00
# Qdrant-Point-ID MUSS int oder UUID sein -> aus note_id eine UUIDv5 machen
point_id = _to_uuid(note_payload["note_id"])
pt = rest.PointStruct(id=point_id, vector=vector, payload=note_payload)
2025-09-04 08:08:33 +02:00
return notes_col, [pt]
2025-09-04 08:15:35 +02:00
def points_for_chunks(
prefix: str,
chunk_payloads: List[dict],
vectors: List[List[float]],
) -> Tuple[str, List[rest.PointStruct]]:
2025-09-04 08:08:33 +02:00
"""
2025-09-04 08:15:35 +02:00
(collection_name, [PointStruct]) für die Chunks-Collection.
Erwartet pro Chunk einen Vektor (oder Nullvektor, wenn --skip-embed).
2025-09-04 08:08:33 +02:00
"""
_, chunks_col, _ = _names(prefix)
points: List[rest.PointStruct] = []
2025-09-03 07:57:31 +02:00
for pl, vec in zip(chunk_payloads, vectors):
2025-09-04 08:15:35 +02:00
point_id = _to_uuid(pl["chunk_id"])
points.append(rest.PointStruct(id=point_id, vector=vec, payload=pl))
2025-09-04 08:08:33 +02:00
return chunks_col, points
def points_for_edges(prefix: str, edge_payloads: List[dict]) -> Tuple[str, List[rest.PointStruct]]:
"""
2025-09-04 08:15:35 +02:00
(collection_name, [PointStruct]) für die Edges-Collection.
Edges-Collection ist ohne Vektor angelegt -> nur Payload + UUID-IDs.
2025-09-04 08:08:33 +02:00
"""
_, _, edges_col = _names(prefix)
points: List[rest.PointStruct] = []
for pl in edge_payloads:
2025-09-04 08:15:35 +02:00
point_id = _to_uuid(pl["edge_id"])
points.append(rest.PointStruct(id=point_id, payload=pl))
2025-09-04 08:08:33 +02:00
return edges_col, points
def upsert_batch(client, collection: str, points: List[rest.PointStruct]) -> None:
if not points:
return
client.upsert(collection_name=collection, points=points, wait=True)