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:16Example
1from kern.agent import Agent2from kern.models.openai import OpenAIResponses3from kern.knowledge.knowledge import Knowledge4from kern.vectordb.pgvector import PgVector, SearchType56db_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)1011if __name__ == "__main__":12 knowledge_base.insert(13 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"14 )1516 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 asyncio23from kern.agent import Agent4from kern.knowledge.knowledge import Knowledge5from kern.vectordb.pgvector import PgVector67db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"89vector_db = PgVector(table_name="recipes", db_url=db_url)1011knowledge_base = Knowledge(12 vector_db=vector_db,13)1415agent = Agent(knowledge=knowledge_base)1617if __name__ == "__main__":18 # Load knowledge base asynchronously19 asyncio.run(knowledge_base.ainsert(20 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"21 )22 )2324 # Create and use the agent asynchronously25 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
| Parameter | Type | Default | Description |
|---|---|---|---|
table_name | str | - | The name of the table to use. |
schema | str | - | The schema to use. |
db_url | str | - | The database URL to connect to. |
db_engine | Engine | - | The database engine to use. |
embedder | Embedder | - | The embedder to use. |
search_type | SearchType | vector | The search type to use. |
vector_index | Union[Ivfflat, HNSW] | - | The vector index to use. |
distance | Distance | cosine | The distance to use. |
prefix_match | bool | - | Whether to use prefix matching. |
vector_score_weight | float | 0.5 | Weight for vector similarity in hybrid search. Must be between 0 and 1. |
content_language | str | - | The content language to use. |
schema_version | int | - | The schema version to use. |
auto_upgrade_schema | bool | - | Whether to auto upgrade the schema. |