Traditional Rag
Standard RAG pipeline with vector search and retrieval.
- Run:
./cookbook/run_pgvector.shto start a postgres container with pgvector.
1"""2Traditional Rag3=============================451. Run: `./cookbook/run_pgvector.sh` to start a postgres container with pgvector.6"""78from kern.agent import Agent9from kern.knowledge.embedder.openai import OpenAIEmbedder10from kern.knowledge.knowledge import Knowledge11from kern.models.openai import OpenAIResponses12from kern.vectordb.pgvector import PgVector, SearchType1314db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"1516knowledge = Knowledge(17 # Use PgVector as the vector database and store embeddings in the `ai.recipes` table18 vector_db=PgVector(19 table_name="recipes",20 db_url=db_url,21 search_type=SearchType.hybrid,22 embedder=OpenAIEmbedder(id="text-embedding-3-small"),23 ),24)2526# ---------------------------------------------------------------------------27# Create Agent28# ---------------------------------------------------------------------------29agent = Agent(30 model=OpenAIResponses(id="gpt-5.2"),31 knowledge=knowledge,32 # Enable RAG by adding context from the `knowledge` to the user prompt.33 add_knowledge_to_context=True,34 # Set as False because Agents default to `search_knowledge=True`35 search_knowledge=False,36 markdown=True,37)3839# ---------------------------------------------------------------------------40# Run Agent41# ---------------------------------------------------------------------------42if __name__ == "__main__":43 knowledge.insert(url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf")44 agent.print_response(45 "How do I make chicken and galangal in coconut milk soup", stream=True46 )Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/07_knowledge45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89# Optiona: Run PgVector (needs docker)10./cookbook/scripts/run_pgvector.sh1112python traditional_rag.py