forked from microsoft/agent-governance-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
77 lines (62 loc) · 2.4 KB
/
memory.py
File metadata and controls
77 lines (62 loc) · 2.4 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""
Simple Episodic Memory for Chat Agent
This is a simplified version for the example.
For production, use the full EMK module.
"""
from typing import List, Dict, Optional
from collections import defaultdict
from datetime import datetime, timezone
class EpisodicMemory:
"""Simple conversation memory store."""
def __init__(self, max_turns: int = 50, summarize_after: int = 20):
"""
Initialize memory.
Args:
max_turns: Maximum turns to store
summarize_after: Summarize after this many turns
"""
self.max_turns = max_turns
self.summarize_after = summarize_after
self._conversations: Dict[str, List[Dict]] = defaultdict(list)
self._summaries: Dict[str, str] = {}
def add_turn(
self,
conversation_id: str,
user_message: str,
assistant_message: str
) -> None:
"""Add a conversation turn."""
turn = {
"user": user_message,
"assistant": assistant_message,
"timestamp": datetime.now(timezone.utc).isoformat()
}
self._conversations[conversation_id].append(turn)
# Trim if too long
if len(self._conversations[conversation_id]) > self.max_turns:
self._conversations[conversation_id] = \
self._conversations[conversation_id][-self.max_turns:]
def get_history(
self,
conversation_id: str,
limit: Optional[int] = None
) -> List[Dict]:
"""Get conversation history."""
history = self._conversations.get(conversation_id, [])
if limit:
return history[-limit:]
return history
def clear(self, conversation_id: str) -> None:
"""Clear conversation history."""
if conversation_id in self._conversations:
del self._conversations[conversation_id]
if conversation_id in self._summaries:
del self._summaries[conversation_id]
def get_summary(self, conversation_id: str) -> Optional[str]:
"""Get conversation summary if available."""
return self._summaries.get(conversation_id)
def set_summary(self, conversation_id: str, summary: str) -> None:
"""Set conversation summary."""
self._summaries[conversation_id] = summary