-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_loader.py
More file actions
69 lines (57 loc) · 2.19 KB
/
Copy pathdocument_loader.py
File metadata and controls
69 lines (57 loc) · 2.19 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
"""Read bounded sample-prefixed documents into ingestion-neutral records."""
from __future__ import annotations
import argparse
import asyncio
import os
from collections.abc import Sequence
from typing import Any
from pymongo import AsyncMongoClient
try:
from samples.ingestion_helpers import MongoDBDocumentLoader
except ModuleNotFoundError as exc:
if exc.name != "samples":
raise
from ingestion_helpers import MongoDBDocumentLoader
def required(name: str) -> str:
value = os.getenv(name, "").strip()
if not value:
raise RuntimeError(f"Set {name} before running the document loader.")
return value
def bounded_integer(value: str) -> int:
try:
parsed = int(value)
except ValueError as exc:
raise argparse.ArgumentTypeError("must be an integer from 1 through 1000") from exc
if not 1 <= parsed <= 1000:
raise argparse.ArgumentTypeError("must be an integer from 1 through 1000")
return parsed
async def main(argv: Sequence[str] | None = None) -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--page-size", type=bounded_integer, default=100)
parser.add_argument("--max-documents", type=bounded_integer, default=10)
args = parser.parse_args(argv)
connection_string = required("MONGODB_URI")
database_name = required("MONGODB_DATABASE")
collection_name = required("MONGODB_INGESTION_SOURCE_COLLECTION")
sample_prefix = required("MONGODB_RAG_SAMPLE_PREFIX")
client: AsyncMongoClient[dict[str, Any]] = AsyncMongoClient(connection_string)
loader = MongoDBDocumentLoader(
client[database_name][collection_name],
sample_prefix=sample_prefix,
page_size=args.page_size,
)
loaded = 0
try:
async for document in loader.load():
print(
f"{document.source_id}: title={document.title!r}, "
f"tenant={document.tenant_id!r}, deleted={document.deleted}"
)
loaded += 1
if loaded == args.max_documents:
break
finally:
await client.close()
print(f"Mapped {loaded} bounded source document(s).")
if __name__ == "__main__":
asyncio.run(main())