Trying to properly detect message limit

This commit is contained in:
AllfatherHatt 2025-02-21 00:57:03 +01:00
parent 799cde4a7a
commit e80509ba9d

View File

@ -95,29 +95,36 @@ class ReginaldCog(commands.Cog):
response_text = await self.generate_response(api_key, formatted_messages) response_text = await self.generate_response(api_key, formatted_messages)
if len(memory) > self.short_term_memory_limit: # ✅ First, add the new user input and response to memory
summary = await self.summarize_memory(memory) memory.append({"user": user_name, "content": prompt})
memory.append({"user": "Reginald", "content": response_text})
if channel_id not in mid_memory: # ✅ Check if pruning is needed
mid_memory[channel_id] = [] if len(memory) > self.short_term_memory_limit:
# 🔹 Generate a summary of the short-term memory
summary = await self.summarize_memory(memory)
mid_memory[channel_id].append({ # 🔹 Ensure mid-term memory exists for the channel
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), mid_memory.setdefault(channel_id, [])
"topics": self.extract_topics_from_summary(summary),
"summary": summary
})
if len(mid_memory[channel_id]) > 10: # Keep only last 10 summaries # 🔹 Store the new summary with timestamp and extracted topics
mid_memory[channel_id].pop(0) mid_memory[channel_id].append({
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
"topics": self.extract_topics_from_summary(summary),
"summary": summary
})
# ✅ Only prune short-term memory if a new summary was made # 🔹 Maintain only the last 10 summaries
retention_ratio = 0.25 # Keep 25% of messages for immediate continuity if len(mid_memory[channel_id]) > 10:
keep_count = max(1, int(len(memory) * retention_ratio)) # Keep at least 1 message mid_memory[channel_id].pop(0)
memory = memory[-keep_count:] # Remove oldest 75%, keep recent
memory.append({"user": user_name, "content": prompt}) # ✅ Only prune short-term memory if a new summary was made
memory.append({"user": "Reginald", "content": response_text}) 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
# ✅ Store updated short-term memory back
short_memory[channel_id] = memory short_memory[channel_id] = memory
await ctx.send(response_text[:2000]) await ctx.send(response_text[:2000])