Skip to content

Commit

Permalink
Use qdrant instead of faiss as vectorstore
Browse files Browse the repository at this point in the history
put back faiss
  • Loading branch information
vemonet committed Jan 11, 2024
1 parent c5817fc commit af9d5af
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
3 changes: 1 addition & 2 deletions config/chat-vectorstore-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ llm:
Helpful answer:
vector:
vector_path: "qdrant"
# vector_path: ./vectorstore/qdrant # Path to the vectorstore to do QA retrieval
vector_path: ./vectorstore/db_faiss # Path to the vectorstore to do QA retrieval
vector_download: null
embeddings_path: ./embeddings/all-MiniLM-L6-v2 # Embeddings used to generate the vectors. To use from HF: sentence-transformers/all-MiniLM-L6-v2
embeddings_download: https://public.ukp.informatik.tu-darmstadt.de/reimers/sentence-transformers/v0.2/all-MiniLM-L6-v2.zip
Expand Down
32 changes: 16 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ services:
- nginx


qdrant:
# https://hub.docker.com/r/qdrant/qdrant/tags
image: qdrant/qdrant:v1.5.1
restart: unless-stopped
volumes:
- ./data/vectorstore/qdrant:/qdrant/storage
# - ./scripts/qdrant_config.yml:/qdrant/config/production.yaml
environment:
- QDRANT_ALLOW_RECOVERY_MODE=true
# - VIRTUAL_HOST=qdrant.137.120.31.148.nip.io
# - LETSENCRYPT_HOST=qdrant.137.120.31.148.nip.io
# - VIRTUAL_PORT=6333
# ports:
# - 6333:6333
# command:
# - ./qdrant --config-path /qdrant/qdrant_config.yml
# qdrant:
# # https://hub.docker.com/r/qdrant/qdrant/tags
# image: qdrant/qdrant:v1.5.1
# restart: unless-stopped
# volumes:
# - ./data/vectorstore/qdrant:/qdrant/storage
# # - ./scripts/qdrant_config.yml:/qdrant/config/production.yaml
# environment:
# - QDRANT_ALLOW_RECOVERY_MODE=true
# # - VIRTUAL_HOST=qdrant.137.120.31.148.nip.io
# # - LETSENCRYPT_HOST=qdrant.137.120.31.148.nip.io
# # - VIRTUAL_PORT=6333
# # ports:
# # - 6333:6333
# # command:
# # - ./qdrant --config-path /qdrant/qdrant_config.yml


# Also required to deploy containers publicly
Expand Down
45 changes: 22 additions & 23 deletions src/libre_chat/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.llms import LlamaCpp
from langchain_community.vectorstores import Qdrant
from langchain_community.vectorstores import FAISS

from libre_chat.conf import ChatConf, default_conf
from libre_chat.utils import BOLD, CYAN, END, log, parallel_download
Expand Down Expand Up @@ -163,18 +163,16 @@ def get_llm(self, config: Optional[Dict[str, Any]] = None) -> LlamaCpp:
def setup_dbqa(self) -> None:
"""Setup the vectorstore for QA"""
if self.has_vectorstore():
from qdrant_client import QdrantClient

embeddings = HuggingFaceEmbeddings(
model_name=self.conf.vector.embeddings_path, model_kwargs={"device": self.device}
)
# FAISS should automatically use GPU?
# vectorstore = FAISS.load_local(self.get_vectorstore(), embeddings)
vectorstore = Qdrant(
QdrantClient(host=self.conf.vector.vector_path, prefer_grpc=True),
collection_name="libre_chat_rag",
embeddings=embeddings,
)
vectorstore = FAISS.load_local(self.get_vectorstore(), embeddings)
# vectorstore = Qdrant(
# QdrantClient(host=self.conf.vector.vector_path, prefer_grpc=True),
# collection_name="libre_chat_rag",
# embeddings=embeddings,
# )

search_args: Dict[str, Any] = {"k": self.conf.vector.return_sources_count}
if self.conf.vector.score_threshold is not None:
Expand All @@ -189,7 +187,7 @@ def setup_dbqa(self) -> None:
chain_type_kwargs={"prompt": self.prompt},
)

def build_vectorstore(self, documents_path: Optional[str] = None) -> Optional[Qdrant]:
def build_vectorstore(self, documents_path: Optional[str] = None) -> Optional[FAISS]:
"""Build vectorstore from documents."""
# https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/qdrant.py
time_start = datetime.now()
Expand Down Expand Up @@ -226,19 +224,20 @@ def build_vectorstore(self, documents_path: Optional[str] = None) -> Optional[Qd
embeddings = HuggingFaceEmbeddings(
model_name=self.conf.vector.embeddings_path, model_kwargs={"device": self.device}
)
os.makedirs(str(self.conf.vector.vector_path), exist_ok=True)
vectorstore = Qdrant.from_documents(
splitted_texts,
embeddings,
# path=self.conf.vector.vector_path,
host=self.conf.vector.vector_path,
collection_name="libre_chat_rag",
prefer_grpc=True,
# force_recreate=True,
)
# vectorstore = FAISS.from_documents(splitted_texts, embeddings)
# if self.vector_path:
# vectorstore.save_local(self.vector_path)
# TODO: use Qdrant vectorstore
# os.makedirs(str(self.conf.vector.vector_path), exist_ok=True)
# vectorstore = Qdrant.from_documents(
# splitted_texts,
# embeddings,
# # path=self.conf.vector.vector_path,
# host=self.conf.vector.vector_path,
# collection_name="libre_chat_rag",
# prefer_grpc=True,
# # force_recreate=True,
# )
vectorstore = FAISS.from_documents(splitted_texts, embeddings)
if self.vector_path:
vectorstore.save_local(self.vector_path)
log.info(f"✅ Vectorstore built in {datetime.now() - time_start}")
return vectorstore
return None
Expand Down

0 comments on commit af9d5af

Please sign in to comment.