-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent.py
More file actions
209 lines (177 loc) · 7.92 KB
/
agent.py
File metadata and controls
209 lines (177 loc) · 7.92 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Main agent that orchestrates all memory systems."""
import re
import config
from memory.working import WorkingMemory
from memory.semantic import SemanticMemory
from memory.episodic import EpisodicMemory
from memory.procedural import ProceduralMemory
from memory.consolidation import Consolidation
# Patterns for query classification (compiled once)
_PERSONAL_PATTERNS = re.compile(
r"we discussed|do you remember|you told me|i told you|i mentioned|"
r"my budget|my preference|last time|our conversation|remember when",
re.IGNORECASE,
)
_FACTUAL_PATTERNS = re.compile(
r"^(what is|what are|how many|how much|tell me about|describe|explain|define)\b",
re.IGNORECASE,
)
_BEHAVIORAL_PATTERNS = re.compile(
r"how should i|recommend|best way to|should i|what do you suggest|advice",
re.IGNORECASE,
)
class CognitiveAgent:
"""Agent with cognitive memory capabilities.
Args:
mode: "full" uses all 5 memory systems.
"semantic_only" uses only working + semantic memory (vanilla RAG baseline).
"""
def __init__(self, mode: str = "full"):
self.mode = mode
self.working = WorkingMemory()
self.semantic = SemanticMemory()
if mode == "full":
self.episodic = EpisodicMemory()
self.procedural = ProceduralMemory()
self.consolidation = Consolidation(self.episodic, self.procedural)
else:
self.episodic = None
self.procedural = None
self.consolidation = None
self.conversation_count = 0
# Ingest any documents in data/
print(f"Loading semantic memory (mode={mode})...")
self.semantic.ingest_all()
def _classify_query(self, user_input: str) -> dict:
"""Classify a query to determine which memory systems to activate.
Returns dict with keys: semantic, episodic, procedural (all bool).
"""
if _PERSONAL_PATTERNS.search(user_input):
return {"semantic": False, "episodic": True, "procedural": False}
if _FACTUAL_PATTERNS.search(user_input):
return {"semantic": True, "episodic": False, "procedural": False}
if _BEHAVIORAL_PATTERNS.search(user_input):
return {"semantic": True, "episodic": True, "procedural": True}
# Default: activate everything
return {"semantic": True, "episodic": True, "procedural": True}
def _detect_conflicts(
self, semantic_text: str, episodic_text: str, query: str
) -> str | None:
"""Check if semantic and episodic contexts contradict each other.
Makes a short LLM call. Returns a conflict description or None.
"""
response = self.working.client.messages.create(
model=config.MODEL_NAME,
max_tokens=150,
temperature=0.0,
system="You detect contradictions between information sources.",
messages=[
{
"role": "user",
"content": (
f"Query: {query}\n\n"
f"Source A (documents):\n{semantic_text}\n\n"
f"Source B (past conversations):\n{episodic_text}\n\n"
"Do these two sources contradict each other regarding the query? "
"If yes, describe the conflict in one sentence. "
"If no, respond with exactly: NONE"
),
}
],
)
result = response.content[0].text.strip()
if result.upper() == "NONE":
return None
return result
def _build_system_prompt(
self, user_input: str, routing: dict = None
) -> str:
"""Construct system prompt with episodic + procedural context.
Args:
user_input: The user's query.
routing: Output of _classify_query. If None, all systems are active.
"""
base = self.working._default_prompt()
parts = [base]
if self.mode != "full":
return "\n\n".join(parts)
if routing is None:
routing = {"semantic": True, "episodic": True, "procedural": True}
# Episodic context
if routing["episodic"]:
episodic_context = self.episodic.recall_as_context(user_input)
if episodic_context:
parts.append(
"[EPISODIC MEMORY - YOUR PAST EXPERIENCES]\n"
"These are YOUR real memories from previous conversations with this user. "
"Reference them naturally as your own experience. When the user asks about "
"past interactions, use these memories to answer accurately.\n\n"
f"{episodic_context}"
)
# Procedural rules
if routing["procedural"]:
rules = self.procedural.get_rules_text()
if rules:
parts.append(
"[PROCEDURAL MEMORY - LEARNED RULES]\n"
"These rules were learned from your accumulated experience. Follow them.\n\n"
f"{rules}"
)
return "\n\n".join(parts)
def chat(self, user_input: str) -> str:
"""Process a user message and return a response."""
# Classify the query to decide which memory systems to activate
routing = self._classify_query(user_input) if self.mode == "full" else None
# Build system prompt with gated memory context
system_prompt = self._build_system_prompt(user_input, routing=routing)
# Retrieve semantic context (if gating allows it)
extra = []
semantic_text = None
if routing is None or routing["semantic"]:
context_msg = self.semantic.recall_as_message(user_input)
if context_msg:
extra.append(context_msg)
semantic_text = context_msg.get("content", "")
# Conflict detection between semantic and episodic sources
if (
config.CONFLICT_DETECTION_ENABLED
and self.mode == "full"
and routing
and routing["semantic"]
and routing["episodic"]
and semantic_text
):
episodic_text = self.episodic.recall_as_context(user_input)
if episodic_text:
conflict = self._detect_conflicts(
semantic_text, episodic_text, user_input
)
if conflict:
system_prompt += (
"\n\n[CONFLICT NOTICE]\n"
"The following contradiction was detected between your document "
"knowledge and your past conversation memories. Address it "
f"transparently in your response.\n\n{conflict}"
)
self.working.update_system_prompt(system_prompt)
self.working.add_user_message(user_input)
response = self.working.get_response(extra_messages=extra if extra else None)
return response
def new_conversation(self):
"""Start a fresh conversation (preserves long-term memory)."""
if self.mode == "full" and self.working.get_turn_count() > 0:
conversation_text = self.working.get_conversation_text()
# Store episodic memory
print(" Saving episodic memory...")
self.episodic.store(conversation_text)
# Update procedural rules with new learnings
episodic_context = self.episodic.recall_as_context("recent learnings")
if episodic_context:
print(" Updating procedural memory...")
self.procedural.update(episodic_context)
self.conversation_count += 1
# Trigger consolidation every N conversations
if self.mode == "full" and self.conversation_count % config.CONSOLIDATION_EVERY_N == 0:
print(" Running memory consolidation (sleep phase)...")
self.consolidation.run()
self.working.reset()