fastembed-haystack 0.0.6__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/PKG-INFO +30 -4
  2. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/README.md +25 -0
  3. {fastembed_haystack-0.0.6/example → fastembed_haystack-1.0.0/examples}/example.py +1 -1
  4. fastembed_haystack-1.0.0/examples/sparse_example.py +32 -0
  5. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/pydoc/config.yml +3 -0
  6. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/pyproject.toml +6 -5
  7. fastembed_haystack-1.0.0/src/haystack_integrations/components/embedders/fastembed/__init__.py +14 -0
  8. fastembed_haystack-1.0.0/src/haystack_integrations/components/embedders/fastembed/embedding_backend/fastembed_backend.py +109 -0
  9. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_document_embedder.py +7 -3
  10. fastembed_haystack-1.0.0/src/haystack_integrations/components/embedders/fastembed/fastembed_sparse_document_embedder.py +161 -0
  11. fastembed_haystack-1.0.0/src/haystack_integrations/components/embedders/fastembed/fastembed_sparse_text_embedder.py +110 -0
  12. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_text_embedder.py +3 -5
  13. fastembed_haystack-1.0.0/tests/test_fastembed_sparse_document_embedder.py +283 -0
  14. fastembed_haystack-1.0.0/tests/test_fastembed_sparse_text_embedder.py +210 -0
  15. fastembed_haystack-0.0.6/src/haystack_integrations/components/embedders/fastembed/__init__.py +0 -7
  16. fastembed_haystack-0.0.6/src/haystack_integrations/components/embedders/fastembed/embedding_backend/fastembed_backend.py +0 -52
  17. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/.gitignore +0 -0
  18. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/LICENSE.txt +0 -0
  19. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/src/haystack_integrations/components/embedders/fastembed/embedding_backend/__init__.py +0 -0
  20. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/tests/__init__.py +0 -0
  21. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/tests/test_fastembed_backend.py +0 -0
  22. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/tests/test_fastembed_document_embedder.py +0 -0
  23. {fastembed_haystack-0.0.6 → fastembed_haystack-1.0.0}/tests/test_fastembed_text_embedder.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: fastembed-haystack
3
- Version: 0.0.6
3
+ Version: 1.0.0
4
4
  Summary: Haystack 2.x component to embed strings and Documents using fastembed embedding model
5
5
  Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations
6
6
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/fastembed/README.md
@@ -9,6 +9,7 @@ Author-email: deepset GmbH <info@deepset.ai>
9
9
  License-Expression: Apache-2.0
10
10
  License-File: LICENSE.txt
11
11
  Classifier: Development Status :: 4 - Beta
12
+ Classifier: License :: OSI Approved :: Apache Software License
12
13
  Classifier: Programming Language :: Python
13
14
  Classifier: Programming Language :: Python :: 3.8
14
15
  Classifier: Programming Language :: Python :: 3.9
@@ -18,8 +19,8 @@ Classifier: Programming Language :: Python :: 3.12
18
19
  Classifier: Programming Language :: Python :: Implementation :: CPython
19
20
  Classifier: Programming Language :: Python :: Implementation :: PyPy
20
21
  Requires-Python: >=3.8
21
- Requires-Dist: fastembed>=0.2
22
- Requires-Dist: haystack-ai
22
+ Requires-Dist: fastembed>=0.2.5
23
+ Requires-Dist: haystack-ai>=2.0.1
23
24
  Description-Content-Type: text/markdown
24
25
 
25
26
  # fastembed-haystack
@@ -67,6 +68,31 @@ doc = Document(content="fastembed is supported by and maintained by Qdrant.", me
67
68
  result = embedder.run(documents=[doc])
68
69
  ```
69
70
 
71
+ You can use `FastembedSparseTextEmbedder` and `FastembedSparseDocumentEmbedder` by importing as:
72
+
73
+ ```python
74
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseTextEmbedder
75
+
76
+ text = "fastembed is supported by and maintained by Qdrant."
77
+ text_embedder = FastembedSparseTextEmbedder(
78
+ model="prithvida/Splade_PP_en_v1"
79
+ )
80
+ text_embedder.warm_up()
81
+ embedding = text_embedder.run(text)["embedding"]
82
+ ```
83
+
84
+ ```python
85
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseDocumentEmbedder
86
+ from haystack.dataclasses import Document
87
+
88
+ embedder = FastembedSparseDocumentEmbedder(
89
+ model="prithvida/Splade_PP_en_v1",
90
+ )
91
+ embedder.warm_up()
92
+ doc = Document(content="fastembed is supported by and maintained by Qdrant.", meta={"long_answer": "no",})
93
+ result = embedder.run(documents=[doc])
94
+ ```
95
+
70
96
  ## License
71
97
 
72
98
  `fastembed-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -43,6 +43,31 @@ doc = Document(content="fastembed is supported by and maintained by Qdrant.", me
43
43
  result = embedder.run(documents=[doc])
44
44
  ```
45
45
 
46
+ You can use `FastembedSparseTextEmbedder` and `FastembedSparseDocumentEmbedder` by importing as:
47
+
48
+ ```python
49
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseTextEmbedder
50
+
51
+ text = "fastembed is supported by and maintained by Qdrant."
52
+ text_embedder = FastembedSparseTextEmbedder(
53
+ model="prithvida/Splade_PP_en_v1"
54
+ )
55
+ text_embedder.warm_up()
56
+ embedding = text_embedder.run(text)["embedding"]
57
+ ```
58
+
59
+ ```python
60
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseDocumentEmbedder
61
+ from haystack.dataclasses import Document
62
+
63
+ embedder = FastembedSparseDocumentEmbedder(
64
+ model="prithvida/Splade_PP_en_v1",
65
+ )
66
+ embedder.warm_up()
67
+ doc = Document(content="fastembed is supported by and maintained by Qdrant.", meta={"long_answer": "no",})
68
+ result = embedder.run(documents=[doc])
69
+ ```
70
+
46
71
  ## License
47
72
 
48
73
  `fastembed-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -26,7 +26,7 @@ query = "Who supports fastembed?"
26
26
 
27
27
  result = query_pipeline.run({"text_embedder": {"text": query}})
28
28
 
29
- print(result["retriever"]["documents"][0]) # noqa: T201
29
+ print(result["retriever"]["documents"][0])
30
30
 
31
31
  # Document(id=...,
32
32
  # content: 'fastembed is supported by and maintained by Qdrant.',
@@ -0,0 +1,32 @@
1
+ # Currently, this example shows how to use the FastembedSparseDocumentEmbedder component to embed a list of documents.
2
+
3
+ # TODO: Once we have a proper SparseEmbeddingRetriever, we should replace this naive example with a more realistic one,
4
+ # involving indexing and retrieval of documents.
5
+
6
+ from haystack import Document
7
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseDocumentEmbedder
8
+
9
+ document_list = [
10
+ Document(
11
+ content="Oxidative stress generated within inflammatory joints can produce autoimmune phenomena and joint destruction. Radical species with oxidative activity, including reactive nitrogen species, represent mediators of inflammation and cartilage damage.",
12
+ meta={
13
+ "pubid": "25,445,628",
14
+ "long_answer": "yes",
15
+ },
16
+ ),
17
+ Document(
18
+ content="Plasma levels of pancreatic polypeptide (PP) rise upon food intake. Although other pancreatic islet hormones, such as insulin and glucagon, have been extensively investigated, PP secretion and actions are still poorly understood.",
19
+ meta={
20
+ "pubid": "25,445,712",
21
+ "long_answer": "yes",
22
+ },
23
+ ),
24
+ ]
25
+
26
+ document_embedder = FastembedSparseDocumentEmbedder()
27
+ document_embedder.warm_up()
28
+ documents_with_embeddings = document_embedder.run(document_list)["documents"]
29
+
30
+ for doc in documents_with_embeddings:
31
+ print(f"Document Text: {doc.content}")
32
+ print(f"Document Sparse Embedding: {doc.sparse_embedding.to_dict()}")
@@ -5,6 +5,8 @@ loaders:
5
5
  [
6
6
  "haystack_integrations.components.embedders.fastembed.fastembed_document_embedder",
7
7
  "haystack_integrations.components.embedders.fastembed.fastembed_text_embedder",
8
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder",
9
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder"
8
10
  ]
9
11
  ignore_when_discovered: ["__init__"]
10
12
  processors:
@@ -24,6 +26,7 @@ renderer:
24
26
  order: 80
25
27
  markdown:
26
28
  descriptive_class_title: false
29
+ classdef_code_block: false
27
30
  descriptive_module_title: true
28
31
  add_method_class_prefix: true
29
32
  add_member_class_prefix: false
@@ -14,6 +14,7 @@ authors = [
14
14
  { name = "deepset GmbH", email = "info@deepset.ai" },
15
15
  ]
16
16
  classifiers = [
17
+ "License :: OSI Approved :: Apache Software License",
17
18
  "Development Status :: 4 - Beta",
18
19
  "Programming Language :: Python",
19
20
  "Programming Language :: Python :: 3.8",
@@ -25,8 +26,8 @@ classifiers = [
25
26
  "Programming Language :: Python :: Implementation :: PyPy",
26
27
  ]
27
28
  dependencies = [
28
- "haystack-ai",
29
- "fastembed>=0.2",
29
+ "haystack-ai>=2.0.1",
30
+ "fastembed>=0.2.5",
30
31
  ]
31
32
 
32
33
  [project.urls]
@@ -95,12 +96,12 @@ all = [
95
96
  ]
96
97
 
97
98
  [tool.black]
98
- target-version = ["py37"]
99
+ target-version = ["py38"]
99
100
  line-length = 120
100
101
  skip-string-normalization = true
101
102
 
102
103
  [tool.ruff]
103
- target-version = "py37"
104
+ target-version = "py38"
104
105
  line-length = 120
105
106
  select = [
106
107
  "A",
@@ -155,7 +156,7 @@ ban-relative-imports = "parents"
155
156
  # Tests can use magic values, assertions, and relative imports
156
157
  "tests/**/*" = ["PLR2004", "S101", "TID252"]
157
158
  # examples can contain "print" commands
158
- "examples/**/*" = ["T201"]
159
+ "examples/**/*" = ["T201", "E501"]
159
160
 
160
161
  [tool.coverage.run]
161
162
  source = ["haystack_integrations"]
@@ -0,0 +1,14 @@
1
+ # SPDX-FileCopyrightText: 2024-present deepset GmbH <info@deepset.ai>
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ from .fastembed_document_embedder import FastembedDocumentEmbedder
5
+ from .fastembed_sparse_document_embedder import FastembedSparseDocumentEmbedder
6
+ from .fastembed_sparse_text_embedder import FastembedSparseTextEmbedder
7
+ from .fastembed_text_embedder import FastembedTextEmbedder
8
+
9
+ __all__ = [
10
+ "FastembedDocumentEmbedder",
11
+ "FastembedTextEmbedder",
12
+ "FastembedSparseDocumentEmbedder",
13
+ "FastembedSparseTextEmbedder",
14
+ ]
@@ -0,0 +1,109 @@
1
+ from typing import ClassVar, Dict, List, Optional
2
+
3
+ from haystack.dataclasses.sparse_embedding import SparseEmbedding
4
+ from tqdm import tqdm
5
+
6
+ from fastembed import TextEmbedding
7
+ from fastembed.sparse.sparse_text_embedding import SparseTextEmbedding
8
+
9
+
10
+ class _FastembedEmbeddingBackendFactory:
11
+ """
12
+ Factory class to create instances of fastembed embedding backends.
13
+ """
14
+
15
+ _instances: ClassVar[Dict[str, "_FastembedEmbeddingBackend"]] = {}
16
+
17
+ @staticmethod
18
+ def get_embedding_backend(
19
+ model_name: str,
20
+ cache_dir: Optional[str] = None,
21
+ threads: Optional[int] = None,
22
+ ):
23
+ embedding_backend_id = f"{model_name}{cache_dir}{threads}"
24
+
25
+ if embedding_backend_id in _FastembedEmbeddingBackendFactory._instances:
26
+ return _FastembedEmbeddingBackendFactory._instances[embedding_backend_id]
27
+
28
+ embedding_backend = _FastembedEmbeddingBackend(model_name=model_name, cache_dir=cache_dir, threads=threads)
29
+ _FastembedEmbeddingBackendFactory._instances[embedding_backend_id] = embedding_backend
30
+ return embedding_backend
31
+
32
+
33
+ class _FastembedEmbeddingBackend:
34
+ """
35
+ Class to manage fastembed embeddings.
36
+ """
37
+
38
+ def __init__(
39
+ self,
40
+ model_name: str,
41
+ cache_dir: Optional[str] = None,
42
+ threads: Optional[int] = None,
43
+ ):
44
+ self.model = TextEmbedding(model_name=model_name, cache_dir=cache_dir, threads=threads)
45
+
46
+ def embed(self, data: List[str], progress_bar=True, **kwargs) -> List[List[float]]:
47
+ # the embed method returns a Iterable[np.ndarray], so we convert it to a list of lists
48
+ embeddings = []
49
+ embeddings_iterable = self.model.embed(data, **kwargs)
50
+ for np_array in tqdm(
51
+ embeddings_iterable, disable=not progress_bar, desc="Calculating embeddings", total=len(data)
52
+ ):
53
+ embeddings.append(np_array.tolist())
54
+ return embeddings
55
+
56
+
57
+ class _FastembedSparseEmbeddingBackendFactory:
58
+ """
59
+ Factory class to create instances of fastembed sparse embedding backends.
60
+ """
61
+
62
+ _instances: ClassVar[Dict[str, "_FastembedSparseEmbeddingBackend"]] = {}
63
+
64
+ @staticmethod
65
+ def get_embedding_backend(
66
+ model_name: str,
67
+ cache_dir: Optional[str] = None,
68
+ threads: Optional[int] = None,
69
+ ):
70
+ embedding_backend_id = f"{model_name}{cache_dir}{threads}"
71
+
72
+ if embedding_backend_id in _FastembedSparseEmbeddingBackendFactory._instances:
73
+ return _FastembedSparseEmbeddingBackendFactory._instances[embedding_backend_id]
74
+
75
+ embedding_backend = _FastembedSparseEmbeddingBackend(
76
+ model_name=model_name, cache_dir=cache_dir, threads=threads
77
+ )
78
+ _FastembedSparseEmbeddingBackendFactory._instances[embedding_backend_id] = embedding_backend
79
+ return embedding_backend
80
+
81
+
82
+ class _FastembedSparseEmbeddingBackend:
83
+ """
84
+ Class to manage fastembed sparse embeddings.
85
+ """
86
+
87
+ def __init__(
88
+ self,
89
+ model_name: str,
90
+ cache_dir: Optional[str] = None,
91
+ threads: Optional[int] = None,
92
+ ):
93
+ self.model = SparseTextEmbedding(model_name=model_name, cache_dir=cache_dir, threads=threads)
94
+
95
+ def embed(self, data: List[List[str]], progress_bar=True, **kwargs) -> List[SparseEmbedding]:
96
+ # The embed method returns a Iterable[SparseEmbedding], so we convert to Haystack SparseEmbedding type.
97
+ # Each SparseEmbedding contains an `indices` key containing a list of int and
98
+ # an `values` key containing a list of floats.
99
+
100
+ sparse_embeddings = []
101
+ sparse_embeddings_iterable = self.model.embed(data, **kwargs)
102
+ for sparse_embedding in tqdm(
103
+ sparse_embeddings_iterable, disable=not progress_bar, desc="Calculating sparse embeddings", total=len(data)
104
+ ):
105
+ sparse_embeddings.append(
106
+ SparseEmbedding(indices=sparse_embedding.indices.tolist(), values=sparse_embedding.values.tolist())
107
+ )
108
+
109
+ return sparse_embeddings
@@ -29,14 +29,18 @@ class FastembedDocumentEmbedder:
29
29
  # Text taken from PubMed QA Dataset (https://huggingface.co/datasets/pubmed_qa)
30
30
  document_list = [
31
31
  Document(
32
- content="Oxidative stress generated within inflammatory joints can produce autoimmune phenomena and joint destruction. Radical species with oxidative activity, including reactive nitrogen species, represent mediators of inflammation and cartilage damage.",
32
+ content=("Oxidative stress generated within inflammatory joints can produce autoimmune phenomena and joint "
33
+ "destruction. Radical species with oxidative activity, including reactive nitrogen species, "
34
+ "represent mediators of inflammation and cartilage damage."),
33
35
  meta={
34
36
  "pubid": "25,445,628",
35
37
  "long_answer": "yes",
36
38
  },
37
39
  ),
38
40
  Document(
39
- content="Plasma levels of pancreatic polypeptide (PP) rise upon food intake. Although other pancreatic islet hormones, such as insulin and glucagon, have been extensively investigated, PP secretion and actions are still poorly understood.",
41
+ content=("Plasma levels of pancreatic polypeptide (PP) rise upon food intake. Although other pancreatic "
42
+ "islet hormones, such as insulin and glucagon, have been extensively investigated, PP secretion "
43
+ "and actions are still poorly understood."),
40
44
  meta={
41
45
  "pubid": "25,445,712",
42
46
  "long_answer": "yes",
@@ -49,7 +53,7 @@ class FastembedDocumentEmbedder:
49
53
  print(f"Document Embedding: {result['documents'][0].embedding}")
50
54
  print(f"Embedding Dimension: {len(result['documents'][0].embedding)}")
51
55
  ```
52
- """ # noqa: E501
56
+ """
53
57
 
54
58
  def __init__(
55
59
  self,
@@ -0,0 +1,161 @@
1
+ from typing import Any, Dict, List, Optional
2
+
3
+ from haystack import Document, component, default_to_dict
4
+
5
+ from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory
6
+
7
+
8
+ @component
9
+ class FastembedSparseDocumentEmbedder:
10
+ """
11
+ FastembedSparseDocumentEmbedder computes Document embeddings using Fastembed sparse models.
12
+
13
+ Usage example:
14
+ ```python
15
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseDocumentEmbedder
16
+ from haystack.dataclasses import Document
17
+
18
+ sparse_doc_embedder = FastembedSparseDocumentEmbedder(
19
+ model="prithvida/Splade_PP_en_v1",
20
+ batch_size=32,
21
+ )
22
+
23
+ sparse_doc_embedder.warm_up()
24
+
25
+ # Text taken from PubMed QA Dataset (https://huggingface.co/datasets/pubmed_qa)
26
+ document_list = [
27
+ Document(
28
+ content=("Oxidative stress generated within inflammatory joints can produce autoimmune phenomena and joint "
29
+ "destruction. Radical species with oxidative activity, including reactive nitrogen species, "
30
+ "represent mediators of inflammation and cartilage damage."),
31
+ meta={
32
+ "pubid": "25,445,628",
33
+ "long_answer": "yes",
34
+ },
35
+ ),
36
+ Document(
37
+ content=("Plasma levels of pancreatic polypeptide (PP) rise upon food intake. Although other pancreatic "
38
+ "islet hormones, such as insulin and glucagon, have been extensively investigated, PP secretion "
39
+ "and actions are still poorly understood."),
40
+ meta={
41
+ "pubid": "25,445,712",
42
+ "long_answer": "yes",
43
+ },
44
+ ),
45
+ ]
46
+
47
+ result = sparse_doc_embedder.run(document_list)
48
+ print(f"Document Text: {result['documents'][0].content}")
49
+ print(f"Document Sparse Embedding: {result['documents'][0].sparse_embedding}")
50
+ print(f"Sparse Embedding Dimension: {len(result['documents'][0].sparse_embedding)}")
51
+ ```
52
+ """
53
+
54
+ def __init__(
55
+ self,
56
+ model: str = "prithvida/Splade_PP_en_v1",
57
+ cache_dir: Optional[str] = None,
58
+ threads: Optional[int] = None,
59
+ batch_size: int = 32,
60
+ progress_bar: bool = True,
61
+ parallel: Optional[int] = None,
62
+ meta_fields_to_embed: Optional[List[str]] = None,
63
+ embedding_separator: str = "\n",
64
+ ):
65
+ """
66
+ Create an FastembedDocumentEmbedder component.
67
+
68
+ :param model: Local path or name of the model in Hugging Face's model hub,
69
+ such as `prithvida/Splade_PP_en_v1`.
70
+ :param cache_dir: The path to the cache directory.
71
+ Can be set using the `FASTEMBED_CACHE_PATH` env variable.
72
+ Defaults to `fastembed_cache` in the system's temp directory.
73
+ :param threads: The number of threads single onnxruntime session can use.
74
+ :param batch_size: Number of strings to encode at once.
75
+ :param progress_bar: If `True`, displays progress bar during embedding.
76
+ :param parallel:
77
+ If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
78
+ If 0, use all available cores.
79
+ If None, don't use data-parallel processing, use default onnxruntime threading instead.
80
+ :param meta_fields_to_embed: List of meta fields that should be embedded along with the Document content.
81
+ :param embedding_separator: Separator used to concatenate the meta fields to the Document content.
82
+ """
83
+
84
+ self.model_name = model
85
+ self.cache_dir = cache_dir
86
+ self.threads = threads
87
+ self.batch_size = batch_size
88
+ self.progress_bar = progress_bar
89
+ self.parallel = parallel
90
+ self.meta_fields_to_embed = meta_fields_to_embed or []
91
+ self.embedding_separator = embedding_separator
92
+
93
+ def to_dict(self) -> Dict[str, Any]:
94
+ """
95
+ Serializes the component to a dictionary.
96
+ :returns:
97
+ Dictionary with serialized data.
98
+ """
99
+ return default_to_dict(
100
+ self,
101
+ model=self.model_name,
102
+ cache_dir=self.cache_dir,
103
+ threads=self.threads,
104
+ batch_size=self.batch_size,
105
+ progress_bar=self.progress_bar,
106
+ parallel=self.parallel,
107
+ meta_fields_to_embed=self.meta_fields_to_embed,
108
+ embedding_separator=self.embedding_separator,
109
+ )
110
+
111
+ def warm_up(self):
112
+ """
113
+ Initializes the component.
114
+ """
115
+ if not hasattr(self, "embedding_backend"):
116
+ self.embedding_backend = _FastembedSparseEmbeddingBackendFactory.get_embedding_backend(
117
+ model_name=self.model_name, cache_dir=self.cache_dir, threads=self.threads
118
+ )
119
+
120
+ def _prepare_texts_to_embed(self, documents: List[Document]) -> List[str]:
121
+ texts_to_embed = []
122
+ for doc in documents:
123
+ meta_values_to_embed = [
124
+ str(doc.meta[key]) for key in self.meta_fields_to_embed if key in doc.meta and doc.meta[key] is not None
125
+ ]
126
+ text_to_embed = self.embedding_separator.join([*meta_values_to_embed, doc.content or ""])
127
+
128
+ texts_to_embed.append(text_to_embed)
129
+ return texts_to_embed
130
+
131
+ @component.output_types(documents=List[Document])
132
+ def run(self, documents: List[Document]):
133
+ """
134
+ Embeds a list of Documents.
135
+
136
+ :param documents: List of Documents to embed.
137
+ :returns: A dictionary with the following keys:
138
+ - `documents`: List of Documents with each Document's `sparse_embedding`
139
+ field set to the computed embeddings.
140
+ """
141
+ if not isinstance(documents, list) or documents and not isinstance(documents[0], Document):
142
+ msg = (
143
+ "FastembedSparseDocumentEmbedder expects a list of Documents as input. "
144
+ "In case you want to embed a list of strings, please use the FastembedTextEmbedder."
145
+ )
146
+ raise TypeError(msg)
147
+ if not hasattr(self, "embedding_backend"):
148
+ msg = "The embedding model has not been loaded. Please call warm_up() before running."
149
+ raise RuntimeError(msg)
150
+
151
+ texts_to_embed = self._prepare_texts_to_embed(documents=documents)
152
+ embeddings = self.embedding_backend.embed(
153
+ texts_to_embed,
154
+ batch_size=self.batch_size,
155
+ show_progress_bar=self.progress_bar,
156
+ parallel=self.parallel,
157
+ )
158
+
159
+ for doc, emb in zip(documents, embeddings):
160
+ doc.sparse_embedding = emb
161
+ return {"documents": documents}
@@ -0,0 +1,110 @@
1
+ from typing import Any, Dict, Optional
2
+
3
+ from haystack import component, default_to_dict
4
+ from haystack.dataclasses.sparse_embedding import SparseEmbedding
5
+
6
+ from .embedding_backend.fastembed_backend import _FastembedSparseEmbeddingBackendFactory
7
+
8
+
9
+ @component
10
+ class FastembedSparseTextEmbedder:
11
+ """
12
+ FastembedSparseTextEmbedder computes string embedding using fastembed sparse models.
13
+
14
+ Usage example:
15
+ ```python
16
+ from haystack_integrations.components.embedders.fastembed import FastembedSparseTextEmbedder
17
+
18
+ text = ("It clearly says online this will work on a Mac OS system. "
19
+ "The disk comes and it does not, only Windows. Do Not order this if you have a Mac!!")
20
+
21
+ sparse_text_embedder = FastembedSparseTextEmbedder(
22
+ model="prithvida/Splade_PP_en_v1"
23
+ )
24
+ sparse_text_embedder.warm_up()
25
+
26
+ sparse_embedding = sparse_text_embedder.run(text)["sparse_embedding"]
27
+ ```
28
+ """
29
+
30
+ def __init__(
31
+ self,
32
+ model: str = "prithvida/Splade_PP_en_v1",
33
+ cache_dir: Optional[str] = None,
34
+ threads: Optional[int] = None,
35
+ progress_bar: bool = True,
36
+ parallel: Optional[int] = None,
37
+ ):
38
+ """
39
+ Create a FastembedSparseTextEmbedder component.
40
+
41
+ :param model: Local path or name of the model in Fastembed's model hub, such as `prithvida/Splade_PP_en_v1`
42
+ :param cache_dir: The path to the cache directory.
43
+ Can be set using the `FASTEMBED_CACHE_PATH` env variable.
44
+ Defaults to `fastembed_cache` in the system's temp directory.
45
+ :param threads: The number of threads single onnxruntime session can use. Defaults to None.
46
+ :param progress_bar: If true, displays progress bar during embedding.
47
+ :param parallel:
48
+ If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
49
+ If 0, use all available cores.
50
+ If None, don't use data-parallel processing, use default onnxruntime threading instead.
51
+ """
52
+
53
+ self.model_name = model
54
+ self.cache_dir = cache_dir
55
+ self.threads = threads
56
+ self.progress_bar = progress_bar
57
+ self.parallel = parallel
58
+
59
+ def to_dict(self) -> Dict[str, Any]:
60
+ """
61
+ Serializes the component to a dictionary.
62
+
63
+ :returns:
64
+ Dictionary with serialized data.
65
+ """
66
+ return default_to_dict(
67
+ self,
68
+ model=self.model_name,
69
+ cache_dir=self.cache_dir,
70
+ threads=self.threads,
71
+ progress_bar=self.progress_bar,
72
+ parallel=self.parallel,
73
+ )
74
+
75
+ def warm_up(self):
76
+ """
77
+ Initializes the component.
78
+ """
79
+ if not hasattr(self, "embedding_backend"):
80
+ self.embedding_backend = _FastembedSparseEmbeddingBackendFactory.get_embedding_backend(
81
+ model_name=self.model_name, cache_dir=self.cache_dir, threads=self.threads
82
+ )
83
+
84
+ @component.output_types(sparse_embedding=SparseEmbedding)
85
+ def run(self, text: str):
86
+ """
87
+ Embeds text using the Fastembed model.
88
+
89
+ :param text: A string to embed.
90
+ :returns: A dictionary with the following keys:
91
+ - `embedding`: A list of floats representing the embedding of the input text.
92
+ :raises TypeError: If the input is not a string.
93
+ :raises RuntimeError: If the embedding model has not been loaded.
94
+ """
95
+ if not isinstance(text, str):
96
+ msg = (
97
+ "FastembedSparseTextEmbedder expects a string as input. "
98
+ "In case you want to embed a list of Documents, please use the FastembedDocumentEmbedder."
99
+ )
100
+ raise TypeError(msg)
101
+ if not hasattr(self, "embedding_backend"):
102
+ msg = "The embedding model has not been loaded. Please call warm_up() before running."
103
+ raise RuntimeError(msg)
104
+
105
+ embedding = self.embedding_backend.embed(
106
+ [text],
107
+ show_progress_bar=self.progress_bar,
108
+ parallel=self.parallel,
109
+ )[0]
110
+ return {"sparse_embedding": embedding}
@@ -12,12 +12,10 @@ class FastembedTextEmbedder:
12
12
 
13
13
  Usage example:
14
14
  ```python
15
- # To use this component, install the "fastembed-haystack" package.
16
- # pip install fastembed-haystack
17
-
18
15
  from haystack_integrations.components.embedders.fastembed import FastembedTextEmbedder
19
16
 
20
- text = "It clearly says online this will work on a Mac OS system. The disk comes and it does not, only Windows. Do Not order this if you have a Mac!!"
17
+ text = ("It clearly says online this will work on a Mac OS system. "
18
+ "The disk comes and it does not, only Windows. Do Not order this if you have a Mac!!")
21
19
 
22
20
  text_embedder = FastembedTextEmbedder(
23
21
  model="BAAI/bge-small-en-v1.5"
@@ -26,7 +24,7 @@ class FastembedTextEmbedder:
26
24
 
27
25
  embedding = text_embedder.run(text)["embedding"]
28
26
  ```
29
- """ # noqa: E501
27
+ """
30
28
 
31
29
  def __init__(
32
30
  self,
@@ -0,0 +1,283 @@
1
+ from unittest.mock import MagicMock, patch
2
+
3
+ import numpy as np
4
+ import pytest
5
+ from haystack import Document, default_from_dict
6
+ from haystack.dataclasses.sparse_embedding import SparseEmbedding
7
+ from haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder import (
8
+ FastembedSparseDocumentEmbedder,
9
+ )
10
+
11
+
12
+ class TestFastembedSparseDocumentEmbedderDoc:
13
+ def test_init_default(self):
14
+ """
15
+ Test default initialization parameters for FastembedSparseDocumentEmbedder.
16
+ """
17
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
18
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
19
+ assert embedder.cache_dir is None
20
+ assert embedder.threads is None
21
+ assert embedder.batch_size == 32
22
+ assert embedder.progress_bar is True
23
+ assert embedder.parallel is None
24
+ assert embedder.meta_fields_to_embed == []
25
+ assert embedder.embedding_separator == "\n"
26
+
27
+ def test_init_with_parameters(self):
28
+ """
29
+ Test custom initialization parameters for FastembedSparseDocumentEmbedder.
30
+ """
31
+ embedder = FastembedSparseDocumentEmbedder(
32
+ model="prithvida/Splade_PP_en_v1",
33
+ cache_dir="fake_dir",
34
+ threads=2,
35
+ batch_size=64,
36
+ progress_bar=False,
37
+ parallel=1,
38
+ meta_fields_to_embed=["test_field"],
39
+ embedding_separator=" | ",
40
+ )
41
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
42
+ assert embedder.cache_dir == "fake_dir"
43
+ assert embedder.threads == 2
44
+ assert embedder.batch_size == 64
45
+ assert embedder.progress_bar is False
46
+ assert embedder.parallel == 1
47
+ assert embedder.meta_fields_to_embed == ["test_field"]
48
+ assert embedder.embedding_separator == " | "
49
+
50
+ def test_to_dict(self):
51
+ """
52
+ Test serialization of FastembedSparseDocumentEmbedder to a dictionary, using default initialization parameters.
53
+ """
54
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
55
+ embedder_dict = embedder.to_dict()
56
+ assert embedder_dict == {
57
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder.FastembedSparseDocumentEmbedder", # noqa
58
+ "init_parameters": {
59
+ "model": "prithvida/Splade_PP_en_v1",
60
+ "cache_dir": None,
61
+ "threads": None,
62
+ "batch_size": 32,
63
+ "progress_bar": True,
64
+ "parallel": None,
65
+ "embedding_separator": "\n",
66
+ "meta_fields_to_embed": [],
67
+ },
68
+ }
69
+
70
+ def test_to_dict_with_custom_init_parameters(self):
71
+ """
72
+ Test serialization of FastembedSparseDocumentEmbedder to a dictionary, using custom initialization parameters.
73
+ """
74
+ embedder = FastembedSparseDocumentEmbedder(
75
+ model="prithvida/Splade_PP_en_v1",
76
+ cache_dir="fake_dir",
77
+ threads=2,
78
+ batch_size=64,
79
+ progress_bar=False,
80
+ parallel=1,
81
+ meta_fields_to_embed=["test_field"],
82
+ embedding_separator=" | ",
83
+ )
84
+ embedder_dict = embedder.to_dict()
85
+ assert embedder_dict == {
86
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder.FastembedSparseDocumentEmbedder", # noqa
87
+ "init_parameters": {
88
+ "model": "prithvida/Splade_PP_en_v1",
89
+ "cache_dir": "fake_dir",
90
+ "threads": 2,
91
+ "batch_size": 64,
92
+ "progress_bar": False,
93
+ "parallel": 1,
94
+ "meta_fields_to_embed": ["test_field"],
95
+ "embedding_separator": " | ",
96
+ },
97
+ }
98
+
99
+ def test_from_dict(self):
100
+ """
101
+ Test deserialization of FastembedSparseDocumentEmbedder from a dictionary,
102
+ using default initialization parameters.
103
+ """
104
+ embedder_dict = {
105
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder.FastembedSparseDocumentEmbedder", # noqa
106
+ "init_parameters": {
107
+ "model": "prithvida/Splade_PP_en_v1",
108
+ "cache_dir": None,
109
+ "threads": None,
110
+ "batch_size": 32,
111
+ "progress_bar": True,
112
+ "parallel": None,
113
+ "meta_fields_to_embed": [],
114
+ "embedding_separator": "\n",
115
+ },
116
+ }
117
+ embedder = default_from_dict(FastembedSparseDocumentEmbedder, embedder_dict)
118
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
119
+ assert embedder.cache_dir is None
120
+ assert embedder.threads is None
121
+ assert embedder.batch_size == 32
122
+ assert embedder.progress_bar is True
123
+ assert embedder.parallel is None
124
+ assert embedder.meta_fields_to_embed == []
125
+ assert embedder.embedding_separator == "\n"
126
+
127
+ def test_from_dict_with_custom_init_parameters(self):
128
+ """
129
+ Test deserialization of FastembedSparseDocumentEmbedder from a dictionary,
130
+ using custom initialization parameters.
131
+ """
132
+ embedder_dict = {
133
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder.FastembedSparseDocumentEmbedder", # noqa
134
+ "init_parameters": {
135
+ "model": "prithvida/Splade_PP_en_v1",
136
+ "cache_dir": "fake_dir",
137
+ "threads": 2,
138
+ "batch_size": 64,
139
+ "progress_bar": False,
140
+ "parallel": 1,
141
+ "meta_fields_to_embed": ["test_field"],
142
+ "embedding_separator": " | ",
143
+ },
144
+ }
145
+ embedder = default_from_dict(FastembedSparseDocumentEmbedder, embedder_dict)
146
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
147
+ assert embedder.cache_dir == "fake_dir"
148
+ assert embedder.threads == 2
149
+ assert embedder.batch_size == 64
150
+ assert embedder.progress_bar is False
151
+ assert embedder.parallel == 1
152
+ assert embedder.meta_fields_to_embed == ["test_field"]
153
+ assert embedder.embedding_separator == " | "
154
+
155
+ @patch(
156
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder._FastembedSparseEmbeddingBackendFactory"
157
+ )
158
+ def test_warmup(self, mocked_factory):
159
+ """
160
+ Test for checking embedder instances after warm-up.
161
+ """
162
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
163
+ mocked_factory.get_embedding_backend.assert_not_called()
164
+ embedder.warm_up()
165
+ mocked_factory.get_embedding_backend.assert_called_once_with(
166
+ model_name="prithvida/Splade_PP_en_v1", cache_dir=None, threads=None
167
+ )
168
+
169
+ @patch(
170
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder._FastembedSparseEmbeddingBackendFactory"
171
+ )
172
+ def test_warmup_does_not_reload(self, mocked_factory):
173
+ """
174
+ Test for checking backend instances after multiple warm-ups.
175
+ """
176
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
177
+ mocked_factory.get_embedding_backend.assert_not_called()
178
+ embedder.warm_up()
179
+ embedder.warm_up()
180
+ mocked_factory.get_embedding_backend.assert_called_once()
181
+
182
+ def _generate_mocked_sparse_embedding(self, n):
183
+ list_of_sparse_vectors = []
184
+ for _ in range(n):
185
+ random_indice_length = np.random.randint(3, 15)
186
+ data = {
187
+ "indices": list(range(random_indice_length)),
188
+ "values": [np.random.random_sample() for _ in range(random_indice_length)],
189
+ }
190
+ list_of_sparse_vectors.append(data)
191
+ return list_of_sparse_vectors
192
+
193
+ def test_embed(self):
194
+ """
195
+ Test for checking output dimensions and embedding dimensions.
196
+ """
197
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
198
+ embedder.embedding_backend = MagicMock()
199
+ embedder.embedding_backend.embed = lambda x, **kwargs: self._generate_mocked_sparse_embedding( # noqa: ARG005
200
+ len(x)
201
+ )
202
+
203
+ documents = [Document(content=f"Sample-document text {i}") for i in range(5)]
204
+
205
+ result = embedder.run(documents=documents)
206
+
207
+ assert isinstance(result["documents"], list)
208
+ assert len(result["documents"]) == len(documents)
209
+ for doc in result["documents"]:
210
+ assert isinstance(doc, Document)
211
+ assert isinstance(doc.sparse_embedding, dict)
212
+ assert isinstance(doc.sparse_embedding["indices"], list)
213
+ assert isinstance(doc.sparse_embedding["indices"][0], int)
214
+ assert isinstance(doc.sparse_embedding["values"], list)
215
+ assert isinstance(doc.sparse_embedding["values"][0], float)
216
+
217
+ def test_embed_incorrect_input_format(self):
218
+ """
219
+ Test for checking incorrect input format when creating embedding.
220
+ """
221
+ embedder = FastembedSparseDocumentEmbedder(model="prithvida/Splade_PP_en_v1")
222
+
223
+ string_input = "text"
224
+ list_integers_input = [1, 2, 3]
225
+
226
+ with pytest.raises(
227
+ TypeError,
228
+ match="FastembedSparseDocumentEmbedder expects a list of Documents as input.",
229
+ ):
230
+ embedder.run(documents=string_input)
231
+
232
+ with pytest.raises(
233
+ TypeError,
234
+ match="FastembedSparseDocumentEmbedder expects a list of Documents as input.",
235
+ ):
236
+ embedder.run(documents=list_integers_input)
237
+
238
+ def test_embed_metadata(self):
239
+ """
240
+ Test for checking output dimensions and embedding dimensions for documents
241
+ with a custom instruction and metadata.
242
+ """
243
+ embedder = FastembedSparseDocumentEmbedder(
244
+ model="model",
245
+ meta_fields_to_embed=["meta_field"],
246
+ embedding_separator="\n",
247
+ )
248
+ embedder.embedding_backend = MagicMock()
249
+
250
+ documents = [Document(content=f"document-number {i}", meta={"meta_field": f"meta_value {i}"}) for i in range(5)]
251
+
252
+ embedder.run(documents=documents)
253
+
254
+ embedder.embedding_backend.embed.assert_called_once_with(
255
+ [
256
+ "meta_value 0\ndocument-number 0",
257
+ "meta_value 1\ndocument-number 1",
258
+ "meta_value 2\ndocument-number 2",
259
+ "meta_value 3\ndocument-number 3",
260
+ "meta_value 4\ndocument-number 4",
261
+ ],
262
+ batch_size=32,
263
+ show_progress_bar=True,
264
+ parallel=None,
265
+ )
266
+
267
+ @pytest.mark.integration
268
+ def test_run(self):
269
+ embedder = FastembedSparseDocumentEmbedder(
270
+ model="prithvida/Splade_PP_en_v1",
271
+ )
272
+ embedder.warm_up()
273
+
274
+ doc = Document(content="Parton energy loss in QCD matter")
275
+
276
+ result = embedder.run(documents=[doc])
277
+ embedding = result["documents"][0].sparse_embedding
278
+ embedding_dict = embedding.to_dict()
279
+ assert isinstance(embedding, SparseEmbedding)
280
+ assert isinstance(embedding_dict["indices"], list)
281
+ assert isinstance(embedding_dict["values"], list)
282
+ assert isinstance(embedding_dict["indices"][0], int)
283
+ assert isinstance(embedding_dict["values"][0], float)
@@ -0,0 +1,210 @@
1
+ from unittest.mock import MagicMock, patch
2
+
3
+ import numpy as np
4
+ import pytest
5
+ from haystack import default_from_dict
6
+ from haystack.dataclasses.sparse_embedding import SparseEmbedding
7
+ from haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder import (
8
+ FastembedSparseTextEmbedder,
9
+ )
10
+
11
+
12
+ class TestFastembedSparseTextEmbedder:
13
+ def test_init_default(self):
14
+ """
15
+ Test default initialization parameters for FastembedSparseTextEmbedder.
16
+ """
17
+ embedder = FastembedSparseTextEmbedder(model="prithvida/Splade_PP_en_v1")
18
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
19
+ assert embedder.cache_dir is None
20
+ assert embedder.threads is None
21
+ assert embedder.progress_bar is True
22
+ assert embedder.parallel is None
23
+
24
+ def test_init_with_parameters(self):
25
+ """
26
+ Test custom initialization parameters for FastembedSparseTextEmbedder.
27
+ """
28
+ embedder = FastembedSparseTextEmbedder(
29
+ model="prithvida/Splade_PP_en_v1",
30
+ cache_dir="fake_dir",
31
+ threads=2,
32
+ progress_bar=False,
33
+ parallel=1,
34
+ )
35
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
36
+ assert embedder.cache_dir == "fake_dir"
37
+ assert embedder.threads == 2
38
+ assert embedder.progress_bar is False
39
+ assert embedder.parallel == 1
40
+
41
+ def test_to_dict(self):
42
+ """
43
+ Test serialization of FastembedSparseTextEmbedder to a dictionary, using default initialization parameters.
44
+ """
45
+ embedder = FastembedSparseTextEmbedder(model="prithvida/Splade_PP_en_v1")
46
+ embedder_dict = embedder.to_dict()
47
+ assert embedder_dict == {
48
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder.FastembedSparseTextEmbedder", # noqa
49
+ "init_parameters": {
50
+ "model": "prithvida/Splade_PP_en_v1",
51
+ "cache_dir": None,
52
+ "threads": None,
53
+ "progress_bar": True,
54
+ "parallel": None,
55
+ },
56
+ }
57
+
58
+ def test_to_dict_with_custom_init_parameters(self):
59
+ """
60
+ Test serialization of FastembedSparseTextEmbedder to a dictionary, using custom initialization parameters.
61
+ """
62
+ embedder = FastembedSparseTextEmbedder(
63
+ model="prithvida/Splade_PP_en_v1",
64
+ cache_dir="fake_dir",
65
+ threads=2,
66
+ progress_bar=False,
67
+ parallel=1,
68
+ )
69
+ embedder_dict = embedder.to_dict()
70
+ assert embedder_dict == {
71
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder.FastembedSparseTextEmbedder", # noqa
72
+ "init_parameters": {
73
+ "model": "prithvida/Splade_PP_en_v1",
74
+ "cache_dir": "fake_dir",
75
+ "threads": 2,
76
+ "progress_bar": False,
77
+ "parallel": 1,
78
+ },
79
+ }
80
+
81
+ def test_from_dict(self):
82
+ """
83
+ Test deserialization of FastembedSparseTextEmbedder from a dictionary, using default initialization parameters.
84
+ """
85
+ embedder_dict = {
86
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder.FastembedSparseTextEmbedder", # noqa
87
+ "init_parameters": {
88
+ "model": "prithvida/Splade_PP_en_v1",
89
+ "cache_dir": None,
90
+ "threads": None,
91
+ "progress_bar": True,
92
+ "parallel": None,
93
+ },
94
+ }
95
+ embedder = default_from_dict(FastembedSparseTextEmbedder, embedder_dict)
96
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
97
+ assert embedder.cache_dir is None
98
+ assert embedder.threads is None
99
+ assert embedder.progress_bar is True
100
+ assert embedder.parallel is None
101
+
102
+ def test_from_dict_with_custom_init_parameters(self):
103
+ """
104
+ Test deserialization of FastembedSparseTextEmbedder from a dictionary, using custom initialization parameters.
105
+ """
106
+ embedder_dict = {
107
+ "type": "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder.FastembedSparseTextEmbedder", # noqa
108
+ "init_parameters": {
109
+ "model": "prithvida/Splade_PP_en_v1",
110
+ "cache_dir": "fake_dir",
111
+ "threads": 2,
112
+ "progress_bar": False,
113
+ "parallel": 1,
114
+ },
115
+ }
116
+ embedder = default_from_dict(FastembedSparseTextEmbedder, embedder_dict)
117
+ assert embedder.model_name == "prithvida/Splade_PP_en_v1"
118
+ assert embedder.cache_dir == "fake_dir"
119
+ assert embedder.threads == 2
120
+ assert embedder.progress_bar is False
121
+ assert embedder.parallel == 1
122
+
123
+ @patch(
124
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder._FastembedSparseEmbeddingBackendFactory"
125
+ )
126
+ def test_warmup(self, mocked_factory):
127
+ """
128
+ Test for checking embedder instances after warm-up.
129
+ """
130
+ embedder = FastembedSparseTextEmbedder(model="prithvida/Splade_PP_en_v1")
131
+ mocked_factory.get_embedding_backend.assert_not_called()
132
+ embedder.warm_up()
133
+ mocked_factory.get_embedding_backend.assert_called_once_with(
134
+ model_name="prithvida/Splade_PP_en_v1", cache_dir=None, threads=None
135
+ )
136
+
137
+ @patch(
138
+ "haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder._FastembedSparseEmbeddingBackendFactory"
139
+ )
140
+ def test_warmup_does_not_reload(self, mocked_factory):
141
+ """
142
+ Test for checking backend instances after multiple warm-ups.
143
+ """
144
+ embedder = FastembedSparseTextEmbedder(model="prithvida/Splade_PP_en_v1")
145
+ mocked_factory.get_embedding_backend.assert_not_called()
146
+ embedder.warm_up()
147
+ embedder.warm_up()
148
+ mocked_factory.get_embedding_backend.assert_called_once()
149
+
150
+ def _generate_mocked_sparse_embedding(self, n):
151
+ list_of_sparse_vectors = []
152
+ for _ in range(n):
153
+ random_indice_length = np.random.randint(3, 15)
154
+ data = {
155
+ "indices": list(range(random_indice_length)),
156
+ "values": [np.random.random_sample() for _ in range(random_indice_length)],
157
+ }
158
+ list_of_sparse_vectors.append(data)
159
+
160
+ return list_of_sparse_vectors
161
+
162
+ def test_embed(self):
163
+ """
164
+ Test for checking output dimensions and embedding dimensions.
165
+ """
166
+ embedder = FastembedSparseTextEmbedder(model="BAAI/bge-base-en-v1.5")
167
+ embedder.embedding_backend = MagicMock()
168
+ embedder.embedding_backend.embed = lambda x, **kwargs: self._generate_mocked_sparse_embedding( # noqa: ARG005
169
+ len(x)
170
+ )
171
+
172
+ text = "Good text to embed"
173
+
174
+ result = embedder.run(text=text)
175
+ embedding = result["sparse_embedding"]
176
+ assert isinstance(embedding, dict)
177
+ assert isinstance(embedding["indices"], list)
178
+ assert isinstance(embedding["indices"][0], int)
179
+ assert isinstance(embedding["values"], list)
180
+ assert isinstance(embedding["values"][0], float)
181
+
182
+ def test_run_wrong_incorrect_format(self):
183
+ """
184
+ Test for checking incorrect input format when creating embedding.
185
+ """
186
+ embedder = FastembedSparseTextEmbedder(model="BAAI/bge-base-en-v1.5")
187
+ embedder.embedding_backend = MagicMock()
188
+
189
+ list_integers_input = [1, 2, 3]
190
+
191
+ with pytest.raises(TypeError, match="FastembedSparseTextEmbedder expects a string as input"):
192
+ embedder.run(text=list_integers_input)
193
+
194
+ @pytest.mark.integration
195
+ def test_run(self):
196
+ embedder = FastembedSparseTextEmbedder(
197
+ model="prithvida/Splade_PP_en_v1",
198
+ )
199
+ embedder.warm_up()
200
+
201
+ text = "Parton energy loss in QCD matter"
202
+
203
+ result = embedder.run(text=text)
204
+ embedding = result["sparse_embedding"]
205
+ embedding_dict = embedding.to_dict()
206
+ assert isinstance(embedding, SparseEmbedding)
207
+ assert isinstance(embedding_dict["indices"], list)
208
+ assert isinstance(embedding_dict["values"], list)
209
+ assert isinstance(embedding_dict["indices"][0], int)
210
+ assert isinstance(embedding_dict["values"][0], float)
@@ -1,7 +0,0 @@
1
- # SPDX-FileCopyrightText: 2024-present deepset GmbH <info@deepset.ai>
2
- #
3
- # SPDX-License-Identifier: Apache-2.0
4
- from .fastembed_document_embedder import FastembedDocumentEmbedder
5
- from .fastembed_text_embedder import FastembedTextEmbedder
6
-
7
- __all__ = ["FastembedDocumentEmbedder", "FastembedTextEmbedder"]
@@ -1,52 +0,0 @@
1
- from typing import ClassVar, Dict, List, Optional
2
-
3
- from tqdm import tqdm
4
-
5
- from fastembed import TextEmbedding
6
-
7
-
8
- class _FastembedEmbeddingBackendFactory:
9
- """
10
- Factory class to create instances of fastembed embedding backends.
11
- """
12
-
13
- _instances: ClassVar[Dict[str, "_FastembedEmbeddingBackend"]] = {}
14
-
15
- @staticmethod
16
- def get_embedding_backend(
17
- model_name: str,
18
- cache_dir: Optional[str] = None,
19
- threads: Optional[int] = None,
20
- ):
21
- embedding_backend_id = f"{model_name}{cache_dir}{threads}"
22
-
23
- if embedding_backend_id in _FastembedEmbeddingBackendFactory._instances:
24
- return _FastembedEmbeddingBackendFactory._instances[embedding_backend_id]
25
-
26
- embedding_backend = _FastembedEmbeddingBackend(model_name=model_name, cache_dir=cache_dir, threads=threads)
27
- _FastembedEmbeddingBackendFactory._instances[embedding_backend_id] = embedding_backend
28
- return embedding_backend
29
-
30
-
31
- class _FastembedEmbeddingBackend:
32
- """
33
- Class to manage fastembed embeddings.
34
- """
35
-
36
- def __init__(
37
- self,
38
- model_name: str,
39
- cache_dir: Optional[str] = None,
40
- threads: Optional[int] = None,
41
- ):
42
- self.model = TextEmbedding(model_name=model_name, cache_dir=cache_dir, threads=threads)
43
-
44
- def embed(self, data: List[str], progress_bar=True, **kwargs) -> List[List[float]]:
45
- # the embed method returns a Iterable[np.ndarray], so we convert it to a list of lists
46
- embeddings = []
47
- embeddings_iterable = self.model.embed(data, **kwargs)
48
- for np_array in tqdm(
49
- embeddings_iterable, disable=not progress_bar, desc="Calculating embeddings", total=len(data)
50
- ):
51
- embeddings.append(np_array.tolist())
52
- return embeddings