Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ipex support to HuggingFaceEmbeddings #29386

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
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.
Expand Down Expand Up @@ -61,7 +70,28 @@ def __init__(self, **kwargs: Any):
"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
)

Expand Down
Loading