Skip to content
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

refactor: Use Qdrant's Query API #400

Merged
merged 7 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ torchvision = { version = ">=0.17.0,<0.18.0", optional = true}
pillow = { version = ">=10.2.0,<11.0.0", optional = true}
tiktoken = ">=0.6.0,<1.0.0"
matplotlib = { version = "^3.8.3", optional = true}
qdrant-client = {version = "^1.8.0", optional = true}
qdrant-client = {version = "^1.11.1", optional = true}
google-cloud-aiplatform = {version = "^1.45.0", optional = true}
requests-mock = "^1.12.1"
boto3 = { version = "^1.34.98", optional = true }
Expand Down
27 changes: 15 additions & 12 deletions semantic_router/index/qdrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from semantic_router.index.base import BaseIndex
from semantic_router.schema import Metric

from semantic_router.utils.logger import logger

DEFAULT_COLLECTION_NAME = "semantic-router-index"
Expand Down Expand Up @@ -97,7 +96,7 @@

def _initialize_clients(self):
try:
from qdrant_client import QdrantClient, AsyncQdrantClient
from qdrant_client import AsyncQdrantClient, QdrantClient

sync_client = QdrantClient(
location=self.location,
Expand Down Expand Up @@ -264,7 +263,7 @@
top_k: int = 5,
route_filter: Optional[List[str]] = None,
) -> Tuple[np.ndarray, List[str]]:
from qdrant_client import models, QdrantClient
from qdrant_client import QdrantClient, models

self.client: QdrantClient
filter = None
Expand All @@ -278,15 +277,17 @@
]
)

results = self.client.search(
results = self.client.query_points(
self.index_name,
query_vector=vector,
query=vector,
limit=top_k,
with_payload=True,
query_filter=filter,
)
scores = [result.score for result in results]
route_names = [result.payload[SR_ROUTE_PAYLOAD_KEY] for result in results]
scores = [result.score for result in results.points]
route_names = [
result.payload[SR_ROUTE_PAYLOAD_KEY] for result in results.points
]
return np.array(scores), route_names

async def aquery(
Expand All @@ -295,7 +296,7 @@
top_k: int = 5,
route_filter: Optional[List[str]] = None,
) -> Tuple[np.ndarray, List[str]]:
from qdrant_client import models, AsyncQdrantClient
from qdrant_client import AsyncQdrantClient, models

Check warning on line 299 in semantic_router/index/qdrant.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/qdrant.py#L299

Added line #L299 was not covered by tests

self.aclient: Optional[AsyncQdrantClient]
if self.aclient is None:
Expand All @@ -313,15 +314,17 @@
]
)

results = await self.aclient.search(
results = await self.aclient.query_points(

Check warning on line 317 in semantic_router/index/qdrant.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/qdrant.py#L317

Added line #L317 was not covered by tests
self.index_name,
query_vector=vector,
query=vector,
limit=top_k,
with_payload=True,
query_filter=filter,
)
scores = [result.score for result in results]
route_names = [result.payload[SR_ROUTE_PAYLOAD_KEY] for result in results]
scores = [result.score for result in results.points]
route_names = [

Check warning on line 325 in semantic_router/index/qdrant.py

View check run for this annotation

Codecov / codecov/patch

semantic_router/index/qdrant.py#L324-L325

Added lines #L324 - L325 were not covered by tests
result.payload[SR_ROUTE_PAYLOAD_KEY] for result in results.points
]
return np.array(scores), route_names

def aget_routes(self):
Expand Down
Loading