-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy path16_agent_session.py
102 lines (80 loc) · 2.87 KB
/
16_agent_session.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
"""🗣️ Persistent Chat History i.e. Session Memory
This example shows how to create an agent with persistent memory stored in a SQLite database.
We set the session_id on the agent when resuming the conversation, this way the previous chat history is preserved.
Key features:
- Stores conversation history in a SQLite database
- Continues conversations across multiple sessions
- References previous context in responses
Run `pip install openai sqlalchemy agno` to install dependencies.
"""
import json
from typing import Optional
import typer
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.storage.sqlite import SqliteStorage
from rich import print
from rich.console import Console
from rich.json import JSON
from rich.panel import Panel
from rich.prompt import Prompt
console = Console()
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?")
# Get existing session if user doesn't want a new one
agent_storage = SqliteStorage(table_name="agent_sessions", 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(
user_id=user,
# Set the session_id on the agent to resume the conversation
session_id=session_id,
model=OpenAIChat(id="gpt-4o"),
storage=agent_storage,
# Add chat history to messages
add_history_to_messages=True,
num_history_responses=3,
markdown=True,
)
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_messages(agent):
"""Print the current chat history in a formatted panel"""
console.print(
Panel(
JSON(
json.dumps(
[
m.model_dump(include={"role", "content"})
for m in agent.memory.messages
]
),
indent=4,
),
title=f"Chat History for session_id: {agent.session_id}",
expand=True,
)
)
def main(user: str = "user"):
agent = create_agent(user)
print("Chat with an OpenAI agent!")
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_messages(agent)
if __name__ == "__main__":
typer.run(main)