Trying to properly detect message limit
This commit is contained in:
parent
799cde4a7a
commit
e80509ba9d
@ -95,19 +95,28 @@ class ReginaldCog(commands.Cog):
|
|||||||
|
|
||||||
response_text = await self.generate_response(api_key, formatted_messages)
|
response_text = await self.generate_response(api_key, formatted_messages)
|
||||||
|
|
||||||
|
# ✅ First, add the new user input and response to memory
|
||||||
|
memory.append({"user": user_name, "content": prompt})
|
||||||
|
memory.append({"user": "Reginald", "content": response_text})
|
||||||
|
|
||||||
|
# ✅ Check if pruning is needed
|
||||||
if len(memory) > self.short_term_memory_limit:
|
if len(memory) > self.short_term_memory_limit:
|
||||||
|
|
||||||
|
# 🔹 Generate a summary of the short-term memory
|
||||||
summary = await self.summarize_memory(memory)
|
summary = await self.summarize_memory(memory)
|
||||||
|
|
||||||
if channel_id not in mid_memory:
|
# 🔹 Ensure mid-term memory exists for the channel
|
||||||
mid_memory[channel_id] = []
|
mid_memory.setdefault(channel_id, [])
|
||||||
|
|
||||||
|
# 🔹 Store the new summary with timestamp and extracted topics
|
||||||
mid_memory[channel_id].append({
|
mid_memory[channel_id].append({
|
||||||
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M"),
|
||||||
"topics": self.extract_topics_from_summary(summary),
|
"topics": self.extract_topics_from_summary(summary),
|
||||||
"summary": summary
|
"summary": summary
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(mid_memory[channel_id]) > 10: # Keep only last 10 summaries
|
# 🔹 Maintain only the last 10 summaries
|
||||||
|
if len(mid_memory[channel_id]) > 10:
|
||||||
mid_memory[channel_id].pop(0)
|
mid_memory[channel_id].pop(0)
|
||||||
|
|
||||||
# ✅ Only prune short-term memory if a new summary was made
|
# ✅ Only prune short-term memory if a new summary was made
|
||||||
@ -115,9 +124,7 @@ class ReginaldCog(commands.Cog):
|
|||||||
keep_count = max(1, int(len(memory) * retention_ratio)) # Keep at least 1 message
|
keep_count = max(1, int(len(memory) * retention_ratio)) # Keep at least 1 message
|
||||||
memory = memory[-keep_count:] # Remove oldest 75%, keep recent
|
memory = memory[-keep_count:] # Remove oldest 75%, keep recent
|
||||||
|
|
||||||
memory.append({"user": user_name, "content": prompt})
|
# ✅ Store updated short-term memory back
|
||||||
memory.append({"user": "Reginald", "content": response_text})
|
|
||||||
|
|
||||||
short_memory[channel_id] = memory
|
short_memory[channel_id] = memory
|
||||||
|
|
||||||
await ctx.send(response_text[:2000])
|
await ctx.send(response_text[:2000])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user