Setup
uv pip install -U sqlalchemy psycopg pgvector pypdf openai agno
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 \
agnohq/pgvector:18
Example
agent_with_knowledge.py
from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector, SearchType
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
knowledge_base = Knowledge(
vector_db=PgVector(table_name="recipes", db_url=db_url, search_type=SearchType.hybrid),
)
if __name__ == "__main__":
knowledge_base.insert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
)
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
knowledge=knowledge_base,
# Add a tool to read chat history.
read_chat_history=True,
markdown=True,
# debug_mode=True,
)
agent.print_response("How do I make chicken and galangal in coconut milk soup", stream=True)
agent.print_response("What was my last question?", stream=True)
Async Support ⚡
PgVector also supports asynchronous operations, enabling concurrency and leading to better performance.
async_pgvector.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__":
# Load knowledge base asynchronously
asyncio.run(knowledge_base.ainsert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
)
)
# Create and use the agent asynchronously
asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
Use
ainsert() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.PgVector Params
| Parameter | Type | Default | Description |
|---|---|---|---|
table_name | str | - | Name of the table to store vector data. |
schema | str | "ai" | Database schema name. |
name | Optional[str] | None | Name of the vector database. |
description | Optional[str] | None | Description of the vector database. |
id | Optional[str] | None | ID of the vector database. Generated from db_url and table_name if not provided. |
db_url | Optional[str] | None | Database connection URL. |
db_engine | Optional[Engine] | None | SQLAlchemy database engine. |
embedder | Optional[Embedder] | None | Embedder for creating embeddings. Defaults to OpenAIEmbedder if not provided. |
search_type | SearchType | vector | Type of search to perform. |
vector_index | Union[Ivfflat, HNSW] | HNSW() | Vector index configuration. |
distance | Distance | cosine | Distance metric for vector comparisons. |
prefix_match | bool | False | Enable prefix matching for full-text search. |
vector_score_weight | float | 0.5 | Weight for vector similarity in hybrid search. Must be between 0 and 1. |
content_language | str | "english" | Language for full-text search. |
schema_version | int | 1 | Version of the database schema. |
auto_upgrade_schema | bool | False | Automatically upgrade the schema if True. |
reranker | Optional[Reranker] | None | Reranker for reranking search results. |
create_schema | bool | True | Create the database schema if it does not exist. Set to False if schema is managed externally. |
similarity_threshold | Optional[float] | None | Minimum similarity score (0.0-1.0) to filter results. |