forked from deepset-ai/haystack-core-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_document_embedder.py
More file actions
270 lines (230 loc) · 11.2 KB
/
test_document_embedder.py
File metadata and controls
270 lines (230 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# SPDX-FileCopyrightText: 2025-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
from unittest.mock import MagicMock, patch
import pytest
from haystack import Document
from haystack.utils.auth import Secret
from haystack_integrations.components.embedders.watsonx.document_embedder import WatsonxDocumentEmbedder
class TestWatsonXDocumentEmbedder:
@pytest.fixture
def mock_watsonx(self, monkeypatch):
"""Fixture for setting up common mocks"""
monkeypatch.setenv("WATSONX_API_KEY", "fake-api-key")
monkeypatch.setenv("WATSONX_PROJECT_ID", "fake-project-id")
with patch(
"haystack_integrations.components.embedders.watsonx.document_embedder.Embeddings"
) as mock_embeddings:
with patch(
"haystack_integrations.components.embedders.watsonx.document_embedder.Credentials"
) as mock_credentials:
mock_creds_instance = MagicMock()
mock_credentials.return_value = mock_creds_instance
mock_embeddings_instance = MagicMock()
mock_embeddings.return_value = mock_embeddings_instance
yield {
"credentials": mock_credentials,
"embeddings": mock_embeddings,
"creds_instance": mock_creds_instance,
"embeddings_instance": mock_embeddings_instance,
}
def test_init_default(self, mock_watsonx):
embedder = WatsonxDocumentEmbedder(project_id=Secret.from_token("fake-project-id"))
mock_watsonx["credentials"].assert_called_once_with(
api_key="fake-api-key", url="https://us-south.ml.cloud.ibm.com"
)
mock_watsonx["embeddings"].assert_called_once_with(
model_id="ibm/slate-30m-english-rtrvr-v2",
credentials=mock_watsonx["creds_instance"],
project_id="fake-project-id",
params=None,
batch_size=1000,
concurrency_limit=5,
max_retries=None,
)
assert embedder.model == "ibm/slate-30m-english-rtrvr-v2"
assert embedder.prefix == ""
assert embedder.suffix == ""
assert embedder.batch_size == 1000
assert embedder.concurrency_limit == 5
assert isinstance(embedder.project_id, Secret)
assert embedder.project_id.resolve_value() == "fake-project-id"
def test_init_with_parameters(self, mock_watsonx):
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_token("fake-api-key"),
api_base_url="https://custom-url.ibm.com",
project_id=Secret.from_token("custom-project-id"),
truncate_input_tokens=128,
prefix="prefix ",
suffix=" suffix",
batch_size=500,
concurrency_limit=3,
timeout=30.0,
max_retries=5,
)
mock_watsonx["credentials"].assert_called_once_with(api_key="fake-api-key", url="https://custom-url.ibm.com")
mock_watsonx["embeddings"].assert_called_once_with(
model_id="ibm/slate-30m-english-rtrvr-v2",
credentials=mock_watsonx["creds_instance"],
project_id="custom-project-id",
params={"truncate_input_tokens": 128},
batch_size=500,
concurrency_limit=3,
max_retries=5,
)
assert isinstance(embedder.project_id, Secret)
assert embedder.project_id.resolve_value() == "custom-project-id"
def test_init_fail_wo_api_key(self, monkeypatch):
monkeypatch.delenv("WATSONX_API_KEY", raising=False)
with pytest.raises(ValueError, match=r"None of the .* environment variables are set"):
WatsonxDocumentEmbedder(project_id=Secret.from_token("fake-project-id"))
def test_init_fail_wo_project_id(self, monkeypatch):
monkeypatch.setenv("WATSONX_API_KEY", "fake-api-key")
monkeypatch.delenv("WATSONX_PROJECT_ID", raising=False)
with pytest.raises(ValueError, match=r"None of the .* environment variables are set"):
WatsonxDocumentEmbedder()
def test_to_dict(self, mock_watsonx):
component = WatsonxDocumentEmbedder(project_id=Secret.from_env_var("WATSONX_PROJECT_ID"))
data = component.to_dict()
assert data == {
"type": "haystack_integrations.components.embedders.watsonx.document_embedder.WatsonxDocumentEmbedder",
"init_parameters": {
"api_key": {"env_vars": ["WATSONX_API_KEY"], "strict": True, "type": "env_var"},
"model": "ibm/slate-30m-english-rtrvr-v2",
"api_base_url": "https://us-south.ml.cloud.ibm.com",
"project_id": {"env_vars": ["WATSONX_PROJECT_ID"], "strict": True, "type": "env_var"},
"truncate_input_tokens": None,
"prefix": "",
"suffix": "",
"batch_size": 1000,
"concurrency_limit": 5,
"timeout": None,
"max_retries": None,
"embedding_separator": "\n",
"meta_fields_to_embed": [],
},
}
def test_from_dict(self, mock_watsonx):
data = {
"type": "haystack_integrations.components.embedders.watsonx.document_embedder.WatsonxDocumentEmbedder",
"init_parameters": {
"api_key": {"env_vars": ["WATSONX_API_KEY"], "strict": True, "type": "env_var"},
"model": "ibm/slate-125m-english-rtrvr",
"api_base_url": "https://custom-url.ibm.com",
"project_id": {"env_vars": ["WATSONX_PROJECT_ID"], "strict": True, "type": "env_var"},
"prefix": "prefix ",
"suffix": " suffix",
"batch_size": 500,
"concurrency_limit": 3,
},
}
component = WatsonxDocumentEmbedder.from_dict(data)
assert component.model == "ibm/slate-125m-english-rtrvr"
assert component.api_base_url == "https://custom-url.ibm.com"
assert isinstance(component.project_id, Secret)
assert component.project_id.resolve_value() == "fake-project-id"
assert component.prefix == "prefix "
assert component.suffix == " suffix"
assert component.batch_size == 500
assert component.concurrency_limit == 3
def test_prepare_texts_to_embed(self, mock_watsonx):
embedder = WatsonxDocumentEmbedder(
project_id=Secret.from_token("fake-project-id"),
prefix="prefix ",
suffix=" suffix",
meta_fields_to_embed=["source"],
)
prepared_text = embedder._prepare_texts_to_embed(
[Document(content="The food was delicious", meta={"source": "test"})]
)
assert prepared_text == ["prefix test\nThe food was delicious suffix"]
def test_run_wrong_input_format(self, mock_watsonx):
embedder = WatsonxDocumentEmbedder(project_id=Secret.from_token("fake-project-id"))
with pytest.raises(TypeError, match=r"WatsonxDocumentEmbedder expects a list of Documents as input\."):
embedder.run(documents="not a list") # type: ignore
def test_run_empty_documents(self, mock_watsonx):
embedder = WatsonxDocumentEmbedder(project_id=Secret.from_token("fake-project-id"))
result = embedder.run(documents=[])
assert result == {
"documents": [],
"meta": {"model": "ibm/slate-30m-english-rtrvr-v2", "truncate_input_tokens": None, "batch_size": 1000},
}
def test_run_does_not_modify_original_documents(self, mock_watsonx):
"""Test that original documents are not modified during embedding"""
embedder = WatsonxDocumentEmbedder(project_id=Secret.from_token("fake-project-id"))
original_docs = [
Document(content="I love cheese"),
Document(content="A transformer is a deep learning architecture"),
]
# Mock the embedder to return embeddings
mock_watsonx["embeddings_instance"].embed_documents.return_value = [
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
]
result = embedder.run(documents=original_docs)
# Check that original documents are not modified
for doc in original_docs:
assert doc.embedding is None
# Check that returned documents have embeddings
for doc_with_embedding in result["documents"]:
assert doc_with_embedding.embedding is not None
@pytest.mark.integration
class TestWatsonxDocumentEmbedderIntegration:
"""Integration tests for WatsonxDocumentEmbedder (requires real credentials)"""
@pytest.fixture
def test_documents(self):
return [
Document(content="The quick brown fox jumps over the lazy dog"),
Document(content="Artificial intelligence is transforming industries"),
Document(content="Haystack is an open-source framework for building search systems"),
]
@pytest.mark.skipif(
not os.environ.get("WATSONX_API_KEY") or not os.environ.get("WATSONX_PROJECT_ID"),
reason="WATSONX_API_KEY or WATSONX_PROJECT_ID not set",
)
def test_run(self, test_documents):
"""Test real API call with documents"""
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
truncate_input_tokens=128,
)
result = embedder.run(test_documents)
assert len(result["documents"]) == 3
for doc in result["documents"]:
assert isinstance(doc.embedding, list)
assert len(doc.embedding) > 0
assert all(isinstance(x, float) for x in doc.embedding)
assert result["meta"]["model"] == "ibm/slate-30m-english-rtrvr-v2"
@pytest.mark.skipif(
not os.environ.get("WATSONX_API_KEY") or not os.environ.get("WATSONX_PROJECT_ID"),
reason="WATSONX_API_KEY or WATSONX_PROJECT_ID not set",
)
def test_batch_processing(self, test_documents):
"""Test that batch processing works"""
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
batch_size=2,
truncate_input_tokens=128,
)
result = embedder.run(test_documents)
assert len(result["documents"]) == 3
assert all(doc.embedding is not None for doc in result["documents"])
@pytest.mark.skipif(
not os.environ.get("WATSONX_API_KEY") or not os.environ.get("WATSONX_PROJECT_ID"),
reason="WATSONX_API_KEY or WATSONX_PROJECT_ID not set",
)
def test_text_truncation(self):
"""Test that truncation works with long documents"""
long_content = "This is a very long document. " * 10
long_document = Document(content=long_content)
embedder = WatsonxDocumentEmbedder(
api_key=Secret.from_env_var("WATSONX_API_KEY"),
project_id=Secret.from_env_var("WATSONX_PROJECT_ID"),
truncate_input_tokens=4,
)
result = embedder.run([long_document])
assert len(result["documents"][0].embedding) > 0
assert result["meta"]["truncate_input_tokens"] == 4