File size: 9,729 Bytes
b3d711f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
"""Utility functions for managing collection statistics.



This module provides standalone functions for enabling, disabling, and retrieving

statistics for ChromaDB collections. These functions work with the attached function

system to automatically compute metadata value frequencies.



Example:

    >>> from chromadb.utils.statistics import attach_statistics_function, get_statistics

    >>> import chromadb

    >>>

    >>> client = chromadb.Client()

    >>> collection = client.get_or_create_collection("my_collection")

    >>>

    >>> # Attach statistics function with output collection name

    >>> attach_statistics_function(collection, "my_collection_statistics")

    >>>

    >>> # Add some data

    >>> collection.add(

    ...     ids=["id1", "id2"],

    ...     documents=["doc1", "doc2"],

    ...     metadatas=[{"category": "A"}, {"category": "B"}]

    ... )

    >>>

    >>> # Get statistics from the named output collection

    >>> stats = get_statistics(collection, "my_collection_statistics")

    >>> print(stats)

"""

from typing import TYPE_CHECKING, Optional, Dict, Any, cast, Tuple
from collections import defaultdict
from chromadb.api.types import OneOrMany, Where, maybe_cast_one_to_many
from chromadb.api.functions import STATISTICS_FUNCTION

if TYPE_CHECKING:
    from chromadb.api.models.Collection import Collection
    from chromadb.api.models.AttachedFunction import AttachedFunction


def get_statistics_fn_name(collection: "Collection") -> str:
    """Generate the default name for the statistics attached function.



    Args:

        collection: The collection to generate the name for



    Returns:

        str: The statistics function name

    """
    return f"{collection.name}_stats"


def attach_statistics_function(

    collection: "Collection", stats_collection_name: str

) -> Tuple["AttachedFunction", bool]:
    """Attach statistics collection function to a collection.



    This attaches the statistics function which will automatically compute

    and update metadata value frequencies whenever records are added, updated,

    or deleted.



    Args:

        collection: The collection to enable statistics for

        stats_collection_name: Name of the collection where statistics will be stored.



    Returns:

        Tuple of (AttachedFunction, created) where created is True if newly created,

        False if already existed (idempotent request)



    Example:

        >>> attached_fn, created = attach_statistics_function(collection, "my_collection_statistics")

        >>> if created:

        ...     print("Statistics function newly attached")

        >>> collection.add(ids=["id1"], documents=["doc1"], metadatas=[{"key": "value"}])

        >>> # Statistics are automatically computed

        >>> stats = get_statistics(collection, "my_collection_statistics")

    """
    return collection.attach_function(
        function=STATISTICS_FUNCTION,
        name=get_statistics_fn_name(collection),
        output_collection=stats_collection_name,
        params=None,
    )


def get_statistics_fn(collection: "Collection") -> "AttachedFunction":
    """Get the statistics attached function for a collection.



    Args:

        collection: The collection to get the statistics function for



    Returns:

        AttachedFunction: The statistics function



    Raises:

        NotFoundError: If statistics are not enabled

        AssertionError: If the attached function is not a statistics function

    """
    af = collection.get_attached_function(get_statistics_fn_name(collection))
    assert (
        af.function_name == "statistics"
    ), "Attached function is not a statistics function"
    return af


def detach_statistics_function(

    collection: "Collection", delete_stats_collection: bool = False

) -> bool:
    """Detach statistics collection function from a collection.



    Args:

        collection: The collection to disable statistics for

        delete_stats_collection: If True, also delete the statistics output collection.

                                  Defaults to False.



    Returns:

        bool: True if successful



    Example:

        >>> detach_statistics_function(collection, delete_stats_collection=True)

    """
    attached_fn = get_statistics_fn(collection)
    return collection.detach_function(
        attached_fn.name, delete_output_collection=delete_stats_collection
    )


def get_statistics(

    collection: "Collection",

    stats_collection_name: str,

    keys: Optional[OneOrMany[str]] = None,

) -> Dict[str, Any]:
    """Get the current statistics for a collection.



    Statistics include frequency counts for all metadata key-value pairs,

    as well as a summary with the total record count.



    Args:

        collection: The collection to get statistics for

        stats_collection_name: Name of the statistics collection to read from.

        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:

            {

                "statistics": {

                    "key1": {

                        "value1": {"count": count, ...},

                        "value2": {"count": count, ...}

                    },

                    "key2": {...},

                    ...

                },

                "summary": {

                    "total_count": count

                }

            }



    Example:

        >>> attach_statistics_function(collection, "my_collection_statistics")

        >>> collection.add(

        ...     ids=["id1", "id2"],

        ...     documents=["doc1", "doc2"],

        ...     metadatas=[{"category": "A", "score": 10}, {"category": "B", "score": 10}]

        ... )

        >>> # Wait for statistics to be computed

        >>> stats = get_statistics(collection, "my_collection_statistics")

        >>> print(stats)

        {

            "statistics": {

                "category": {

                    "A": {"count": 1},

                    "B": {"count": 1}

                },

                "score": {

                    "10": {"count": 2}

                }

            },

            "summary": {

                "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
    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

    # Get the statistics output collection model from the server
    stats_collection_model = collection._client.get_collection(
        name=stats_collection_name,
        tenant=collection.tenant,
        database=collection.database,
    )

    # Wrap it in a Collection object to access get/query methods
    stats_collection = Collection(
        client=collection._client,
        model=stats_collection_model,
        embedding_function=None,  # Statistics collections don't need embedding functions
        data_loader=None,
    )

    # Get all statistics records by paginating through the stats collection
    stats: Dict[str, Dict[str, Dict[str, int]]] = defaultdict(lambda: defaultdict(dict))
    summary: Dict[str, Any] = {}

    offset = 0
    # When filtering by keys, also include "summary" entries to get total_count
    where_filter: Optional[Where] = (
        cast(Where, {"key": {"$in": keys_list + ["summary"]}})
        if keys_list is not None
        else None
    )

    while True:
        page = stats_collection.get(
            include=["metadatas"], offset=offset, where=where_filter
        )

        metadatas = page.get("metadatas") or []
        if not metadatas:
            break

        for metadata in metadatas:
            if metadata is None:
                continue

            meta_key = metadata.get("key")
            value = metadata.get("value")
            value_label = metadata.get("value_label")
            value_type = metadata.get("type")
            count = metadata.get("count")

            if (
                meta_key is not None
                and value is not None
                and value_type is not None
                and count is not None
            ):
                if meta_key == "summary":
                    if value == "total_count":
                        summary["total_count"] = count
                else:
                    # Prioritize value_label if present, otherwise use value
                    stats_key = value_label if value_label is not None else value
                    assert isinstance(meta_key, str)
                    assert isinstance(stats_key, str)
                    assert isinstance(count, int)
                    stats[meta_key][stats_key]["count"] = count

        # Advance to next page using the actual number of items returned
        offset += len(metadatas)

    result = {"statistics": dict(stats)}
    if summary:
        result["summary"] = summary

    return result