Initial implementation of RAG agent with Qdrant and Tavily: update vectorstore.py
This commit is contained in:
+14
-9
@@ -1,31 +1,32 @@
|
|||||||
"""
|
"""
|
||||||
RAG vector store using ChromaDB and Ollama embeddings.
|
RAG vector store using Qdrant and Ollama embeddings.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Iterable
|
from typing import List
|
||||||
|
|
||||||
import chromadb
|
import chromadb # kept for compatibility if needed
|
||||||
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
|
||||||
|
|
||||||
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):
|
||||||
"""Create or load a Chroma vector store.
|
"""Create or load a Qdrant vector store.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
persist_directory:
|
persist_directory:
|
||||||
Directory where the Chroma database is stored. If it does not exist, it will be created.
|
Directory where the Qdrant database is stored. If it does not exist, it will be created.
|
||||||
"""
|
"""
|
||||||
embeddings = OllamaEmbeddings(model=EMBED_MODEL)
|
embeddings = OllamaEmbeddings(model=EMBED_MODEL)
|
||||||
client = chromadb.PersistentClient(path=persist_directory)
|
# Qdrant can use a local file store via `path` argument
|
||||||
# Use a single collection named "documents"
|
client = Qdrant(persist_directory=persist_directory, embedding_function=embeddings)
|
||||||
return client.get_or_create_collection(name="documents", embedding_function=embeddings)
|
return client
|
||||||
|
|
||||||
|
|
||||||
def load_documents(directory: str, vectorstore) -> None:
|
def load_documents(directory: str, vectorstore) -> None:
|
||||||
@@ -43,4 +44,8 @@ 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:
|
||||||
vectorstore.add(documents=docs)
|
# Qdrant expects texts and metadatas lists
|
||||||
|
vectorstore.add_texts(
|
||||||
|
texts=[doc.page_content for doc in docs],
|
||||||
|
metadatas=[doc.metadata for doc in docs],
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user