DP_PRACA/vector_store.py
2026-12-04 09:22:31 +01:00

33 lines
1.3 KiB
Python

"""ChromaDB retrieval using multilingual-e5-large with the required query/passage prefixes."""
from __future__ import annotations
from pathlib import Path
from ingest import load_documents
class ChromaVectorStore:
def __init__(self, path: str = "data/chroma", model_name: str = "intfloat/multilingual-e5-large"):
import chromadb
from sentence_transformers import SentenceTransformer
self.client = chromadb.PersistentClient(path=path)
self.collection = self.client.get_or_create_collection("lesson_documents")
self.model = SentenceTransformer(model_name)
def index_documents(self, folder: str = "documents") -> int:
docs = load_documents(folder)
ids, texts, metadatas = [], [], []
for document in docs:
text = document["content"]
for index in range(0, len(text), 6000):
chunk = text[index:index + 6000].strip()
if chunk:
ids.append(f"{document['filename']}:{index}")
texts.append(f"passage: {chunk}")
metadatas.append({"source": document["filename"]})
if texts:
embeddings = self.model.encode(texts, normalize_embeddings=True).tolist()
self.collection.upsert(ids=ids, documents=texts, embeddings=embeddings, metadatas=metadatas)
return len(texts)