-
Notifications
You must be signed in to change notification settings - Fork 2k
[ENH]: Allow specifiying multiple filter keys in get_statistics #5963
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
Merged
+55
−9
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ | |
| from typing import TYPE_CHECKING, Optional, Dict, Any, cast | ||
| from collections import defaultdict | ||
|
|
||
| from chromadb.api.types import Where | ||
| from chromadb.api.types import OneOrMany, Where, maybe_cast_one_to_many | ||
|
|
||
| if TYPE_CHECKING: | ||
| from chromadb.api.models.Collection import Collection | ||
|
|
@@ -121,7 +121,9 @@ def detach_statistics_function( | |
|
|
||
|
|
||
| def get_statistics( | ||
| collection: "Collection", stats_collection_name: str, key: Optional[str] = None | ||
| collection: "Collection", | ||
| stats_collection_name: str, | ||
| keys: Optional[OneOrMany[str]] = None, | ||
| ) -> Dict[str, Any]: | ||
| """Get the current statistics for a collection. | ||
|
|
||
|
|
@@ -131,8 +133,9 @@ def get_statistics( | |
| Args: | ||
| collection: The collection to get statistics for | ||
| stats_collection_name: Name of the statistics collection to read from. | ||
| key: Optional metadata key to filter statistics for. If provided, | ||
| only returns statistics for that specific key. | ||
| keys: Optional metadata key(s) to filter statistics for. Can be a single key | ||
| string or a list of keys. If provided, only returns statistics for | ||
| those specific keys. | ||
|
|
||
| Returns: | ||
| Dict[str, Any]: A dictionary with the structure: | ||
|
|
@@ -174,7 +177,22 @@ def get_statistics( | |
| "total_count": 2 | ||
| } | ||
| } | ||
|
|
||
| Raises: | ||
| ValueError: If more than 30 keys are provided in the keys filter. | ||
| """ | ||
| # Normalize keys to list | ||
| keys_list = maybe_cast_one_to_many(keys) | ||
|
|
||
| # Validate keys count to avoid issues with large $in queries | ||
| MAX_KEYS = 30 | ||
|
Contributor
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. [Maintainability] Consider defining Context for Agents |
||
| if keys_list is not None and len(keys_list) > MAX_KEYS: | ||
| raise ValueError( | ||
| f"Too many keys provided: {len(keys_list)}. " | ||
| f"Maximum allowed is {MAX_KEYS} keys per request. " | ||
| "Consider calling get_statistics multiple times with smaller key batches." | ||
| ) | ||
|
|
||
| # Import here to avoid circular dependency | ||
| from chromadb.api.models.Collection import Collection | ||
|
|
||
|
|
@@ -198,10 +216,10 @@ def get_statistics( | |
| summary: Dict[str, Any] = {} | ||
|
|
||
| offset = 0 | ||
| # When filtering by key, also include "summary" entries to get total_count | ||
| # When filtering by keys, also include "summary" entries to get total_count | ||
| where_filter: Optional[Where] = ( | ||
| cast(Where, {"$or": [{"key": key}, {"key": "summary"}]}) | ||
| if key is not None | ||
| cast(Where, {"key": {"$in": keys_list + ["summary"]}}) | ||
| if keys_list is not None | ||
| else None | ||
| ) | ||
|
|
||
|
|
||
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.
[Maintainability] Renaming the
keyparameter tokeysis a breaking API change for any callers using keyword arguments. While the new name is more descriptive, this could be made backward-compatible to avoid breaking existing user code and provide a smoother transition.If this breaking change is intentional, it should be clearly documented in the project's release notes. If not, you could consider handling the old
keyparameter with a deprecation warning.Here's an example of how you could support both for a transition period by modifying the function signature and adding logic to the function body:
Signature:
Function Body Logic:
Context for Agents