Traditional RAG with PgVector
This example demonstrates traditional RAG implementation using PgVector database with OpenAI embeddings, where knowledge context is automatically added to prompts without search functionality.
Code
1from kern.agent import Agent2from kern.knowledge.embedder.openai import OpenAIEmbedder3from kern.knowledge.knowledge import Knowledge4from kern.models.openai import OpenAIResponses5from kern.vectordb.pgvector import PgVector, SearchType67db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"89knowledge = Knowledge(10 # Use PgVector as the vector database and store embeddings in the `ai.recipes` table11 vector_db=PgVector(12 table_name="recipes",13 db_url=db_url,14 search_type=SearchType.hybrid,15 embedder=OpenAIEmbedder(id="text-embedding-3-small"),16 ),17)1819knowledge.insert(20 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"21)2223agent = Agent(24 model=OpenAIResponses(id="gpt-5.2"),25 knowledge=knowledge,26 # Enable RAG by adding context from the `knowledge` to the user prompt.27 add_knowledge_to_context=True,28 # Set as False because Agents default to `search_knowledge=True`29 search_knowledge=False,30 markdown=True,31)32agent.print_response(33 "How do I make chicken and galangal in coconut milk soup", stream=True34)Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openai sqlalchemy psycopg pgvectorSetup PgVector
Start PostgreSQL with pgvector extension and update the connection string in the code as needed.
Export your OpenAI API key
1export OPENAI_API_KEY=your_openai_api_key_hereRun Agent
1python traditional_rag_pgvector.py