laife.embed.sentence_transformer_embeddings
¶
Porting of HuggingFace sentence-transformers wrapper.
To avoid dependencies issues with ollama.
Original file: https://github.com/langchain-ai/langchain/blob/master/libs/partners/huggingface/langchain_huggingface/embeddings/huggingface.py
Classes:
-
SentenceTransformersEmbeddings–HuggingFace sentence_transformers embedding models.
SentenceTransformersEmbeddings
¶
Bases: BaseModel, Embeddings
HuggingFace sentence_transformers embedding models.
To use, you should have the sentence_transformers python package installed.
Example
.. code-block:: python
from langchain_huggingface import SentenceTransformersEmbeddings
model_name = "sentence-transformers/all-mpnet-base-v2"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
hf = SentenceTransformersEmbeddings(
model_name=model_name,
model_kwargs=model_kwargs,
encode_kwargs=encode_kwargs
)
Initialize the sentence_transformer.
Methods:
-
embed_documents–Compute doc embeddings using a HuggingFace transformer model.
-
embed_query–Compute query embeddings using a HuggingFace transformer model.
Attributes:
-
cache_folder(str | None) –Path to store models.
-
encode_kwargs(dict[str, Any]) –Keyword arguments to pass when calling the
encodemethod of the Sentence -
model_kwargs(dict[str, Any]) –Keyword arguments to pass to the Sentence Transformer model, such as
device, -
model_name(str) –Model name to use.
-
multi_process(bool) –Run encode() on multiple GPUs.
-
show_progress(bool) –Whether to show a progress bar.
Source code in src/laife/embed/sentence_transformer_embeddings.py
cache_folder
class-attribute
instance-attribute
¶
Path to store models. Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.
encode_kwargs
class-attribute
instance-attribute
¶
Keyword arguments to pass when calling the encode method of the Sentence
Transformer model, such as prompt_name, prompt, batch_size, precision,
normalize_embeddings, and more.
See also the Sentence Transformer documentation: https://sbert.net/docs/package_reference/SentenceTransformer.html#sentence_transformers.SentenceTransformer.encode
model_kwargs
class-attribute
instance-attribute
¶
Keyword arguments to pass to the Sentence Transformer model, such as device,
prompts, default_prompt_name, revision, trust_remote_code, or token.
See also the Sentence Transformer documentation: https://sbert.net/docs/package_reference/SentenceTransformer.html#sentence_transformers.SentenceTransformer
model_name
class-attribute
instance-attribute
¶
Model name to use.
multi_process
class-attribute
instance-attribute
¶
Run encode() on multiple GPUs.
show_progress
class-attribute
instance-attribute
¶
Whether to show a progress bar.
embed_documents
¶
Compute doc embeddings using a HuggingFace transformer model.
Parameters:
Returns: