Update vectorstore.py
This commit is contained in:
+37
-56
@@ -1,82 +1,63 @@
|
|||||||
"""
|
"""Utilities for creating and populating a ChromaDB vector store.
|
||||||
Vector store utilities for ChromaDB.
|
|
||||||
|
This module provides two functions:
|
||||||
|
|
||||||
|
- :func:`create_vectorstore` – creates a Chroma vector store backed by a local directory.
|
||||||
|
- :func:`load_documents` – reads all ``.txt`` and ``.md`` files from a directory, splits them into chunks using
|
||||||
|
:class:`langchain_text_splitters.RecursiveCharacterTextSplitter`, and upserts the chunks into the
|
||||||
|
provided vector store.
|
||||||
|
|
||||||
|
The vector store is persistent across runs – the ``persist_directory`` argument defaults to
|
||||||
|
``"./chroma_db"``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
from langchain_chroma import Chroma
|
from langchain_chroma import Chroma
|
||||||
from langchain_ollama import OllamaEmbeddings
|
from langchain_ollama import OllamaEmbeddings
|
||||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||||
from langchain_community.document_loaders import TextLoader, UnstructuredMarkdownLoader
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# Default embedding model used by the vector store
|
||||||
# Create a persistent Chroma vector store
|
EMBEDDING_MODEL = "nomic-embed-text"
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def create_vectorstore(persist_directory: str = "./chroma_db") -> Chroma:
|
def create_vectorstore(persist_directory: str = "./chroma_db") -> Chroma:
|
||||||
"""Create or load a Chroma vector store.
|
"""Create a Chroma vector store.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
persist_directory: str
|
persist_directory: str
|
||||||
Directory where the Chroma DB will be persisted.
|
Directory where the vector store will be persisted.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
Chroma
|
Chroma
|
||||||
A Chroma vector store instance.
|
A Chroma vector store instance.
|
||||||
"""
|
"""
|
||||||
os.makedirs(persist_directory, exist_ok=True)
|
embeddings = OllamaEmbeddings(model=EMBEDDING_MODEL)
|
||||||
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
|
||||||
return Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
return Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Load documents from a directory and add them to the vector store
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def load_documents(directory: str, vectorstore: Chroma, chunk_size: int = 500, chunk_overlap: int = 50) -> None:
|
def _read_text_files(directory: str) -> Iterable[str]:
|
||||||
"""Load `.txt` and `.md` files from *directory*, split them into chunks, and add to *vectorstore*.
|
"""Yield the content of all ``.txt`` and ``.md`` files in *directory*.
|
||||||
|
"""
|
||||||
Parameters
|
path = Path(directory)
|
||||||
----------
|
for file_path in path.rglob("*.txt"):
|
||||||
directory: str
|
yield file_path.read_text(encoding="utf-8")
|
||||||
Directory containing the source documents.
|
for file_path in path.rglob("*.md"):
|
||||||
vectorstore: Chroma
|
yield file_path.read_text(encoding="utf-8")
|
||||||
The vector store to which documents will be added.
|
|
||||||
chunk_size: int, optional
|
|
||||||
Maximum chunk size in characters.
|
def load_documents(directory: str, vectorstore: Chroma, chunk_size: int = 1000, chunk_overlap: int = 200) -> None:
|
||||||
chunk_overlap: int, optional
|
"""Load documents from *directory* into *vectorstore*.
|
||||||
Number of overlapping characters between consecutive chunks.
|
|
||||||
|
The documents are split into chunks using :class:`RecursiveCharacterTextSplitter` and then
|
||||||
|
upserted into the vector store.
|
||||||
"""
|
"""
|
||||||
loader_classes = {
|
|
||||||
".txt": TextLoader,
|
|
||||||
".md": UnstructuredMarkdownLoader,
|
|
||||||
}
|
|
||||||
splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
||||||
|
for text in _read_text_files(directory):
|
||||||
|
chunks = splitter.split_text(text)
|
||||||
|
vectorstore.add_texts(chunks)
|
||||||
|
|
||||||
docs = []
|
# End of vectorstore.py
|
||||||
for root, _, files in os.walk(directory):
|
|
||||||
for file in files:
|
|
||||||
ext = Path(file).suffix.lower()
|
|
||||||
if ext not in loader_classes:
|
|
||||||
continue
|
|
||||||
loader = loader_classes[ext](os.path.join(root, file))
|
|
||||||
loaded_docs = loader.load()
|
|
||||||
docs.extend(loaded_docs)
|
|
||||||
|
|
||||||
if not docs:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Split documents into smaller chunks
|
|
||||||
split_docs = splitter.split_documents(docs)
|
|
||||||
vectorstore.add_documents(split_docs)
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Example usage
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# This block is only executed when running the module directly.
|
|
||||||
store = create_vectorstore()
|
|
||||||
load_documents("documents", store)
|
|
||||||
print("Vector store populated.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user