-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_manager.py
More file actions
147 lines (130 loc) · 5.81 KB
/
memory_manager.py
File metadata and controls
147 lines (130 loc) · 5.81 KB
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
139
140
141
142
143
144
145
146
147
import os
import json
import pickle
from datetime import datetime
from typing import List, Dict, Any
from langchain.memory import ConversationBufferMemory
import config
class MemoryManager:
def __init__(self):
self.memory_path = config.MEMORY_PATH
self.conversation_memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
self.vector_store = None
self._load_memory()
def _load_memory(self):
"""Load existing memory from disk"""
try:
# Load conversation memory
memory_file = os.path.join(self.memory_path, "conversation_memory.json")
if os.path.exists(memory_file):
with open(memory_file, 'r') as f:
memory_data = json.load(f)
self.conversation_memory.chat_memory.messages = memory_data.get("messages", [])
# Try to initialize FAISS if OpenAI API key is available
if config.OPENAI_API_KEY and config.OPENAI_API_KEY != "your_openai_api_key_here":
try:
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.schema import Document
self.embeddings = OpenAIEmbeddings(openai_api_key=config.OPENAI_API_KEY)
# Load FAISS index
faiss_path = config.FAISS_INDEX_PATH
if os.path.exists(faiss_path):
self.vector_store = FAISS.load_local(faiss_path, self.embeddings)
else:
# Create initial document to initialize FAISS
initial_doc = Document(
page_content="Initial memory document",
metadata={
'task_id': 'initial',
'timestamp': datetime.now().isoformat(),
'task_type': 'system',
'result': 'System initialization'
}
)
self.vector_store = FAISS.from_documents([initial_doc], self.embeddings)
print("✅ FAISS vector store initialized successfully")
except Exception as e:
print(f"⚠️ Could not initialize FAISS: {e}")
self.vector_store = None
else:
print("⚠️ OpenAI API key not configured, FAISS will be disabled")
self.vector_store = None
except Exception as e:
print(f"Warning: Could not load memory: {e}")
self.vector_store = None
def save_memory(self):
"""Save memory to disk"""
try:
# Save conversation memory
memory_file = os.path.join(self.memory_path, "conversation_memory.json")
memory_data = {
"messages": [msg.dict() for msg in self.conversation_memory.chat_memory.messages],
"last_updated": datetime.now().isoformat()
}
with open(memory_file, 'w') as f:
json.dump(memory_data, f, indent=2)
# Save FAISS index
if self.vector_store:
faiss_path = config.FAISS_INDEX_PATH
self.vector_store.save_local(faiss_path)
except Exception as e:
print(f"Warning: Could not save memory: {e}")
def add_task_memory(self, task_id: str, task_data: Dict[str, Any]):
"""Add task data to memory"""
try:
# Add to conversation memory
self.conversation_memory.chat_memory.add_user_message(
f"Task {task_id}: {task_data.get('prompt', '')}"
)
# Add to vector store for semantic search if available
if self.vector_store:
from langchain.schema import Document
doc = Document(
page_content=task_data.get('prompt', ''),
metadata={
'task_id': task_id,
'timestamp': datetime.now().isoformat(),
'task_type': task_data.get('type', 'general'),
'result': task_data.get('result', '')
}
)
self.vector_store.add_documents([doc])
# Save memory
self.save_memory()
except Exception as e:
print(f"Warning: Could not add task memory: {e}")
def search_similar_tasks(self, query: str, k: int = 5) -> List[Dict]:
"""Search for similar tasks in memory"""
try:
if not self.vector_store:
return []
docs = self.vector_store.similarity_search(query, k=k)
return [
{
'content': doc.page_content,
'metadata': doc.metadata
}
for doc in docs
]
except Exception as e:
print(f"Warning: Could not search memory: {e}")
return []
def get_task_history(self, limit: int = 10) -> List[Dict]:
"""Get recent task history"""
try:
messages = self.conversation_memory.chat_memory.messages[-limit:]
return [
{
'role': msg.type,
'content': msg.content,
'timestamp': datetime.now().isoformat()
}
for msg in messages
]
except Exception as e:
print(f"Warning: Could not get task history: {e}")
return []