-
Notifications
You must be signed in to change notification settings - Fork 102
Qdrant vs implementation #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AmoghTantradi
wants to merge
65
commits into
main
Choose a base branch
from
qdrant_vs_implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
e3abd90
initial scaffolding for adding vector store / vector database integra…
AmoghTantradi bd1e8fd
fixed linting, ruff checks pass
AmoghTantradi 880c31f
added changes to requirements.txt file and added additional abstract …
AmoghTantradi 7b5dfd3
refactored
AmoghTantradi 08dfaba
added tests for clustering and filtering
AmoghTantradi f3a82c1
made edits to test_filter
AmoghTantradi fc62846
added implementations for weaviate and pinecone vs
AmoghTantradi 3e89b5f
fixed merge conflicts
AmoghTantradi f2937ad
added extra refactoring and added implementations for qdrant and chro…
AmoghTantradi a4c7418
fixed some type errors
AmoghTantradi 1357fb3
made further corrections
AmoghTantradi c76b658
edit uuid type
AmoghTantradi 9f257f7
changed uuid type
AmoghTantradi 99cb535
made type changes to weaviate file
AmoghTantradi 3c8a742
made another change
AmoghTantradi ccd9e48
typecheck passes for weaviate?
AmoghTantradi 89bf974
type changes for weaviate and qdrant files
AmoghTantradi a76adb7
made changes to weaviate file
AmoghTantradi c3e0f0c
made changes to weaviate file
AmoghTantradi 1782281
fixed pinecone type errors
AmoghTantradi 0621b9b
fixed pinecone type errors
AmoghTantradi b568d1e
type checks all pass locally
AmoghTantradi 9b33a1f
fixed linting errors
AmoghTantradi 820f3be
made refactors to allow for testing
AmoghTantradi a0a70d2
made changes to tests
AmoghTantradi 6dbd1db
fixed
AmoghTantradi bea1d19
changed setattr to getattr
AmoghTantradi f93f7ed
fixed a test
AmoghTantradi 38ff87d
over
AmoghTantradi c885dbc
another change
AmoghTantradi 8eefac0
fixed type check errors
AmoghTantradi 23bafa5
second refactor (removed index_dir)
AmoghTantradi 75d11ea
fixed type checks
AmoghTantradi 0b0bf38
fixed retriever module errors
AmoghTantradi 6bf7926
fixed key error
AmoghTantradi f7071a2
added fixes to failing rm tests
AmoghTantradi 6ebe407
fixed chroma
AmoghTantradi e588bee
removed dynamic indexing for weaviatevs
AmoghTantradi d6a86e1
fixed type errors
AmoghTantradi ddfd549
changed weaviate index config
AmoghTantradi 20206e1
changed rm tests index name to avoid pinecone failures
AmoghTantradi e7ea24f
fixed naming convention for index_dir and fixed serverless spec for p…
AmoghTantradi f152b54
changed serverless spec for pc index due to free plan
AmoghTantradi 2e21a97
added debug statement
AmoghTantradi 524b501
made changes to errors
AmoghTantradi 87f57e1
Merge branch 'main' of github.com:guestrin-lab/lotus into at/vs_imple…
AmoghTantradi e995996
added some fixes to collection upload error handling
AmoghTantradi 1a75486
made some other change
AmoghTantradi c5f50f6
fixed type errors for
8000
qdrant vs
AmoghTantradi 85daf51
changed endpoint
AmoghTantradi 6b80fd3
added changes
AmoghTantradi 4bafdb7
Merge branch 'main' of github.com:guestrin-lab/lotus into at/vs_imple…
AmoghTantradi f90ff0f
added fixes
AmoghTantradi cccfa39
added some changes
AmoghTantradi 6cf4f0a
added some change
AmoghTantradi 0438b18
another set of changes
AmoghTantradi 43e9bc3
added other logs
AmoghTantradi 90d07d0
added logging
AmoghTantradi 9c371a0
pr for qdrant
AmoghTantradi 154c2be
removed redundant imports
AmoghTantradi bdd2463
removed pinecone reference
AmoghTantradi 0998e24
resolved merge conflicts with qdrant
AmoghTantradi 9b1928c
added type check and integration for qdrant vs
AmoghTantradi 3cc4537
added another test to qdrant
AmoghTantradi 08a309f
fixed imports
AmoghTantradi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,6 @@ def __call__( | |
|
||
df_idxs = self._obj.index | ||
cur_min = len(df_idxs) | ||
|
||
K = min(K, cur_min) | ||
|
||
search_K = K | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from lotus.vector_store.vs import VS | ||
from lotus.vector_store.faiss_vs import FaissVS | ||
from lotus.vector_store.qdrant_vs import QdrantVS | ||
|
||
__all__ = ["VS", "FaissVS"] | ||
__all__ = ["VS", "FaissVS", "QdrantVS"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
from typing import Any, Optional | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from numpy.typing import NDArray | ||
from tqdm import tqdm | ||
|
||
from lotus.types import RMOutput | ||
from lotus.vector_store.vs import VS | ||
|
||
try: | ||
from qdrant_client import QdrantClient | ||
from qdrant_client.http.models import Distance, FieldCondition, Filter, MatchValue, PointStruct, VectorParams | ||
except ImportError as err: | ||
raise ImportError("Please install the qdrant client") from err | ||
|
||
class QdrantVS(VS): | ||
def __init__(self, max_batch_size: int = 64): | ||
|
||
API_KEY = '_Mic3dVln2gAkS6NLyia6p-CCyMScK42ayuq8Rapm5-xsV5j5_UlIA' | ||
|
||
URL = "https://6f8b9aec-a788-4aac-9aeb-417d307493e8.europe-west3-0.gcp.cloud.qdrant.io:6333" | ||
|
||
client: QdrantClient = QdrantClient( | ||
url=URL, | ||
api_key=API_KEY | ||
) | ||
|
||
"""Initialize with Qdrant client and embedding model""" | ||
super() # Fixed the super() call syntax | ||
self.client: QdrantClient = client | ||
self.max_batch_size = max_batch_size | ||
|
||
def __del__(self): | ||
self.client.close() | ||
|
||
def index(self, docs:pd.Series, embeddings, index_dir: str, **kwargs: dict[str, Any]): | ||
"""Create a collection and add documents with their embeddings""" | ||
self.index_dir = index_dir | ||
|
||
# Get sample embedding to determine vector dimension | ||
dimension = np.reshape(embeddings, (len(embeddings), -1)).shape[1] | ||
|
||
# Create collection if it doesn't exist | ||
if not self.client.collection_exists(index_dir): | ||
self.client.create_collection( | ||
collection_name=index_dir, | ||
vectors_config=VectorParams(size=dimension, distance=Distance.COSINE) | ||
) | ||
collection_info = self.client.get_collection(index_dir) | ||
if (collection_info is not None and collection_info.config is not None and collection_info.config.params and collection_info.config.params.vectors): | ||
|
||
vectors = collection_info.config.params.vectors | ||
if isinstance(vectors, dict): | ||
# If it's a dict, decide how to handle it. | ||
# Here we extract the first vector, but you may need a different logic. | ||
vector = next(iter(vectors.values())) | ||
size = vector.size | ||
elif isinstance(vectors, VectorParams): | ||
size = vectors.size | ||
else: | ||
size = None | ||
|
||
if size != dimension: | ||
# If there's a discrepancy, create a new version of that collection | ||
self.client.delete_collection(index_dir) | ||
self.client.create_collection( | ||
collection_name=index_dir, | ||
vectors_config=VectorParams(size=dimension, distance=Distance.COSINE) | ||
) | ||
|
||
# Convert docs to list if it's a pandas Series | ||
docs_list = docs.tolist() if isinstance(docs, pd.Series) else docs | ||
|
||
# Prepare points for upload | ||
points = [] | ||
for idx, (doc, embedding) in enumerate(zip(docs_list, embeddings)): | ||
points.append( | ||
PointStruct( | ||
id=idx, | ||
vector=embedding.tolist(), | ||
payload={ | ||
"content": doc, | ||
"doc_id": idx | ||
} | ||
) | ||
) | ||
|
||
# Upload in batches | ||
batch_size = 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove hard code |
||
for i in tqdm(range(0, len(points), batch_size), desc="Uploading to Qdrant"): | ||
batch = points[i:i + batch_size] | ||
self.client.upsert( | ||
collection_name=index_dir, | ||
points=batch | ||
) | ||
|
||
def load_index(self, index_dir: str): | ||
"""Set the collection name to use""" | ||
if not self.client.collection_exists(index_dir): | ||
raise ValueError(f"Collection {index_dir} not found") | ||
self.index_dir = index_dir | ||
|
||
def __call__( | ||
self, | ||
query_vectors, | ||
K: int, | ||
ids: Optional[list[int]] = None, | ||
**kwargs: dict[str, Any] | ||
) -> RMOutput: | ||
"""Perform vector search using Qdrant""" | ||
if self.index_dir is None: | ||
raise ValueError("No collection loaded. Call load_index first.") | ||
|
||
# Perform searches | ||
all_distances = [] | ||
all_indices = [] | ||
|
||
for query_vector in query_vectors: | ||
results = self.client.search( | ||
collection_name=self.index_dir, | ||
query_vector=query_vector.tolist(), | ||
limit=K, | ||
with_payload=True, | ||
query_filter=Filter( | ||
should=[ | ||
FieldCondition(key="doc_id", match=MatchValue(value=id)) for id in ids | ||
] | ||
) if ids is not None else None | ||
) | ||
|
||
# Extract distances and indices | ||
distances = [] | ||
indices = [] | ||
|
||
for result in results: | ||
indices.append(result.id) | ||
distances.append(result.score) # Qdrant returns cosine similarity directly | ||
|
||
# Pad results if fewer than K matches | ||
while len(indices) < K: | ||
indices.append(-1) | ||
distances.append(0.0) | ||
|
||
all_distances.append(d 109CB istances) | ||
all_indices.append(indices) | ||
|
||
return RMOutput( | ||
distances=np.array(all_distances, dtype=np.float32).tolist(), | ||
indices=np.array(all_indices, dtype=np.int64).tolist() | ||
) | ||
|
||
def get_vectors_from_index(self, index_dir: str, ids: list[int]) -> NDArray[np.float64]: | ||
"""Retrieve vectors for specific document IDs""" | ||
if self.index_dir != index_dir: | ||
self.load_index(index_dir) | ||
|
||
# Fetch points from Qdrant | ||
points = self.client.retrieve( | ||
collection_name=index_dir, | ||
ids=ids, | ||
with_vectors=True, | ||
with_payload=False | ||
) | ||
|
||
# Extract and return vectors | ||
vectors = [] | ||
for point in points: | ||
if point.vector is not None: | ||
vectors.append(point.vector) | ||
else: | ||
raise ValueError(f"Vector not found for id {point.id}") | ||
|
||
return np.array(vectors, dtype=np.float64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove hardcode and remove api key