Phidata Refresher
Agents with built-in Memory (Postgres) and Knowledge.
1. Philosophy
Phidata solves the "Amensia" problem. It connects Agents directly to a database (Postgres) to store sessions, and uses the same database for Vector Search (pgvector).
Technique: Persistent Storage
from phi.assistant import Assistant
from phi.storage.agent.postgres import PgAgentStorage
# 1. Define Storage (Postgres)
storage = PgAgentStorage(
table_name="agent_sessions",
db_url="postgresql://user:pass@localhost:5432/db"
)
# 2. Agent with Storage
agent = Assistant(
name="MemoryAgent",
storage=storage,
add_history_to_messages=True, # Sends chat history to LLM
)
# 3. Resume Session
# If I run this with session_id="123", it loads previous chats.
agent.print_response("Hello", session_id="123")
Technique: Knowledge Base
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2
kb = PDFUrlKnowledgeBase(
urls=["https://.../report.pdf"],
vector_db=PgVector2(collection="reports", db_url="...")
)
kb.load()
agent = Assistant(knowledge_base=kb, search_knowledge=True)