mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-21 14:18:52 +00:00
huggingface: Add ipex support to HuggingFaceEmbeddings (#29386)
ONNX and OpenVINO models are available by specifying the `backend` argument (the model is loaded using `optimum` https://github.com/huggingface/optimum) ```python from langchain_huggingface import HuggingFaceEmbeddings embedding = HuggingFaceEmbeddings( model_name=model_id, model_kwargs={"backend": "onnx"}, ) ``` With this PR we also enable the IPEX backend ```python from langchain_huggingface import HuggingFaceEmbeddings embedding = HuggingFaceEmbeddings( model_name=model_id, model_kwargs={"backend": "ipex"}, ) ```
This commit is contained in:
parent
3eaf561561
commit
c401254770
@ -3,8 +3,17 @@ from typing import Any, Dict, List, Optional
|
||||
from langchain_core.embeddings import Embeddings
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from ..utils.import_utils import (
|
||||
IMPORT_ERROR,
|
||||
is_ipex_available,
|
||||
is_optimum_intel_available,
|
||||
is_optimum_intel_version,
|
||||
)
|
||||
|
||||
DEFAULT_MODEL_NAME = "sentence-transformers/all-mpnet-base-v2"
|
||||
|
||||
_MIN_OPTIMUM_VERSION = "1.22"
|
||||
|
||||
|
||||
class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
||||
"""HuggingFace sentence_transformers embedding models.
|
||||
@ -61,7 +70,28 @@ class HuggingFaceEmbeddings(BaseModel, Embeddings):
|
||||
"Please install it with `pip install sentence-transformers`."
|
||||
) from exc
|
||||
|
||||
self._client = sentence_transformers.SentenceTransformer(
|
||||
if self.model_kwargs.get("backend", "torch") == "ipex":
|
||||
if not is_optimum_intel_available() or not is_ipex_available():
|
||||
raise ImportError(
|
||||
f'Backend: ipex {IMPORT_ERROR.format("optimum[ipex]")}'
|
||||
)
|
||||
|
||||
if is_optimum_intel_version("<", _MIN_OPTIMUM_VERSION):
|
||||
raise ImportError(
|
||||
f"Backend: ipex requires optimum-intel>="
|
||||
f"{_MIN_OPTIMUM_VERSION}. You can install it with pip: "
|
||||
"`pip install --upgrade --upgrade-strategy eager "
|
||||
"`optimum[ipex]`."
|
||||
)
|
||||
|
||||
from optimum.intel import IPEXSentenceTransformer # type: ignore[import]
|
||||
|
||||
model_cls = IPEXSentenceTransformer
|
||||
|
||||
else:
|
||||
model_cls = sentence_transformers.SentenceTransformer
|
||||
|
||||
self._client = model_cls(
|
||||
self.model_name, cache_folder=self.cache_folder, **self.model_kwargs
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user