feat: add MockTextEmbedder and MockDocumentEmbedder#11709
Open
julian-risch wants to merge 6 commits into
Open
Conversation
…rototyping Add `MockTextEmbedder` and `MockDocumentEmbedder`, embedders that return deterministic embeddings without calling any API. They are zero-cost drop-in replacements for real embedders in tests, smoke tests, and quick prototypes, following the same design as `MockChatGenerator` (#11708) and inspired by model-layer fakes in other frameworks (LangChain `DeterministicFakeEmbedding`/`FakeEmbeddings`, LlamaIndex `MockEmbedding`). By default each embedding is derived from a stable SHA-256 hash of the (prepared) text, so the same text always yields the same unit-length embedding and different texts yield different embeddings. This makes the mocks usable in retrieval pipelines and reproducible across runs and processes. A fixed `embedding` vector or a custom `embedding_fn` callable can be provided instead. Both implement the full embedder interface: `run`, `run_async`, and serialization. The deterministic-embedding helpers are shared via `haystack/components/embedders/mock_utils.py`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
Reduce the number of test functions via parametrization (init validation, serialization roundtrips) and by testing the shared deterministic-embedding algorithm once instead of duplicating it across both embedder test files. Coverage is unchanged: both components stay at 100% and mock_utils reaches 100% (the zero-vector normalization guard is now pinned by a direct test). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Document the empty-`embedding` ValueError and the non-numeric-`embedding` TypeError that `_validate_embedding` raises during construction, matching the docstring-completeness pass done for MockChatGenerator. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Real embedders that load a local model (e.g. SentenceTransformers) expose warm_up(); add a no-op warm_up to MockTextEmbedder and MockDocumentEmbedder so they are drop-in replacements for those too and an explicit warm_up() call never fails. Matches MockChatGenerator, which already has warm_up. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Mirror MockChatGenerator's warm-up handling: initialize `_is_warmed_up` to False, set it True in warm_up(), and auto-warm in run() (run_async delegates to run, so it is covered too). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
warm_up() is an idempotent no-op, so guard with `if not self._is_warmed_up` is unnecessary; call it directly. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Related Issues
Proposed Changes:
Adds
MockTextEmbedderandMockDocumentEmbedder, embedders that produce embeddings without calling any API. Drop-in replacements for real embedders (e.g.OpenAITextEmbedderin tests and prototypes.Embedding selection modes (consistent across both components, and mirroring
MockChatGenerator's fixed /*_fn/ default trio):embeddingvector; returned/assigned for every input.embedding_fn=callable(text) -> list[float].Both components accept the parameters of real embedders (
prefix,suffix; plusmeta_fields_to_embed/embedding_separatorfor the document embedder). They report approximate tokenusageinmeta, consistent withMockChatGenerator. They implement the full embedder interface:run,run_async,to_dict/from_dict(includingembedding_fnnamed-callable serialization).How did you test it?
Added unit tests
Notes for the reviewer
Checklist
🤖 Generated with Claude Code