Persistence Module

Database persistence and session management for PraisonAI

Persistence Module

The Persistence module provides database adapters and session management for storing agent conversations, runs, and traces.

Installation

bash
pip install praisonai

# For specific database support
pip install "praisonai[postgres]"
pip install "praisonai[redis]"

Quick Start

The recommended way to enable persistence is via the memory={} parameter:

python
from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    instructions="You are helpful.",
    memory={
        "db": "postgresql://localhost/mydb",
        "session_id": "my-session"
    }
)

response = agent.start("Hello!")

Supported Databases

Conversation Stores

DatabaseInstallURL Format
PostgreSQLpip install "praisonai[postgres]"postgresql://user:pass@host/db
MySQLpip install "praisonai[mysql]"mysql://user:pass@host/db
SQLiteBuilt-insqlite:///path/to/db.sqlite
JSONBuilt-injson:///path/to/data.json

Knowledge Stores (Vector)

DatabaseInstall
Qdrantpip install "praisonai[qdrant]"
Chromapip install "praisonai[chroma]"
Pineconepip install "praisonai[pinecone]"
PGVectorpip install "praisonai[pgvector]"

State Stores

DatabaseInstall
Redispip install "praisonai[redis]"
MongoDBpip install "praisonai[mongodb]"

Database Examples

PostgreSQL

python
from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={
        "db": "postgresql://user:pass@localhost/mydb",
        "session_id": "user-123"
    }
)

SQLite

python
from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={
        "db": "sqlite:///./agent.db",
        "session_id": "local-session"
    }
)

Redis

python
from praisonaiagents import Agent

agent = Agent(
    name="Assistant",
    memory={
        "backend": "redis",
        "db": "redis://localhost:6379",
        "session_id": "redis-session"
    }
)

Session Management

Sessions allow you to maintain conversational context:

python
from praisonaiagents import Agent

# Create agent with session
agent = Agent(
    name="Assistant",
    memory={"db": "sqlite:///data.db", "session_id": "user-123"}
)

# Conversations persist across runs
response1 = agent.start("Remember my name is Alice")
# Later...
response2 = agent.start("What's my name?")  # Remembers "Alice"

Data Models

DbMessage

Stored message format:

python
{
    "id": "msg-uuid",
    "session_id": "session-123",
    "agent_id": "agent-name",
    "role": "user",  # or "assistant"
    "content": "Message content",
    "timestamp": "2024-01-01T00:00:00Z"
}

DbRun

Execution run record:

python
{
    "id": "run-uuid",
    "session_id": "session-123",
    "agent_id": "agent-name",
    "status": "completed",
    "started_at": "2024-01-01T00:00:00Z",
    "completed_at": "2024-01-01T00:00:01Z"
}

DbToolCall

Tool execution record:

python
{
    "id": "tool-uuid",
    "run_id": "run-uuid",
    "tool_name": "search",
    "arguments": {"query": "AI news"},
    "result": "Search results...",
    "timestamp": "2024-01-01T00:00:00Z"
}

See Also