Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions chromadol/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,22 @@ class ChromaDocuments(ChromaCollection):
@appendable(item2kv=uuid_key)
@ValueCodecs.single_nested_value("uris")
class ChromaUris(ChromaCollection):
"""ChromaCollection but reading and writing only the 'uris' field."""
"""ChromaCollection but reading and writing only the 'uris' field.

Writing uris needs a collection created with a ``data_loader`` (see
``chromadol.data_loaders``); ``chromadb`` refuses uris without one.
"""

@appendable(item2kv=uuid_key)
@ValueCodecs.single_nested_value("metadata")
class ChromaUris(ChromaCollection):
"""ChromaCollection but reading and writing only the 'uris' field."""

@ValueCodecs.single_nested_value("metadatas")
class ChromaMetadatas(ChromaCollection):
"""ChromaCollection but reading and writing only the 'metadatas' field.

Mainly a read view: ``chromadb`` refuses an ``upsert`` that carries neither
documents nor images, so writing metadata alone raises (that is also why this
one has no ``append``). Write metadata with the documents, through
``ChromaCollection`` or ``AppendableChromaCollection``.
"""


class ChromaClient(MutableMapping):
Expand Down
33 changes: 33 additions & 0 deletions chromadol/tests/test_appendable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
AppendableChromaCollection,
ChromaCollection,
ChromaDocuments,
ChromaMetadatas,
ChromaUris,
)

Expand Down Expand Up @@ -100,3 +101,35 @@ def test_appendable_chroma_collection_appends_raw_chromadb_kwargs(tmp_path):
record = raw[key]
assert record["documents"] == ["raw document"]
assert record["metadatas"] == [{"author": "me"}]


@pytest.mark.parametrize(
"store_cls, field",
[
(ChromaDocuments, "documents"),
(ChromaUris, "uris"),
(ChromaMetadatas, "metadatas"),
],
ids=lambda x: getattr(x, "__name__", x),
)
def test_single_field_stores_write_their_own_chromadb_field(store_cls, field):
"""Each single-field store must write the ``chromadb`` kwarg it is named for.

``ChromaUris`` used to be silently redefined by a second class (field
``"metadata"``, not even a ``chromadb`` kwarg), so every write raised
``TypeError: upsert() got an unexpected keyword argument 'metadata'``.
"""
collection = _RecordingCollection()
store_cls(collection)["a_key"] = "a value"
((_ids, kwargs),) = collection.upserts
assert list(kwargs) == [field]


def test_chroma_metadatas_reads_the_metadatas_field(tmp_path):
client = chromadb.PersistentClient(str(tmp_path / "metas"))
collection = client.create_collection("metas", get_or_create=True)
ChromaCollection(collection)["k"] = {
"documents": "doc",
"metadatas": {"author": "me"},
}
assert ChromaMetadatas(collection)["k"] == [{"author": "me"}]
Loading