initial commit
This commit is contained in:
61
utils/conversation.py
Normal file
61
utils/conversation.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import pickle
|
||||
|
||||
|
||||
def load_conversation(model_id):
|
||||
folder = 'conversations/' + model_id.replace('/', '_')
|
||||
mkdir('../conversations')
|
||||
mkdir(folder)
|
||||
|
||||
files = os.listdir(folder)
|
||||
files = [file for file in files if file.endswith(".pickle") and os.path.isfile(folder + '/' + file)]
|
||||
files.sort(reverse=True)
|
||||
if len(files) > 0:
|
||||
pickle_filename = folder + '/' + files[0]
|
||||
print('Loading last conversation from ' + pickle_filename)
|
||||
with open(pickle_filename, 'rb') as file:
|
||||
return pickle.load(file)
|
||||
return []
|
||||
|
||||
|
||||
def save_conversation(model_id, messages):
|
||||
folder = 'conversations/' + model_id.replace('/', '_')
|
||||
mkdir('../conversations')
|
||||
mkdir(folder)
|
||||
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
|
||||
pickle_filename = folder + '/' + timestamp + '.pickle'
|
||||
with open(pickle_filename, 'wb') as file:
|
||||
pickle.dump(messages, file)
|
||||
|
||||
|
||||
def load_conversation_json(model_id):
|
||||
folder = 'conversations/' + model_id.replace('/', '_')
|
||||
mkdir('../conversations')
|
||||
mkdir(folder)
|
||||
|
||||
files = os.listdir(folder)
|
||||
files = [file for file in files if file.endswith(".json") and os.path.isfile(folder + '/' + file)]
|
||||
files.sort(reverse=True)
|
||||
if len(files) > 0:
|
||||
pickle_filename = folder + '/' + files[0]
|
||||
print('Loading last conversation from ' + pickle_filename)
|
||||
with open(pickle_filename, 'r') as file:
|
||||
return json.load(file)
|
||||
return []
|
||||
|
||||
|
||||
def save_conversation_json(model_id, messages):
|
||||
folder = 'conversations/' + model_id.replace('/', '_')
|
||||
mkdir('../conversations')
|
||||
mkdir(folder)
|
||||
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S')
|
||||
pickle_filename = folder + '/' + timestamp + '.json'
|
||||
with open(pickle_filename, 'w') as file:
|
||||
json.dump(messages, file)
|
||||
|
||||
|
||||
def mkdir(path):
|
||||
if not os.path.isdir(path):
|
||||
os.mkdir(path)
|
||||
Reference in New Issue
Block a user