ragit 0.4.5

git-like rag pipeline
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import json
import os
import requests
import subprocess
import time
from typing import Optional, Tuple
from utils import (
    cargo_run,
    goto_root,
    mk_and_cd_tmp_dir,
    read_string,
    write_string,
)

def server():
    goto_root()
    server_process = None

    try:
        server_process = spawn_ragit_server()
        mk_and_cd_tmp_dir()

        # step 1: we'll do experiments with these repos
        cargo_run(["clone", "https://ragit.baehyunsol.com/sample/rustc", "sample-rustc"])

        os.mkdir("sample-empty")
        os.chdir("sample-empty")
        cargo_run(["init"])
        os.chdir("..")

        os.mkdir("sample-small")
        os.chdir("sample-small")
        cargo_run(["init"])
        cargo_run(["config", "--set", "model", "dummy"])
        write_string("sample.md", "Hello, World!")
        cargo_run(["add", "sample.md"])
        cargo_run(["build"])
        os.chdir("..")
        create_user(id="test-user", password="12345678")
        api_key = get_api_key(id="test-user", password="12345678")

        # step 2: test loop
        for index, repo in enumerate(["sample-empty", "sample-small", "sample-rustc"]):
            os.chdir(repo)
            sample_chunk_uids = [chunk["uid"] for chunk in json.loads(cargo_run(["ls-chunks", "--json"], stdout=True))[:5]]
            sample_file_uids = [file["uid"] for file in json.loads(cargo_run(["ls-files", "--json"], stdout=True)) if file["chunks"] > 1][:5]

            cargo_run(["meta", "--set", "whatever-key", "whatever-value"])
            create_repo(user="test-user", repo=repo, api_key=api_key, public_read=True, public_write=True)
            cargo_run(["uid"])  # `index.json` has `uid` field. in order to `assert_eq_json`, we have to init uid of the local index
            cargo_run(["push", "--configs", "--prompts", f"--remote=http://127.0.0.1:41127/test-user/{repo}/"])
            index_json = get_json(url="index", repo=repo)

            # running `rag init` in local initializes an ii, but not in server.
            # That's intentional: local knowledge-base is usually for querying,
            # and server is usually for storing. So building an ii for server
            # is waste of time.
            assert_eq_json("index.json", index_json, ignore_keys=["ii_status"])

            for config in ["build", "api", "query"]:
                config_json = get_json(url=f"config/{config}", repo=repo)
                assert_eq_json(os.path.join("configs", f"{config}.json"), config_json)

            for prompt in os.listdir(os.path.join(".ragit", "prompts")):
                prompt_name = os.path.basename(prompt)
                prompt_pdl = request_text(url=f"prompt/{prompt_name[:-4]}", repo=repo)
                assert_eq_text(os.path.join("prompts", prompt_name), prompt_pdl)

            chunk_count = get_json(url="chunk-count", repo=repo)
            assert chunk_count == len(json.loads(cargo_run(["ls-chunks", "--json"], stdout=True)))

            all_chunk_uids = get_json(url="chunk-list", repo=repo)
            all_chunk_uids_local = json.loads(cargo_run(["ls-chunks", "--json", "--uid-only", "--abbrev=64"], stdout=True))
            assert set(all_chunk_uids) == set(all_chunk_uids_local)

            all_image_uids = json.loads(cargo_run(["ls-images", "--json", "--uid-only", "--abbrev=64"], stdout=True))

            for chunk_uid in all_chunk_uids:
                chunk = get_json(url = f"chunk/{chunk_uid}", repo = repo)
                chunk_local = json.loads(cargo_run(["ls-chunks", "--json", chunk_uid], stdout=True))[0]

                # due to prettifier, the json values are a bit different
                assert compare_chunk_data(chunk["data"], chunk_local["data"])

            for image_uid in all_image_uids:
                image = request_bytes(url=f"image/{image_uid}", repo=repo)
                assert_eq_bytes(os.path.join("images", image_uid[:2], f"{image_uid[2:]}.png"), image)
                image_desc = get_json(url=f"image-desc/{image_uid}", repo=repo)
                image_desc_local = json.loads(cargo_run(["ls-images", "--json", image_uid], stdout=True))[0]

                assert image_desc["explanation"] == image_desc_local["explanation"]
                assert image_desc["extracted_text"] == image_desc_local["extracted_text"]

            for prefix in range(256):
                prefix = f"{prefix:02x}"

                if any([uid.startswith(prefix) for uid in all_chunk_uids]):
                    uids_from_api = get_json(url=f"chunk-list/{prefix}", repo=repo)
                    uids_local = json.loads(cargo_run(["ls-chunks", "--json", "--uid-only", prefix, "--abbrev=64"], stdout=True))
                    assert set(uids_from_api) == set(uids_local)

                else:
                    assert get_json(url=f"chunk-list/{prefix}", repo=repo) == []

                if any([uid.startswith(prefix) for uid in all_image_uids]):
                    uids_from_api = get_json(url=f"image-list/{prefix}", repo=repo)
                    uids_local = json.loads(cargo_run(["ls-images", "--json", "--uid-only", prefix, "--abbrev=64"], stdout=True))
                    assert set(uids_from_api) == set(uids_local)

                else:
                    assert get_json(url=f"image-list/{prefix}", repo=repo) == []

            meta_api = get_json(url="meta", repo=repo)
            assert_eq_json("meta.json", meta_api)

            meta_by_key_api = get_json(url="meta/whatever-key", repo=repo)
            assert meta_by_key_api == "whatever-value"

            meta_by_key_api = get_json(url="meta/invalid-key", repo=repo)
            assert meta_by_key_api is None

            version_api = request_text(url="version", repo=repo).strip()
            version_local = json.loads(read_string(os.path.join(".ragit", "index.json")))["ragit_version"]
            assert version_api == version_local

            version_api = request_text(url="http://127.0.0.1:41127/version", repo=repo, raw_url=True).strip()
            version_local = cargo_run(["version"], stdout=True).strip()
            assert version_api in version_local

            user_list = get_json(url="http://127.0.0.1:41127/user-list", repo=repo, raw_url=True)
            assert len(user_list) == 1 and user_list[0]["id"] == "test-user"

            repo_list = get_json(url="http://127.0.0.1:41127/repo-list/test-user", repo=repo, raw_url=True)
            assert len(repo_list) == index + 1 and any([r["name"] == repo for r in repo_list]) and all([r["owner"] == "test-user" for r in repo_list])

            for sample_chunk_uid in sample_chunk_uids:
                cat_file_chunk_api = request_text(url=f"cat-file/{sample_chunk_uid}", repo=repo).strip()
                cat_file_chunk_local = cargo_run(["cat-file", sample_chunk_uid], stdout=True).strip()
                get_content_api = get_json(url=f"content/{sample_chunk_uid}", repo=repo)
                get_content_local = json.loads(cargo_run(["cat-file", "--json", sample_chunk_uid], stdout=True))
                assert cat_file_chunk_api == cat_file_chunk_local
                assert get_content_api == get_content_local

            for sample_file_uid in sample_file_uids:
                cat_file_file_api = request_text(url=f"cat-file/{sample_file_uid}", repo=repo).strip()
                cat_file_file_local = cargo_run(["cat-file", sample_file_uid], stdout=True).strip()
                get_content_api = get_json(url=f"content/{sample_file_uid}", repo=repo)
                get_content_local = json.loads(cargo_run(["cat-file", "--json", sample_file_uid], stdout=True))
                assert cat_file_file_api == cat_file_file_local
                assert get_content_api == get_content_local

            os.chdir("..")

        # step 3: really empty knowledge-base: only create a knowledge-base with the API, and no pushes
        repo = "really-empty"
        create_repo(user="test-user", repo=repo, api_key=api_key, public_read=True, public_write=True)

        # when you `create_repo`, the server creates a dummy knowledge-base even if you don't push anything
        index_json = get_json(url="index", repo=repo)  # a dummy `index.json`
        chunks = get_json(url="chunk-list", repo=repo)
        assert chunks == []

    finally:
        if server_process is not None:
            server_process.kill()

# 0. truncate all the data in the server
# 1. spawns a ragithub-backend process
# 2. waits until the server becomes healthy
# 3. go to root and return the server process
def spawn_ragit_server(
    truncate: bool = True,
):
    goto_root()

    if health_check():
        raise Exception("ragithub-backend is already running. Please run this test in an isolated environment.")

    os.chdir("ragithub/backend")

    if truncate:
        subprocess.Popen(["cargo", "run", "--release", "--", "truncate-all", "--force", "--repo-data", "./data", "--blob-data", "./blobs"]).wait()

    server_process = subprocess.Popen(["cargo", "run", "--release", "--features=log_sql", "--", "run", "--force-default-config"])

    for _ in range(300):
        if health_check():
            break

        print("waiting for ragithub-backend to start...")
        time.sleep(1)

    else:
        raise Exception("failed to run `ragithub-backend`")

    os.chdir("../..")
    return server_process

def create_user(
    id: str,
    password: str = "12345678",
    email: str = "sample@email.com",
    readme: Optional[str] = None,
    public: bool = True,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
):
    body = {
        "id": id,
        "password": password,
        "email": email,
        "readme": readme,
        "public": public,
    }
    post_json(
        url="http://127.0.0.1:41127/user-list",
        body=body,
        raw_url=True,
        api_key=api_key,
        expected_status_code=expected_status_code,
    )

def create_repo(
    user: str,
    repo: str,
    api_key: Optional[str] = None,
    description: Optional[str] = None,
    website: Optional[str] = None,
    tags: Optional[list[str]] = None,
    public_read: bool = True,
    public_write: bool = True,
    public_clone: bool = True,
    public_push: bool = True,
    public_chat: bool = True,
    expected_status_code: Optional[int] = 200,
) -> Optional[int]:  # returns repo_id, if successful
    body = {
        "name": repo,
        "description": description,
        "website": website,
        "tags": tags or [],
        "public_read": public_read,
        "public_write": public_write,
        "public_clone": public_clone,
        "public_push": public_push,
        "public_chat": public_chat,
    }
    headers = {} if api_key is None else { "x-api-key": api_key }
    response = requests.post(f"http://127.0.0.1:41127/repo-list/{user}", json=body, headers=headers)

    if expected_status_code is not None:
        assert response.status_code == expected_status_code

    if expected_status_code == 200:
        return response.json()

def get_repo_stat(
    user: str,
    repo: str,
    key: str = "all",
    expected_status_code: Optional[int] = 200,
) -> Tuple[int, int]:  # returns (push, clone)
    response = requests.get(f"http://127.0.0.1:41127/{user}/{repo}/traffic")

    if expected_status_code is not None:
        assert response.status_code == expected_status_code

    if expected_status_code == 200:
        response = response.json()
        return (response[key]["push"], response[key]["clone"])

    else:
        return (0, 0)

def get_api_key(
    id: str,
    password: str = "12345678",
    name: str = "login_token",
) -> str:
    body = {
        "name": name,
        "expire_after": 14,
        "password": password,
    }
    response = requests.post(f"http://127.0.0.1:41127/user-list/{id}/api-key-list", json=body)
    assert response.status_code == 200
    return response.text

def rest_api(
    method: str,  # GET | POST | PUT | DELETE
    url: str,
    body = None,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
    parse_response: bool = False,
    query: Optional[dict[str, str]] = None,
):
    kwargs = {}
    headers = {} if api_key is None else { "x-api-key": api_key }

    if len(headers) > 0:
        kwargs["headers"] = headers

    if body is not None:
        kwargs["json"] = body

    url = os.path.join(f"http://127.0.0.1:41127/{user}/{repo}", url) if not raw_url else url

    if query:
        query_str = "&".join([f"{key}={value}" for key, value in query.items()])
        url += f"?{query_str}"

    response = requests.request(
        method,
        url,
        **kwargs,
    )

    if expected_status_code is not None:
        assert response.status_code == expected_status_code

    if parse_response and expected_status_code == 200:
        return json.loads(response.text)

def get_json(
    url: str,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
    parse_response: bool = True,
    query: Optional[dict[str, str]] = None,
):
    return rest_api(
        method="GET",
        url=url,
        body=None,
        user=user,
        repo=repo,
        raw_url=raw_url,
        api_key=api_key,
        expected_status_code=expected_status_code,
        parse_response=parse_response,
        query=query,
    )

def post_json(
    url: str,
    body,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
    parse_response: bool = False,
    query: Optional[dict[str, str]] = None,
):
    return rest_api(
        method="POST",
        url=url,
        body=body,
        user=user,
        repo=repo,
        raw_url=raw_url,
        api_key=api_key,
        expected_status_code=expected_status_code,
        parse_response=parse_response,
        query=query,
    )

def put_json(
    url: str,
    body,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
    parse_response: bool = False,
    query: Optional[dict[str, str]] = None,
):
    return rest_api(
        method="PUT",
        url=url,
        body=body,
        user=user,
        repo=repo,
        raw_url=raw_url,
        api_key=api_key,
        expected_status_code=expected_status_code,
        parse_response=parse_response,
        query=query,
    )

def delete_json(
    url: str,
    body = None,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    api_key: Optional[str] = None,
    expected_status_code: Optional[int] = 200,
    parse_response: bool = False,
    query: Optional[dict[str, str]] = None,
):
    return rest_api(
        method="DELETE",
        url=url,
        body=body,
        user=user,
        repo=repo,
        raw_url=raw_url,
        api_key=api_key,
        expected_status_code=expected_status_code,
        parse_response=parse_response,
        query=query,
    )

def assert_eq_json(path: str, value, ignore_keys: Optional[list[str]] = None):
    file = json.loads(read_string(os.path.join(".ragit", path)))

    if ignore_keys is not None:
        for key in ignore_keys:
            file.pop(key)
            value.pop(key)

    if file != value:
        raise ValueError(f"{file.__repr__()} != {value.__repr__()}")

def request_text(
    url: str,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    expected_status_code: Optional[int] = 200,
) -> str:
    response = requests.get(os.path.join(f"http://127.0.0.1:41127/{user}/{repo}", url) if not raw_url else url)

    if expected_status_code is not None:
        assert response.status_code == expected_status_code

    if expected_status_code == 200:
        return response.text

    else:
        return ""

def assert_eq_text(path: str, value: str):
    file = read_string(os.path.join(".ragit", path))

    if file != value:
        raise ValueError(f"{file.__repr__()} != {value.__repr__()}")

def request_bytes(
    url: str,
    user: Optional[str] = "test-user",
    repo: Optional[str] = None,
    raw_url: bool = False,
    expected_status_code: Optional[int] = 200,
) -> bytes:
    response = requests.get(os.path.join(f"http://127.0.0.1:41127/{user}/{repo}", url) if not raw_url else url)

    if expected_status_code is not None:
        assert response.status_code == expected_status_code

    if expected_status_code == 200:
        return response.content

    else:
        return b""

def assert_eq_bytes(path: str, value: bytes):
    with open(os.path.join(".ragit", path), "rb") as f:
        assert f.read() == value

def health_check(port: int = 41127):
    try:
        response = requests.get(f"http://127.0.0.1:{port}/health", timeout=1)
        return response.status_code == 200

    except:
        return False

# There are too many abstractions and fragmentations :(
def compare_chunk_data(data_from_api, data_from_local) -> bool:
    return data_from_local == "".join([(data["content"] if data["type"] == "Text" else ("img_" + data["uid"])) for data in data_from_api])