Skip to main content
A RAG-powered knowledge agent that provides intelligent access to internal company documentation. Enables employees to ask questions and get accurate answers with source citations from company knowledge bases.

Key Concepts

ConceptDescription
RAGRetrieval Augmented Generation for accurate answers
Hybrid SearchCombines semantic and keyword search for comprehensive results
Source CitationsEvery answer references source documents
Uncertainty HandlingAcknowledges when information is not in the knowledge base

Prerequisites

  • Python 3.12+
  • Docker (for PostgreSQL with pgvector)
  • OpenAI API key

Setup

1

Clone the repository

git clone https://github.com/agno-agi/agno.git
cd agno
2

Create and activate virtual environment

uv venv --python 3.12
source .venv/bin/activate
3

Install dependencies

uv pip install -r cookbook/01_showcase/01_agents/knowledge_agent/requirements.in
4

Set environment variables

export OPENAI_API_KEY=your-openai-key
5

Start PostgreSQL with pgvector

./cookbook/scripts/run_pgvector.sh
6

Load sample knowledge base

python cookbook/01_showcase/01_agents/knowledge_agent/scripts/load_knowledge.py

Run the Agent

Basic Query

Ask a question from the knowledge base:
python cookbook/01_showcase/01_agents/knowledge_agent/examples/basic_query.py
Demonstrates:
  • Searching the knowledge base
  • Answer synthesis with citations
  • Confidence levels

Multi-Turn Conversation

Have a follow-up conversation:
python cookbook/01_showcase/01_agents/knowledge_agent/examples/conversation.py
Demonstrates:
  • Conversation memory
  • Context-aware follow-ups
  • Building on previous answers

Edge Cases

Handle uncertainty and ambiguity:
python cookbook/01_showcase/01_agents/knowledge_agent/examples/edge_cases.py

Agent Configuration

knowledge_agent = Agent(
    name="Knowledge Agent",
    model=OpenAIResponses(id="gpt-5.2"),
    knowledge=company_knowledge,
    system_message=SYSTEM_MESSAGE,
    tools=[
        ReasoningTools(add_instructions=True),
    ],
    add_datetime_to_context=True,
    add_history_to_context=True,
    num_history_runs=5,
    read_chat_history=True,
    enable_agentic_memory=True,
    search_knowledge=True,
    markdown=True,
)
ParameterPurpose
modelGPT-5.2 for question answering
knowledgePgVector database with hybrid search
search_knowledgeAuto-search knowledge base before answering
read_chat_historyAccess conversation history
num_history_runsInclude 5 previous turns for context
ReasoningToolsPlan search and synthesis approach

How It Works

RAG Workflow

1. User asks a question
2. Agent searches knowledge base (hybrid search)
3. Top-k relevant chunks retrieved
4. Agent synthesizes answer from context
5. Answer includes source citations
The knowledge base uses hybrid search combining:
Search TypeDescription
SemanticVector similarity for conceptual matches
KeywordBM25 for exact term matches
CombinedBest of both for comprehensive results

Knowledge Base Configuration

company_knowledge = Knowledge(
    name="Company Knowledge Base",
    vector_db=PgVector(
        table_name="company_knowledge",
        db_url=DB_URL,
        search_type=SearchType.hybrid,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
    max_results=10,
)

Handling Uncertainty

ScenarioBehavior
Not in docsStates “I don’t have information about X”
AmbiguousAsks for clarification or lists options
Partial matchProvides what’s known, notes limitations
Conflicting sourcesNotes discrepancy, cites both sources

Sample Documents

The knowledge base includes sample company documents:
DocumentContentUse Case
employee_handbook.mdPTO, benefits, policiesPolicy questions
engineering_wiki.mdSetup, coding standardsTechnical questions
onboarding_checklist.mdFirst week tasksNew hire questions
product_guide.mdPlatform features, APIProduct questions

Troubleshooting

Ensure PostgreSQL is running:
docker ps | grep pgvector
./cookbook/scripts/run_pgvector.sh
Ensure the knowledge base is loaded:
python cookbook/01_showcase/01_agents/knowledge_agent/scripts/load_knowledge.py
The agent relies on the knowledge base content. Add more relevant documents or improve existing document quality.

Source Code