-
Notifications
You must be signed in to change notification settings - Fork 84
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
WIP on threadpool impl of query_namespaces #405
Closed
Closed
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 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 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 |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
from pinecone.core.openapi.data.api.data_plane_api import DataPlaneApi | ||
from ..utils import setup_openapi_client, parse_non_empty_args | ||
from .vector_factory import VectorFactory | ||
from .query_results_aggregator import QueryResultsAggregator, QueryNamespacesResults | ||
from multiprocessing.pool import ApplyResult | ||
|
||
__all__ = [ | ||
"Index", | ||
|
@@ -361,7 +363,7 @@ def query( | |
Union[SparseValues, Dict[str, Union[List[float], List[int]]]] | ||
] = None, | ||
**kwargs, | ||
) -> QueryResponse: | ||
) -> Union[QueryResponse, ApplyResult]: | ||
""" | ||
The Query operation searches a namespace, using a query vector. | ||
It retrieves the ids of the most similar items in a namespace, along with their similarity scores. | ||
|
@@ -403,6 +405,39 @@ def query( | |
and namespace name. | ||
""" | ||
|
||
response = self._query( | ||
*args, | ||
top_k=top_k, | ||
vector=vector, | ||
id=id, | ||
namespace=namespace, | ||
filter=filter, | ||
include_values=include_values, | ||
include_metadata=include_metadata, | ||
sparse_vector=sparse_vector, | ||
**kwargs, | ||
) | ||
|
||
if kwargs.get("async_req", False): | ||
return response | ||
else: | ||
return parse_query_response(response) | ||
|
||
def _query( | ||
self, | ||
*args, | ||
top_k: int, | ||
vector: Optional[List[float]] = None, | ||
id: Optional[str] = None, | ||
namespace: Optional[str] = None, | ||
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None, | ||
include_values: Optional[bool] = None, | ||
include_metadata: Optional[bool] = None, | ||
sparse_vector: Optional[ | ||
Union[SparseValues, Dict[str, Union[List[float], List[int]]]] | ||
] = None, | ||
**kwargs, | ||
) -> QueryResponse: | ||
if len(args) > 0: | ||
raise ValueError( | ||
"The argument order for `query()` has changed; please use keyword arguments instead of positional arguments. Example: index.query(vector=[0.1, 0.2, 0.3], top_k=10, namespace='my_namespace')" | ||
|
@@ -435,7 +470,58 @@ def query( | |
), | ||
**{k: v for k, v in kwargs.items() if k in _OPENAPI_ENDPOINT_PARAMS}, | ||
) | ||
return parse_query_response(response) | ||
return response | ||
|
||
@validate_and_convert_errors | ||
def query_namespaces( | ||
self, | ||
vector: List[float], | ||
namespaces: List[str], | ||
top_k: Optional[int] = None, | ||
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None, | ||
include_values: Optional[bool] = None, | ||
include_metadata: Optional[bool] = None, | ||
sparse_vector: Optional[ | ||
Union[SparseValues, Dict[str, Union[List[float], List[int]]]] | ||
] = None, | ||
**kwargs, | ||
) -> QueryNamespacesResults: | ||
if len(namespaces) == 0: | ||
raise ValueError("At least one namespace must be specified") | ||
if len(vector) == 0: | ||
raise ValueError("Query vector must not be empty") | ||
|
||
# The caller may only want the top_k=1 result across all queries, | ||
# but we need to get at least 2 results from each query in order to | ||
# aggregate them correctly. So we'll temporarily set topK to 2 for the | ||
# subqueries, and then we'll take the topK=1 results from the aggregated | ||
# results. | ||
overall_topk = top_k if top_k is not None else 10 | ||
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. what is this magic number 10? 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. It's just a default value. Could be anything, but the API requires a value to be passed. |
||
aggregator = QueryResultsAggregator(top_k=overall_topk) | ||
subquery_topk = overall_topk if overall_topk > 2 else 2 | ||
|
||
target_namespaces = set(namespaces) # dedup namespaces | ||
async_results = [ | ||
self.query( | ||
vector=vector, | ||
namespace=ns, | ||
top_k=subquery_topk, | ||
filter=filter, | ||
include_values=include_values, | ||
include_metadata=include_metadata, | ||
sparse_vector=sparse_vector, | ||
async_req=True, | ||
**kwargs, | ||
) | ||
for ns in target_namespaces | ||
] | ||
|
||
for result in async_results: | ||
response = result.get() | ||
aggregator.add_results(response) | ||
|
||
final_results = aggregator.get_results() | ||
return final_results | ||
|
||
@validate_and_convert_errors | ||
def update( | ||
|
File renamed without changes.
This file contains 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
2 changes: 1 addition & 1 deletion
2
...nit_grpc/test_query_results_aggregator.py → tests/unit/test_query_results_aggregator.py
This file contains 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
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.
I'm not a python expert, but isn;t there some library that provides a decorator to retry functions? seems like this should be a solved problem
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.
There's nothing in the standard library afaik, and I'm very hesitant to add third-party dependencies because of the overall dependency hell situation in python.