Публикация решения: update vectorstore.py
This commit is contained in:
+15
-16
@@ -1,35 +1,37 @@
|
|||||||
"""
|
"""
|
||||||
RAG vector store using Qdrant and Ollama embeddings.
|
RAG vector store using ChromaDB and Ollama embeddings.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import chromadb # kept for compatibility if needed
|
import chromadb
|
||||||
from langchain.embeddings.ollama import OllamaEmbeddings
|
from langchain.embeddings.ollama import OllamaEmbeddings
|
||||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||||
from langchain.schema.document import Document
|
from langchain.schema.document import Document
|
||||||
from langchain.vectorstores import Qdrant
|
from langchain.vectorstores import Chroma
|
||||||
|
|
||||||
CHROMA_DIR = "./chroma_db"
|
CHROMA_DIR = "./chroma_db"
|
||||||
EMBED_MODEL = "nomic-embed-text"
|
EMBED_MODEL = "nomic-embed-text"
|
||||||
|
|
||||||
|
|
||||||
def create_vectorstore(persist_directory: str = CHROMA_DIR):
|
def create_vectorstore(persist_directory: str = CHROMA_DIR) -> Chroma:
|
||||||
"""Create or load a Qdrant vector store.
|
"""Create or load a Chroma vector store.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
persist_directory:
|
persist_directory : str, optional
|
||||||
Directory where the Qdrant database is stored. If it does not exist, it will be created.
|
Directory where the Chroma database is stored. If it does not exist,
|
||||||
|
it will be created automatically by Chroma.
|
||||||
"""
|
"""
|
||||||
embeddings = OllamaEmbeddings(model=EMBED_MODEL)
|
embeddings = OllamaEmbeddings(model=EMBED_MODEL)
|
||||||
# Qdrant can use a local file store via `path` argument
|
# Chroma can use a local directory for persistence
|
||||||
client = Qdrant(persist_directory=persist_directory, embedding_function=embeddings)
|
client = chromadb.PersistentClient(path=persist_directory)
|
||||||
return client
|
collection = client.get_or_create_collection(name="documents", embedding_function=embeddings)
|
||||||
|
return Chroma(collection=collection, embedding_function=embeddings)
|
||||||
|
|
||||||
|
|
||||||
def load_documents(directory: str, vectorstore) -> None:
|
def load_documents(directory: str, vectorstore: Chroma) -> None:
|
||||||
"""Load all .txt and .md files from *directory*, chunk them and add to the vector store.
|
"""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.
|
The function does not return anything; it mutates the provided collection.
|
||||||
@@ -44,8 +46,5 @@ def load_documents(directory: str, vectorstore) -> None:
|
|||||||
docs.extend(text_splitter.create_documents([content], metadata={"source": str(path)}))
|
docs.extend(text_splitter.create_documents([content], metadata={"source": str(path)}))
|
||||||
|
|
||||||
if docs:
|
if docs:
|
||||||
# Qdrant expects texts and metadatas lists
|
# Chroma expects a list of documents via add_documents
|
||||||
vectorstore.add_texts(
|
vectorstore.add_documents(docs)
|
||||||
texts=[doc.page_content for doc in docs],
|
|
||||||
metadatas=[doc.metadata for doc in docs],
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user