Deep Knowledge
This agent performs iterative searches through its knowledge base, breaking down complex queries into sub-questions, and synthesizing comprehensive answers. It's designed to explore topics deeply and thoroughly by following chains of reasoning.
In this example, the agent uses the Kern documentation as a knowledge base
Key Features:
- Iteratively searches a knowledge base
- Source attribution and citations
Code
1from textwrap import dedent2from typing import List, Optional34import inquirer5import typer6from kern.agent import Agent7from kern.db.base import SessionType8from kern.db.sqlite import SqliteDb9from kern.knowledge.embedder.openai import OpenAIEmbedder10from kern.knowledge.knowledge import Knowledge11from kern.models.openai import OpenAIResponses12from kern.vectordb.lancedb import LanceDb, SearchType13from rich import print141516def initialize_knowledge_base():17 """Initialize the knowledge base with your preferred documentation or knowledge source18 Here we use Kern docs as an example, but you can replace with any relevant URLs19 """20 agent_knowledge = Knowledge(21 vector_db=LanceDb(22 uri="tmp/lancedb",23 table_name="deep_knowledge_knowledge",24 search_type=SearchType.hybrid,25 embedder=OpenAIEmbedder(id="text-embedding-3-small"),26 ),27 )28 agent_knowledge.insert(29 url="https://kern.ndx.rocks/llms-full.txt",30 )31 return agent_knowledge323334def get_agent_db():35 """Return agent storage"""36 return SqliteDb(session_table="deep_knowledge_sessions", db_file="tmp/agents.db")373839def create_agent(session_id: Optional[str] = None) -> Agent:40 """Create and return a configured DeepKnowledge agent."""41 agent_knowledge = initialize_knowledge_base()42 agent_db = get_agent_db()43 return Agent(44 name="DeepKnowledge",45 session_id=session_id,46 model=OpenAIResponses(id="gpt-5.2"),47 description=dedent("""\48 You are DeepKnowledge, an advanced reasoning agent designed to provide thorough,49 well-researched answers to any query by searching your knowledge base.5051 Your strengths include:52 - Breaking down complex topics into manageable components53 - Connecting information across multiple domains54 - Providing nuanced, well-researched answers55 - Maintaining intellectual honesty and citing sources56 - Explaining complex concepts in clear, accessible terms"""),57 instructions=dedent("""\58 Your mission is to leave no stone unturned in your pursuit of the correct answer.5960 To achieve this, follow these steps:61 1. **Analyze the input and break it down into key components**.62 2. **Search terms**: You must identify at least 3-5 key search terms to search for.63 3. **Initial Search:** Searching your knowledge base for relevant information. You must make atleast 3 searches to get all relevant information.64 4. **Evaluation:** If the answer from the knowledge base is incomplete, ambiguous, or insufficient - Ask the user for clarification. Do not make informed guesses.65 5. **Iterative Process:**66 - Continue searching your knowledge base till you have a comprehensive answer.67 - Reevaluate the completeness of your answer after each search iteration.68 - Repeat the search process until you are confident that every aspect of the question is addressed.69 4. **Reasoning Documentation:** Clearly document your reasoning process:70 - Note when additional searches were triggered.71 - Indicate which pieces of information came from the knowledge base and where it was sourced from.72 - Explain how you reconciled any conflicting or ambiguous information.73 5. **Final Synthesis:** Only finalize and present your answer once you have verified it through multiple search passes.74 Include all pertinent details and provide proper references.75 6. **Continuous Improvement:** If new, relevant information emerges even after presenting your answer,76 be prepared to update or expand upon your response.7778 **Communication Style:**79 - Use clear and concise language.80 - Organize your response with numbered steps, bullet points, or short paragraphs as needed.81 - Be transparent about your search process and cite your sources.82 - Ensure that your final answer is comprehensive and leaves no part of the query unaddressed.8384 Remember: **Do not finalize your answer until every angle of the question has been explored.**"""),85 additional_context=dedent("""\86 You should only respond with the final answer and the reasoning process.87 No need to include irrelevant information.8889 - User ID: {user_id}90 - Memory: You have access to your previous search results and reasoning process.91 """),92 knowledge=agent_knowledge,93 db=agent_db,94 add_history_to_context=True,95 num_history_runs=3,96 read_chat_history=True,97 markdown=True,98 )99100101def get_example_topics() -> List[str]:102 """Return a list of example topics for the agent."""103 return [104 "What are AI agents and how do they work in Kern?",105 "What chunking strategies does Kern support for text processing?",106 "How can I implement custom tools in Kern?",107 "How does knowledge retrieval work in Kern?",108 "What types of embeddings does Kern support?",109 ]110111112def handle_session_selection() -> Optional[str]:113 """Handle session selection and return the selected session ID."""114 agent_db = get_agent_db()115116 new = typer.confirm("Do you want to start a new session?", default=True)117 if new:118 return None119120 existing_sessions: List[str] = agent_db.get_sessions(session_type=SessionType.AGENT)121 if not existing_sessions:122 print("No existing sessions found. Starting a new session.")123 return None124125 print("\nExisting sessions:")126 for i, session in enumerate(existing_sessions, 1):127 print(f"{i}. {session}")128129 session_idx = typer.prompt(130 "Choose a session number to continue (or press Enter for most recent)",131 default=1,132 )133134 try:135 return existing_sessions[int(session_idx) - 1]136 except (ValueError, IndexError):137 return existing_sessions[0]138139140def run_interactive_loop(agent: Agent):141 """Run the interactive question-answering loop."""142 example_topics = get_example_topics()143144 while True:145 choices = [f"{i + 1}. {topic}" for i, topic in enumerate(example_topics)]146 choices.extend(["Enter custom question...", "Exit"])147148 questions = [149 inquirer.List(150 "topic",151 message="Select a topic or ask a different question:",152 choices=choices,153 )154 ]155 answer = inquirer.prompt(questions)156157 if answer["topic"] == "Exit":158 break159160 if answer["topic"] == "Enter custom question...":161 questions = [inquirer.Text("custom", message="Enter your question:")]162 custom_answer = inquirer.prompt(questions)163 topic = custom_answer["custom"]164 else:165 topic = example_topics[int(answer["topic"].split(".")[0]) - 1]166167 agent.print_response(topic, stream=True)168169170def deep_knowledge_agent():171 """Main function to run the DeepKnowledge agent."""172173 session_id = handle_session_selection()174 agent = create_agent(session_id)175176 print("\nš¤ Welcome to DeepKnowledge - Your Advanced Research Assistant! š")177 if session_id is None:178 session_id = agent.session_id179 if session_id is not None:180 print(f"[bold green]Started New Session: {session_id}[/bold green]\n")181 else:182 print("[bold green]Started New Session[/bold green]\n")183 else:184 print(f"[bold blue]Continuing Previous Session: {session_id}[/bold blue]\n")185186 run_interactive_loop(agent)187188189if __name__ == "__main__":190 typer.run(deep_knowledge_agent)191192# Example prompts to try:193"""194Explore Kern's capabilities with these queries:1951. "What are the different types of agents in Kern?"1962. "How does Kern handle knowledge base management?"1973. "What embedding models does Kern support?"1984. "How can I implement custom tools in Kern?"1995. "What storage options are available for workflow caching?"2006. "How does Kern handle streaming responses?"2017. "What types of LLM providers does Kern support?"2028. "How can I implement custom knowledge sources?"203"""Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateSet your API key
1export OPENAI_API_KEY=****Install dependencies
1uv pip install -U kern-ai openai lancedb tantivy inquirerRun Agent
1python cookbook/01_showcase/01_agents/deep_knowledge_agent.py1python cookbook/01_showcase/01_agents/deep_knowledge_agent.py