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

feat: Add option to select ONNXRuntime Execution Providers for fastembed #1119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -5,6 +5,7 @@

from fastembed import TextEmbedding
from fastembed.sparse.sparse_text_embedding import SparseTextEmbedding
from fastembed.common.types import OnnxProvider


class _FastembedEmbeddingBackendFactory:
Expand All @@ -20,14 +21,15 @@ def get_embedding_backend(
cache_dir: Optional[str] = None,
threads: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
embedding_backend_id = f"{model_name}{cache_dir}{threads}"

if embedding_backend_id in _FastembedEmbeddingBackendFactory._instances:
return _FastembedEmbeddingBackendFactory._instances[embedding_backend_id]

embedding_backend = _FastembedEmbeddingBackend(
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only, onnx_providers=onnx_providers
)
_FastembedEmbeddingBackendFactory._instances[embedding_backend_id] = embedding_backend
return embedding_backend
Expand All @@ -44,9 +46,10 @@ def __init__(
cache_dir: Optional[str] = None,
threads: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
self.model = TextEmbedding(
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only, providers=onnx_providers
)

def embed(self, data: List[str], progress_bar=True, **kwargs) -> List[List[float]]:
Expand All @@ -73,14 +76,15 @@ def get_embedding_backend(
cache_dir: Optional[str] = None,
threads: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
embedding_backend_id = f"{model_name}{cache_dir}{threads}"

if embedding_backend_id in _FastembedSparseEmbeddingBackendFactory._instances:
return _FastembedSparseEmbeddingBackendFactory._instances[embedding_backend_id]

embedding_backend = _FastembedSparseEmbeddingBackend(
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only, onnx_providers=onnx_providers
)
_FastembedSparseEmbeddingBackendFactory._instances[embedding_backend_id] = embedding_backend
return embedding_backend
Expand All @@ -97,9 +101,10 @@ def __init__(
cache_dir: Optional[str] = None,
threads: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
self.model = SparseTextEmbedding(
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only
model_name=model_name, cache_dir=cache_dir, threads=threads, local_files_only=local_files_only, providers=onnx_providers
)

def embed(self, data: List[List[str]], progress_bar=True, **kwargs) -> List[SparseEmbedding]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from haystack import Document, component, default_to_dict

from .embedding_backend.fastembed_backend import _FastembedEmbeddingBackendFactory
from .embedding_backend.fastembed_backend import _FastembedEmbeddingBackendFactory, OnnxProvider


@component
Expand Down Expand Up @@ -68,6 +68,7 @@ def __init__(
local_files_only: bool = False,
meta_fields_to_embed: Optional[List[str]] = None,
embedding_separator: str = "\n",
onnx_providers: Optional[List[OnnxProvider]] = None,
):
"""
Create an FastembedDocumentEmbedder component.
Expand All @@ -89,6 +90,7 @@ def __init__(
:param local_files_only: If `True`, only use the model files in the `cache_dir`.
:param meta_fields_to_embed: List of meta fields that should be embedded along with the Document content.
:param embedding_separator: Separator used to concatenate the meta fields to the Document content.
:param onnx_providers: ONNX Runtime's Execution providers (EPs)
"""

self.model_name = model
Expand All @@ -102,6 +104,7 @@ def __init__(
self.local_files_only = local_files_only
self.meta_fields_to_embed = meta_fields_to_embed or []
self.embedding_separator = embedding_separator
self.onnx_providers = onnx_providers

def to_dict(self) -> Dict[str, Any]:
"""
Expand All @@ -122,6 +125,7 @@ def to_dict(self) -> Dict[str, Any]:
local_files_only=self.local_files_only,
meta_fields_to_embed=self.meta_fields_to_embed,
embedding_separator=self.embedding_separator,
onnx_providers=self.onnx_providers,
)

def warm_up(self):
Expand All @@ -134,6 +138,7 @@ def warm_up(self):
cache_dir=self.cache_dir,
threads=self.threads,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers,
)

def _prepare_texts_to_embed(self, documents: List[Document]) -> List[str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from haystack import Document, component, default_to_dict

from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory
from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory, OnnxProvider


@component
Expand Down Expand Up @@ -62,6 +62,7 @@ def __init__(
local_files_only: bool = False,
meta_fields_to_embed: Optional[List[str]] = None,
embedding_separator: str = "\n",
onnx_providers: Optional[List[OnnxProvider]] = None,
):
"""
Create an FastembedDocumentEmbedder component.
Expand Down Expand Up @@ -92,6 +93,7 @@ def __init__(
self.local_files_only = local_files_only
self.meta_fields_to_embed = meta_fields_to_embed or []
self.embedding_separator = embedding_separator
self.onnx_providers = onnx_providers

def to_dict(self) -> Dict[str, Any]:
"""
Expand All @@ -110,6 +112,7 @@ def to_dict(self) -> Dict[str, Any]:
local_files_only=self.local_files_only,
meta_fields_to_embed=self.meta_fields_to_embed,
embedding_separator=self.embedding_separator,
onnx_providers=self.onnx_providers,
)

def warm_up(self):
Expand All @@ -122,6 +125,7 @@ def warm_up(self):
cache_dir=self.cache_dir,
threads=self.threads,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers
)

def _prepare_texts_to_embed(self, documents: List[Document]) -> List[str]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, List

from haystack import component, default_to_dict
from haystack.dataclasses.sparse_embedding import SparseEmbedding

from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory
from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory, OnnxProvider


@component
Expand Down Expand Up @@ -35,6 +35,7 @@ def __init__(
progress_bar: bool = True,
parallel: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
"""
Create a FastembedSparseTextEmbedder component.
Expand All @@ -58,6 +59,7 @@ def __init__(
self.progress_bar = progress_bar
self.parallel = parallel
self.local_files_only = local_files_only
self.onnx_providers = onnx_providers

def to_dict(self) -> Dict[str, Any]:
"""
Expand All @@ -74,6 +76,7 @@ def to_dict(self) -> Dict[str, Any]:
progress_bar=self.progress_bar,
parallel=self.parallel,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers,
)

def warm_up(self):
Expand All @@ -86,6 +89,7 @@ def warm_up(self):
cache_dir=self.cache_dir,
threads=self.threads,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers,
)

@component.output_types(sparse_embedding=SparseEmbedding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from haystack import component, default_to_dict

from .embedding_backend.fastembed_backend import _FastembedEmbeddingBackendFactory
from .embedding_backend.fastembed_backend import _FastembedEmbeddingBackendFactory, OnnxProvider


@component
Expand Down Expand Up @@ -36,6 +36,7 @@ def __init__(
progress_bar: bool = True,
parallel: Optional[int] = None,
local_files_only: bool = False,
onnx_providers: Optional[List[OnnxProvider]] = None,
):
"""
Create a FastembedTextEmbedder component.
Expand Down Expand Up @@ -63,6 +64,7 @@ def __init__(
self.progress_bar = progress_bar
self.parallel = parallel
self.local_files_only = local_files_only
self.onnx_providers = onnx_providers

def to_dict(self) -> Dict[str, Any]:
"""
Expand All @@ -81,6 +83,7 @@ def to_dict(self) -> Dict[str, Any]:
progress_bar=self.progress_bar,
parallel=self.parallel,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers,
)

def warm_up(self):
Expand All @@ -93,6 +96,7 @@ def warm_up(self):
cache_dir=self.cache_dir,
threads=self.threads,
local_files_only=self.local_files_only,
onnx_providers=self.onnx_providers,
)

@component.output_types(embedding=List[float])
Expand Down
Loading