[docs]classHuggingFaceEndpointEmbeddings(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: .. code-block:: python from langchain_huggingface import HuggingFaceEndpointEmbeddings model = "sentence-transformers/all-mpnet-base-v2" hf = HuggingFaceEndpointEmbeddings( model=model, task="feature-extraction", huggingfacehub_api_token="my-api-key", ) """client:Any=None#: :meta private:async_client:Any=None#: :meta private:model:Optional[str]=None"""Model name to use."""repo_id:Optional[str]=None"""Huggingfacehub repository id, for backward compatibility."""task:Optional[str]="feature-extraction""""Task to call the model with."""model_kwargs:Optional[dict]=None"""Keyword arguments to pass to the model."""huggingfacehub_api_token:Optional[str]=Field(default_factory=from_env("HUGGINGFACEHUB_API_TOKEN",default=None))model_config=ConfigDict(extra="forbid",protected_namespaces=(),)@model_validator(mode="after")defvalidate_environment(self)->Self:"""Validate that api key and python package exists in environment."""huggingfacehub_api_token=self.huggingfacehub_api_tokenoros.getenv("HF_TOKEN")try:fromhuggingface_hubimport(# type: ignore[import]AsyncInferenceClient,InferenceClient,)ifself.model:self.repo_id=self.modelelifself.repo_id:self.model=self.repo_idelse:self.model=DEFAULT_MODELself.repo_id=DEFAULT_MODELclient=InferenceClient(model=self.model,token=huggingfacehub_api_token,)async_client=AsyncInferenceClient(model=self.model,token=huggingfacehub_api_token,)ifself.tasknotinVALID_TASKS:raiseValueError(f"Got invalid task {self.task}, "f"currently only {VALID_TASKS} are supported")self.client=clientself.async_client=async_clientexceptImportError:raiseImportError("Could not import huggingface_hub python package. ""Please install it with `pip install huggingface_hub`.")returnself
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:"""Call out to HuggingFaceHub's embedding endpoint for embedding search docs. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """# replace newlines, which can negatively affect performance.texts=[text.replace("\n"," ")fortextintexts]_model_kwargs=self.model_kwargsor{}# api doc: https://huggingface.github.io/text-embeddings-inference/#/Text%20Embeddings%20Inference/embedresponses=self.client.post(json={"inputs":texts,**_model_kwargs},task=self.task)returnjson.loads(responses.decode())
[docs]asyncdefaembed_documents(self,texts:List[str])->List[List[float]]:"""Async Call to HuggingFaceHub's embedding endpoint for embedding search docs. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """# replace newlines, which can negatively affect performance.texts=[text.replace("\n"," ")fortextintexts]_model_kwargs=self.model_kwargsor{}responses=awaitself.async_client.post(json={"inputs":texts,**_model_kwargs},task=self.task)returnjson.loads(responses.decode())
[docs]defembed_query(self,text:str)->List[float]:"""Call out to HuggingFaceHub's embedding endpoint for embedding query text. Args: text: The text to embed. Returns: Embeddings for the text. """response=self.embed_documents([text])[0]returnresponse
[docs]asyncdefaembed_query(self,text:str)->List[float]:"""Async Call to HuggingFaceHub's embedding endpoint for embedding query text. Args: text: The text to embed. Returns: Embeddings for the text. """response=(awaitself.aembed_documents([text]))[0]returnresponse