Trying to add memory retention

This commit is contained in:
AllfatherHatt 2025-02-21 00:30:35 +01:00
parent 8c93e45c2f
commit 814102e921

View File

@ -97,24 +97,29 @@ class ReginaldCog(commands.Cog):
memory.append({"user": user_name, "content": prompt}) memory.append({"user": user_name, "content": prompt})
memory.append({"user": "Reginald", "content": response_text}) memory.append({"user": "Reginald", "content": response_text})
if len(memory) > self.short_term_memory_limit:
summary = await self.summarize_memory(memory)
if channel_id not in mid_memory: if len(memory) > self.short_term_memory_limit:
mid_memory[channel_id] = [] summary = await self.summarize_memory(memory)
mid_memory[channel_id].append({ if channel_id not in mid_memory:
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), mid_memory[channel_id] = []
"topics": self.extract_topics_from_summary(summary),
"summary": summary
})
if len(mid_memory[channel_id]) > 10: # Keep only last 10 summaries mid_memory[channel_id].append({
mid_memory[channel_id].pop(0) "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
"topics": self.extract_topics_from_summary(summary),
"summary": summary
})
memory = memory[-100:] if len(mid_memory[channel_id]) > 10: # Keep only last 10 summaries
mid_memory[channel_id].pop(0)
short_memory[channel_id] = memory # ✅ Only prune short-term memory if a new summary was made
retention_ratio = 0.25 # Keep 25% of messages for immediate continuity
keep_count = max(1, int(len(memory) * retention_ratio)) # Keep at least 1 message
memory = memory[-keep_count:] # Remove oldest 75%, keep recent
short_memory[channel_id] = memory
await ctx.send(response_text[:2000]) await ctx.send(response_text[:2000])