-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy path17_user_memories_and_summaries.py
156 lines (129 loc) · 4.79 KB
/
17_user_memories_and_summaries.py
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
"""🧠 Long Term User Memories and Session Summaries
This example shows how to create an agent with persistent memory that stores:
1. Personalized user memories - facts and preferences learned about specific users
2. Session summaries - key points and context from conversations
3. Chat history - stored in SQLite for persistence
Key features:
- Stores user-specific memories in SQLite database
- Maintains session summaries for context
- Continues conversations across sessions with memory
- References previous context and user information in responses
Examples:
User: "My name is John and I live in NYC"
Agent: *Creates memory about John's location*
User: "What do you remember about me?"
Agent: *Recalls previous memories about John*
Run: `pip install openai sqlalchemy agno` to install dependencies
"""
import json
from textwrap import dedent
from typing import Optional
import typer
from agno.agent import Agent, AgentMemory
from agno.memory.db.sqlite import SqliteMemoryDb
from agno.models.openai import OpenAIChat
from agno.storage.sqlite import SqliteStorage
from rich.console import Console
from rich.json import JSON
from rich.panel import Panel
from rich.prompt import Prompt
def create_agent(user: str = "user"):
session_id: Optional[str] = None
# Ask if user wants to start new session or continue existing one
new = typer.confirm("Do you want to start a new session?")
# Initialize storage for both agent sessions and memories
agent_storage = SqliteStorage(table_name="agent_memories", db_file="tmp/agents.db")
if not new:
existing_sessions = agent_storage.get_all_session_ids(user)
if len(existing_sessions) > 0:
session_id = existing_sessions[0]
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
user_id=user,
session_id=session_id,
# Configure memory system with SQLite storage
memory=AgentMemory(
db=SqliteMemoryDb(
table_name="agent_memory",
db_file="tmp/agent_memory.db",
),
create_user_memories=True,
update_user_memories_after_run=True,
create_session_summary=True,
update_session_summary_after_run=True,
),
storage=agent_storage,
add_history_to_messages=True,
num_history_responses=3,
# Enhanced system prompt for better personality and memory usage
description=dedent("""\
You are a helpful and friendly AI assistant with excellent memory.
- Remember important details about users and reference them naturally
- Maintain a warm, positive tone while being precise and helpful
- When appropriate, refer back to previous conversations and memories
- Always be truthful about what you remember or don't remember"""),
)
if session_id is None:
session_id = agent.session_id
if session_id is not None:
print(f"Started Session: {session_id}\n")
else:
print("Started Session\n")
else:
print(f"Continuing Session: {session_id}\n")
return agent
def print_agent_memory(agent):
"""Print the current state of agent's memory systems"""
console = Console()
# Print chat history
console.print(
Panel(
JSON(
json.dumps([m.to_dict() for m in agent.memory.messages]),
indent=4,
),
title=f"Chat History for session_id: {agent.session_id}",
expand=True,
)
)
# Print user memories
console.print(
Panel(
JSON(
json.dumps(
[
m.model_dump(include={"memory", "input"})
for m in agent.memory.memories
]
),
indent=4,
),
title=f"Memories for user_id: {agent.user_id}",
expand=True,
)
)
# Print session summary
console.print(
Panel(
JSON(json.dumps(agent.memory.summary.model_dump(), indent=4)),
title=f"Summary for session_id: {agent.session_id}",
expand=True,
)
)
def main(user: str = "user"):
"""Interactive chat loop with memory display"""
agent = create_agent(user)
print("Try these example inputs:")
print("- 'My name is [name] and I live in [city]'")
print("- 'I love [hobby/interest]'")
print("- 'What do you remember about me?'")
print("- 'What have we discussed so far?'\n")
exit_on = ["exit", "quit", "bye"]
while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in exit_on:
break
agent.print_response(message=message, stream=True, markdown=True)
print_agent_memory(agent)
if __name__ == "__main__":
typer.run(main)