/ examples / persistence / minimal_agent_db.py
minimal_agent_db.py
 1  """
 2  Minimal Agent with Database Persistence
 3  
 4  This is the simplest way to add persistence to your agent.
 5  Messages, runs, and traces are automatically saved.
 6  
 7  Run:
 8      python minimal_agent_db.py
 9  
10  Expected output:
11      - Agent responds to your question
12      - Data is persisted to PostgreSQL
13      - Session can be resumed later with same session_id
14  """
15  
16  from praisonaiagents import Agent, db
17  
18  # Create database connection (PostgreSQL + Redis)
19  my_db = db(
20      database_url="postgresql://postgres:praison123@localhost:5432/praisonai",
21      state_url="redis://localhost:6379"  # Optional: for state/tracing
22  )
23  
24  # Create agent with persistence
25  agent = Agent(
26      name="Assistant",
27      instructions="You are a helpful assistant. Be concise.",
28      db=my_db,
29      # session_id is optional - defaults to per-hour ID (YYYYMMDDHH-hash)
30      # Set explicitly for session continuity across runs:
31      session_id="my-persistent-session"
32  )
33  
34  # Chat - messages are automatically persisted
35  response = agent.chat("What is the capital of France?")
36  print(f"Response: {response}")
37  
38  # Verify persistence
39  data = my_db.export_session("my-persistent-session")
40  print(f"\nMessages stored: {len(data.get('messages', []))}")
41  
42  # Clean up
43  my_db.close()