-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_memory_store.py
85 lines (70 loc) · 2.85 KB
/
chat_memory_store.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
# chat_memory_store.py
import json
import os
from langchain_core.messages import HumanMessage, AIMessage
from langchain_community.chat_message_histories import ChatMessageHistory
class ChatMemoryStore:
def __init__(self, store_file="chat_memory/store.json"):
self.store_file = store_file
self.store = self.load_store()
def load_store(self):
if os.path.exists(self.store_file):
with open(self.store_file, "r") as file:
data = json.load(file)
return {
k: [self.deserialize_message(msg) for msg in v]
for k, v in data.items()
}
return {}
def save_store(self):
serialized_data = {
k: [self.serialize_message(msg) for msg in v]
for k, v in self.store.items()
}
with open(self.store_file, "w") as file:
json.dump(serialized_data, file)
def get_session_history(self, session_id: str) -> ChatMessageHistory:
if session_id not in self.store:
self.store[session_id] = []
return ChatMessageHistory(messages=self.store[session_id])
def update_session_history(self, session_id: str, history: ChatMessageHistory):
self.store[session_id] = history.messages
self.save_store()
@staticmethod
def serialize_message(message):
return {"type": message.__class__.__name__, "data": message.dict()}
@staticmethod
def deserialize_message(message_data):
message_type = message_data["type"]
if message_type == "HumanMessage":
return HumanMessage(**message_data["data"])
elif message_type == "AIMessage":
return AIMessage(**message_data["data"])
else:
raise ValueError(f"Unknown message type: {message_type}")
if __name__ == "__main__":
# Test case
store_file = "chat_memory/test_store.json"
memory_store = ChatMemoryStore(store_file)
# Create test messages
human_message = HumanMessage(content="Hello, how are you?")
ai_message = AIMessage(content="I'm doing well, thank you!")
# Create a test conversation
session_id = "test_conversation"
memory_store.update_session_history(
session_id, ChatMessageHistory(messages=[human_message, ai_message])
)
# Retrieve the session history
session_history = memory_store.get_session_history(session_id)
# Print the retrieved messages
print("Retrieved messages:")
for message in session_history.messages:
print(f"Type: {message.__class__.__name__}, Content: {message.content}")
# Load the stored data from the JSON file
with open(store_file, "r") as file:
stored_data = json.load(file)
# Print the stored data
print("\nStored data:")
print(json.dumps(stored_data, indent=2))
# Clean up the test store file
#os.remove(store_file)