-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_astra.py
445 lines (376 loc) · 14.8 KB
/
test_astra.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import json
import logging
from typing import List
import pytest
from astrapy.api import APIRequestError
from httpx import ConnectError, HTTPStatusError
from langchain.schema.embeddings import Embeddings
from langchain_astradb import AstraDBVectorStore
from langchain_core.documents import Document
from langchain_core.runnables import RunnableConfig
from langchain_core.vectorstores import VectorStore
from e2e_tests.conftest import (
is_astra,
)
from e2e_tests.test_utils import skip_test_due_to_implementation_not_supported
from e2e_tests.test_utils.astradb_vector_store_handler import AstraDBVectorStoreHandler
from e2e_tests.test_utils.vector_store_handler import VectorStoreImplementation
def test_basic_vector_search(vectorstore: AstraDBVectorStore):
print("Running test_basic_vector_search")
vectorstore.add_texts(["RAGStack is a framework to run LangChain in production"])
retriever = vectorstore.as_retriever()
assert len(retriever.get_relevant_documents("RAGStack")) > 0
def test_ingest_errors(vectorstore: AstraDBVectorStore):
print("Running test_ingestion")
empty_text = ""
try:
# empty text computes embeddings vector as all zeroes and this is not allowed
vectorstore.add_texts([empty_text])
except ValueError as e:
print("Error:", e)
# API Exception while running bulk insertion: [{'message': "Failed to insert document with _id 'b388435404254c17b720816ee9e0ddc4': Zero vectors cannot be indexed or queried with cosine similarity"}] # noqa: E501
if (
"Zero and near-zero vectors cannot be indexed "
"or queried with cosine similarity" not in e.args[0]
):
pytest.fail(
f"Should have thrown ValueError with Zero vectors cannot be indexed "
f"or queried with cosine similarity but it was {e}"
)
very_long_text = "RAGStack is a framework to run LangChain in production. " * 10_000
# body is not indexed by default, but metadata is
vectorstore.add_texts([very_long_text])
vectorstore.add_documents([Document(page_content=very_long_text, metadata={})])
try:
vectorstore.add_documents(
[
Document(
page_content="some short text", metadata={"text": very_long_text}
)
]
)
pytest.fail("Should have thrown ValueError")
except ValueError as e:
print("Error:", e)
# API Exception while running bulk insertion: {'errors': [{'message': 'Document size limitation violated: String value length (56000) exceeds maximum allowed (16000)', 'errorCode': 'SHRED_DOC_LIMIT_VIOLATION'}]} # noqa: E501
if "SHRED_DOC_LIMIT_VIOLATION" not in e.args[0]:
pytest.fail(
f"Should have thrown ValueError with SHRED_DOC_LIMIT_VIOLATION "
f"but it was {e}"
)
def test_wrong_connection_parameters(vectorstore: AstraDBVectorStore):
try:
AstraDBVectorStore(
collection_name="something",
embedding=MockEmbeddings(),
token="xxxxx", # noqa: S106
# we assume that post 1234 is not open locally
api_endpoint="https://locahost:1234",
)
pytest.fail("Should have thrown exception")
except ConnectError as e:
print("Error:", e)
# This is expected to be a valid endpoint,
# because we want to test an AUTHENTICATION error
api_endpoint = vectorstore.api_endpoint
try:
print("api_endpoint:", api_endpoint)
AstraDBVectorStore(
collection_name="something",
embedding=MockEmbeddings(),
token="this-is-a-wrong-token", # noqa: S106
api_endpoint=api_endpoint,
)
pytest.fail("Should have thrown exception")
except HTTPStatusError as e:
print("Error:", e)
if "401 Unauthorized" not in str(e):
pytest.fail(
f"Should have thrown HTTPStatusError with '401 Unauthorized' "
f"but it was {e}"
)
def test_basic_metadata_filtering_no_vector(vectorstore: AstraDBVectorStore):
collection = vectorstore.collection
vectorstore.add_texts(
texts=["RAGStack is a framework to run LangChain in production"],
metadatas=[
{
"id": "http://mywebsite",
"language": "en",
"source": "website",
"name": "Homepage",
}
],
)
response = collection.find_one(filter={}).get("data").get("document")
print("Response:", response)
verify_document(
response,
"RAGStack is a framework to run LangChain in production",
{
"id": "http://mywebsite",
"language": "en",
"source": "website",
"name": "Homepage",
},
)
response = (
collection.find_one(filter={"metadata.source": "website"})
.get("data")
.get("document")
)
print("Response:", response)
verify_document(
response,
"RAGStack is a framework to run LangChain in production",
{
"id": "http://mywebsite",
"language": "en",
"source": "website",
"name": "Homepage",
},
)
response = (
collection.find_one(
filter={
"$and": [{"metadata.language": "en"}, {"metadata.source": "website"}]
}
)
.get("data")
.get("document")
)
print("Response:", response)
verify_document(
response,
"RAGStack is a framework to run LangChain in production",
{
"id": "http://mywebsite",
"language": "en",
"source": "website",
"name": "Homepage",
},
)
try:
collection.find_one(filter={"metadata.chunks": {"$invalid": 2}})
pytest.fail("Should have thrown ValueError")
except APIRequestError as e:
print("Error:", e)
# Parse the error message
errors = json.loads(e.response.text)
# Check that the errors field has been properly retrieved
assert "errors" in errors
errors = errors["errors"]
if len(errors) == 1:
error = errors[0]
assert error.get("errorCode") == "UNSUPPORTED_FILTER_OPERATION"
elif len(errors) > 1:
assert (
errors[0].get("errorCode") == "UNSUPPORTED_FILTER_OPERATION"
or errors[1].get("errorCode") == "UNSUPPORTED_FILTER_OPERATION"
)
else:
pytest.fail(
f"Should have thrown ValueError with UNSUPPORTED_FILTER_OPERATION "
f"but it was {e}"
)
def verify_document(document, expected_content, expected_metadata):
if isinstance(document, Document):
assert document.page_content == expected_content
assert document.metadata == expected_metadata
else:
assert document.get("content") == expected_content
assert document.get("metadata") == expected_metadata
def test_vector_search_with_metadata(vectorstore: VectorStore):
print("Running test_vector_search_with_metadata")
document_ids = vectorstore.add_texts(
texts=[
"RAGStack is a framework to run LangChain in production",
"RAGStack is developed by DataStax",
],
metadatas=[
{
"id": "http://mywebsite/intro",
"source": "website",
"context": "homepage",
},
{"id": "http://mywebsite/about", "source": "website", "context": "other"},
],
)
# test for search
documents = vectorstore.search(
"RAGStack", "similarity", filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents = vectorstore.search("RAGStack", "similarity")
assert len(documents) == 2
documents = vectorstore.search(
"RAGStack", "similarity", filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents = vectorstore.search("RAGStack", "mmr")
assert len(documents) == 2
documents = vectorstore.search("RAGStack", "mmr", filter={"context": "homepage"})
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents = vectorstore.similarity_search(
"RAGStack", filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents = vectorstore.similarity_search(
"RAGStack", distance_threshold=0.9, filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
# test for similarity_search_with_score
documents_with_score = vectorstore.similarity_search_with_score(
"RAGStack", filter={"context": "homepage"}
)
assert len(documents_with_score) == 1
# th elements are Tuple(document, score)
score = documents_with_score[0][1]
assert score > 0.1
verify_document(
documents_with_score[0][0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
# test for similarity_search_with_relevance_scores
documents_with_score = vectorstore.similarity_search_with_relevance_scores(
query="RAGStack", k=1, filter={"context": "homepage"}
)
assert len(documents_with_score) == 1
# the elements are Tuple(document, score)
score = documents_with_score[0][1]
assert score > 0.1
verify_document(
documents_with_score[0][0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents_with_score = vectorstore.similarity_search_with_relevance_scores(
query="RAGStack", k=1
)
assert len(documents_with_score) == 1
# the elements are Tuple(document, score)
score = documents_with_score[0][1]
assert score > 0.1
# test for max_marginal_relevance_search_by_vector
embeddings: Embeddings = vectorstore.embeddings
vector = embeddings.embed_query("RAGStack")
documents = vectorstore.max_marginal_relevance_search_by_vector(
embedding=vector, k=1
)
assert len(documents) == 1
documents = vectorstore.max_marginal_relevance_search_by_vector(
embedding=vector, k=1, filter={"context": "none"}
)
assert len(documents) == 0
documents = vectorstore.max_marginal_relevance_search_by_vector(
embedding=vector, k=1, filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
documents = vectorstore.similarity_search_by_vector(embedding=vector, k=1)
assert len(documents) == 1
documents = vectorstore.similarity_search_by_vector(embedding=vector, k=2)
assert len(documents) == 2
documents = vectorstore.similarity_search_by_vector(
embedding=vector, k=1, filter={"context": "none"}
)
assert len(documents) == 0
documents = vectorstore.similarity_search_by_vector(
embedding=vector, k=1, filter={"context": "homepage"}
)
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
# Use Retriever
retriever = vectorstore.as_retriever(
search_kwargs={"filter": {"context": "homepage"}}
)
documents = retriever.get_relevant_documents("RAGStack")
assert len(documents) == 1
verify_document(
documents[0],
"RAGStack is a framework to run LangChain in production",
{"id": "http://mywebsite/intro", "source": "website", "context": "homepage"},
)
retriever = vectorstore.as_retriever()
documents = retriever.get_relevant_documents("RAGStack")
assert len(documents) == 2
documents = retriever.invoke("RAGStack", RunnableConfig(tags=["custom_retriever"]))
assert len(documents) == 2
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
documents = retriever.get_relevant_documents("RAGStack")
assert len(documents) == 1
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
documents = retriever.get_relevant_documents("RAGStack")
assert len(documents) == 2
# delete all the documents
vectorstore.delete(document_ids)
documents = vectorstore.search("RAGStack", "similarity")
assert len(documents) == 0
@pytest.mark.skip()
def test_stress_astra():
handler = AstraDBVectorStoreHandler(VectorStoreImplementation.ASTRADB)
while True:
context = handler.before_test()
logging.info("mocking test")
vstore = context.new_langchain_vector_store(embedding=MockEmbeddings())
vstore.add_texts(["hello world, im a document"])
result = vstore.search("hello", search_type="similarity")
print(str(result))
logging.info("test finished")
handler.after_test()
class MockEmbeddings(Embeddings):
def __init__(self):
self.embedded_documents = None
self.embedded_query = None
@staticmethod
def mock_embedding(text: str):
return [len(text) / 2, len(text) / 5, len(text) / 10]
def embed_documents(self, texts: List[str]) -> List[List[float]]:
self.embedded_documents = texts
return [self.mock_embedding(text) for text in texts]
def embed_query(self, text: str) -> List[float]:
self.embedded_query = text
return self.mock_embedding(text)
@pytest.fixture()
def vectorstore() -> AstraDBVectorStore:
if not is_astra:
skip_test_due_to_implementation_not_supported("astradb")
handler = AstraDBVectorStoreHandler(VectorStoreImplementation.ASTRADB)
context = handler.before_test()
vector_db = context.new_langchain_vector_store(embedding=MockEmbeddings())
yield vector_db
handler.after_test()