-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_config_methods.py
More file actions
332 lines (295 loc) · 11.6 KB
/
Copy pathtest_config_methods.py
File metadata and controls
332 lines (295 loc) · 11.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
from typing import Any, Dict
from weaviate.collections.classes.config import VectorIndexType
from weaviate.collections.classes.config_methods import (
_collection_config_from_json,
_collection_config_simple_from_json,
_collection_configs_simple_from_json,
_nested_properties_from_config,
_properties_from_config,
)
HNSW_CONFIG = {
"skip": False,
"cleanupIntervalSeconds": 300,
"maxConnections": 64,
"efConstruction": 128,
"ef": -1,
"dynamicEfMin": 100,
"dynamicEfMax": 500,
"dynamicEfFactor": 8,
"vectorCacheMaxObjects": 1000000000000,
"flatSearchCutoff": 40000,
"distance": "cosine",
}
def _schema_with_vector_config(vector_config: Dict[str, Any]) -> Dict[str, Any]:
"""Build a minimal collection schema, as returned by Weaviate, around the given vectorConfig."""
return {
"class": "TestCollection",
"vectorConfig": vector_config,
"properties": [],
"invertedIndexConfig": {
"bm25": {"b": 0.75, "k1": 1.2},
"cleanupIntervalSeconds": 60,
"stopwords": {"preset": "en", "additions": None, "removals": None},
},
"multiTenancyConfig": {"enabled": False},
"replicationConfig": {"factor": 1, "deletionStrategy": "NoAutomatedResolution"},
"shardingConfig": {
"virtualPerPhysical": 128,
"desiredCount": 1,
"actualCount": 1,
"desiredVirtualCount": 128,
"actualVirtualCount": 128,
"key": "_id",
"strategy": "hash",
"function": "murmur3",
},
}
def test_collection_config_from_json_with_dropped_vector_index() -> None:
"""A vector whose index was dropped is returned without a vectorIndexConfig."""
# Shape returned by Weaviate after `collection.config.delete_vector_index("dropped")`:
# the entry stays in the schema, `vectorIndexType` becomes "none" and `vectorIndexConfig`
# is omitted entirely.
schema = _schema_with_vector_config(
{
"dropped": {"vectorizer": {"none": {}}, "vectorIndexType": "none"},
"kept": {
"vectorizer": {"none": {}},
"vectorIndexType": "hnsw",
"vectorIndexConfig": HNSW_CONFIG,
},
}
)
config = _collection_config_from_json(schema)
assert config.vector_config is not None
assert config.vector_config["dropped"].vector_index_config is None
assert config.vector_config["kept"].vector_index_config is not None
# The dropped vector must round-trip back to the "none" index type the server reported.
as_dict = config.to_dict()
assert as_dict["vectorConfig"]["dropped"]["vectorIndexType"] == VectorIndexType.NONE.value
assert "vectorIndexConfig" not in as_dict["vectorConfig"]["dropped"]
assert as_dict["vectorConfig"]["kept"]["vectorIndexType"] == VectorIndexType.HNSW.value
def test_collection_config_simple_from_json_with_dropped_vector_index() -> None:
"""`collections.list_all()` must not choke on a collection with a dropped vector index."""
schema = _schema_with_vector_config(
{"dropped": {"vectorizer": {"none": {}}, "vectorIndexType": "none"}}
)
config = _collection_config_simple_from_json(schema)
assert config.vector_config is not None
assert config.vector_config["dropped"].vector_index_config is None
def test_collection_config_simple_from_json_with_none_vectorizer_config() -> None:
"""Test that _collection_configs_simple_from_json handles None vectorizer config."""
schema = {
"classes": [
{
"class": "TestCollection",
"vectorConfig": {
"default": {
"vectorizer": {"text2vec-transformers": None},
"vectorIndexType": "hnsw",
"vectorIndexConfig": {
"skip": False,
"cleanupIntervalSeconds": 300,
"maxConnections": 64,
"efConstruction": 128,
"ef": -1,
"dynamicEfMin": 100,
"dynamicEfMax": 500,
"dynamicEfFactor": 8,
"vectorCacheMaxObjects": 1000000000000,
"flatSearchCutoff": 40000,
"distance": "cosine",
},
}
},
"properties": [],
"invertedIndexConfig": {
"bm25": {"b": 0.75, "k1": 1.2},
"cleanupIntervalSeconds": 60,
"stopwords": {"preset": "en", "additions": None, "removals": None},
},
"replicationConfig": {"factor": 1, "deletionStrategy": "NoAutomatedResolution"},
"shardingConfig": {
"virtualPerPhysical": 128,
"desiredCount": 1,
"actualCount": 1,
"desiredVirtualCount": 128,
"actualVirtualCount": 128,
"key": "_id",
"strategy": "hash",
"function": "murmur3",
},
"vectorIndexType": "hnsw",
"vectorIndexConfig": {
"skip": False,
"cleanupIntervalSeconds": 300,
"maxConnections": 64,
"efConstruction": 128,
"ef": -1,
"dynamicEfMin": 100,
"dynamicEfMax": 500,
"dynamicEfFactor": 8,
"vectorCacheMaxObjects": 1000000000000,
"flatSearchCutoff": 40000,
"distance": "cosine",
},
}
]
}
configs = _collection_configs_simple_from_json(schema)
assert "TestCollection" in configs
vec_config = configs["TestCollection"].vector_config
assert vec_config is not None
assert "default" in vec_config
assert vec_config["default"].vectorizer.model == {}
assert vec_config["default"].vectorizer.source_properties is None
def _make_text_prop(name: str, **extra) -> dict:
base = {
"name": name,
"dataType": ["text"],
"indexFilterable": True,
"indexSearchable": True,
"indexRangeFilters": False,
"tokenization": "word",
}
base.update(extra)
return base
def test_properties_from_config_parses_text_analyzer() -> None:
schema = {
"vectorizer": "none",
"properties": [
_make_text_prop(
"title",
textAnalyzer={"asciiFold": True, "asciiFoldIgnore": ["é"]},
),
_make_text_prop("body"),
],
}
props = _properties_from_config(schema)
title = next(p for p in props if p.name == "title")
body = next(p for p in props if p.name == "body")
assert title.text_analyzer is not None
assert title.text_analyzer.ascii_fold is True
assert title.text_analyzer.ascii_fold_ignore == ["é"]
assert body.text_analyzer is None
# The dataclass round-trips back to the wire format.
assert title.to_dict()["textAnalyzer"] == {
"asciiFold": True,
"asciiFoldIgnore": ["é"],
}
assert "textAnalyzer" not in body.to_dict()
def test_properties_from_config_text_analyzer_omitted_when_no_ascii_fold() -> None:
"""If the server response omits asciiFold, the client treats text_analyzer as unset."""
schema = {
"vectorizer": "none",
"properties": [
# Server response with textAnalyzer present but no asciiFold key
_make_text_prop("title", textAnalyzer={"asciiFoldIgnore": ["é"]}),
],
}
title = _properties_from_config(schema)[0]
assert title.text_analyzer is None
def test_nested_properties_from_config_parses_text_analyzer() -> None:
nested = _nested_properties_from_config(
[
_make_text_prop(
"title",
textAnalyzer={"asciiFold": True, "asciiFoldIgnore": ["ñ"]},
),
]
)
assert nested[0].text_analyzer is not None
assert nested[0].text_analyzer.ascii_fold is True
assert nested[0].text_analyzer.ascii_fold_ignore == ["ñ"]
assert nested[0].to_dict()["textAnalyzer"] == {
"asciiFold": True,
"asciiFoldIgnore": ["ñ"],
}
def test_properties_from_config_parses_stopword_preset_only() -> None:
"""A property with only stopwordPreset (no asciiFold) must still produce a text_analyzer."""
schema = {
"vectorizer": "none",
"properties": [
_make_text_prop("title", textAnalyzer={"stopwordPreset": "fr"}),
],
}
title = _properties_from_config(schema)[0]
assert title.text_analyzer is not None
assert title.text_analyzer.ascii_fold is False
assert title.text_analyzer.ascii_fold_ignore is None
assert title.text_analyzer.stopword_preset == "fr"
def test_properties_from_config_parses_combined_text_analyzer() -> None:
schema = {
"vectorizer": "none",
"properties": [
_make_text_prop(
"title",
textAnalyzer={
"asciiFold": True,
"asciiFoldIgnore": ["é"],
"stopwordPreset": "fr",
},
),
],
}
title = _properties_from_config(schema)[0]
assert title.text_analyzer is not None
assert title.text_analyzer.ascii_fold is True
assert title.text_analyzer.ascii_fold_ignore == ["é"]
assert title.text_analyzer.stopword_preset == "fr"
def _full_schema(class_name: str, **inverted_overrides) -> dict:
inverted = {
"bm25": {"b": 0.75, "k1": 1.2},
"cleanupIntervalSeconds": 60,
"stopwords": {"preset": "en", "additions": None, "removals": None},
}
inverted.update(inverted_overrides)
return {
"class": class_name,
"vectorizer": "none",
"properties": [],
"invertedIndexConfig": inverted,
"replicationConfig": {"factor": 1, "deletionStrategy": "NoAutomatedResolution"},
"shardingConfig": {
"virtualPerPhysical": 128,
"desiredCount": 1,
"actualCount": 1,
"desiredVirtualCount": 128,
"actualVirtualCount": 128,
"key": "_id",
"strategy": "hash",
"function": "murmur3",
},
"vectorIndexType": "hnsw",
"vectorIndexConfig": {
"skip": False,
"cleanupIntervalSeconds": 300,
"maxConnections": 64,
"efConstruction": 128,
"ef": -1,
"dynamicEfMin": 100,
"dynamicEfMax": 500,
"dynamicEfFactor": 8,
"vectorCacheMaxObjects": 1000000000000,
"flatSearchCutoff": 40000,
"distance": "cosine",
},
}
def test_collection_config_parses_stopword_presets() -> None:
"""The inverted index config exposes stopwordPresets when present in the schema."""
schema = _full_schema(
"TestStopwordPresets",
stopwordPresets={
"fr": ["le", "la", "les"],
"es": ["el", "la", "los"],
},
)
full = _collection_config_from_json(schema)
assert full.inverted_index_config.stopword_presets == {
"fr": ["le", "la", "les"],
"es": ["el", "la", "los"],
}
def test_collection_config_stopword_presets_absent() -> None:
"""If the server response omits stopwordPresets, the parsed value is None."""
schema = _full_schema("TestNoStopwordPresets")
full = _collection_config_from_json(schema)
assert full.inverted_index_config.stopword_presets is None