-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathtest_document_embedder.py
More file actions
384 lines (322 loc) · 15.6 KB
/
test_document_embedder.py
File metadata and controls
384 lines (322 loc) · 15.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
import random
from unittest.mock import AsyncMock, MagicMock
import numpy as np
import pytest
from haystack import Document
from haystack.utils.auth import Secret
from haystack_integrations.components.embedders.google_genai import GoogleGenAIDocumentEmbedder, GoogleGenAITextEmbedder
def mock_google_response(contents: list[str], model: str = "gemini-embedding-001", **kwargs) -> dict:
secure_random = random.SystemRandom()
dict_response = {
"embedding": [[secure_random.random() for _ in range(3072)] for _ in contents],
"meta": {"model": model},
}
return dict_response
class TestGoogleGenAIDocumentEmbedder:
def test_init_default(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder()
assert embedder._api_key.resolve_value() == "fake-api-key"
assert embedder._api == "gemini"
assert embedder._vertex_ai_project is None
assert embedder._vertex_ai_location is None
assert embedder._model == "gemini-embedding-001"
assert embedder._prefix == ""
assert embedder._suffix == ""
assert embedder._batch_size == 32
assert embedder._progress_bar is True
assert embedder._meta_fields_to_embed == []
assert embedder._embedding_separator == "\n"
assert embedder._config is None
def test_init_with_parameters(self, monkeypatch):
embedder = GoogleGenAIDocumentEmbedder(
api_key=Secret.from_token("fake-api-key-2"),
model="model",
prefix="prefix",
suffix="suffix",
batch_size=64,
progress_bar=False,
meta_fields_to_embed=["test_field"],
embedding_separator=" | ",
config={"task_type": "CLASSIFICATION"},
)
assert embedder._api_key.resolve_value() == "fake-api-key-2"
assert embedder._model == "model"
assert embedder._prefix == "prefix"
assert embedder._suffix == "suffix"
assert embedder._batch_size == 64
assert embedder._progress_bar is False
assert embedder._meta_fields_to_embed == ["test_field"]
assert embedder._embedding_separator == " | "
assert embedder._config == {"task_type": "CLASSIFICATION"}
def test_init_fail_wo_api_key(self, monkeypatch):
monkeypatch.delenv("GOOGLE_API_KEY", raising=False)
monkeypatch.delenv("GEMINI_API_KEY", raising=False)
with pytest.raises(ValueError, match="you must export the GOOGLE_API_KEY or GEMINI_API_KEY"):
GoogleGenAIDocumentEmbedder()
def test_to_dict(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
component = GoogleGenAIDocumentEmbedder()
data = component.to_dict()
assert data == {
"type": (
"haystack_integrations.components.embedders.google_genai.document_embedder.GoogleGenAIDocumentEmbedder"
),
"init_parameters": {
"model": "gemini-embedding-001",
"prefix": "",
"suffix": "",
"batch_size": 32,
"progress_bar": True,
"meta_fields_to_embed": [],
"embedding_separator": "\n",
"api_key": {"type": "env_var", "env_vars": ["GOOGLE_API_KEY", "GEMINI_API_KEY"], "strict": False},
"config": None,
"api": "gemini",
"vertex_ai_project": None,
"vertex_ai_location": None,
},
}
def test_to_dict_with_custom_init_parameters(self, monkeypatch):
monkeypatch.setenv("ENV_VAR", "fake-api-key")
component = GoogleGenAIDocumentEmbedder(
api_key=Secret.from_env_var("ENV_VAR", strict=False),
model="model",
prefix="prefix",
suffix="suffix",
batch_size=64,
progress_bar=False,
meta_fields_to_embed=["test_field"],
embedding_separator=" | ",
config={"task_type": "CLASSIFICATION"},
)
data = component.to_dict()
assert data == {
"type": (
"haystack_integrations.components.embedders.google_genai.document_embedder.GoogleGenAIDocumentEmbedder"
),
"init_parameters": {
"model": "model",
"prefix": "prefix",
"suffix": "suffix",
"batch_size": 64,
"progress_bar": False,
"meta_fields_to_embed": ["test_field"],
"embedding_separator": " | ",
"api_key": {"type": "env_var", "env_vars": ["ENV_VAR"], "strict": False},
"config": {"task_type": "CLASSIFICATION"},
"api": "gemini",
"vertex_ai_project": None,
"vertex_ai_location": None,
},
}
def test_from_dict(self, monkeypatch):
data = {
"type": (
"haystack_integrations.components.embedders.google_genai.document_embedder.GoogleGenAIDocumentEmbedder"
),
"init_parameters": {
"model": "gemini-embedding-001",
"prefix": "",
"suffix": "",
"batch_size": 32,
"progress_bar": True,
"meta_fields_to_embed": [],
"embedding_separator": "\n",
"api_key": {"type": "env_var", "env_vars": ["GOOGLE_API_KEY", "GEMINI_API_KEY"], "strict": False},
"config": {"task_type": "SEMANTIC_SIMILARITY"},
"api": "gemini",
"vertex_ai_project": None,
"vertex_ai_location": None,
},
}
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder.from_dict(data)
assert embedder._api_key.resolve_value() == "fake-api-key"
assert embedder._model == "gemini-embedding-001"
assert embedder._prefix == ""
assert embedder._suffix == ""
assert embedder._batch_size == 32
assert embedder._progress_bar is True
assert embedder._meta_fields_to_embed == []
assert embedder._embedding_separator == "\n"
assert embedder._config == {"task_type": "SEMANTIC_SIMILARITY"}
assert embedder._api == "gemini"
assert embedder._vertex_ai_project is None
assert embedder._vertex_ai_location is None
def test_prepare_texts_to_embed_w_metadata(self):
documents = [
Document(id=f"{i}", content=f"document number {i}:\ncontent", meta={"meta_field": f"meta_value {i}"})
for i in range(5)
]
embedder = GoogleGenAIDocumentEmbedder(
api_key=Secret.from_token("fake-api-key"), meta_fields_to_embed=["meta_field"], embedding_separator=" | "
)
prepared_texts = embedder._prepare_texts_to_embed(documents)
assert prepared_texts == [
"meta_value 0 | document number 0:\ncontent",
"meta_value 1 | document number 1:\ncontent",
"meta_value 2 | document number 2:\ncontent",
"meta_value 3 | document number 3:\ncontent",
"meta_value 4 | document number 4:\ncontent",
]
def test_run_wrong_input_format(self):
embedder = GoogleGenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"))
# wrong formats
string_input = "text"
list_integers_input = [1, 2, 3]
with pytest.raises(TypeError, match="GoogleGenAIDocumentEmbedder expects a list of Documents as input"):
embedder.run(documents=string_input)
with pytest.raises(TypeError, match="GoogleGenAIDocumentEmbedder expects a list of Documents as input"):
embedder.run(documents=list_integers_input)
def test_run_on_empty_list(self):
embedder = GoogleGenAIDocumentEmbedder(api_key=Secret.from_token("fake-api-key"))
empty_list_input = []
result = embedder.run(documents=empty_list_input)
assert result["documents"] is not None
assert not result["documents"] # empty list
def test_run_does_not_modify_original_documents(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder()
docs = [
Document(content="I love cheese", meta={"topic": "Cuisine"}),
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
]
# Mock the _embed_batch method to return fake embeddings
def mock_embed_batch(texts_to_embed, batch_size):
embeddings = [[0.1, 0.2, 0.3] for _ in texts_to_embed]
meta = {"model": "gemini-embedding-001"}
return embeddings, meta
embedder._embed_batch = mock_embed_batch
result = embedder.run(documents=docs)
# Check that the original documents are not modified
for doc in docs:
assert doc.embedding is None
# Check that the returned documents have embeddings
for doc_with_embedding in result["documents"]:
assert doc_with_embedding.embedding == [0.1, 0.2, 0.3]
@pytest.mark.asyncio
async def test_run_async_does_not_modify_original_documents(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder()
docs = [
Document(content="I love cheese", meta={"topic": "Cuisine"}),
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
]
# Mock the _embed_batch_async method to return fake embeddings
async def mock_embed_batch_async(texts_to_embed, batch_size):
embeddings = [[0.1, 0.2, 0.3] for _ in texts_to_embed]
meta = {"model": "gemini-embedding-001"}
return embeddings, meta
embedder._embed_batch_async = mock_embed_batch_async
result = await embedder.run_async(documents=docs)
# Check that the original documents are not modified
for doc in docs:
assert doc.embedding is None
# Check that the returned documents have embeddings
for doc_with_embedding in result["documents"]:
assert doc_with_embedding.embedding == [0.1, 0.2, 0.3]
def test_embed_batch_passes_full_texts(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder(batch_size=2)
texts = ["first document text", "second document text", "third document text"]
mock_embedding = MagicMock()
mock_embedding.values = [0.1, 0.2, 0.3]
mock_response = MagicMock()
mock_response.embeddings = [mock_embedding]
embedder._client = MagicMock()
embedder._client.models.embed_content.return_value = mock_response
embedder._embed_batch(texts, batch_size=2)
calls = embedder._client.models.embed_content.call_args_list
assert len(calls) == 2
assert [c.parts[0].text for c in calls[0].kwargs["contents"]] == [
"first document text",
"second document text",
]
assert [c.parts[0].text for c in calls[1].kwargs["contents"]] == ["third document text"]
@pytest.mark.asyncio
async def test_embed_batch_async_passes_full_texts(self, monkeypatch):
monkeypatch.setenv("GOOGLE_API_KEY", "fake-api-key")
embedder = GoogleGenAIDocumentEmbedder(batch_size=2)
texts = ["first document text", "second document text", "third document text"]
mock_embedding = MagicMock()
mock_embedding.values = [0.1, 0.2, 0.3]
mock_response = MagicMock()
mock_response.embeddings = [mock_embedding]
embedder._client = MagicMock()
embedder._client.aio.models.embed_content = AsyncMock(return_value=mock_response)
await embedder._embed_batch_async(texts, batch_size=2)
calls = embedder._client.aio.models.embed_content.call_args_list
assert len(calls) == 2
assert [c.parts[0].text for c in calls[0].kwargs["contents"]] == [
"first document text",
"second document text",
]
assert [c.parts[0].text for c in calls[1].kwargs["contents"]] == ["third document text"]
@pytest.mark.skipif(
not os.environ.get("GOOGLE_API_KEY", None),
reason="Export an env var called GOOGLE_API_KEY containing the Google API key to run this test.",
)
@pytest.mark.integration
@pytest.mark.parametrize(
"model,doc_config,query_config",
[
("gemini-embedding-001", {"task_type": "RETRIEVAL_DOCUMENT"}, {"task_type": "RETRIEVAL_QUERY"}),
("gemini-embedding-2", None, None),
],
)
def test_run(self, model, doc_config, query_config):
docs = [
Document(content="The capybara is the largest rodent in the world and lives near rivers in South America."),
Document(content="Dogs are domesticated mammals known for their loyalty and bond with humans."),
Document(content="The tiger is the largest big cat, recognized by its orange coat with black stripes."),
]
embedder = GoogleGenAIDocumentEmbedder(model=model, config=doc_config)
result = embedder.run(documents=docs)
documents_with_embeddings = result["documents"]
assert isinstance(documents_with_embeddings, list)
assert len(documents_with_embeddings) == len(docs)
for doc in documents_with_embeddings:
assert isinstance(doc, Document)
assert isinstance(doc.embedding, list)
assert len(doc.embedding) == 3072
assert all(isinstance(x, float) for x in doc.embedding)
assert result["meta"]["model"] == model
text_embedder = GoogleGenAITextEmbedder(model=model, config=query_config)
query_embedding = text_embedder.run("capybara")["embedding"]
query_vec = np.array(query_embedding)
similarities = []
for doc in documents_with_embeddings:
doc_vec = np.array(doc.embedding)
cosine_sim = np.dot(query_vec, doc_vec) / (np.linalg.norm(query_vec) * np.linalg.norm(doc_vec))
similarities.append(cosine_sim)
assert similarities[0] == max(similarities)
@pytest.mark.asyncio
@pytest.mark.skipif(
not os.environ.get("GOOGLE_API_KEY", None),
reason="Export an env var called GOOGLE_API_KEY containing the Google API key to run this test.",
)
@pytest.mark.integration
@pytest.mark.parametrize("model", ["gemini-embedding-001", "gemini-embedding-2"])
async def test_run_async(self, model):
docs = [
Document(content="I love cheese", meta={"topic": "Cuisine"}),
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
]
embedder = GoogleGenAIDocumentEmbedder(model=model, meta_fields_to_embed=["topic"], embedding_separator=" | ")
result = await embedder.run_async(documents=docs)
documents_with_embeddings = result["documents"]
assert isinstance(documents_with_embeddings, list)
assert len(documents_with_embeddings) == len(docs)
for doc in documents_with_embeddings:
assert isinstance(doc, Document)
assert isinstance(doc.embedding, list)
assert len(doc.embedding) == 3072
assert all(isinstance(x, float) for x in doc.embedding)
assert result["meta"]["model"] == model
assert result["documents"][0].meta == {"topic": "Cuisine"}
assert result["documents"][1].meta == {"topic": "ML"}