Skip to content

Commit 1542c7f

Browse files
committed
[ENH] Add where filtering support on list collections for local chroma
1 parent b752329 commit 1542c7f

File tree

24 files changed

+305
-36
lines changed

24 files changed

+305
-36
lines changed

chromadb/api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ class ClientAPI(BaseAPI, ABC):
343343
@abstractmethod
344344
def list_collections(
345345
self,
346+
where: Optional[Where] = None,
346347
limit: Optional[int] = None,
347348
offset: Optional[int] = None,
348349
) -> Sequence[Collection]:
349350
"""List all collections.
350351
Args:
352+
where: Conditional filtering on collection metadata. Defaults to None.
351353
limit: The maximum number of entries to return. Defaults to None.
352354
offset: The number of entries to skip before returning. Defaults to None.
353355
@@ -576,6 +578,7 @@ def count_collections(
576578
@abstractmethod
577579
def list_collections(
578580
self,
581+
where: Optional[Where] = None,
579582
limit: Optional[int] = None,
580583
offset: Optional[int] = None,
581584
tenant: str = DEFAULT_TENANT,

chromadb/api/async_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,13 @@ class AsyncClientAPI(AsyncBaseAPI, ABC):
336336
@abstractmethod
337337
async def list_collections(
338338
self,
339+
where: Optional[Where] = None,
339340
limit: Optional[int] = None,
340341
offset: Optional[int] = None,
341342
) -> Sequence[AsyncCollection]:
342343
"""List all collections.
343344
Args:
345+
where: Conditional filtering on collection metadata. Defaults to None.
344346
limit: The maximum number of entries to return. Defaults to None.
345347
offset: The number of entries to skip before returning. Defaults to None.
346348
@@ -562,6 +564,7 @@ class AsyncServerAPI(AsyncBaseAPI, AsyncAdminAPI, Component):
562564
@abstractmethod
563565
async def list_collections(
564566
self,
567+
where: Optional[Where] = None,
565568
limit: Optional[int] = None,
566569
offset: Optional[int] = None,
567570
tenant: str = DEFAULT_TENANT,

chromadb/api/async_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,13 @@ async def heartbeat(self) -> int:
159159

160160
@override
161161
async def list_collections(
162-
self, limit: Optional[int] = None, offset: Optional[int] = None
162+
self,
163+
where: Optional[Where] = None,
164+
limit: Optional[int] = None,
165+
offset: Optional[int] = None,
163166
) -> Sequence[AsyncCollection]:
164167
models = await self._server.list_collections(
165-
limit, offset, tenant=self.tenant, database=self.database
168+
where, limit, offset, tenant=self.tenant, database=self.database
166169
)
167170
return [AsyncCollection(client=self._server, model=model) for model in models]
168171

chromadb/api/async_fastapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from uuid import UUID
33
import urllib.parse
44
import orjson
5+
import json
56
from typing import Any, Optional, cast, Tuple, Sequence, Dict
67
import logging
78
import httpx
@@ -251,18 +252,21 @@ async def get_user_identity(self) -> UserIdentity:
251252
@override
252253
async def list_collections(
253254
self,
255+
where: Optional[Where] = None,
254256
limit: Optional[int] = None,
255257
offset: Optional[int] = None,
256258
tenant: str = DEFAULT_TENANT,
257259
database: str = DEFAULT_DATABASE,
258260
) -> Sequence[CollectionModel]:
261+
where_str = json.dumps(where) if where else None
259262
resp_json = await self._make_request(
260263
"get",
261264
f"/tenants/{tenant}/databases/{database}/collections",
262265
params=BaseHTTPClient._clean_params(
263266
{
264267
"limit": limit,
265268
"offset": offset,
269+
"where": where_str,
266270
}
267271
),
268272
)

chromadb/api/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ def heartbeat(self) -> int:
124124

125125
@override
126126
def list_collections(
127-
self, limit: Optional[int] = None, offset: Optional[int] = None
127+
self,
128+
where: Optional[Where] = None,
129+
limit: Optional[int] = None,
130+
offset: Optional[int] = None,
128131
) -> Sequence[Collection]:
129132
return [
130133
Collection(client=self._server, model=model)
131134
for model in self._server.list_collections(
132-
limit, offset, tenant=self.tenant, database=self.database
135+
where, limit, offset, tenant=self.tenant, database=self.database
133136
)
134137
]
135138

chromadb/api/fastapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from chromadb.api.base_http_client import BaseHTTPClient
1818
from chromadb.types import Database, Tenant, Collection as CollectionModel
1919
from chromadb.api import ServerAPI
20+
import json
2021

2122
from chromadb.api.types import (
2223
Documents,
@@ -205,19 +206,22 @@ def get_user_identity(self) -> UserIdentity:
205206
@override
206207
def list_collections(
207208
self,
209+
where: Optional[Where] = None,
208210
limit: Optional[int] = None,
209211
offset: Optional[int] = None,
210212
tenant: str = DEFAULT_TENANT,
211213
database: str = DEFAULT_DATABASE,
212214
) -> Sequence[CollectionModel]:
213215
"""Returns a list of all collections"""
216+
where_str = json.dumps(where) if where else None
214217
json_collections = self._make_request(
215218
"get",
216219
f"/tenants/{tenant}/databases/{database}/collections",
217220
params=BaseHTTPClient._clean_params(
218221
{
219222
"limit": limit,
220223
"offset": offset,
224+
"where": where_str,
221225
}
222226
),
223227
)

chromadb/api/rust.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ def count_collections(
180180
@override
181181
def list_collections(
182182
self,
183+
where: Optional[Where] = None,
183184
limit: Optional[int] = None,
184185
offset: Optional[int] = None,
185186
tenant: str = DEFAULT_TENANT,
186187
database: str = DEFAULT_DATABASE,
187188
) -> Sequence[CollectionModel]:
188-
collections = self.bindings.list_collections(limit, offset, tenant, database)
189+
collections = self.bindings.list_collections(
190+
limit, offset, tenant, database, json.dumps(where) if where else None
191+
)
189192
return [
190193
CollectionModel(
191194
id=collection.id,

chromadb/api/segment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def get_collection(
325325
@rate_limit
326326
def list_collections(
327327
self,
328+
where: Optional[Where] = None,
328329
limit: Optional[int] = None,
329330
offset: Optional[int] = None,
330331
tenant: str = DEFAULT_TENANT,

chromadb/chromadb_rust_bindings.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class Bindings:
8686
) -> int: ...
8787
def list_collections(
8888
self,
89+
where: Optional[str] = None,
8990
limit: Optional[int] = None,
9091
offset: Optional[int] = None,
9192
tenant: str = DEFAULT_TENANT,

chromadb/db/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def get_collection(self, name: str) -> Sequence: # type: ignore
2929

3030
@abstractmethod
3131
def list_collections(
32-
self, limit: Optional[int] = None, offset: Optional[int] = None
32+
self,
33+
where: Optional[Where] = None,
34+
limit: Optional[int] = None,
35+
offset: Optional[int] = None,
3336
) -> Sequence: # type: ignore
3437
pass
3538

0 commit comments

Comments
 (0)