ChromaDBDataset¶
ChromaDBDataset loads and saves data to ChromaDB collections.
kedro_datasets_experimental.chromadb.ChromaDBDataset ¶
ChromaDBDataset(
*,
collection_name,
client_type="ephemeral",
client_settings=None,
load_args=None,
save_args=None,
metadata=None
)
Bases: AbstractDataset[dict[str, Any], dict[str, Any]]
ChromaDBDataset loads and saves data from/to ChromaDB collections.
ChromaDB is a vector database for building AI applications. This dataset allows you to interact with ChromaDB collections for storing and retrieving documents with embeddings.
Examples:
Using the YAML API:
my_collection:
type: chromadb.ChromaDBDataset
collection_name: "documents"
client_type: "persistent"
client_settings:
path: "./chroma_db"
Using the Python API:
>>> from kedro_datasets_experimental.chromadb import ChromaDBDataset
>>>
>>> # Save data to ChromaDB
>>> data = {
... "documents": ["This is a document", "This is another document"],
... "metadatas": [{"type": "text"}, {"type": "text"}],
... "ids": ["doc1", "doc2"]
... }
>>> dataset = ChromaDBDataset(collection_name="test_collection")
>>> dataset.save(data)
>>>
>>> # Load data from ChromaDB
>>> loaded_data = dataset.load()
>>> print(loaded_data["documents"]) # ['This is a document', 'This is another document']
>>>
>>> # Query for similar vectors (efficient for large datasets)
>>> query_dataset = ChromaDBDataset(
... collection_name="documents",
... load_args={
... "query_texts": ["machine learning"],
... "n_results": 5,
... "include": ["documents", "metadatas", "distances"]
... }
... )
>>> results = query_dataset.load() # Returns top-5 similar documents
Parameters:
-
collection_name(str) –The name of the ChromaDB collection.
-
client_type(str, default:'ephemeral') –Type of ChromaDB client. Options: "ephemeral", "persistent", "http". Defaults to "ephemeral".
-
client_settings(dict[str, Any] | None, default:None) –Settings for the ChromaDB client. For "persistent", use {"path": "/path/to/db"}. For "http", use {"host": "localhost", "port": 8000}.
-
load_args(dict[str, Any] | None, default:None) –Additional arguments for loading data from ChromaDB collection. Can include "where", "where_document", "include", "n_results", etc. For vector similarity queries, use: - "query_embeddings": List of embeddings to query for similarity - "query_texts": List of texts to query for similarity - "n_results": Number of results to return (default: 10) - "where": Metadata filter conditions - "where_document": Document content filter conditions
-
save_args(dict[str, Any] | None, default:None) –Additional arguments for saving data to ChromaDB collection. Can include "embeddings" if you want to provide custom embeddings.
-
metadata(dict[str, Any] | None, default:None) –Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |
_create_client ¶
_create_client()
Create ChromaDB client based on configuration.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
_describe ¶
_describe()
Returns a dictionary describing the dataset configuration.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
155 156 157 158 159 160 161 162 163 | |
_get_client ¶
_get_client()
Get or create the ChromaDB client.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
126 127 128 129 130 | |
_get_collection ¶
_get_collection(create_if_missing=True)
Get or create the ChromaDB collection.
Parameters:
-
create_if_missing(bool, default:True) –If True, creates the collection if it doesn't exist. If False, returns None when collection is not found.
Returns:
-
Collection | None–Collection object if found/created, None if not found and create_if_missing=False.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
exists ¶
exists()
Checks if the collection exists and contains data.
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
load ¶
load()
Loads data from the ChromaDB collection.
Returns:
-
dict[str, Any]–A dictionary containing the collection data with keys: - "documents": List of document texts - "metadatas": List of metadata dictionaries - "ids": List of document IDs - "embeddings": List of embeddings (if included)
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |
save ¶
save(data)
Saves data to the ChromaDB collection.
Parameters:
-
data(dict[str, Any]) –A dictionary containing the data to save. Expected keys: - "documents": List of document texts (required) - "ids": List of document IDs (required) - "metadatas": List of metadata dictionaries (optional) - "embeddings": List of embeddings (optional, will be auto-generated if not provided)
Source code in kedro_datasets_experimental/chromadb/chromadb_dataset.py
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 | |