PgVector Vector Database

Use PgVector as a vector database for your Knowledge Base.

Setup

1docker run -d \
2 -e POSTGRES_DB=ai \
3 -e POSTGRES_USER=ai \
4 -e POSTGRES_PASSWORD=ai \
5 -e PGDATA=/var/lib/postgresql/data/pgdata \
6 -v pgvolume:/var/lib/postgresql/data \
7 -p 5532:5432 \
8 --name pgvector \
9 agnohq/pgvector:16

Example

1from kern.agent import Agent
2from kern.models.openai import OpenAIResponses
3from kern.knowledge.knowledge import Knowledge
4from kern.vectordb.pgvector import PgVector, SearchType
5
6db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
7knowledge_base = Knowledge(
8 vector_db=PgVector(table_name="recipes", db_url=db_url, search_type=SearchType.hybrid),
9)
10
11if __name__ == "__main__":
12 knowledge_base.insert(
13 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
14 )
15
16 agent = Agent(
17 model=OpenAIResponses(id="gpt-5.2"),
18 knowledge=knowledge_base,
19 # Add a tool to read chat history.
20 read_chat_history=True,
21 markdown=True,
22 # debug_mode=True,
23 )
24 agent.print_response("How do I make chicken and galangal in coconut milk soup", stream=True)
25 agent.print_response("What was my last question?", stream=True)

Async Support ⚡

PgVector also supports asynchronous operations, enabling concurrency and leading to better performance.

1import asyncio
2
3from kern.agent import Agent
4from kern.knowledge.knowledge import Knowledge
5from kern.vectordb.pgvector import PgVector
6
7db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
8
9vector_db = PgVector(table_name="recipes", db_url=db_url)
10
11knowledge_base = Knowledge(
12 vector_db=vector_db,
13)
14
15agent = Agent(knowledge=knowledge_base)
16
17if __name__ == "__main__":
18 # Load knowledge base asynchronously
19 asyncio.run(knowledge_base.ainsert(
20 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
21 )
22 )
23
24 # Create and use the agent asynchronously
25 asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
Tip

Use aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.

PgVector Params

ParameterTypeDefaultDescription
table_namestr-The name of the table to use.
schemastr-The schema to use.
db_urlstr-The database URL to connect to.
db_engineEngine-The database engine to use.
embedderEmbedder-The embedder to use.
search_typeSearchTypevectorThe search type to use.
vector_indexUnion[Ivfflat, HNSW]-The vector index to use.
distanceDistancecosineThe distance to use.
prefix_matchbool-Whether to use prefix matching.
vector_score_weightfloat0.5Weight for vector similarity in hybrid search. Must be between 0 and 1.
content_languagestr-The content language to use.
schema_versionint-The schema version to use.
auto_upgrade_schemabool-Whether to auto upgrade the schema.