Assistants come with built-in memory but it only lasts while the session is active. To continue conversations across sessions, we store Assistant runs in a database like PostgreSQL.

Storage is a necessary component when building user facing AI products as any production application will require users to be able to “continue” their conversation with the AI.

The general syntax for adding storage to an Assistant looks like:

storage.py
from phi.storage.assistant.postgres import PgAssistantStorage

# Create a storage backend using the Postgres database
storage = PgAssistantStorage(
    # store runs in the ai.assistant_runs table
    table_name="assistant_runs",
    # db_url: Postgres database URL
    db_url=db_url,
)

# Add storage to the Assistant
assistant = Assistant(storage=storage)

Params

table_name
str

The name of the table to store assistant runs in.

schema
str
default: "ai"

The schema to store the table in.

db_url
str

The database URL to connect to.

db_engine
Engine

The database engine to use.

Example

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate
2

Install libraries

Install libraries using pip

pip install -U phidata openai pgvector pypdf "psycopg[binary]" sqlalchemy
3

Run PgVector

Install docker desktop and run PgVector on port 5532 using:

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16
4

Create an Assistant with Storage

Create a file pdf_assistant.py with the following contents

pdf_assistant.py
import typer
from rich.prompt import Prompt
from typing import Optional, List
from phi.assistant import Assistant
from phi.storage.assistant.postgres import PgAssistantStorage
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=PgVector2(collection="recipes", db_url=db_url),
)
# Comment out after first run
knowledge_base.load()

storage = PgAssistantStorage(table_name="pdf_assistant", db_url=db_url)


def pdf_assistant(new: bool = False, user: str = "user"):
    run_id: Optional[str] = None

    if not new:
        existing_run_ids: List[str] = storage.get_all_run_ids(user)
        if len(existing_run_ids) > 0:
            run_id = existing_run_ids[0]

    assistant = Assistant(
        run_id=run_id,
        user_id=user,
        knowledge_base=knowledge_base,
        storage=storage,
        # Show tool calls in the response
        show_tool_calls=True,
        # Enable the assistant to search the knowledge base
        search_knowledge=True,
        # Enable the assistant to read the chat history
        read_chat_history=True,
    )
    if run_id is None:
        run_id = assistant.run_id
        print(f"Started Run: {run_id}\n")
    else:
        print(f"Continuing Run: {run_id}\n")

    # Runs the assistant as a cli app
    assistant.cli_app(markdown=True)


if __name__ == "__main__":
    typer.run(pdf_assistant)
5

Run the assistant

Run the pdf_assistant.py file

python pdf_assistant.py

Now the assistant continues across sessions. Ask a question:

How do I make pad thai?

Then message bye to exit, start the app again and ask:

What was my last message?
6

Start a new run

Run the pdf_assistant.py file with the --new flag to start a new run.

python pdf_assistant.py --new