-
Notifications
You must be signed in to change notification settings - Fork 194
new: add gemmaembedding-300m #592
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
+132
−2
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from typing import Any, Iterable, Type | ||
|
|
||
|
|
||
| from fastembed.common.types import NumpyArray | ||
| from fastembed.common.onnx_model import OnnxOutputContext | ||
| from fastembed.text.onnx_embedding import OnnxTextEmbedding, OnnxTextEmbeddingWorker | ||
| from fastembed.common.model_description import DenseModelDescription, ModelSource | ||
|
|
||
|
|
||
| supported_builtin_sentence_embedding_models: list[DenseModelDescription] = [ | ||
| DenseModelDescription( | ||
| model="google/embeddinggemma-300m", | ||
| dim=768, | ||
| description=( | ||
| "Text embeddings, Unimodal (text), multilingual, 2048 input tokens truncation, " | ||
| "Prefixes for queries/documents: `task: search result | query: {content}` for query, " | ||
| "`title: {title | 'none'} | text: {content}` for documents, 2025 year." | ||
| ), | ||
| license="apache-2.0", | ||
| size_in_GB=1.24, | ||
| sources=ModelSource( | ||
| hf="onnx-community/embeddinggemma-300m-ONNX", | ||
| ), | ||
| model_file="onnx/model.onnx", | ||
| additional_files=["onnx/model.onnx_data"], | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| class BuiltinSentenceEmbedding(OnnxTextEmbedding): | ||
| """Builtin Sentence Embedding uses built-in pooling and normalization of underlying onnx models""" | ||
|
|
||
| @classmethod | ||
| def _get_worker_class(cls) -> Type[OnnxTextEmbeddingWorker]: | ||
| return BuiltinSentenceEmbeddingWorker | ||
|
|
||
| @classmethod | ||
| def _list_supported_models(cls) -> list[DenseModelDescription]: | ||
| """Lists the supported models. | ||
|
|
||
| Returns: | ||
| list[DenseModelDescription]: A list of DenseModelDescription objects containing the model information. | ||
| """ | ||
| return supported_builtin_sentence_embedding_models | ||
|
|
||
| def _post_process_onnx_output( | ||
| self, output: OnnxOutputContext, **kwargs: Any | ||
| ) -> Iterable[NumpyArray]: | ||
| return output.model_output | ||
|
|
||
| def _run_model( | ||
| self, onnx_input: dict[str, Any], onnx_output_names: list[str] | None = None | ||
| ) -> NumpyArray: | ||
| return self.model.run(onnx_output_names, onnx_input)[1] # type: ignore[union-attr] | ||
|
|
||
|
|
||
| class BuiltinSentenceEmbeddingWorker(OnnxTextEmbeddingWorker): | ||
| def init_embedding( | ||
| self, | ||
| model_name: str, | ||
| cache_dir: str, | ||
| **kwargs: Any, | ||
| ) -> OnnxTextEmbedding: | ||
| return BuiltinSentenceEmbedding( | ||
| model_name=model_name, | ||
| cache_dir=cache_dir, | ||
| threads=1, | ||
| **kwargs, | ||
| ) | ||
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
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
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.
Hardcoded output index
[1]is brittle and lacks validation.The method assumes the ONNX model always has at least two outputs with
sentence_embeddingat index 1. If the model's output structure changes or differs across versions, this will raise anIndexErrorwithout a clear diagnostic message.Consider adding validation or a more descriptive error:
🛡️ Proposed fix with validation
def _run_model( self, onnx_input: dict[str, Any], onnx_output_names: list[str] | None = None ) -> NumpyArray: - return self.model.run(onnx_output_names, onnx_input)[1] # type: ignore[union-attr] + result = self.model.run(onnx_output_names, onnx_input) # type: ignore[union-attr] + if len(result) < 2: + raise ValueError( + f"Expected at least 2 ONNX outputs (last_hidden_state, sentence_embedding), " + f"but got {len(result)}. Ensure the model has built-in pooling." + ) + return result[1]🤖 Prompt for AI Agents