ChromaDBDataset¶
ChromaDBDataset loads a handle for adding, searching, and deleting entries in ChromaDB vector database collections.
kedro_datasets_experimental.chromadb.ChromaDBDataset ¶
ChromaDBDataset(
*,
collection_name,
client_type="ephemeral",
client_settings=None,
create_collection_if_missing=True,
metadata=None
)
Bases: AbstractVectorStoreDataset
Connect to a ChromaDB collection and return a ChromaVectorStoreHandle.
load() creates a Chroma client, resolves (or creates) the target
collection, and returns a handle. All read/write operations go through
the handle. save() is intentionally disabled and raises
DatasetError.
Three client types are supported, selected with client_type:
"ephemeral"(default) — an in-memory store. Note that ephemeral clients created within the same process share state, and the data is lost when the process exits."persistent"— an on-disk store; set the location withclient_settings: {path: ...}."http"— a running Chroma server; setclient_settings: {host: ..., port: ...}.
collection_name is a lookup key. When
create_collection_if_missing=True (the default) the collection is
created on first use with Chroma's defaults — including the default
embedding function (all-MiniLM-L6-v2,
downloaded on first use), which serves text search and vector-less
add() calls. To use a custom embedding function or collection
configuration, create the collection yourself first via the Chroma
client.
Examples:
Using the YAML API:
my_store:
type: kedro_datasets_experimental.chromadb.ChromaDBDataset
collection_name: documents
client_type: persistent
client_settings:
path: ./chroma_db
Using the Python API:
>>> from kedro_datasets_experimental.chromadb import ChromaDBDataset
>>>
>>> dataset = ChromaDBDataset(collection_name="documents")
>>> store = dataset.load()
>>> ids = store.add(
... [
... {
... "properties": {"document": "A document about ML", "topic": "ml"},
... "vector": [0.1, 0.2, 0.3],
... }
... ]
... )
>>> hits = store.search(vector=[0.1, 0.2, 0.3], top_k=5)
>>> store.delete(ids=ids)
Parameters:
-
collection_name(str) –Name of the ChromaDB collection to connect to.
-
client_type(Literal['ephemeral', 'persistent', 'http'], default:'ephemeral') –Type of ChromaDB client —
"ephemeral","persistent", or"http". Defaults to"ephemeral". -
client_settings(dict[str, Any] | None, default:None) –Keyword arguments for the client. For
"persistent":{"path": "/path/to/db"}(default path"./chroma_db"). For"http":{"host": ..., "port": ...}(defaults"localhost"and8000). Extra keys are forwarded to the underlyingchromadbclient constructor. -
create_collection_if_missing(bool, default:True) –When
True(default), the collection is created — with Chroma's default configuration and embedding function — if it does not already exist. WhenFalse,load()raises if the collection is absent. -
metadata(dict[str, Any] | None, default:None) –Arbitrary metadata passed through by Kedro; ignored by this dataset.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
_create_collection_if_missing
instance-attribute
¶
_create_collection_if_missing = create_collection_if_missing
_create_client ¶
_create_client()
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
_describe ¶
_describe()
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
407 408 409 410 411 412 413 414 | |
_load ¶
_load()
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
kedro_datasets_experimental.chromadb.ChromaVectorStoreHandle ¶
ChromaVectorStoreHandle(
client, collection, close_client=True
)
Bases: VectorStoreHandle
Handle for interacting with a ChromaDB collection.
Returned by ChromaDBDataset.load(). All reads and writes go through
the handle; the dataset only configures the connection.
Records passed to add() are dicts of the form
{"properties": dict, "vector": list[float], "id": str}. The reserved
"document" key inside properties maps to Chroma's documents field;
all other keys are stored as Chroma metadata. search() merges them
back into a single "properties" dict. When records carry no
"vector", Chroma embeds each record's "document" with the
collection's embedding function (by default
all-MiniLM-L6-v2,
downloaded on first use).
close() releases the client's resources (SQLite and HNSW file handles
for persistent clients, the connection pool for HTTP clients); a closed
handle must not be reused. For ephemeral clients close() is a no-op,
since their in-memory store is shared by every ephemeral client in the
process. Kedro never closes the handle for you — close it explicitly or
use it as a context manager::
with catalog.load("my_store") as store:
store.add([{"properties": {"document": "hello"}, "vector": [0.1, 0.2]}])
hits = store.search(vector=[0.1, 0.2], top_k=5)
raw_client exposes the underlying chromadb client for operations
outside this interface (e.g. where_document filters or upsert).
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
51 52 53 54 55 56 57 58 59 60 | |
add ¶
add(records)
Insert records into the collection and return their IDs.
See the class docstring for the record format. Either all records
in the batch carry a "vector" or none do. A UUID is generated for
any record without an "id". Records whose ID already exists in
the collection are rejected (Chroma would otherwise skip them
silently); to update existing records, use raw_client with
collection.upsert().
Parameters:
-
records(list[dict[str, Any]]) –Records to insert.
Returns:
-
list[str]–List of ID strings for the inserted records, in input order.
Raises:
-
DatasetError–If some records have a
"vector"and others do not, if a record's ID already exists in the collection, or if the insert call to Chroma fails.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |
close ¶
close()
Release the client's resources. Idempotent; a no-op for ephemeral clients.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
67 68 69 70 71 72 | |
delete ¶
delete(*, ids=None, filters=None)
Delete records from the collection by ID or metadata filter.
Exactly one of ids or filters must be provided.
Parameters:
-
ids(list[str] | None, default:None) –List of ID strings to delete.
-
filters(Any, default:None) –A Chroma
wherefilter dict (backend-native, MongoDB-style — e.g.{"topic": "ml"}or{"$and": [...]}) selecting the records to delete. Forwhere_documentfull-text filters, useraw_client.
Raises:
-
DatasetError–If neither or both arguments are supplied, or if the deletion call to Chroma fails.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |
describe ¶
describe()
Return collection name and current record count.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
74 75 76 77 78 79 | |
search ¶
search(*, vector=None, text=None, top_k=10, filters=None)
Search the collection by vector or text and return the top matches.
Exactly one of vector or text must be provided. vector
queries with the embedding directly; text is embedded first with
the collection's embedding function (downloading the default model
on first use if the collection has no custom one).
Parameters:
-
vector(list[float] | None, default:None) –Query embedding for similarity search.
-
text(str | None, default:None) –Query string, embedded with the collection's embedding function.
-
top_k(int, default:10) –Maximum number of results to return. Defaults to 10.
-
filters(Any, default:None) –A Chroma
wherefilter dict (backend-native, MongoDB-style) restricting the search scope. Forwhere_documentfull-text filters, useraw_client.
Returns:
-
list[dict[str, Any]]–List of result dicts, each containing
"id"(str), -
list[dict[str, Any]]–"properties"(dict of stored metadata, plus the -
list[dict[str, Any]]–"document"key when the record has one), and"distance" -
list[dict[str, Any]]–(float).
Raises:
-
DatasetError–If neither or both of
vector/textare supplied, or if the query call to Chroma fails.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |