-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix(backends): wrap bare-str OneOrMany inputs before embedding #1707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,126 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """EmbeddingCollection OneOrMany handling (PR #1706 review). | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| A bare ``str`` passed as ``documents``/``query_texts`` (ChromaDB's OneOrMany | ||||||||||||||||||||||||||||||||||||||||||||||||||
| shape) must be wrapped, not iterated — otherwise ``list("abc")`` embeds per | ||||||||||||||||||||||||||||||||||||||||||||||||||
| character and breaks length alignment with ids/metadatas on explicit-vector | ||||||||||||||||||||||||||||||||||||||||||||||||||
| backends. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| from mempalace.backends import embedding_wrapper as ew | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| class _FakeInner: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Captures what the wrapper delegates to the backend.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.calls = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def add(self, *, documents, ids, metadatas=None, embeddings=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.calls["add"] = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "documents": documents, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ids": ids, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "metadatas": metadatas, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "embeddings": embeddings, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def upsert(self, *, documents, ids, metadatas=None, embeddings=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.calls["upsert"] = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "documents": documents, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ids": ids, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "metadatas": metadatas, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "embeddings": embeddings, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def update(self, *, ids, documents=None, metadatas=None, embeddings=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.calls["update"] = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "documents": documents, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "ids": ids, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "metadatas": metadatas, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| "embeddings": embeddings, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def query(self, *, query_texts=None, query_embeddings=None, **_kw): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| self.calls["query"] = {"query_texts": query_texts, "query_embeddings": query_embeddings} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from mempalace.backends.base import QueryResult | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return QueryResult.empty() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def _patch_embed(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """Stub the embedder: one vector per input text, recording the inputs.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def fake(texts): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen["texts"] = texts | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return [[0.0, 0.0] for _ in texts] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| monkeypatch.setattr(ew, "_embed_texts", fake) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return seen | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_as_list_wraps_bare_string(): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ew._as_list("hello world") == ["hello world"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ew._as_list({"k": 1}) == [{"k": 1}] # bare dict wrapped, not -> ["k"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| src = ["a", "b"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ew._as_list(src) is src # list returned as-is (no copy) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert ew._as_list(("a", "b")) == ["a", "b"] # other iterables materialized | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_add_wraps_bare_string_document(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).add(documents="hello world", ids=["d1"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # embedded as one whole document, not per character | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert seen["texts"] == ["hello world"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # and the backend receives a list, length-aligned with ids | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert inner.calls["add"]["documents"] == ["hello world"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(inner.calls["add"]["embeddings"]) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_upsert_wraps_bare_string_document(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).upsert(documents="solo", ids=["d1"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert seen["texts"] == ["solo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert inner.calls["upsert"]["documents"] == ["solo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(inner.calls["upsert"]["embeddings"]) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_update_wraps_bare_string_document(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).update(ids=["d1"], documents="changed") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert seen["texts"] == ["changed"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert inner.calls["update"]["documents"] == ["changed"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(inner.calls["update"]["embeddings"]) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_query_wraps_bare_string(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).query(query_texts="find me") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert seen["texts"] == ["find me"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # query_texts is consumed into a single query embedding | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(inner.calls["query"]["query_embeddings"]) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert inner.calls["query"]["query_texts"] is None | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_list_inputs_unaffected(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| seen = _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).add(documents=["one", "two"], ids=["a", "b"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert seen["texts"] == ["one", "two"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(inner.calls["add"]["embeddings"]) == 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+108
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a unit test to verify that bare string
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_add_wraps_bare_string_ids_and_dict_metadatas(monkeypatch): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| _patch_embed(monkeypatch) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| inner = _FakeInner() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # a single id (str) and a single metadata (dict) are OneOrMany shapes too | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ew.EmbeddingCollection(inner).add(documents="solo", ids="d1", metadatas={"src": "web"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| call = inner.calls["add"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert call["ids"] == ["d1"] # not ['d', '1'] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert call["metadatas"] == [{"src": "web"}] # not ['src'] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| # documents / embeddings / ids / metadatas all length-aligned at 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert call["documents"] == ["solo"] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| assert len(call["embeddings"]) == 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently,
_as_listonly handlesstrby wrapping it in a list. However, ChromaDB'sOneOrManyshape also allows a single dictionary formetadatas(e.g.,{"source": "web"}). If a single dictionary is passed, callinglist(value)on it will extract only its keys (e.g.,["source"]), which leads to data loss and schema mismatch.By extending
_as_listto also check fordict, we can safely wrap single metadata dictionaries as well.