Agentic Rag With Reasoning
Demonstrates agentic RAG with reranking and explicit reasoning tools.
1"""2Agentic Rag With Reasoning3=============================45Demonstrates agentic RAG with reranking and explicit reasoning tools.6"""78import asyncio910from kern.agent import Agent11from kern.knowledge.embedder.cohere import CohereEmbedder12from kern.knowledge.knowledge import Knowledge13from kern.knowledge.reranker.cohere import CohereReranker14from kern.models.openai import OpenAIResponses15from kern.tools.reasoning import ReasoningTools16from kern.vectordb.lancedb import LanceDb, SearchType1718# ---------------------------------------------------------------------------19# Setup20# ---------------------------------------------------------------------------21knowledge = Knowledge(22 # Use LanceDB as the vector database, store embeddings in the `agno_docs` table23 vector_db=LanceDb(24 uri="tmp/lancedb",25 table_name="agno_docs",26 search_type=SearchType.hybrid,27 embedder=CohereEmbedder(id="embed-v4.0"),28 reranker=CohereReranker(model="rerank-v3.5"),29 ),30)3132# ---------------------------------------------------------------------------33# Create Agent34# ---------------------------------------------------------------------------35agent = Agent(36 model=OpenAIResponses(id="gpt-5.2"),37 # Agentic RAG is enabled by default when `knowledge` is provided to the Agent.38 knowledge=knowledge,39 # search_knowledge=True gives the Agent the ability to search on demand40 # search_knowledge is True by default41 search_knowledge=True,42 tools=[ReasoningTools(add_instructions=True)],43 instructions=[44 "Include sources in your response.",45 "Always search your knowledge before answering the question.",46 ],47 markdown=True,48)4950# ---------------------------------------------------------------------------51# Run Agent52# ---------------------------------------------------------------------------53if __name__ == "__main__":54 asyncio.run(55 knowledge.ainsert_many(urls=["https://kern.ndx.rocks/basics/agents/overview.md"])56 )57 agent.print_response(58 "What are Agents?",59 stream=True,60 show_full_reasoning=True,61 )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/activate89python agentic_rag_with_reasoning.py