forked from Doriandarko/omni-engineer
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjson_chat_log_reformat.py
More file actions
60 lines (48 loc) · 1.83 KB
/
Copy pathjson_chat_log_reformat.py
File metadata and controls
60 lines (48 loc) · 1.83 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
import json
import argparse
import os
def parse_args():
parser = argparse.ArgumentParser(description='Reformat JSON chat log to markdown')
parser.add_argument('input_file', type=str, help='Path to JSON input file')
return parser.parse_args()
def json_to_markdown(json_data):
"""
Convert JSON data containing messages into a human-readable markdown format
"""
markdown = []
for item in json_data:
role = item.get('role', '').title()
content = item.get('content', '')
if role == "System":
markdown.append(f"## {role} Message")
else:
markdown.append(f"### {role}")
# Process content for markdown formatting
lines = content.split('\n')
for line in lines:
# Handle code blocks
if line.startswith('```'):
markdown.append('```')
elif line.strip().startswith('#'):
# Handle headers
header_level = line.strip().find('#') + 1
markdown.append(f"{'#' * header_level} {line.strip()[header_level:]}")
elif line.strip() == '':
markdown.append('') # Add empty line for spacing
else:
markdown.append(line)
markdown.append('---') # Add horizontal line between sections
return '\n\n'.join(markdown)
if __name__ == "__main__":
args = parse_args()
# Load JSON data from file
with open(args.input_file, 'r') as f:
json_data = json.load(f)
# Convert to markdown
markdown_content = json_to_markdown(json_data)
# Get output filename
base, ext = os.path.splitext(args.input_file)
output_file = f"{base}_reformatted.md"
# Save to file
with open(output_file, 'w') as f:
f.write(markdown_content)