-
Notifications
You must be signed in to change notification settings - Fork 127
Add qdrant native engine #271
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
tellet-q
wants to merge
4
commits into
master
Choose a base branch
from
qdrant-native-engine
base: master
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from .configure import QdrantNativeConfigurator | ||
| from .search import QdrantNativeSearcher | ||
| from .upload import QdrantNativeUploader | ||
|
|
||
| __all__ = [ | ||
| "QdrantNativeConfigurator", | ||
| "QdrantNativeUploader", | ||
| "QdrantNativeSearcher", | ||
| ] |
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,4 @@ | ||
| import os | ||
|
|
||
| QDRANT_COLLECTION_NAME = os.getenv("QDRANT_COLLECTION_NAME", "benchmark") | ||
| QDRANT_API_KEY = os.getenv("QDRANT_API_KEY", None) |
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,132 @@ | ||
| import httpx | ||
|
|
||
| from benchmark.dataset import Dataset | ||
| from engine.base_client.configure import BaseConfigurator | ||
| from engine.base_client.distances import Distance | ||
| from engine.clients.qdrant_native.config import QDRANT_API_KEY, QDRANT_COLLECTION_NAME | ||
|
|
||
|
|
||
| class QdrantNativeConfigurator(BaseConfigurator): | ||
| SPARSE_VECTOR_SUPPORT = True | ||
| DISTANCE_MAPPING = { | ||
| Distance.L2: "Euclid", | ||
| Distance.COSINE: "Cosine", | ||
| Distance.DOT: "Dot", | ||
| } | ||
| INDEX_TYPE_MAPPING = { | ||
| "int": "integer", | ||
| "keyword": "keyword", | ||
| "text": "text", | ||
| "float": "float", | ||
| "geo": "geo", | ||
| } | ||
|
|
||
| def __init__(self, host, collection_params: dict, connection_params: dict): | ||
| super().__init__(host, collection_params, connection_params) | ||
|
|
||
| self.host = f"http://{host.rstrip('/')}:6333" | ||
| self.connection_params = connection_params | ||
|
|
||
| self.headers = {"Content-Type": "application/json"} | ||
| if QDRANT_API_KEY: | ||
| self.headers["api-key"] = QDRANT_API_KEY | ||
|
|
||
| timeout = connection_params.get("timeout", 30) | ||
| self.client = httpx.Client( | ||
| headers=self.headers, | ||
| timeout=httpx.Timeout(timeout=timeout), | ||
| ) | ||
|
|
||
| def clean(self): | ||
| """Delete the collection""" | ||
| url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}" | ||
| response = self.client.delete(url) | ||
| # 404 is ok if collection doesn't exist | ||
| if response.status_code not in [200, 404]: | ||
| response.raise_for_status() | ||
|
|
||
| def recreate(self, dataset: Dataset, collection_params): | ||
| """Create collection with proper configuration""" | ||
| url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}" | ||
|
|
||
| # Build vectors configuration | ||
| if dataset.config.type == "sparse": | ||
| vectors_config = {} | ||
| sparse_vectors_config = { | ||
| "sparse": { | ||
| "index": { | ||
| "on_disk": False, | ||
| } | ||
| } | ||
| } | ||
| else: | ||
| is_vectors_on_disk = self.collection_params.get("vectors_config", {}).get( | ||
| "on_disk", False | ||
| ) | ||
| self.collection_params.pop("vectors_config", None) | ||
|
|
||
| vectors_config = { | ||
| "size": dataset.config.vector_size, | ||
| "distance": self.DISTANCE_MAPPING.get(dataset.config.distance), | ||
| "on_disk": is_vectors_on_disk, | ||
| } | ||
| sparse_vectors_config = None | ||
|
|
||
| payload_index_params = self.collection_params.pop("payload_index_params", {}) | ||
| if not set(payload_index_params.keys()).issubset(dataset.config.schema.keys()): | ||
| raise ValueError("payload_index_params are not found in dataset schema") | ||
|
|
||
| # Set optimizers config - disable index building during upload by default | ||
| optimizers_config = self.collection_params.setdefault("optimizers_config", {}) | ||
| optimizers_config.setdefault("max_optimization_threads", 0) | ||
|
|
||
| # Build the collection creation payload | ||
| payload = {} | ||
| if vectors_config: | ||
| payload["vectors"] = vectors_config | ||
| if sparse_vectors_config: | ||
| payload["sparse_vectors"] = sparse_vectors_config | ||
|
|
||
| for key, value in self.collection_params.items(): | ||
| payload[key] = value | ||
|
|
||
| response = self.client.put(url, json=payload) | ||
| response.raise_for_status() | ||
|
|
||
| for field_name, field_type in dataset.config.schema.items(): | ||
| self._create_payload_index(field_name, field_type, payload_index_params) | ||
|
|
||
| def _create_payload_index( | ||
| self, field_name: str, field_type: str, payload_index_params: dict | ||
| ): | ||
| """Create a payload index for a specific field""" | ||
| url = f"{self.host}/collections/{QDRANT_COLLECTION_NAME}/index" | ||
|
|
||
| # Build the field schema based on type | ||
| if field_type in ["keyword", "uuid"]: | ||
| field_schema = { | ||
| "type": self.INDEX_TYPE_MAPPING.get(field_type, "keyword"), | ||
| } | ||
|
|
||
| # Add optional parameters if provided | ||
| params = payload_index_params.get(field_name, {}) | ||
| if "is_tenant" in params and params["is_tenant"] is not None: | ||
| field_schema["is_tenant"] = params["is_tenant"] | ||
| if "on_disk" in params and params["on_disk"] is not None: | ||
| field_schema["on_disk"] = params["on_disk"] | ||
| else: | ||
| # For other types, just use the type string | ||
| field_schema = self.INDEX_TYPE_MAPPING.get(field_type, field_type) | ||
|
|
||
| payload = { | ||
| "field_name": field_name, | ||
| "field_schema": field_schema, | ||
| } | ||
|
|
||
| response = self.client.put(url, json=payload) | ||
| response.raise_for_status() | ||
|
|
||
| def delete_client(self): | ||
| """Cleanup HTTP client""" | ||
| if hasattr(self, "client") and self.client is not None: | ||
| self.client.close() | ||
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,70 @@ | ||
| from typing import Any, List, Optional | ||
|
|
||
| from engine.base_client.parser import BaseConditionParser, FieldValue | ||
|
|
||
|
|
||
| class QdrantNativeConditionParser(BaseConditionParser): | ||
| """ | ||
| Parser that converts internal filter format to Qdrant REST API JSON format. | ||
| Returns plain dictionaries instead of Pydantic models. | ||
| """ | ||
|
|
||
| def build_condition( | ||
| self, and_subfilters: Optional[List[Any]], or_subfilters: Optional[List[Any]] | ||
| ) -> Optional[Any]: | ||
| """Build a filter condition combining AND/OR subfilters""" | ||
| filter_dict = {} | ||
|
|
||
| if and_subfilters: | ||
| filter_dict["must"] = and_subfilters | ||
|
|
||
| if or_subfilters: | ||
| filter_dict["should"] = or_subfilters | ||
|
|
||
| return filter_dict if filter_dict else None | ||
|
|
||
| def build_exact_match_filter(self, field_name: str, value: FieldValue) -> Any: | ||
| """Build an exact match filter""" | ||
| return { | ||
| "key": field_name, | ||
| "match": {"value": value}, | ||
| } | ||
|
|
||
| def build_range_filter( | ||
| self, | ||
| field_name: str, | ||
| lt: Optional[FieldValue], | ||
| gt: Optional[FieldValue], | ||
| lte: Optional[FieldValue], | ||
| gte: Optional[FieldValue], | ||
| ) -> Any: | ||
| """Build a range filter""" | ||
| range_dict = {} | ||
| if lt is not None: | ||
| range_dict["lt"] = lt | ||
| if gt is not None: | ||
| range_dict["gt"] = gt | ||
| if lte is not None: | ||
| range_dict["lte"] = lte | ||
| if gte is not None: | ||
| range_dict["gte"] = gte | ||
|
|
||
| return { | ||
| "key": field_name, | ||
| "range": range_dict, | ||
| } | ||
|
|
||
| def build_geo_filter( | ||
| self, field_name: str, lat: float, lon: float, radius: float | ||
| ) -> Any: | ||
| """Build a geo radius filter""" | ||
| return { | ||
| "key": field_name, | ||
| "geo_radius": { | ||
| "center": { | ||
| "lon": lon, | ||
| "lat": lat, | ||
| }, | ||
| "radius": radius, | ||
| }, | ||
| } |
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,93 @@ | ||
| from typing import List, Tuple | ||
|
|
||
| import httpx | ||
|
|
||
| from dataset_reader.base_reader import Query | ||
| from engine.base_client.search import BaseSearcher | ||
| from engine.clients.qdrant_native.config import QDRANT_API_KEY, QDRANT_COLLECTION_NAME | ||
| from engine.clients.qdrant_native.parser import QdrantNativeConditionParser | ||
|
|
||
|
|
||
| class QdrantNativeSearcher(BaseSearcher): | ||
| search_params = {} | ||
| client: httpx.Client = None | ||
| parser = QdrantNativeConditionParser() | ||
| host = None | ||
| headers = {} | ||
|
|
||
| @classmethod | ||
| def init_client(cls, host, distance, connection_params: dict, search_params: dict): | ||
| cls.host = f"http://{host.rstrip('/')}:6333" | ||
| cls.search_params = search_params | ||
|
|
||
| # Build headers | ||
| cls.headers = {"Content-Type": "application/json"} | ||
| if QDRANT_API_KEY: | ||
| cls.headers["api-key"] = QDRANT_API_KEY | ||
|
|
||
| # Create HTTP client | ||
| timeout = connection_params.get("timeout", 30) | ||
| cls.client = httpx.Client( | ||
| headers=cls.headers, | ||
| timeout=httpx.Timeout(timeout=timeout), | ||
| limits=httpx.Limits(max_connections=None, max_keepalive_connections=0), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def search_one(cls, query: Query, top: int) -> List[Tuple[int, float]]: | ||
| """Execute a single search query using REST API""" | ||
| url = f"{cls.host}/collections/{QDRANT_COLLECTION_NAME}/points/query" | ||
|
|
||
| if query.sparse_vector is None: | ||
| query_vector = query.vector | ||
| else: | ||
| query_vector = { | ||
| "indices": query.sparse_vector.indices, | ||
| "values": query.sparse_vector.values, | ||
| } | ||
|
|
||
| payload = { | ||
| "query": query_vector, | ||
| "limit": top, | ||
| } | ||
|
|
||
| if query.sparse_vector is not None: | ||
| payload["using"] = "sparse" | ||
|
|
||
| query_filter = cls.parser.parse(query.meta_conditions) | ||
| if query_filter: | ||
| payload["filter"] = query_filter | ||
|
|
||
| search_config = cls.search_params.get("config", {}) | ||
| if search_config: | ||
| payload["params"] = search_config | ||
|
|
||
| prefetch_config = cls.search_params.get("prefetch") | ||
| if prefetch_config: | ||
| prefetch = { | ||
| **prefetch_config, | ||
| "query": query_vector, | ||
| } | ||
| payload["prefetch"] = [prefetch] | ||
|
|
||
| with_payload = cls.search_params.get("with_payload", False) | ||
| payload["with_payload"] = with_payload | ||
|
|
||
| try: | ||
| response = cls.client.post(url, json=payload) | ||
| response.raise_for_status() | ||
| result = response.json() | ||
|
|
||
| points = result["result"]["points"] | ||
| return [(point["id"], point["score"]) for point in points] | ||
|
|
||
| except Exception as ex: | ||
| print(f"Something went wrong during search: {ex}") | ||
| raise ex | ||
|
|
||
| @classmethod | ||
| def delete_client(cls): | ||
| """Cleanup HTTP client""" | ||
| if cls.client is not None: | ||
| cls.client.close() | ||
| cls.client = None |
Oops, something went wrong.
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.
The field type 'uuid' is checked but not present in the INDEX_TYPE_MAPPING dictionary (lines 16-22). This will cause
INDEX_TYPE_MAPPING.get(field_type, 'keyword')on line 106 to fall back to the default 'keyword', which may not be the intended behavior. Either add 'uuid' to INDEX_TYPE_MAPPING or remove it from this condition.