-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_stackit_document_embedder.py
More file actions
142 lines (125 loc) · 5.87 KB
/
test_stackit_document_embedder.py
File metadata and controls
142 lines (125 loc) · 5.87 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
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import os
import pytest
from haystack import Document
from haystack.utils import Secret
from haystack_integrations.components.embedders.stackit.document_embedder import STACKITDocumentEmbedder
class TestSTACKITDocumentEmbedder:
def test_init_default(self, monkeypatch):
monkeypatch.setenv("STACKIT_API_KEY", "test-api-key")
embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
assert embedder.api_key == Secret.from_env_var(["STACKIT_API_KEY"])
assert embedder.model == "intfloat/e5-mistral-7b-instruct"
assert embedder.api_base_url == "https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1"
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"
def test_init_with_parameters(self):
embedder = STACKITDocumentEmbedder(
api_key=Secret.from_token("test-api-key"),
model="intfloat/e5-mistral-7b-instruct",
api_base_url="https://custom-api-base-url.com",
prefix="START",
suffix="END",
batch_size=64,
progress_bar=False,
meta_fields_to_embed=["test_field"],
embedding_separator="-",
)
assert embedder.api_key == Secret.from_token("test-api-key")
assert embedder.model == "intfloat/e5-mistral-7b-instruct"
assert embedder.api_base_url == "https://custom-api-base-url.com"
assert embedder.prefix == "START"
assert embedder.suffix == "END"
assert embedder.batch_size == 64
assert embedder.progress_bar is False
assert embedder.meta_fields_to_embed == ["test_field"]
assert embedder.embedding_separator == "-"
def test_to_dict(self, monkeypatch):
monkeypatch.setenv("STACKIT_API_KEY", "test-api-key")
embedder_component = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
component_dict = embedder_component.to_dict()
assert component_dict == {
"type": "haystack_integrations.components.embedders.stackit.document_embedder.STACKITDocumentEmbedder",
"init_parameters": {
"api_key": {"env_vars": ["STACKIT_API_KEY"], "strict": True, "type": "env_var"},
"model": "intfloat/e5-mistral-7b-instruct",
"api_base_url": "https://api.openai-compat.model-serving.eu01.onstackit.cloud/v1",
"dimensions": None,
"organization": None,
"prefix": "",
"suffix": "",
"batch_size": 32,
"progress_bar": True,
"meta_fields_to_embed": [],
"embedding_separator": "\n",
"http_client_kwargs": None,
},
}
def test_to_dict_with_custom_init_parameters(self, monkeypatch):
monkeypatch.setenv("ENV_VAR", "test-secret-key")
embedder = STACKITDocumentEmbedder(
api_key=Secret.from_env_var("ENV_VAR", strict=False),
model="intfloat/e5-mistral-7b-instruct",
api_base_url="https://custom-api-base-url.com",
prefix="START",
suffix="END",
batch_size=64,
progress_bar=False,
meta_fields_to_embed=["test_field"],
embedding_separator="-",
)
component_dict = embedder.to_dict()
assert component_dict == {
"type": "haystack_integrations.components.embedders.stackit.document_embedder.STACKITDocumentEmbedder",
"init_parameters": {
"api_key": {"env_vars": ["ENV_VAR"], "strict": False, "type": "env_var"},
"model": "intfloat/e5-mistral-7b-instruct",
"dimensions": None,
"api_base_url": "https://custom-api-base-url.com",
"organization": None,
"prefix": "START",
"suffix": "END",
"batch_size": 64,
"progress_bar": False,
"meta_fields_to_embed": ["test_field"],
"embedding_separator": "-",
"http_client_kwargs": None,
},
}
@pytest.mark.skipif(
not os.environ.get("STACKIT_API_KEY", None),
reason="Export an env var called STACKIT_API_KEY containing the STACKIT API key to run this test.",
)
@pytest.mark.integration
def test_run(self):
embedder = STACKITDocumentEmbedder(model="intfloat/e5-mistral-7b-instruct")
docs = [
Document(content="I love cheese", meta={"topic": "Cuisine"}),
Document(content="A transformer is a deep learning architecture", meta={"topic": "ML"}),
]
result = embedder.run(docs)
docs_with_embeddings = result["documents"]
assert isinstance(docs_with_embeddings, list)
assert len(docs_with_embeddings) == len(docs)
for doc in docs_with_embeddings:
assert isinstance(doc.embedding, list)
assert isinstance(doc.embedding[0], float)
def test_run_wrong_input_format(self):
embedder = STACKITDocumentEmbedder(
model="intfloat/e5-mistral-7b-instruct", api_key=Secret.from_token("test-api-key")
)
match_error_msg = (
"OpenAIDocumentEmbedder expects a list of Documents as input.In case you want to embed a string, "
"please use the OpenAITextEmbedder."
)
with pytest.raises(TypeError, match=match_error_msg):
embedder.run(documents="text")
with pytest.raises(TypeError, match=match_error_msg):
embedder.run(documents=[1, 2, 3])
assert embedder.run(documents=[]) == {"documents": [], "meta": {}}