-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestus.py
130 lines (102 loc) · 4.31 KB
/
testus.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
import datetime
import json
import os
from typing import Dict, Any, List
class ChatFormatter:
def __init__(self, template, role_names: Dict[str, str] = None):
self.template = template
self.role_names = role_names or {}
def format_messages(self, messages):
formatted_chat = []
for message in messages:
role = message['role']
content = message['content']
display_name = self.role_names.get(role, role.capitalize())
formatted_message = self.template.format(role=display_name, content=content)
formatted_chat.append(formatted_message)
return '\n'.join(formatted_chat)
class Message:
def __init__(self, role: str, content: str, message_id: int = None):
self.role = role
self.content = content
self.id = message_id
def to_dict(self) -> Dict[str, Any]:
return {"role": self.role, "content": self.content, "id": self.id}
class ChatHistory:
def __init__(self, history_folder: str):
self.messages: List[Message] = []
self.history_folder = history_folder
def add_message(self, message: Message):
self.messages.append(message)
def edit_message(self, message_id: int, new_content: str) -> bool:
for message in self.messages:
if message.id == message_id:
message.content = new_content
return True
return False
def to_list(self) -> List[Dict[str, Any]]:
return [message.to_dict() for message in self.messages]
def assign_message_ids(self) -> None:
"""Assign incremental IDs to messages that don't have them."""
next_id = 0
for message in self.messages:
if message.id is None:
message.id = next_id
next_id += 1
else:
next_id = max(next_id, message.id + 1)
def save_history(self):
if not os.path.exists(self.history_folder):
os.makedirs(self.history_folder)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
save_id = f"{timestamp}"
filename = f"chat_history_{save_id}.json"
with open(f"{self.history_folder}/{filename}", "w") as f:
json.dump(self.to_list(), f)
def load_history(self):
if not os.path.exists(self.history_folder):
os.makedirs(self.history_folder)
print("No chat history found. Starting with an empty history.")
self.messages = []
return
history_files = [f for f in os.listdir(self.history_folder) if
f.startswith("chat_history_") and f.endswith(".json")]
if not history_files:
print("No chat history found. Starting with an empty history.")
self.messages = []
return
# Sort history files based on the timestamp in the filename
latest_history = sorted(history_files, reverse=True)[0]
try:
with open(f"{self.history_folder}/{latest_history}", "r") as f:
loaded_history = json.load(f)
self.messages = [Message(msg['role'], msg['content'], msg.get('id')) for msg in loaded_history]
print(f"Loaded the most recent chat history: {latest_history}")
self.assign_message_ids() # Ensure all messages have IDs
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error loading chat history: {e}. Starting with an empty history.")
self.messages = []
history = ChatHistory("chat_history/new_gameClaude")
history.load_history()
messages = history.to_list()
template = "{role}: {content}\n---"
role_names = {
"assistant": "Game Master",
"user": "Player"
}
formatter = ChatFormatter(template, role_names)
formatted_chat = formatter.format_messages(messages)
# print(formatted_chat.strip())
import difflib
def generate_diff_example(old_content, new_content):
"""Generate a diff example to show the LLM."""
diff = difflib.unified_diff(
old_content.splitlines(keepends=True),
new_content.splitlines(keepends=True),
fromfile="old",
tofile="new",
lineterm="\n"
)
return ''.join(diff)
print(generate_diff_example("Hello, World!\nHello, World!\nHello, World!\n",
"Hello, World.\nHello, World.\nHello, World.\n"))