From 30620c0201bfbb1737c64b1106501cd44078af78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B8=D0=BD=D0=B0=D1=80=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D1=82=D0=BE=D0=B2?= Date: Thu, 11 Jun 2026 09:16:31 +0000 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20=D0=BA=20=D0=BF=D1=83?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=86=D0=B8=D0=B8:=20add=20vecto?= =?UTF-8?q?rstore.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vectorstore.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 vectorstore.py diff --git a/vectorstore.py b/vectorstore.py new file mode 100644 index 0000000..d0192e9 --- /dev/null +++ b/vectorstore.py @@ -0,0 +1,46 @@ +""" +RAG vector store using ChromaDB and Ollama embeddings. +""" + +from pathlib import Path +from typing import List, Iterable + +import chromadb +from langchain.embeddings.ollama import OllamaEmbeddings +from langchain.text_splitter import RecursiveCharacterTextSplitter +from langchain.schema.document import Document + +CHROMA_DIR = "./chroma_db" +EMBED_MODEL = "nomic-embed-text" + + +def create_vectorstore(persist_directory: str = CHROMA_DIR): + """Create or load a Chroma vector store. + + Parameters + ---------- + persist_directory: + Directory where the Chroma database is stored. If it does not exist, it will be created. + """ + embeddings = OllamaEmbeddings(model=EMBED_MODEL) + client = chromadb.PersistentClient(path=persist_directory) + # Use a single collection named "documents" + return client.get_or_create_collection(name="documents", embedding_function=embeddings) + + +def load_documents(directory: str, vectorstore) -> None: + """Load all .txt and .md files from *directory*, chunk them and add to the vector store. + + The function does not return anything; it mutates the provided collection. + """ + text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) + docs: List[Document] = [] + for path in Path(directory).rglob("*.txt"): + content = path.read_text(encoding="utf-8") + docs.extend(text_splitter.create_documents([content], metadata={"source": str(path)})) + for path in Path(directory).rglob("*.md"): + content = path.read_text(encoding="utf-8") + docs.extend(text_splitter.create_documents([content], metadata={"source": str(path)})) + + if docs: + vectorstore.add(documents=docs)