-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcustom_jsonl_parser.py
More file actions
81 lines (63 loc) · 2.68 KB
/
custom_jsonl_parser.py
File metadata and controls
81 lines (63 loc) · 2.68 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
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations
import json
from pathlib import Path
from typing import List, Optional, Union
from openviking.parse.base import ParseResult
from openviking.parse.parsers.base_parser import BaseParser
from openviking.parse.parsers.markdown import MarkdownParser
from openviking_cli.utils.config.parser_config import ParserConfig
from openviking_cli.utils.logger import get_logger
logger = get_logger(__name__)
class MyCustomJsonlParser(BaseParser):
"""Example JSONL parser for records with `title` and `content` fields."""
def __init__(
self,
config: Optional[ParserConfig] = None,
):
self.config = config or ParserConfig()
self._md_parser = MarkdownParser(config=self.config)
logger.info("MyCustomJsonlParser initialized")
@property
def supported_extensions(self) -> List[str]:
return [".jsonl"]
async def parse(self, source: Union[str, Path], instruction: str = "", **kwargs) -> ParseResult:
path = Path(source)
if path.exists():
content = self._read_file(path)
return await self.parse_content(
content,
source_path=str(path),
instruction=instruction,
**kwargs,
)
return await self.parse_content(str(source), instruction=instruction, **kwargs)
async def parse_content(
self, content: str, source_path: Optional[str] = None, instruction: str = "", **kwargs
) -> ParseResult:
markdown_content = self._jsonl_to_markdown(content)
result = await self._md_parser.parse_content(
markdown_content,
source_path=source_path,
instruction=instruction,
**kwargs,
)
result.source_format = "jsonl"
result.parser_name = "MyCustomJsonlParser"
return result
def _jsonl_to_markdown(self, content: str) -> str:
sections = []
for line_number, raw_line in enumerate(content.splitlines(), start=1):
line = raw_line.strip()
if not line:
continue
try:
record = json.loads(line)
except json.JSONDecodeError as exc:
raise ValueError(f"Invalid JSON on line {line_number}") from exc
if not isinstance(record, dict):
raise ValueError(f"Line {line_number} must be a JSON object")
if "title" not in record or "content" not in record:
raise ValueError(f"Line {line_number} must contain 'title' and 'content'")
sections.append(f"# {record['title']}\n\n{record['content']}")
return "\n\n".join(sections)
__all__ = ["MyCustomJsonlParser"]