Update vectorstore.py
This commit is contained in:
+23
-51
@@ -1,31 +1,18 @@
|
|||||||
"""Utilities for creating and populating a Chroma vector store.
|
"""Vector store utilities for ChromaDB.
|
||||||
|
|
||||||
This module provides two helper functions:
|
This module provides functions to create a persistent ChromaDB vector store using
|
||||||
|
Ollama embeddings and to load documents from a directory into the store.
|
||||||
* ``create_vectorstore`` – creates a Chroma collection backed by Ollama embeddings.
|
|
||||||
* ``load_documents`` – reads ``.txt``/``.md`` files, splits them into chunks and adds them to the collection.
|
|
||||||
|
|
||||||
The vector store is persisted in ``./chroma_db`` by default.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
|
||||||
|
|
||||||
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_core.documents import Document
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Configuration
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
DEFAULT_PERSIST_DIR = "./chroma_db"
|
|
||||||
EMBEDDING_MODEL = "nomic-embed-text"
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
def create_vectorstore(persist_directory: str = "./chroma_db") -> Chroma:
|
||||||
# Public API
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
def create_vectorstore(persist_directory: str = DEFAULT_PERSIST_DIR) -> Chroma:
|
|
||||||
"""Create a Chroma vector store with Ollama embeddings.
|
"""Create a Chroma vector store with Ollama embeddings.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@@ -36,44 +23,29 @@ def create_vectorstore(persist_directory: str = DEFAULT_PERSIST_DIR) -> Chroma:
|
|||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
Chroma
|
Chroma
|
||||||
A Chroma collection ready for adding documents.
|
A Chroma vector store instance.
|
||||||
"""
|
"""
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
def load_documents(directory: str, vectorstore: Chroma, chunk_size: int = 1000, chunk_overlap: int = 200) -> None:
|
def load_documents(directory: str, vectorstore: Chroma) -> None:
|
||||||
"""Load all ``.txt`` and ``.md`` files from *directory*, split them into chunks and add to *vectorstore*.
|
"""Load .txt and .md files from *directory* into *vectorstore*.
|
||||||
|
|
||||||
Parameters
|
The documents are split into chunks using ``RecursiveCharacterTextSplitter``
|
||||||
----------
|
before being added to the vector store.
|
||||||
directory: str
|
|
||||||
Path to the folder containing documents.
|
|
||||||
vectorstore: Chroma
|
|
||||||
The Chroma collection to populate.
|
|
||||||
chunk_size: int
|
|
||||||
Number of characters per chunk.
|
|
||||||
chunk_overlap: int
|
|
||||||
Number of characters that overlap between consecutive chunks.
|
|
||||||
"""
|
"""
|
||||||
splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
||||||
docs: List[str] = []
|
docs = []
|
||||||
|
for file_path in Path(directory).glob("*"):
|
||||||
for path in Path(directory).glob("**/*"):
|
if file_path.suffix.lower() not in {".txt", ".md"}:
|
||||||
if path.is_file() and path.suffix.lower() in {".txt", ".md"}:
|
continue
|
||||||
text = path.read_text(encoding="utf-8")
|
with open(file_path, "r", encoding="utf-8") as f:
|
||||||
docs.extend(splitter.split_text(text))
|
text = f.read()
|
||||||
|
chunks = splitter.split_text(text)
|
||||||
|
docs.extend([Document(page_content=c, metadata={"source": str(file_path)}) for c in chunks])
|
||||||
if docs:
|
if docs:
|
||||||
vectorstore.add_texts(docs)
|
vectorstore.add_documents(docs)
|
||||||
|
print(f"Loaded {len(docs)} chunks from {directory} into ChromaDB.")
|
||||||
else:
|
else:
|
||||||
print("[vectorstore] No documents found in", directory)
|
print(f"No .txt/.md files found in {directory}.")
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Example usage (uncomment to run as a script)
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# if __name__ == "__main__":
|
|
||||||
# store = create_vectorstore()
|
|
||||||
# load_documents("documents", store)
|
|
||||||
# print("Vector store populated.")
|
|
||||||
"""
|
|
||||||
|
|||||||
Reference in New Issue
Block a user