misc

sentence_transformers.util.misc.disable_datasets_caching()[source]

A context manager that will disable caching in the datasets library.

sentence_transformers.util.misc.disable_logging(highest_level=50)[source]

A context manager that will prevent any logging messages triggered during the body from being processed.

Parameters:

highest_level – the maximum logging level allowed.

sentence_transformers.util.misc.fullname(obj) str[source]

Gives a full name (package_name.class_name) for a class / object in Python. Will be used to load the correct classes from JSON files

Parameters:

obj – The object for which to get the full name, e.g. an instance of a class or the class itself.

Returns:

The full name of the object.

Return type:

str

Example

>>> from sentence_transformers.sentence_transformer.losses import MultipleNegativesRankingLoss
>>> from sentence_transformers import SentenceTransformer
>>> from sentence_transformers.util import fullname
>>> model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
>>> loss = MultipleNegativesRankingLoss(model)
>>> fullname(loss)
'sentence_transformers.sentence_transformer.losses.multiple_negatives_ranking.MultipleNegativesRankingLoss'
sentence_transformers.util.misc.import_from_string(dotted_path: str) type[source]

Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import failed.

Parameters:

dotted_path (str) – The dotted module path.

Returns:

The attribute/class designated by the last name in the path.

Return type:

Any

Raises:

ImportError – If the import failed.

Example

>>> import_from_string('sentence_transformers.sentence_transformer.losses.multiple_negatives_ranking.MultipleNegativesRankingLoss')
<class 'sentence_transformers.sentence_transformer.losses.multiple_negatives_ranking.MultipleNegativesRankingLoss'>
sentence_transformers.util.misc.import_module_class(class_ref: str, model_name_or_path: str | None = None, *, trust_remote_code: bool = False, revision: str | None = None, code_revision: str | None = None, token: bool | str | None = None, cache_folder: str | None = None, local_files_only: bool = False) type[source]

Resolve a module class reference to a class object.

For class refs in the sentence_transformers.* namespace, this imports directly via import_from_string(). For other class refs (e.g. repository-local custom classes like modeling_my_model.CustomTransformer), it first tries transformers.dynamic_module_utils.get_class_from_dynamic_module() to fetch the modeling file from the model directory, then falls back to import_from_string() if dynamic loading is not applicable or fails.

Dynamic loading is attempted when trust_remote_code is set, or when model_name_or_path resolves to a local directory (i.e. the user already has the file on disk and is implicitly trusted).

Parameters:
  • class_ref – Dotted class path. Either a fully-qualified sentence_transformers.* path or a repository-local reference like modeling_<name>.<ClassName>.

  • model_name_or_path – Hub repo id or local directory used to source repository-local modeling files. Required for dynamic loading.

  • trust_remote_code – Whether to permit dynamic loading from an unverified Hub repo.

  • revision – Hub revision to fetch the modeling file from.

  • code_revision – Optional separate revision pinning for the modeling code (overrides revision when set).

  • token – Hugging Face Hub authentication token. Required for fetching modeling files from private repositories.

  • cache_folder – Optional override for the Hugging Face Hub cache directory.

  • local_files_only – If True, only use cached files and never reach out to the Hub.

Returns:

The resolved class.