fastembed-haystack 1.3.0__tar.gz → 1.4.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.
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/CHANGELOG.md +10 -1
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/PKG-INFO +3 -4
- fastembed_haystack-1.4.0/examples/ranker_example.py +22 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/pydoc/config.yml +2 -1
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/pyproject.toml +8 -2
- fastembed_haystack-1.4.0/src/haystack_integrations/components/rankers/fastembed/__init__.py +3 -0
- fastembed_haystack-1.4.0/src/haystack_integrations/components/rankers/fastembed/ranker.py +202 -0
- fastembed_haystack-1.4.0/tests/test_fastembed_ranker.py +292 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/.gitignore +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/LICENSE.txt +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/README.md +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/examples/example.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/examples/sparse_example.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/__init__.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/embedding_backend/__init__.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/embedding_backend/fastembed_backend.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_document_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_sparse_document_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_sparse_text_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/src/haystack_integrations/components/embedders/fastembed/fastembed_text_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/__init__.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_backend.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_document_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_sparse_document_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_sparse_text_embedder.py +0 -0
- {fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_text_embedder.py +0 -0
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## [
|
|
3
|
+
## [integrations/fastembed-v1.3.0] - 2024-10-07
|
|
4
|
+
|
|
5
|
+
### 🚀 Features
|
|
6
|
+
|
|
7
|
+
- Introduce `model_kwargs` in Sparse Embedders (can be used for BM25 parameters) (#1126)
|
|
8
|
+
|
|
9
|
+
### 🧪 Testing
|
|
10
|
+
|
|
11
|
+
- Do not retry tests in `hatch run test` command (#954)
|
|
4
12
|
|
|
5
13
|
### ⚙️ Miscellaneous Tasks
|
|
6
14
|
|
|
7
15
|
- Retry tests to reduce flakyness (#836)
|
|
8
16
|
- Update ruff invocation to include check parameter (#853)
|
|
17
|
+
- Update ruff linting scripts and settings (#1105)
|
|
9
18
|
|
|
10
19
|
### Fix
|
|
11
20
|
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: fastembed-haystack
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.4.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
|
|
7
7
|
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
|
8
8
|
Author-email: deepset GmbH <info@deepset.ai>
|
|
9
|
-
License
|
|
10
|
-
License-File: LICENSE.txt
|
|
9
|
+
License: Apache-2.0
|
|
11
10
|
Classifier: Development Status :: 4 - Beta
|
|
12
11
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
12
|
Classifier: Programming Language :: Python
|
|
@@ -19,7 +18,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
19
18
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
20
19
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
21
20
|
Requires-Python: >=3.8
|
|
22
|
-
Requires-Dist: fastembed>=0.2
|
|
21
|
+
Requires-Dist: fastembed>=0.4.2
|
|
23
22
|
Requires-Dist: haystack-ai>=2.0.1
|
|
24
23
|
Description-Content-Type: text/markdown
|
|
25
24
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from haystack import Document
|
|
2
|
+
|
|
3
|
+
from haystack_integrations.components.rankers.fastembed import FastembedRanker
|
|
4
|
+
|
|
5
|
+
query = "Who is maintaining Qdrant?"
|
|
6
|
+
documents = [
|
|
7
|
+
Document(
|
|
8
|
+
content="This is built to be faster and lighter than other embedding libraries e.g. Transformers, Sentence-Transformers, etc."
|
|
9
|
+
),
|
|
10
|
+
Document(content="fastembed is supported by and maintained by Qdrant."),
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-6-v2")
|
|
14
|
+
ranker.warm_up()
|
|
15
|
+
reranked_documents = ranker.run(query=query, documents=documents)["documents"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
print(reranked_documents["documents"][0])
|
|
19
|
+
|
|
20
|
+
# Document(id=...,
|
|
21
|
+
# content: 'fastembed is supported by and maintained by Qdrant.',
|
|
22
|
+
# score: 5.472434997558594..)
|
|
@@ -6,7 +6,8 @@ loaders:
|
|
|
6
6
|
"haystack_integrations.components.embedders.fastembed.fastembed_document_embedder",
|
|
7
7
|
"haystack_integrations.components.embedders.fastembed.fastembed_text_embedder",
|
|
8
8
|
"haystack_integrations.components.embedders.fastembed.fastembed_sparse_document_embedder",
|
|
9
|
-
"haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder"
|
|
9
|
+
"haystack_integrations.components.embedders.fastembed.fastembed_sparse_text_embedder",
|
|
10
|
+
"haystack_integrations.components.rankers.fastembed.ranker"
|
|
10
11
|
]
|
|
11
12
|
ignore_when_discovered: ["__init__"]
|
|
12
13
|
processors:
|
|
@@ -23,7 +23,7 @@ classifiers = [
|
|
|
23
23
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
24
24
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
25
25
|
]
|
|
26
|
-
dependencies = ["haystack-ai>=2.0.1", "fastembed>=0.2
|
|
26
|
+
dependencies = ["haystack-ai>=2.0.1", "fastembed>=0.4.2"]
|
|
27
27
|
|
|
28
28
|
[project.urls]
|
|
29
29
|
Source = "https://github.com/deepset-ai/haystack-core-integrations"
|
|
@@ -42,6 +42,7 @@ root = "../.."
|
|
|
42
42
|
git_describe_command = 'git describe --tags --match="integrations/fastembed-v[0-9]*"'
|
|
43
43
|
|
|
44
44
|
[tool.hatch.envs.default]
|
|
45
|
+
installer = "uv"
|
|
45
46
|
dependencies = [
|
|
46
47
|
"coverage[toml]>=6.5",
|
|
47
48
|
"pytest",
|
|
@@ -62,8 +63,9 @@ docs = ["pydoc-markdown pydoc/config.yml"]
|
|
|
62
63
|
python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
|
|
63
64
|
|
|
64
65
|
[tool.hatch.envs.lint]
|
|
66
|
+
installer = "uv"
|
|
65
67
|
detached = true
|
|
66
|
-
dependencies = ["black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243", "numpy"]
|
|
68
|
+
dependencies = ["pip", "black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243", "numpy"]
|
|
67
69
|
[tool.hatch.envs.lint.scripts]
|
|
68
70
|
typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}"
|
|
69
71
|
style = ["ruff check {args:.}", "black --check --diff {args:.}"]
|
|
@@ -152,6 +154,10 @@ omit = ["*/tests/*", "*/__init__.py"]
|
|
|
152
154
|
show_missing = true
|
|
153
155
|
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]
|
|
154
156
|
|
|
157
|
+
[tool.pytest.ini_options]
|
|
158
|
+
minversion = "6.0"
|
|
159
|
+
markers = ["unit: unit tests", "integration: integration tests"]
|
|
160
|
+
|
|
155
161
|
[[tool.mypy.overrides]]
|
|
156
162
|
module = [
|
|
157
163
|
"haystack.*",
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
from typing import Any, Dict, List, Optional
|
|
2
|
+
|
|
3
|
+
from haystack import Document, component, default_from_dict, default_to_dict, logging
|
|
4
|
+
|
|
5
|
+
from fastembed.rerank.cross_encoder import TextCrossEncoder
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@component
|
|
11
|
+
class FastembedRanker:
|
|
12
|
+
"""
|
|
13
|
+
Ranks Documents based on their similarity to the query using
|
|
14
|
+
[Fastembed models](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
|
15
|
+
|
|
16
|
+
Documents are indexed from most to least semantically relevant to the query.
|
|
17
|
+
|
|
18
|
+
Usage example:
|
|
19
|
+
```python
|
|
20
|
+
from haystack import Document
|
|
21
|
+
from haystack_integrations.components.rankers.fastembed import FastembedRanker
|
|
22
|
+
|
|
23
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-6-v2", top_k=2)
|
|
24
|
+
|
|
25
|
+
docs = [Document(content="Paris"), Document(content="Berlin")]
|
|
26
|
+
query = "What is the capital of germany?"
|
|
27
|
+
output = ranker.run(query=query, documents=docs)
|
|
28
|
+
print(output["documents"][0].content)
|
|
29
|
+
|
|
30
|
+
# Berlin
|
|
31
|
+
```
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
model_name: str = "Xenova/ms-marco-MiniLM-L-6-v2",
|
|
37
|
+
top_k: int = 10,
|
|
38
|
+
cache_dir: Optional[str] = None,
|
|
39
|
+
threads: Optional[int] = None,
|
|
40
|
+
batch_size: int = 64,
|
|
41
|
+
parallel: Optional[int] = None,
|
|
42
|
+
local_files_only: bool = False,
|
|
43
|
+
meta_fields_to_embed: Optional[List[str]] = None,
|
|
44
|
+
meta_data_separator: str = "\n",
|
|
45
|
+
):
|
|
46
|
+
"""
|
|
47
|
+
Creates an instance of the 'FastembedRanker'.
|
|
48
|
+
|
|
49
|
+
:param model_name: Fastembed model name. Check the list of supported models in the [Fastembed documentation](https://qdrant.github.io/fastembed/examples/Supported_Models/).
|
|
50
|
+
:param top_k: The maximum number of documents to return.
|
|
51
|
+
:param cache_dir: The path to the cache directory.
|
|
52
|
+
Can be set using the `FASTEMBED_CACHE_PATH` env variable.
|
|
53
|
+
Defaults to `fastembed_cache` in the system's temp directory.
|
|
54
|
+
:param threads: The number of threads single onnxruntime session can use. Defaults to None.
|
|
55
|
+
:param batch_size: Number of strings to encode at once.
|
|
56
|
+
:param parallel:
|
|
57
|
+
If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
|
|
58
|
+
If 0, use all available cores.
|
|
59
|
+
If None, don't use data-parallel processing, use default onnxruntime threading instead.
|
|
60
|
+
:param local_files_only: If `True`, only use the model files in the `cache_dir`.
|
|
61
|
+
:param meta_fields_to_embed: List of meta fields that should be concatenated
|
|
62
|
+
with the document content for reranking.
|
|
63
|
+
:param meta_data_separator: Separator used to concatenate the meta fields
|
|
64
|
+
to the Document content.
|
|
65
|
+
"""
|
|
66
|
+
if top_k <= 0:
|
|
67
|
+
msg = f"top_k must be > 0, but got {top_k}"
|
|
68
|
+
raise ValueError(msg)
|
|
69
|
+
|
|
70
|
+
self.model_name = model_name
|
|
71
|
+
self.top_k = top_k
|
|
72
|
+
self.cache_dir = cache_dir
|
|
73
|
+
self.threads = threads
|
|
74
|
+
self.batch_size = batch_size
|
|
75
|
+
self.parallel = parallel
|
|
76
|
+
self.local_files_only = local_files_only
|
|
77
|
+
self.meta_fields_to_embed = meta_fields_to_embed or []
|
|
78
|
+
self.meta_data_separator = meta_data_separator
|
|
79
|
+
self._model = None
|
|
80
|
+
|
|
81
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
82
|
+
"""
|
|
83
|
+
Serializes the component to a dictionary.
|
|
84
|
+
|
|
85
|
+
:returns:
|
|
86
|
+
Dictionary with serialized data.
|
|
87
|
+
"""
|
|
88
|
+
return default_to_dict(
|
|
89
|
+
self,
|
|
90
|
+
model_name=self.model_name,
|
|
91
|
+
top_k=self.top_k,
|
|
92
|
+
cache_dir=self.cache_dir,
|
|
93
|
+
threads=self.threads,
|
|
94
|
+
batch_size=self.batch_size,
|
|
95
|
+
parallel=self.parallel,
|
|
96
|
+
local_files_only=self.local_files_only,
|
|
97
|
+
meta_fields_to_embed=self.meta_fields_to_embed,
|
|
98
|
+
meta_data_separator=self.meta_data_separator,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
@classmethod
|
|
102
|
+
def from_dict(cls, data: Dict[str, Any]) -> "FastembedRanker":
|
|
103
|
+
"""
|
|
104
|
+
Deserializes the component from a dictionary.
|
|
105
|
+
|
|
106
|
+
:param data:
|
|
107
|
+
The dictionary to deserialize from.
|
|
108
|
+
:returns:
|
|
109
|
+
The deserialized component.
|
|
110
|
+
"""
|
|
111
|
+
return default_from_dict(cls, data)
|
|
112
|
+
|
|
113
|
+
def warm_up(self):
|
|
114
|
+
"""
|
|
115
|
+
Initializes the component.
|
|
116
|
+
"""
|
|
117
|
+
if self._model is None:
|
|
118
|
+
self._model = TextCrossEncoder(
|
|
119
|
+
model_name=self.model_name,
|
|
120
|
+
cache_dir=self.cache_dir,
|
|
121
|
+
threads=self.threads,
|
|
122
|
+
local_files_only=self.local_files_only,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
def _prepare_fastembed_input_docs(self, documents: List[Document]) -> List[str]:
|
|
126
|
+
"""
|
|
127
|
+
Prepare the input by concatenating the document text with the metadata fields specified.
|
|
128
|
+
:param documents: The list of Document objects.
|
|
129
|
+
|
|
130
|
+
:return: A list of strings to be given as input to Fastembed model.
|
|
131
|
+
"""
|
|
132
|
+
concatenated_input_list = []
|
|
133
|
+
for doc in documents:
|
|
134
|
+
meta_values_to_embed = [
|
|
135
|
+
str(doc.meta[key]) for key in self.meta_fields_to_embed if key in doc.meta and doc.meta.get(key)
|
|
136
|
+
]
|
|
137
|
+
concatenated_input = self.meta_data_separator.join([*meta_values_to_embed, doc.content or ""])
|
|
138
|
+
concatenated_input_list.append(concatenated_input)
|
|
139
|
+
|
|
140
|
+
return concatenated_input_list
|
|
141
|
+
|
|
142
|
+
@component.output_types(documents=List[Document])
|
|
143
|
+
def run(self, query: str, documents: List[Document], top_k: Optional[int] = None):
|
|
144
|
+
"""
|
|
145
|
+
Returns a list of documents ranked by their similarity to the given query, using FastEmbed.
|
|
146
|
+
|
|
147
|
+
:param query:
|
|
148
|
+
The input query to compare the documents to.
|
|
149
|
+
:param documents:
|
|
150
|
+
A list of documents to be ranked.
|
|
151
|
+
:param top_k:
|
|
152
|
+
The maximum number of documents to return.
|
|
153
|
+
|
|
154
|
+
:returns:
|
|
155
|
+
A dictionary with the following keys:
|
|
156
|
+
- `documents`: A list of documents closest to the query, sorted from most similar to least similar.
|
|
157
|
+
|
|
158
|
+
:raises ValueError: If `top_k` is not > 0.
|
|
159
|
+
"""
|
|
160
|
+
if not isinstance(documents, list) or documents and not isinstance(documents[0], Document):
|
|
161
|
+
msg = "FastembedRanker expects a list of Documents as input. "
|
|
162
|
+
raise TypeError(msg)
|
|
163
|
+
if query == "":
|
|
164
|
+
msg = "No query provided"
|
|
165
|
+
raise ValueError(msg)
|
|
166
|
+
|
|
167
|
+
if not documents:
|
|
168
|
+
return {"documents": []}
|
|
169
|
+
|
|
170
|
+
top_k = top_k or self.top_k
|
|
171
|
+
if top_k <= 0:
|
|
172
|
+
msg = f"top_k must be > 0, but got {top_k}"
|
|
173
|
+
raise ValueError(msg)
|
|
174
|
+
|
|
175
|
+
if self._model is None:
|
|
176
|
+
msg = "The ranker model has not been loaded. Please call warm_up() before running."
|
|
177
|
+
raise RuntimeError(msg)
|
|
178
|
+
|
|
179
|
+
fastembed_input_docs = self._prepare_fastembed_input_docs(documents)
|
|
180
|
+
|
|
181
|
+
scores = list(
|
|
182
|
+
self._model.rerank(
|
|
183
|
+
query=query,
|
|
184
|
+
documents=fastembed_input_docs,
|
|
185
|
+
batch_size=self.batch_size,
|
|
186
|
+
parallel=self.parallel,
|
|
187
|
+
)
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
# Combine the two lists into a single list of tuples
|
|
191
|
+
doc_scores = list(zip(documents, scores))
|
|
192
|
+
|
|
193
|
+
# Sort the list of tuples by the score in descending order
|
|
194
|
+
sorted_doc_scores = sorted(doc_scores, key=lambda x: x[1], reverse=True)
|
|
195
|
+
|
|
196
|
+
# Get the top_k documents
|
|
197
|
+
top_k_documents = []
|
|
198
|
+
for doc, score in sorted_doc_scores[:top_k]:
|
|
199
|
+
doc.score = score
|
|
200
|
+
top_k_documents.append(doc)
|
|
201
|
+
|
|
202
|
+
return {"documents": top_k_documents}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
from unittest.mock import MagicMock
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from haystack import Document, default_from_dict
|
|
5
|
+
|
|
6
|
+
from haystack_integrations.components.rankers.fastembed.ranker import (
|
|
7
|
+
FastembedRanker,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestFastembedRanker:
|
|
12
|
+
def test_init_default(self):
|
|
13
|
+
"""
|
|
14
|
+
Test default initialization parameters for FastembedRanker.
|
|
15
|
+
"""
|
|
16
|
+
ranker = FastembedRanker(model_name="BAAI/bge-reranker-base")
|
|
17
|
+
assert ranker.model_name == "BAAI/bge-reranker-base"
|
|
18
|
+
assert ranker.top_k == 10
|
|
19
|
+
assert ranker.cache_dir is None
|
|
20
|
+
assert ranker.threads is None
|
|
21
|
+
assert ranker.batch_size == 64
|
|
22
|
+
assert ranker.parallel is None
|
|
23
|
+
assert not ranker.local_files_only
|
|
24
|
+
assert ranker.meta_fields_to_embed == []
|
|
25
|
+
assert ranker.meta_data_separator == "\n"
|
|
26
|
+
|
|
27
|
+
def test_init_with_parameters(self):
|
|
28
|
+
"""
|
|
29
|
+
Test custom initialization parameters for FastembedRanker.
|
|
30
|
+
"""
|
|
31
|
+
ranker = FastembedRanker(
|
|
32
|
+
model_name="BAAI/bge-reranker-base",
|
|
33
|
+
top_k=64,
|
|
34
|
+
cache_dir="fake_dir",
|
|
35
|
+
threads=2,
|
|
36
|
+
batch_size=50,
|
|
37
|
+
parallel=1,
|
|
38
|
+
local_files_only=True,
|
|
39
|
+
meta_fields_to_embed=["test_field"],
|
|
40
|
+
meta_data_separator=" | ",
|
|
41
|
+
)
|
|
42
|
+
assert ranker.model_name == "BAAI/bge-reranker-base"
|
|
43
|
+
assert ranker.top_k == 64
|
|
44
|
+
assert ranker.cache_dir == "fake_dir"
|
|
45
|
+
assert ranker.threads == 2
|
|
46
|
+
assert ranker.batch_size == 50
|
|
47
|
+
assert ranker.parallel == 1
|
|
48
|
+
assert ranker.local_files_only
|
|
49
|
+
assert ranker.meta_fields_to_embed == ["test_field"]
|
|
50
|
+
assert ranker.meta_data_separator == " | "
|
|
51
|
+
|
|
52
|
+
def test_init_with_incorrect_input(self):
|
|
53
|
+
"""
|
|
54
|
+
Test for checking incorrect input format on init
|
|
55
|
+
"""
|
|
56
|
+
with pytest.raises(
|
|
57
|
+
ValueError,
|
|
58
|
+
match="top_k must be > 0, but got 0",
|
|
59
|
+
):
|
|
60
|
+
FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-12-v2", top_k=0)
|
|
61
|
+
|
|
62
|
+
with pytest.raises(
|
|
63
|
+
ValueError,
|
|
64
|
+
match="top_k must be > 0, but got -3",
|
|
65
|
+
):
|
|
66
|
+
FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-12-v2", top_k=-3)
|
|
67
|
+
|
|
68
|
+
def test_to_dict(self):
|
|
69
|
+
"""
|
|
70
|
+
Test serialization of FastembedRanker to a dictionary, using default initialization parameters.
|
|
71
|
+
"""
|
|
72
|
+
ranker = FastembedRanker(model_name="BAAI/bge-reranker-base")
|
|
73
|
+
ranker_dict = ranker.to_dict()
|
|
74
|
+
assert ranker_dict == {
|
|
75
|
+
"type": "haystack_integrations.components.rankers.fastembed.ranker.FastembedRanker",
|
|
76
|
+
"init_parameters": {
|
|
77
|
+
"model_name": "BAAI/bge-reranker-base",
|
|
78
|
+
"top_k": 10,
|
|
79
|
+
"cache_dir": None,
|
|
80
|
+
"threads": None,
|
|
81
|
+
"batch_size": 64,
|
|
82
|
+
"parallel": None,
|
|
83
|
+
"local_files_only": False,
|
|
84
|
+
"meta_fields_to_embed": [],
|
|
85
|
+
"meta_data_separator": "\n",
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
def test_to_dict_with_custom_init_parameters(self):
|
|
90
|
+
"""
|
|
91
|
+
Test serialization of FastembedRanker to a dictionary, using custom initialization parameters.
|
|
92
|
+
"""
|
|
93
|
+
ranker = FastembedRanker(
|
|
94
|
+
model_name="BAAI/bge-reranker-base",
|
|
95
|
+
cache_dir="fake_dir",
|
|
96
|
+
threads=2,
|
|
97
|
+
top_k=5,
|
|
98
|
+
batch_size=50,
|
|
99
|
+
parallel=1,
|
|
100
|
+
local_files_only=True,
|
|
101
|
+
meta_fields_to_embed=["test_field"],
|
|
102
|
+
meta_data_separator=" | ",
|
|
103
|
+
)
|
|
104
|
+
ranker_dict = ranker.to_dict()
|
|
105
|
+
assert ranker_dict == {
|
|
106
|
+
"type": "haystack_integrations.components.rankers.fastembed.ranker.FastembedRanker",
|
|
107
|
+
"init_parameters": {
|
|
108
|
+
"model_name": "BAAI/bge-reranker-base",
|
|
109
|
+
"cache_dir": "fake_dir",
|
|
110
|
+
"threads": 2,
|
|
111
|
+
"top_k": 5,
|
|
112
|
+
"batch_size": 50,
|
|
113
|
+
"parallel": 1,
|
|
114
|
+
"local_files_only": True,
|
|
115
|
+
"meta_fields_to_embed": ["test_field"],
|
|
116
|
+
"meta_data_separator": " | ",
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
def test_from_dict(self):
|
|
121
|
+
"""
|
|
122
|
+
Test deserialization of FastembedRanker from a dictionary, using default initialization parameters.
|
|
123
|
+
"""
|
|
124
|
+
ranker_dict = {
|
|
125
|
+
"type": "haystack_integrations.components.rankers.fastembed.ranker.FastembedRanker",
|
|
126
|
+
"init_parameters": {
|
|
127
|
+
"model_name": "BAAI/bge-reranker-base",
|
|
128
|
+
"cache_dir": None,
|
|
129
|
+
"threads": None,
|
|
130
|
+
"top_k": 5,
|
|
131
|
+
"batch_size": 50,
|
|
132
|
+
"parallel": None,
|
|
133
|
+
"local_files_only": False,
|
|
134
|
+
"meta_fields_to_embed": [],
|
|
135
|
+
"meta_data_separator": "\n",
|
|
136
|
+
},
|
|
137
|
+
}
|
|
138
|
+
ranker = default_from_dict(FastembedRanker, ranker_dict)
|
|
139
|
+
assert ranker.model_name == "BAAI/bge-reranker-base"
|
|
140
|
+
assert ranker.cache_dir is None
|
|
141
|
+
assert ranker.threads is None
|
|
142
|
+
assert ranker.top_k == 5
|
|
143
|
+
assert ranker.batch_size == 50
|
|
144
|
+
assert ranker.parallel is None
|
|
145
|
+
assert not ranker.local_files_only
|
|
146
|
+
assert ranker.meta_fields_to_embed == []
|
|
147
|
+
assert ranker.meta_data_separator == "\n"
|
|
148
|
+
|
|
149
|
+
def test_from_dict_with_custom_init_parameters(self):
|
|
150
|
+
"""
|
|
151
|
+
Test deserialization of FastembedRanker from a dictionary, using custom initialization parameters.
|
|
152
|
+
"""
|
|
153
|
+
ranker_dict = {
|
|
154
|
+
"type": "haystack_integrations.components.rankers.fastembed.ranker.FastembedRanker",
|
|
155
|
+
"init_parameters": {
|
|
156
|
+
"model_name": "BAAI/bge-reranker-base",
|
|
157
|
+
"cache_dir": "fake_dir",
|
|
158
|
+
"threads": 2,
|
|
159
|
+
"top_k": 5,
|
|
160
|
+
"batch_size": 50,
|
|
161
|
+
"parallel": 1,
|
|
162
|
+
"local_files_only": True,
|
|
163
|
+
"meta_fields_to_embed": ["test_field"],
|
|
164
|
+
"meta_data_separator": " | ",
|
|
165
|
+
},
|
|
166
|
+
}
|
|
167
|
+
ranker = default_from_dict(FastembedRanker, ranker_dict)
|
|
168
|
+
assert ranker.model_name == "BAAI/bge-reranker-base"
|
|
169
|
+
assert ranker.cache_dir == "fake_dir"
|
|
170
|
+
assert ranker.threads == 2
|
|
171
|
+
assert ranker.top_k == 5
|
|
172
|
+
assert ranker.batch_size == 50
|
|
173
|
+
assert ranker.parallel == 1
|
|
174
|
+
assert ranker.local_files_only
|
|
175
|
+
assert ranker.meta_fields_to_embed == ["test_field"]
|
|
176
|
+
assert ranker.meta_data_separator == " | "
|
|
177
|
+
|
|
178
|
+
def test_run_incorrect_input_format(self):
|
|
179
|
+
"""
|
|
180
|
+
Test for checking incorrect input format.
|
|
181
|
+
"""
|
|
182
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-12-v2")
|
|
183
|
+
ranker._model = "mock_model"
|
|
184
|
+
|
|
185
|
+
query = "query"
|
|
186
|
+
string_input = "text"
|
|
187
|
+
list_integers_input = [1, 2, 3]
|
|
188
|
+
list_document = [Document("Document 1")]
|
|
189
|
+
|
|
190
|
+
with pytest.raises(
|
|
191
|
+
TypeError,
|
|
192
|
+
match="FastembedRanker expects a list of Documents as input.",
|
|
193
|
+
):
|
|
194
|
+
ranker.run(query=query, documents=string_input)
|
|
195
|
+
|
|
196
|
+
with pytest.raises(
|
|
197
|
+
TypeError,
|
|
198
|
+
match="FastembedRanker expects a list of Documents as input.",
|
|
199
|
+
):
|
|
200
|
+
ranker.run(query=query, documents=list_integers_input)
|
|
201
|
+
|
|
202
|
+
with pytest.raises(
|
|
203
|
+
ValueError,
|
|
204
|
+
match="No query provided",
|
|
205
|
+
):
|
|
206
|
+
ranker.run(query="", documents=list_document)
|
|
207
|
+
|
|
208
|
+
with pytest.raises(
|
|
209
|
+
ValueError,
|
|
210
|
+
match="top_k must be > 0, but got -3",
|
|
211
|
+
):
|
|
212
|
+
ranker.run(query=query, documents=list_document, top_k=-3)
|
|
213
|
+
|
|
214
|
+
def test_run_no_warmup(self):
|
|
215
|
+
"""
|
|
216
|
+
Test for checking error when calling without a warmup.
|
|
217
|
+
"""
|
|
218
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-12-v2")
|
|
219
|
+
|
|
220
|
+
query = "query"
|
|
221
|
+
list_document = [Document("Document 1")]
|
|
222
|
+
|
|
223
|
+
with pytest.raises(
|
|
224
|
+
RuntimeError,
|
|
225
|
+
):
|
|
226
|
+
ranker.run(query=query, documents=list_document)
|
|
227
|
+
|
|
228
|
+
def test_run_empty_document_list(self):
|
|
229
|
+
"""
|
|
230
|
+
Test for no error when sending no documents.
|
|
231
|
+
"""
|
|
232
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-12-v2")
|
|
233
|
+
ranker._model = "mock_model"
|
|
234
|
+
|
|
235
|
+
query = "query"
|
|
236
|
+
list_document = []
|
|
237
|
+
|
|
238
|
+
result = ranker.run(query=query, documents=list_document)
|
|
239
|
+
assert len(result["documents"]) == 0
|
|
240
|
+
|
|
241
|
+
def test_embed_metadata(self):
|
|
242
|
+
"""
|
|
243
|
+
Tests the embedding of metadata fields in document content for ranking.
|
|
244
|
+
"""
|
|
245
|
+
ranker = FastembedRanker(
|
|
246
|
+
model_name="model_name",
|
|
247
|
+
meta_fields_to_embed=["meta_field"],
|
|
248
|
+
)
|
|
249
|
+
ranker._model = MagicMock()
|
|
250
|
+
|
|
251
|
+
documents = [Document(content=f"document-number {i}", meta={"meta_field": f"meta_value {i}"}) for i in range(5)]
|
|
252
|
+
query = "test"
|
|
253
|
+
ranker.run(query=query, documents=documents)
|
|
254
|
+
|
|
255
|
+
ranker._model.rerank.assert_called_once_with(
|
|
256
|
+
query=query,
|
|
257
|
+
documents=[
|
|
258
|
+
"meta_value 0\ndocument-number 0",
|
|
259
|
+
"meta_value 1\ndocument-number 1",
|
|
260
|
+
"meta_value 2\ndocument-number 2",
|
|
261
|
+
"meta_value 3\ndocument-number 3",
|
|
262
|
+
"meta_value 4\ndocument-number 4",
|
|
263
|
+
],
|
|
264
|
+
batch_size=64,
|
|
265
|
+
parallel=None,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
@pytest.mark.integration
|
|
269
|
+
def test_run(self):
|
|
270
|
+
ranker = FastembedRanker(model_name="Xenova/ms-marco-MiniLM-L-6-v2", top_k=2)
|
|
271
|
+
ranker.warm_up()
|
|
272
|
+
|
|
273
|
+
query = "Who is maintaining Qdrant?"
|
|
274
|
+
documents = [
|
|
275
|
+
Document(
|
|
276
|
+
content="This is built to be faster and lighter than other embedding \
|
|
277
|
+
libraries e.g. Transformers, Sentence-Transformers, etc."
|
|
278
|
+
),
|
|
279
|
+
Document(content="This is some random input"),
|
|
280
|
+
Document(content="fastembed is supported by and maintained by Qdrant."),
|
|
281
|
+
]
|
|
282
|
+
|
|
283
|
+
result = ranker.run(query=query, documents=documents)
|
|
284
|
+
|
|
285
|
+
assert len(result["documents"]) == 2
|
|
286
|
+
first_document = result["documents"][0]
|
|
287
|
+
second_document = result["documents"][1]
|
|
288
|
+
|
|
289
|
+
assert isinstance(first_document, Document)
|
|
290
|
+
assert isinstance(second_document, Document)
|
|
291
|
+
assert first_document.content == "fastembed is supported by and maintained by Qdrant."
|
|
292
|
+
assert first_document.score > second_document.score
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_document_embedder.py
RENAMED
|
File without changes
|
|
File without changes
|
{fastembed_haystack-1.3.0 → fastembed_haystack-1.4.0}/tests/test_fastembed_sparse_text_embedder.py
RENAMED
|
File without changes
|
|
File without changes
|