-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_management.py
138 lines (108 loc) · 4.02 KB
/
file_management.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import readline
import json
import os
from datetime import datetime
from utils import (
display_title,
confirm_text,
filename_input,
numeric_input,
featured_input,
)
from ollama_interface import choose_model
def write_settings(model, system_message):
try:
if not "settings" in os.listdir():
os.mkdir("./settings")
with open("./settings/model", "w") as settings:
settings.writelines(model)
with open("./settings/system_message", "w") as settings:
settings.writelines(system_message)
except:
confirm_text("Failed to save settings")
def save_message_log(messages, model, system_message, deafult_save_name=""):
successfull = False
while not successfull:
if deafult_save_name == "":
deafult_save_name = f"conv_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"
print("Save conversation? Enter 'Discard' to not save.")
print(f"Default save name is {deafult_save_name}\n")
conversation_name = filename_input("Conversation name: ")
if conversation_name == "Discard":
confirm_text("Conversation not saved")
return
if conversation_name == "":
conversation_name = deafult_save_name
conversation_dir = "./conversations"
try:
if not os.path.exists(conversation_dir):
os.mkdir(conversation_dir)
conversation_log_path = os.path.join(
conversation_dir, f"{conversation_name}.json"
)
conversation_log = open(conversation_log_path, "w+")
data = {
"messages": messages,
"model": model,
"system_message": system_message["content"],
}
json.dump(data, conversation_log, indent=4)
conversation_log.close()
confirm_text("Saving the conversation was successful")
successfull = True
except:
confirm_text("Saving the conversation failed!")
def get_settings(no_interact=False):
model = ""
system_message = ""
settings_path = "./settings"
try:
if not os.path.isdir(settings_path):
if no_interact:
return "mistral", ""
print("Defaults are not set")
model = choose_model(fallback="mistral")
system_message = input("System Message: ")
write_settings(model, system_message)
return model, system_message
with open(os.path.join(settings_path, "model"), "r") as settings:
model = settings.readline().strip()
with open(os.path.join(settings_path, "system_message"), "r") as settings:
system_message = "\n".join([line.strip() for line in settings]).strip()
except FileNotFoundError:
print("Settings file not found.")
return "mistral", ""
except Exception as e:
confirm_text(f"Setting retrieval failed: {str(e)}")
return "mistral", ""
return model, system_message
def load_messages():
display_title("Load")
if not "conversations" in os.listdir():
confirm_text("Missing conversation folder")
return [], "", "", ""
list_of_conversations = os.listdir("./conversations/")
names = []
index = 0
for conversation_name in list_of_conversations:
display = conversation_name.removesuffix(".json")
if display != conversation_name:
print(f"[{index}] {display}")
names.append(display)
index += 1
print()
try:
choice = numeric_input("Choice Index: ")
loaded_conversation = json.load(open(f"./conversations/{names[choice]}.json"))
return (
loaded_conversation["messages"],
loaded_conversation["model"],
loaded_conversation["system_message"],
names[choice],
)
except:
confirm_text("Invalid Choice")
return [], "", "", ""
if __name__ == "__main__":
from utils import wrong_start_script
wrong_start_script()