Agentic Rag
Build an agentic RAG pipeline with autonomous retrieval.
- Run:
./cookbook/run_pgvector.shto start a postgres container with pgvector.
1"""2Agentic 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"15knowledge = Knowledge(16 # Use PgVector as the vector database and store embeddings in the `ai.recipes` table17 vector_db=PgVector(18 table_name="recipes",19 db_url=db_url,20 search_type=SearchType.hybrid,21 embedder=OpenAIEmbedder(id="text-embedding-3-small"),22 ),23)2425# ---------------------------------------------------------------------------26# Create Agent27# ---------------------------------------------------------------------------28agent = Agent(29 model=OpenAIResponses(id="gpt-5.2"),30 knowledge=knowledge,31 # Add a tool to search the knowledge base which enables agentic RAG.32 # This is enabled by default when `knowledge` is provided to the Agent.33 search_knowledge=True,34 markdown=True,35)3637# ---------------------------------------------------------------------------38# Run Agent39# ---------------------------------------------------------------------------40if __name__ == "__main__":41 knowledge.insert(url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf")42 agent.print_response(43 "How do I make chicken and galangal in coconut milk soup", stream=True44 )45 # agent.print_response(46 # "Hi, i want to make a 3 course meal. Can you recommend some recipes. "47 # "I'd like to start with a soup, then im thinking a thai curry for the main course and finish with a dessert",48 # stream=True,49 # )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 agentic_rag.py