| 1 | from typing import Iterator |
| 2 | from graphrag_sdk.document import Document |
| 3 | |
| 4 | |
| 5 | class JSONLLoader: |
| 6 | """ |
| 7 | JSONL loader |
| 8 | """ |
| 9 | |
| 10 | def __init__(self, path: str, rows_per_document: int = 500): |
| 11 | self.path = path |
| 12 | self.rows_per_document = rows_per_document |
| 13 | |
| 14 | def load(self) -> Iterator[Document]: |
| 15 | with open(self.path, "r") as f: |
| 16 | rows = f.readlines() |
| 17 | num_rows = len(rows) |
| 18 | num_documents = num_rows // self.rows_per_document |
| 19 | for i in range(num_documents): |
| 20 | content = "\n".join( |
| 21 | rows[ |
| 22 | i |
| 23 | * self.rows_per_document : (i + 1) |
| 24 | * self.rows_per_document |
| 25 | ] |
| 26 | ) |
| 27 | yield Document(content) |