The simplest way to use sessions is with the Agent class:
Copy
from praisonaiagents import Agent# Create agent with session persistenceagent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"})# First conversationagent.start("My name is Alice")# Later, in a new process - history restored automatically!agent = Agent( name="Assistant", instructions="You are a helpful assistant", memory={"session_id": "my-session-123"})agent.start("What's my name?") # Remembers: "Alice"
For advanced control over memory and knowledge, use the Session class:
Copy
from praisonaiagents import Session# Create a sessionsession = Session( session_id="chat_123", user_id="user_456")# Create agent within session contextagent = session.Agent( name="Assistant", instructions="You are a helpful assistant")
# Create new session instancesession = Session(session_id="chat_123")# Restore previous statesession.restore_state()# Continue where you left offagent = session.Agent(name="Assistant")response = agent.chat("What were we discussing?")
from praisonaiagents import Session, Task, AgentTeam# Create or restore sessionsession = Session( session_id="personal_assistant", user_id="john_doe", memory={ "provider": "rag", "use_embedding": True }, knowledge={ "vector_store": { "provider": "chroma", "config": {"collection_name": "personal_docs"} } })# Try to restore previous statetry: session.restore_state() print("Restored previous session")except: print("Starting new session")# Create session agentassistant = session.Agent( name="Personal Assistant", instructions="""You are a personal assistant that remembers everything. Use your memory and knowledge to provide personalised help.""", memory=True, knowledge=True)# Add some knowledgesession.add_knowledge("calendar.pdf")session.add_memory("User prefers morning meetings")# Have a conversationresponse = assistant.chat("Schedule a meeting for tomorrow")print(response)# Save state for next timesession.save_state()