From 7250b26b9bd42a85ac7039cd1159fd6acca521c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Fri, 5 Jun 2026 11:21:25 +0000 Subject: [PATCH] Update vectorstore.py --- vectorstore.py | 77 +++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/vectorstore.py b/vectorstore.py index f9b0825..1f256c6 100644 --- a/vectorstore.py +++ b/vectorstore.py @@ -1,19 +1,20 @@ """Vector store utilities using ChromaDB and Ollama embeddings. -This module provides functions to create a persistent Chroma vector store -and to load documents from a directory into the store. +This module provides functions to create a persistent Chroma vector store and load +text documents from a directory into it. The store is exposed via the global +``store`` variable so that other modules (e.g. tools) can access it. """ from pathlib import Path -from typing import Iterable +from typing import List from langchain_chroma import Chroma from langchain_ollama import OllamaEmbeddings from langchain_text_splitters import RecursiveCharacterTextSplitter +from langchain_core.documents import Document -# --------------------------------------------------------------------------- -# Create a persistent Chroma vector store. -# --------------------------------------------------------------------------- +# Global store that will be initialised in ``create_vectorstore``. +store: Chroma | None = None def create_vectorstore(persist_directory: str = "./chroma_db") -> Chroma: """Create a Chroma vector store with Ollama embeddings. @@ -21,41 +22,45 @@ def create_vectorstore(persist_directory: str = "./chroma_db") -> Chroma: Parameters ---------- persist_directory: str - Directory where the Chroma database will be stored. - - Returns - ------- - Chroma - A Chroma vector store instance. + Directory where the vector data will be persisted. """ + global store embeddings = OllamaEmbeddings(model="nomic-embed-text") - return Chroma(persist_directory=persist_directory, embedding_function=embeddings) + store = Chroma( + collection_name="rag_collection", + embedding_function=embeddings, + persist_directory=persist_directory, + ) + return store -# --------------------------------------------------------------------------- -# Load documents from a directory into the vector store. -# --------------------------------------------------------------------------- - -def load_documents(directory: str, vectorstore: Chroma) -> None: +def load_documents(directory: str | Path, vectorstore: Chroma) -> None: """Load all .txt and .md files from *directory* into *vectorstore*. - The documents are split into chunks using a RecursiveCharacterTextSplitter - before being added to the vector store. + The documents are split using ``RecursiveCharacterTextSplitter`` before + being added to the collection. """ - splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) - docs = [] - for path in Path(directory).rglob("*.txt"): - docs.append(path.read_text(encoding="utf-8")) - for path in Path(directory).rglob("*.md"): - docs.append(path.read_text(encoding="utf-8")) - if not docs: + dir_path = Path(directory) + txt_files = list(dir_path.rglob("*.txt")) + list(dir_path.rglob("*.md")) + if not txt_files: + print(f"No .txt or .md files found in {dir_path}") return - # Split the documents into chunks. - chunks = splitter.split_text("\n\n".join(docs)) - # Create LangChain Document objects. - from langchain.docstore.document import Document - documents = [Document(page_content=chunk) for chunk in chunks] - vectorstore.add_documents(documents) -# --------------------------------------------------------------------------- -# End of vectorstore.py -# --------------------------------------------------------------------------- \ No newline at end of file + splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) + docs: List[Document] = [] + for file_path in txt_files: + text = file_path.read_text(encoding="utf-8") + docs.extend(splitter.create_documents([text], metadata={"source": str(file_path)})) + + vectorstore.add_documents(docs) + # Persist the collection to disk. + vectorstore.persist() + print(f"Loaded {len(docs)} documents from {dir_path} into Chroma.") + +# Helper to get the global store. + +def get_vectorstore() -> Chroma: + if store is None: + raise RuntimeError("Vector store has not been initialised. Call create_vectorstore() first.") + return store + +"""End of vectorstore.py""" \ No newline at end of file