langchain.embeddings.huggingface_hub.HuggingFaceHubEmbeddings¶

class langchain.embeddings.huggingface_hub.HuggingFaceHubEmbeddings(*, client: Any = None, repo_id: str = 'sentence-transformers/all-mpnet-base-v2', task: Optional[str] = 'feature-extraction', model_kwargs: Optional[dict] = None, huggingfacehub_api_token: Optional[str] = None)[source]¶

Bases: BaseModel, Embeddings

HuggingFaceHub embedding models.

To use, you should have the huggingface_hub python package installed, and the environment variable HUGGINGFACEHUB_API_TOKEN set with your API token, or pass it as a named parameter to the constructor.

Example

from langchain.embeddings import HuggingFaceHubEmbeddings
repo_id = "sentence-transformers/all-mpnet-base-v2"
hf = HuggingFaceHubEmbeddings(
    repo_id=repo_id,
    task="feature-extraction",
    huggingfacehub_api_token="my-api-key",
)

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param huggingfacehub_api_token: Optional[str] = None¶
param model_kwargs: Optional[dict] = None¶

Key word arguments to pass to the model.

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

Model name to use.

param task: Optional[str] = 'feature-extraction'¶

Task to call the model with.

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

Call out to HuggingFaceHub’s embedding endpoint for embedding search docs.

Parameters

texts – The list of texts to embed.

Returns

List of embeddings, one for each text.

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

Call out to HuggingFaceHub’s embedding endpoint for embedding query text.

Parameters

text – The text to embed.

Returns

Embeddings for the text.

validator validate_environment  »  all fields[source]¶

Validate that api key and python package exists in environment.

model Config[source]¶

Bases: object

Configuration for this pydantic object.

extra = 'forbid'¶

Examples using HuggingFaceHubEmbeddings¶