Repository navigation
Expand file tree
/
Copy pathexport.py
More file actions
84 lines (56 loc) · 1.6 KB
/
Copy pathexport.py
File metadata and controls
84 lines (56 loc) · 1.6 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
82
83
84
import json
import os
from datetime import datetime
from memory import get_conversation
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
EXPORT_DIR = BASE_DIR / "exports"
EXPORT_DIR.mkdir(exist_ok=True, parents=True)
def _filename(extension):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return str(EXPORT_DIR / f"conversation_{timestamp}.{extension}")
def export_to_json():
conversation = get_conversation()
file_name = _filename("json")
with open(
file_name,
"w",
encoding="utf-8"
) as file:
json.dump(
conversation,
file,
indent=4,
ensure_ascii=False
)
return file_name
def export_to_txt():
conversation = get_conversation()
file_name = _filename("txt")
with open(
file_name,
"w",
encoding="utf-8"
) as file:
for message in conversation:
role = message["role"].upper()
content = message["content"]
file.write(f"{role}\n")
file.write(f"{content}\n")
file.write("-" * 60 + "\n")
return file_name
def export_to_markdown():
conversation = get_conversation()
file_name = _filename("md")
with open(
file_name,
"w",
encoding="utf-8"
) as file:
file.write("# Conversation Export\n\n")
for message in conversation:
role = message["role"].capitalize()
content = message["content"]
file.write(f"## {role}\n\n")
file.write(f"{content}\n\n")
return file_name