File size: 2,935 Bytes
3af75d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Shared utilities for the vLLM pipeline.
Based on patterns from example.py (lines 14-63).
"""

import json
import os
from typing import Dict, Iterator, List


def get_last_processed_id(filepath: str) -> int:
    """
    Get the highest ID that has been processed in a JSONL file.
    Used for resumability - based on example.py lines 14-31.
    """
    if not os.path.exists(filepath):
        return -1
    last_id = -1
    try:
        with open(filepath, "r", encoding="utf-8") as f:
            for line in f:
                try:
                    data = json.loads(line)
                    current_id = data.get("id")
                    if isinstance(current_id, int) and current_id > last_id:
                        last_id = current_id
                except (json.JSONDecodeError, AttributeError):
                    continue
    except Exception as e:
        print(f"Error reading progress file: {e}")
        return -1
    return last_id


def iter_jsonl_batches(
    jsonl_path: str,
    batch_size: int,
    start_from_id: int = 0,
    required_fields: List[str] | None = None,
) -> Iterator[List[Dict]]:
    """
    Reads a JSONL and yields batches starting from start_from_id.
    Based on example.py lines 34-63.
    
    Args:
        jsonl_path: Path to the JSONL file
        batch_size: Number of items per batch
        start_from_id: Skip items with id < start_from_id
        required_fields: List of field names that must be present in each item
    """
    required_fields = required_fields or []
    
    with open(jsonl_path, "r", encoding="utf-8") as f:
        batch = []
        for line in f:
            try:
                obj = json.loads(line)
            except json.JSONDecodeError:
                continue

            # Check required fields
            if not all(k in obj for k in required_fields):
                continue
            if not isinstance(obj.get("id"), int):
                continue
            if obj["id"] < start_from_id:
                continue

            batch.append(obj)
            if len(batch) == batch_size:
                yield batch
                batch = []
        if batch:
            yield batch


def write_jsonl_line(filepath: str, data: Dict) -> None:
    """Append a single JSON object as a line to a JSONL file."""
    with open(filepath, "a", encoding="utf-8") as f:
        f.write(json.dumps(data, ensure_ascii=False) + "\n")


def write_jsonl_batch(filepath: str, batch: List[Dict]) -> None:
    """Append multiple JSON objects to a JSONL file."""
    with open(filepath, "a", encoding="utf-8") as f:
        for data in batch:
            f.write(json.dumps(data, ensure_ascii=False) + "\n")


def count_jsonl_lines(filepath: str) -> int:
    """Count the number of lines in a JSONL file."""
    if not os.path.exists(filepath):
        return 0
    with open(filepath, "r", encoding="utf-8") as f:
        return sum(1 for _ in f)