feat: Allow custom model and params with huggingface_base_url #3574
+35
−1
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.
Description
This change addresses an issue where using huggingface_base_url would incorrectly restrict the embedding model to a hardcoded "tei" value and prevent the use of custom model parameters.
With this fix, users can now specify any model name and pass additional parameters (e.g., truncate=True) when connecting to a custom Hugging Face Text Embeddings Inference (TEI) endpoint or any other OpenAI-compatible API. This provides greater flexibility for users hosting their own embedding models.
Fixes #3553
Type of change
Please delete options that are not relevant.
HuggingFace embedding configuration and usage
"tei"if not specified, and otherwise uses the provided model name. (mem0/embeddings/huggingface.py)model_kwargs, allowing for greater customization. (mem0/embeddings/huggingface.py)Testing improvements
test_embed_with_huggingface_base_url, to verify that custom HuggingFace endpoints, models, and model parameters are correctly handled and passed to the embedding client. (tests/embeddings/test_huggingface_embeddings.py)] Bug fix (non-breaking change which fixes an issue)How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Please delete options that are not relevant.
Checklist:
Maintainer Checklist
This pull request improves the flexibility and configurability of the HuggingFace embedding integration by allowing custom model names and model keyword arguments to be passed through when using a custom HuggingFace endpoint. It also adds a new test to ensure this functionality works as expected.
Enhancements to HuggingFace embedding configuration:
HuggingFaceEmbeddingclass now sets the model name to"tei"by default only if a custom HuggingFace base URL is provided and no model is explicitly specified, allowing users to override the model name as needed.embedmethod now uses the model name and additional keyword arguments from the config when calling the HuggingFace endpoint, making it possible to customize embedding requests (e.g., to set truncation or other model-specific options).Testing improvements:
test_embed_with_huggingface_base_url, to verify that the embedding logic correctly passes the custom model name and keyword arguments to the HuggingFace endpoint when a base URL is provided.