import os import glob import re import json import shutil from datetime import datetime def remove_parts(file_name): # Create a backup before modifying the file backup_file_name = f"{file_name}_{datetime.now().strftime('%Y%m%d%H%M%S')}.bak" shutil.copy(file_name, backup_file_name) print(f"Created a backup: {backup_file_name}") with open(file_name, 'r', encoding='utf-8') as file: data = [json.loads(line) for line in file] # Reverse the data so we can work with the last three messages data.reverse() for i, message in enumerate(data): # Only modify messages that are not in the last three if i >= 3 and 'mes' in message: matches = list(re.finditer(r'```[\s\S]*?```', message['mes'])) for match in matches: message['mes'] = message['mes'].replace(match.group(), '') # Reverse the data back to its original order data.reverse() with open(file_name, 'w', encoding='utf-8') as file: for item in data: json.dump(item, file) file.write('\n') def find_last_modified_file(directory): files = glob.glob(directory + '/**/*.jsonl', recursive=True) if not files: return None latest_file = max(files, key=os.path.getmtime) return latest_file directory = 'C:\\Games\\SillyTavern\\public\\chats' # replace with your directory latest_file = find_last_modified_file(directory) if latest_file is not None: print(f"Modifying file: {latest_file}") remove_parts(latest_file) else: print("No .jsonl files found in the directory.")