Elffuss BraidHub

kikocis/lancedb public

feat(python): add support for trust_remote_code in hf embeddings (#1712)

Resovles #1709. Adds `trust_remote_code` as a parameter to the
`TransformersEmbeddingFunction` class with a default of False. Updated
relevant documentation with the same.
Changeset9434ba17655a703767fafc859388ab8ff12b85411244a648ac0508f9be3ca1d8
AuthorAkash Saravanan <akashsara@outlook.com> · 2024-09-30 13:36 -06:00
CommitterGitHub <noreply@github.com> · 2024-10-01 01:06 +05:30
Parentse241c31f237f

Browse the files at this changeset

2 files changed

modified docs/src/embeddings/available_embedding_models/text_embedding_functions/huggingface_embedding.md +1 −1
@@ -1,5 +1,5 @@
11 # Huggingface embedding models
2-We offer support for all huggingface models (which can be loaded via [transformers](https://huggingface.co/docs/transformers/en/index) library). The default model is `colbert-ir/colbertv2.0` which also has its own special callout - `registry.get("colbert")`
2+We offer support for all Hugging Face models (which can be loaded via [transformers](https://huggingface.co/docs/transformers/en/index) library). The default model is `colbert-ir/colbertv2.0` which also has its own special callout - `registry.get("colbert")`. Some Hugging Face models might require custom models defined on the HuggingFace Hub in their own modeling files. You may enable this by setting `trust_remote_code=True`. This option should only be set to True for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine.
33
44 Example usage -
55 ```python
modified python/python/lancedb/embeddings/transformers.py +9 −1
@@ -40,6 +40,11 @@
4040 The device to use for the model. Default is "cpu".
4141 show_progress_bar : bool
4242 Whether to show a progress bar when loading the model. Default is True.
43+ trust_remote_code : bool
44+ Whether or not to allow for custom models defined on the HuggingFace
45+ Hub in their own modeling files. This option should only be set to True
46+ for repositories you trust and in which you have read the code, as it
47+ will execute code present on the Hub on your local machine.
4348
4449 to download package, run :
4550 `pip install transformers`
@@ -49,6 +54,7 @@
4954
5055 name: str = "colbert-ir/colbertv2.0"
5156 device: str = "cpu"
57+ trust_remote_code: bool = False
5258 _tokenizer: Any = PrivateAttr()
5359 _model: Any = PrivateAttr()
5460
@@ -57,7 +63,9 @@
5763 self._ndims = None
5864 transformers = attempt_import_or_raise("transformers")
5965 self._tokenizer = transformers.AutoTokenizer.from_pretrained(self.name)
60- self._model = transformers.AutoModel.from_pretrained(self.name)
66+ self._model = transformers.AutoModel.from_pretrained(
67+ self.name, trust_remote_code=self.trust_remote_code
68+ )
6169 self._model.to(self.device)
6270
6371 if PYDANTIC_VERSION.major < 2: # Pydantic 1.x compat