langchain.embeddings.huggingface.HuggingFaceEmbeddings

class langchain.embeddings.huggingface.HuggingFaceEmbeddings(*, client: Any = None, model_name: str = 'sentence-transformers/all-mpnet-base-v2', cache_folder: Optional[str] = None, model_kwargs: Dict[str, Any] = None, encode_kwargs: Dict[str, Any] = None)[source]

Bases: BaseModel, Embeddings

HuggingFace sentence_transformers embedding models.

To use, you should have the sentence_transformers python package installed.

Example

from langchain.embeddings import HuggingFaceEmbeddings

model_name = "sentence-transformers/all-mpnet-base-v2"
model_kwargs = {'device': 'cpu'}
encode_kwargs = {'normalize_embeddings': False}
hf = HuggingFaceEmbeddings(
    model_name=model_name,
    model_kwargs=model_kwargs,
    encode_kwargs=encode_kwargs
)

Initialize the sentence_transformer.

param cache_folder: Optional[str] = None

Path to store models. Can be also set by SENTENCE_TRANSFORMERS_HOME environment variable.

param encode_kwargs: Dict[str, Any] [Optional]

Key word arguments to pass when calling the encode method of the model.

param model_kwargs: Dict[str, Any] [Optional]

Key word arguments to pass to the model.

param model_name: str = 'sentence-transformers/all-mpnet-base-v2'

Model name to use.

embed_documents(texts: List[str]) List[List[float]][source]

Compute doc embeddings using a HuggingFace transformer model.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

embed_query(text: str) List[float][source]

Compute query embeddings using a HuggingFace transformer model.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

model Config[source]

Bases: object

Configuration for this pydantic object.

extra = 'forbid'

Examples using HuggingFaceEmbeddings