Skip to main content

Code

cookbook/08_knowledge/vector_db/pgvector/async_pg_vector.py
import asyncio

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector

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

vector_db = PgVector(table_name="recipes", db_url=db_url)

knowledge_base = Knowledge(
    vector_db=vector_db,
)

agent = Agent(knowledge=knowledge_base)

if __name__ == "__main__":

    asyncio.run(
        knowledge_base.ainsert(url="https://docs.agno.com/introduction/agents.md")
    )

    asyncio.run(
        agent.aprint_response("What is the purpose of an Agno Agent?", markdown=True)
    )

Usage

1

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate
2

Install dependencies

uv pip install -U psycopg2-binary pgvector pypdf openai agno
3
Run PgVector on Docker
4
Create a file resources.py with the following contents:
5
from agno.docker.app.postgres import PgVectorDb
from agno.docker.resources import DockerResources

# -*- PgVector running on port 5432:5432
vector_db = PgVectorDb(
    pg_user="ai",
    pg_password="ai",
    pg_database="ai",
    debug_mode=True,
)

# -*- DockerResources
dev_docker_resources = DockerResources(apps=[vector_db])
6
Start resources using:
7
Mac
ag start resources.py
Windows
ag start resources.py
8
Press Enter to confirm and verify container status on the docker dashboard.
9

Set environment variables

export OPENAI_API_KEY=xxx
10

Run Agent

python cookbook/08_knowledge/vector_db/pgvector/async_pg_vector.py