forked from Marijn-Bergman/copilot-chat-export-formatter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_chat_log.py
55 lines (43 loc) · 1.55 KB
/
format_chat_log.py
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
from datetime import datetime
import json
import sys
def format_chat_log(chat_log):
"""
Formats the chat log in markdown format.
Requests are prefixed with `> ` to format as block quotes.
The formatted chat log is returned as a string.
"""
formatted_chat_log = "# From chat.json\n\n---\n\n"
requests = chat_log['requests']
for request in requests:
request_text = request['message'].get('text')
responses = request['response']
response_text = ""
for response in responses:
response_value = response.get('value')
if response_value:
response_text += response_value + "\n"
formatted_chat_log += f"> {request_text}\n\n {response_text}\n\n---\n"
return formatted_chat_log
def main():
# Path to the JSON file
if len(sys.argv) == 2:
file_path = sys.argv[1]
else:
file_path = "chat.json"
# Path to the output markdown file (newly created)
output_file_path = (
f'formatted_chat_log-{datetime.now().strftime("%Y%m%d_%H%M%S")}.md'
)
# Reading the JSON file
with open(file_path, 'r') as file:
raw_data = file.read()
# Parsing the JSON data
chat_log = json.loads(raw_data)
# Formatting the chat log
formatted_chat_log = format_chat_log(chat_log)
# Creating and saving the formatted chat log to a new text file
with open(output_file_path, 'w') as file:
file.write(formatted_chat_log)
if __name__ == '__main__':
main()