aboutsummaryrefslogtreecommitdiff
path: root/R2R/tests/test_end_to_end.py
blob: 5e13ab5ca364cbda7d0369b6be69ebf6cee65358 (about) (plain)
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
import asyncio
import os
import uuid

import pytest
from fastapi.datastructures import UploadFile

from r2r import (
    Document,
    KVLoggingSingleton,
    R2RConfig,
    R2REngine,
    R2RPipeFactory,
    R2RPipelineFactory,
    R2RProviderFactory,
    VectorSearchSettings,
    generate_id_from_label,
)
from r2r.base.abstractions.llm import GenerationConfig


@pytest.fixture(scope="session", autouse=True)
def event_loop_policy():
    asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())


@pytest.fixture(scope="function")
def event_loop():
    loop = asyncio.get_event_loop_policy().new_event_loop()
    yield loop
    loop.close()
    asyncio.set_event_loop(None)


@pytest.fixture(scope="session", autouse=True)
async def cleanup_tasks():
    yield
    for task in asyncio.all_tasks():
        if task is not asyncio.current_task():
            task.cancel()
            try:
                await task
            except asyncio.CancelledError:
                pass


@pytest.fixture(scope="function")
def app(request):
    config = R2RConfig.from_json()
    config.logging.provider = "local"
    config.logging.logging_path = uuid.uuid4().hex

    vector_db_provider = request.param
    if vector_db_provider == "pgvector":
        config.vector_database.provider = "pgvector"
        config.vector_database.extra_fields["vecs_collection"] = (
            config.logging.logging_path
        )
    try:
        providers = R2RProviderFactory(config).create_providers()
        pipes = R2RPipeFactory(config, providers).create_pipes()
        pipelines = R2RPipelineFactory(config, pipes).create_pipelines()

        r2r = R2REngine(
            config=config,
            providers=providers,
            pipelines=pipelines,
        )

        try:
            KVLoggingSingleton.configure(config.logging)
        except:
            KVLoggingSingleton._config.logging_path = (
                config.logging.logging_path
            )

        yield r2r
    finally:
        if os.path.exists(config.logging.logging_path):
            os.remove(config.logging.logging_path)


@pytest.fixture
def logging_connection():
    return KVLoggingSingleton()


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_ingest_txt_document(app, logging_connection):
    try:
        await app.aingest_documents(
            [
                Document(
                    id=generate_id_from_label("doc_1"),
                    data="The quick brown fox jumps over the lazy dog.",
                    type="txt",
                    metadata={"author": "John Doe"},
                ),
            ]
        )
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_ingest_txt_file(app, logging_connection):
    try:
        # Prepare the test data
        metadata = {"author": "John Doe"}
        files = [
            UploadFile(
                filename="test.txt",
                file=open(
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "r2r",
                        "examples",
                        "data",
                        "test.txt",
                    ),
                    "rb",
                ),
            )
        ]
        # Set file size manually
        for file in files:
            file.file.seek(0, 2)  # Move to the end of the file
            file.size = file.file.tell()  # Get the file size
            file.file.seek(0)  # Move back to the start of the file

        await app.aingest_files(metadatas=[metadata], files=files)
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_ingest_search_txt_file(app, logging_connection):
    try:
        # Prepare the test data
        metadata = {}
        files = [
            UploadFile(
                filename="aristotle.txt",
                file=open(
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "r2r",
                        "examples",
                        "data",
                        "aristotle.txt",
                    ),
                    "rb",
                ),
            ),
        ]

        # Set file size manually
        for file in files:
            file.file.seek(0, 2)  # Move to the end of the file
            file.size = file.file.tell()  # Get the file size
            file.file.seek(0)  # Move back to the start of the file

        await app.aingest_files(metadatas=[metadata], files=files)

        search_results = await app.asearch("who was aristotle?")
        assert len(search_results["vector_search_results"]) == 10
        assert (
            "was an Ancient Greek philosopher and polymath"
            in search_results["vector_search_results"][0]["metadata"]["text"]
        )

        search_results = await app.asearch(
            "who was aristotle?",
            vector_search_settings=VectorSearchSettings(search_limit=20),
        )
        assert len(search_results["vector_search_results"]) == 20
        assert (
            "was an Ancient Greek philosopher and polymath"
            in search_results["vector_search_results"][0]["metadata"]["text"]
        )
        run_info = await logging_connection.get_run_info(
            log_type_filter="search"
        )

        assert len(run_info) == 2, f"Expected 2 runs, but got {len(run_info)}"

        logs = await logging_connection.get_logs(
            [run.run_id for run in run_info], 100
        )
        assert len(logs) == 6, f"Expected 6 logs, but got {len(logs)}"

        ## test stream
        response = await app.arag(
            query="Who was aristotle?",
            rag_generation_config=GenerationConfig(
                **{"model": "gpt-3.5-turbo", "stream": True}
            ),
        )
        collector = ""
        async for chunk in response:
            collector += chunk
        assert "Aristotle" in collector
        assert "Greek" in collector
        assert "philosopher" in collector
        assert "polymath" in collector
        assert "Ancient" in collector
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_ingest_search_then_delete(app, logging_connection):
    try:
        # Ingest a document
        await app.aingest_documents(
            [
                Document(
                    id=generate_id_from_label("doc_1"),
                    data="The quick brown fox jumps over the lazy dog.",
                    type="txt",
                    metadata={"author": "John Doe"},
                ),
            ]
        )

        # Search for the document
        search_results = await app.asearch("who was aristotle?")

        # Verify that the search results are not empty
        assert (
            len(search_results["vector_search_results"]) > 0
        ), "Expected search results, but got none"
        assert (
            search_results["vector_search_results"][0]["metadata"]["text"]
            == "The quick brown fox jumps over the lazy dog."
        )

        # Delete the document
        delete_result = await app.adelete(["author"], ["John Doe"])

        # Verify the deletion was successful
        expected_deletion_message = "deleted successfully"
        assert (
            expected_deletion_message in delete_result
        ), f"Expected successful deletion message, but got {delete_result}"

        # Search for the document again
        search_results_2 = await app.asearch("who was aristotle?")

        # Verify that the search results are empty
        assert (
            len(search_results_2["vector_search_results"]) == 0
        ), f"Expected no search results, but got {search_results_2['results']}"
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["local", "pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_ingest_user_documents(app, logging_connection):
    try:
        user_id_0 = generate_id_from_label("user_0")
        user_id_1 = generate_id_from_label("user_1")

        try:
            await app.aingest_documents(
                [
                    Document(
                        id=generate_id_from_label("doc_01"),
                        data="The quick brown fox jumps over the lazy dog.",
                        type="txt",
                        metadata={"author": "John Doe", "user_id": user_id_0},
                    ),
                    Document(
                        id=generate_id_from_label("doc_11"),
                        data="The lazy dog jumps over the quick brown fox.",
                        type="txt",
                        metadata={"author": "John Doe", "user_id": user_id_1},
                    ),
                ]
            )
            user_id_results = await app.ausers_overview([user_id_0, user_id_1])
            assert set([stats.user_id for stats in user_id_results]) == set(
                [user_id_0, user_id_1]
            ), f"Expected user ids {user_id_0} and {user_id_1}, but got {user_id_results}"

            user_0_docs = await app.adocuments_overview(user_ids=[user_id_0])
            user_1_docs = await app.adocuments_overview(user_ids=[user_id_1])

            assert (
                len(user_0_docs) == 1
            ), f"Expected 1 document for user {user_id_0}, but got {len(user_0_docs)}"
            assert (
                len(user_1_docs) == 1
            ), f"Expected 1 document for user {user_id_1}, but got {len(user_1_docs)}"
            assert user_0_docs[0].document_id == generate_id_from_label(
                "doc_01"
            ), f"Expected document id {str(generate_id_from_label('doc_0'))} for user {user_id_0}, but got {user_0_docs[0].document_id}"
            assert user_1_docs[0].document_id == generate_id_from_label(
                "doc_11"
            ), f"Expected document id {str(generate_id_from_label('doc_1'))} for user {user_id_1}, but got {user_1_docs[0].document_id}"
        finally:
            await app.adelete(
                ["document_id", "document_id"],
                [
                    str(generate_id_from_label("doc_01")),
                    str(generate_id_from_label("doc_11")),
                ],
            )
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_delete_by_id(app, logging_connection):
    try:
        await app.aingest_documents(
            [
                Document(
                    id=generate_id_from_label("doc_1"),
                    data="The quick brown fox jumps over the lazy dog.",
                    type="txt",
                    metadata={"author": "John Doe"},
                ),
            ]
        )
        search_results = await app.asearch("who was aristotle?")

        assert len(search_results["vector_search_results"]) > 0
        await app.adelete(
            ["document_id"], [str(generate_id_from_label("doc_1"))]
        )
        search_results = await app.asearch("who was aristotle?")
        assert len(search_results["vector_search_results"]) == 0
    except asyncio.CancelledError:
        pass


@pytest.mark.parametrize("app", ["pgvector"], indirect=True)
@pytest.mark.asyncio
async def test_double_ingest(app, logging_connection):
    try:
        await app.aingest_documents(
            [
                Document(
                    id=generate_id_from_label("doc_1"),
                    data="The quick brown fox jumps over the lazy dog.",
                    type="txt",
                    metadata={"author": "John Doe"},
                ),
            ]
        )
        search_results = await app.asearch("who was aristotle?")

        assert len(search_results["vector_search_results"]) == 1
        with pytest.raises(Exception):
            await app.aingest_documents(
                [
                    Document(
                        id=generate_id_from_label("doc_1"),
                        data="The quick brown fox jumps over the lazy dog.",
                        type="txt",
                        metadata={"author": "John Doe"},
                    ),
                ]
            )
    except asyncio.CancelledError:
        pass