-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
test_simple.py
236 lines (204 loc) · 8.66 KB
/
test_simple.py
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
"""Test vector store indexes."""
import pickle
from typing import Any, List, cast
from llama_index.legacy.indices.loading import load_index_from_storage
from llama_index.legacy.indices.vector_store.base import VectorStoreIndex
from llama_index.legacy.llms import OpenAI
from llama_index.legacy.schema import Document
from llama_index.legacy.service_context import ServiceContext
from llama_index.legacy.storage.storage_context import StorageContext
from llama_index.legacy.vector_stores.simple import SimpleVectorStore
def test_build_simple(
mock_service_context: ServiceContext,
documents: List[Document],
) -> None:
"""Test build VectorStoreIndex."""
index = VectorStoreIndex.from_documents(
documents=documents, service_context=mock_service_context
)
assert isinstance(index, VectorStoreIndex)
assert len(index.index_struct.nodes_dict) == 4
# check contents of nodes
actual_node_tups = [
("Hello world.", [1, 0, 0, 0, 0]),
("This is a test.", [0, 1, 0, 0, 0]),
("This is another test.", [0, 0, 1, 0, 0]),
("This is a test v2.", [0, 0, 0, 1, 0]),
]
for text_id in index.index_struct.nodes_dict:
node_id = index.index_struct.nodes_dict[text_id]
node = index.docstore.get_node(node_id)
# NOTE: this test breaks abstraction
assert isinstance(index._vector_store, SimpleVectorStore)
embedding = index._vector_store.get(text_id)
assert (node.get_content(), embedding) in actual_node_tups
# test ref doc info
all_ref_doc_info = index.ref_doc_info
for idx, ref_doc_id in enumerate(all_ref_doc_info.keys()):
assert documents[idx].node_id == ref_doc_id
def test_simple_insert(
documents: List[Document],
mock_service_context: ServiceContext,
) -> None:
"""Test insert VectorStoreIndex."""
index = VectorStoreIndex.from_documents(
documents=documents, service_context=mock_service_context
)
assert isinstance(index, VectorStoreIndex)
# insert into index
index.insert(Document(text="This is a test v3."))
# check contenst of nodes
actual_node_tups = [
("Hello world.", [1, 0, 0, 0, 0]),
("This is a test.", [0, 1, 0, 0, 0]),
("This is another test.", [0, 0, 1, 0, 0]),
("This is a test v2.", [0, 0, 0, 1, 0]),
("This is a test v3.", [0, 0, 0, 0, 1]),
]
for text_id in index.index_struct.nodes_dict:
node_id = index.index_struct.nodes_dict[text_id]
node = index.docstore.get_node(node_id)
# NOTE: this test breaks abstraction
assert isinstance(index._vector_store, SimpleVectorStore)
embedding = index._vector_store.get(text_id)
assert (node.get_content(), embedding) in actual_node_tups
def test_simple_delete(
mock_service_context: ServiceContext,
) -> None:
"""Test delete VectorStoreIndex."""
new_documents = [
Document(text="Hello world.", id_="test_id_0"),
Document(text="This is a test.", id_="test_id_1"),
Document(text="This is another test.", id_="test_id_2"),
Document(text="This is a test v2.", id_="test_id_3"),
]
index = VectorStoreIndex.from_documents(
documents=new_documents, service_context=mock_service_context
)
assert isinstance(index, VectorStoreIndex)
# test delete
index.delete_ref_doc("test_id_0")
assert len(index.index_struct.nodes_dict) == 3
actual_node_tups = [
("This is a test.", [0, 1, 0, 0, 0], "test_id_1"),
("This is another test.", [0, 0, 1, 0, 0], "test_id_2"),
("This is a test v2.", [0, 0, 0, 1, 0], "test_id_3"),
]
for text_id in index.index_struct.nodes_dict:
node_id = index.index_struct.nodes_dict[text_id]
node = index.docstore.get_node(node_id)
# NOTE: this test breaks abstraction
assert isinstance(index._vector_store, SimpleVectorStore)
embedding = index._vector_store.get(text_id)
assert (node.get_content(), embedding, node.ref_doc_id) in actual_node_tups
# test insert
index.insert(Document(text="Hello world backup.", id_="test_id_0"))
assert len(index.index_struct.nodes_dict) == 4
actual_node_tups = [
("Hello world backup.", [1, 0, 0, 0, 0], "test_id_0"),
("This is a test.", [0, 1, 0, 0, 0], "test_id_1"),
("This is another test.", [0, 0, 1, 0, 0], "test_id_2"),
("This is a test v2.", [0, 0, 0, 1, 0], "test_id_3"),
]
for text_id in index.index_struct.nodes_dict:
node_id = index.index_struct.nodes_dict[text_id]
node = index.docstore.get_node(node_id)
# NOTE: this test breaks abstraction
assert isinstance(index._vector_store, SimpleVectorStore)
embedding = index._vector_store.get(text_id)
assert (node.get_content(), embedding, node.ref_doc_id) in actual_node_tups
def test_simple_delete_ref_node_from_docstore(
mock_service_context: ServiceContext,
) -> None:
"""Test delete VectorStoreIndex."""
new_documents = [
Document(text="This is a test.", id_="test_id_1"),
Document(text="This is another test.", id_="test_id_2"),
]
index = VectorStoreIndex.from_documents(
documents=new_documents, service_context=mock_service_context
)
assert isinstance(index, VectorStoreIndex)
docstore = index.docstore.get_ref_doc_info("test_id_1")
assert docstore is not None
# test delete
index.delete_ref_doc("test_id_1", delete_from_docstore=True)
docstore = index.docstore.get_ref_doc_info("test_id_1")
assert docstore is None
def test_simple_async(
allow_networking: Any,
documents: List[Document],
mock_service_context: ServiceContext,
) -> None:
"""Test simple vector index with use_async."""
index = VectorStoreIndex.from_documents(
documents=documents, use_async=True, service_context=mock_service_context
)
assert isinstance(index, VectorStoreIndex)
assert len(index.index_struct.nodes_dict) == 4
# check contents of nodes
actual_node_tups = [
("Hello world.", [1, 0, 0, 0, 0]),
("This is a test.", [0, 1, 0, 0, 0]),
("This is another test.", [0, 0, 1, 0, 0]),
("This is a test v2.", [0, 0, 0, 1, 0]),
]
for text_id in index.index_struct.nodes_dict:
node_id = index.index_struct.nodes_dict[text_id]
node = index.docstore.get_node(node_id)
vector_store = cast(SimpleVectorStore, index._vector_store)
embedding = vector_store.get(text_id)
assert (node.get_content(), embedding) in actual_node_tups
def test_simple_insert_save(
documents: List[Document],
mock_service_context: ServiceContext,
) -> None:
storage_context = StorageContext.from_defaults()
index = VectorStoreIndex.from_documents(
documents=documents,
service_context=mock_service_context,
storage_context=storage_context,
)
assert isinstance(index, VectorStoreIndex)
loaded_index = load_index_from_storage(storage_context=storage_context)
assert isinstance(loaded_index, VectorStoreIndex)
assert index.index_struct == loaded_index.index_struct
# insert into index
index.insert(Document(text="This is a test v3."))
loaded_index = load_index_from_storage(storage_context=storage_context)
assert isinstance(loaded_index, VectorStoreIndex)
assert index.index_struct == loaded_index.index_struct
def test_simple_pickle(
mock_service_context: ServiceContext,
documents: List[Document],
) -> None:
"""Test build VectorStoreIndex."""
service_context = ServiceContext.from_service_context(
mock_service_context,
llm=OpenAI(),
)
index = VectorStoreIndex.from_documents(
documents=documents, service_context=service_context
)
data = pickle.dumps(index)
new_index = pickle.loads(data)
assert isinstance(new_index, VectorStoreIndex)
assert len(new_index.index_struct.nodes_dict) == 4
# check contents of nodes
actual_node_tups = [
("Hello world.", [1, 0, 0, 0, 0]),
("This is a test.", [0, 1, 0, 0, 0]),
("This is another test.", [0, 0, 1, 0, 0]),
("This is a test v2.", [0, 0, 0, 1, 0]),
]
for text_id in new_index.index_struct.nodes_dict:
node_id = new_index.index_struct.nodes_dict[text_id]
node = new_index.docstore.get_node(node_id)
# NOTE: this test breaks abstraction
assert isinstance(new_index._vector_store, SimpleVectorStore)
embedding = new_index._vector_store.get(text_id)
assert (node.get_content(), embedding) in actual_node_tups
# test ref doc info
all_ref_doc_info = new_index.ref_doc_info
for idx, ref_doc_id in enumerate(all_ref_doc_info.keys()):
assert documents[idx].node_id == ref_doc_id