feat(ollama): add dimensions parameter to OllamaDocumentEmbedder and OllamaTextEmbedder#3322
Conversation
|
The chroma test failures are caused by an extra commit (63e66be) The ollama test failures are integration tests that require My actual changes only touch the ollama embedder files and their tests. |
5a3b834 to
d50a95f
Compare
d50a95f to
4b0367b
Compare
bogdankostic
left a comment
There was a problem hiding this comment.
Hi @ShubhamGond105, thank your for this PR! The linter check is currently failing, please make sure to run the linter as described in our contributing guidelines.
Coverage report (ollama)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||
|
Hi @bogdankostic , I have fixed the lint errors — all imports have been moved to the top level as required. All checks are now passing. Please let me know if there is anything else to address. Thank you! |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for fixing the lint errors! The PR is almost good to go, I just added a couple of minor comments.
| The desired number of dimensions in the embedding output. Only supported by models | ||
| that implement Matryoshka Representation Learning (MRL), such as nomic-embed-text-v1.5, | ||
| mxbai-embed-large, and qwen3-embedding. If None (default), the full vector is returned. | ||
| Requires ollama-python >= 0.6.2. |
There was a problem hiding this comment.
Let's remove this line from the docstring and instead bump the version of ollama-python in pyproject.toml
| Computes the embeddings of a string using embedding models compatible with the Ollama Library. | ||
|
|
||
| It uses embedding models compatible with the Ollama Library. | ||
|
|
||
| Usage example: | ||
| Usage example: | ||
| ```python | ||
| from haystack_integrations.components.embedders.ollama import OllamaTextEmbedder | ||
| from haystack_integrations.components.embedders.ollama import OllamaTextEmbedder | ||
|
|
||
| embedder = OllamaTextEmbedder() | ||
| result = embedder.run(text="What do llamas say once you have thanked them? No probllama!") | ||
| print(result['embedding']) | ||
| embedder = OllamaTextEmbedder() | ||
| result = embedder.run(text="What do llamas say once you have thanked them? No probllama!") | ||
| print(result['embedding']) |
There was a problem hiding this comment.
Let's revert the extra indent introduced here.
| The desired number of dimensions in the embedding output. Only supported by models | ||
| that implement Matryoshka Representation Learning (MRL), such as nomic-embed-text-v1.5, | ||
| mxbai-embed-large, and qwen3-embedding. If None (default), the full vector is returned. | ||
| Requires ollama-python >= 0.6.2. |
There was a problem hiding this comment.
Let's remove this line from the docstring and instead bump the version of ollama-python in pyproject.toml
| assert embedder.dimensions == 512 | ||
|
|
||
| def test_dimensions_passed_to_embed_client(self): | ||
|
|
There was a problem hiding this comment.
Let's remove these empty lines in tests between the test name and test code.
| mock_response = {"embeddings": [[0.1, 0.2, 0.3]]} | ||
| embedder._async_client.embed = AsyncMock(return_value=mock_response) | ||
|
|
||
| asyncio.run(embedder._embed_batch_async(["hello"], batch_size=32)) |
There was a problem hiding this comment.
For async tests, let's use the @pytest.mark.async decorator, change def to async def and call async methods using await.
… version note, bump ollama version
|
Hi @bogdankostic, I have addressed all the feedback points:
All checks are now passing. Please take a look when you get a chance! Thank you! |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for addressing the feedback @ShubhamGond105! :)
Related Issues
dimensionsparameter toOllamaDocumentEmbedderandOllamaTextEmbedder#3295Proposed Changes:
Added
dimensions: int | None = Noneparameter to bothOllamaDocumentEmbedderand
OllamaTextEmbedder.The Ollama SDK (>= 0.6.2) supports a
dimensionsparameter onclient.embed()that enables server-side embedding truncation via Matryoshka Representation Learning (MRL).
This parameter is a top-level argument of the request payload and cannot be passed
via
generation_kwargs/options.With this change, users can now do:
instead of manually truncating and re-normalizing vectors client-side.
How did you test it?
OllamaDocumentEmbedderandOllamaTextEmbeddercovering:dimensionsdefaults toNonedimensionsis stored on the instancedimensionsis forwarded to the sync clientdimensionsis forwarded to the async clientdimensionssurvives serialization viadefault_to_dict/default_from_dictNotes for the reviewer
dimensions=Nonepreserves existing behaviour — fully backward compatiblerun) and async (run_async) paths updated in both embedderstext_embedder.pyto use the newerclient.embed()APIinstead of the deprecated
client.embeddings()for consistencyChecklist
feat: