> ## Documentation Index
> Fetch the complete documentation index at: https://spacesail.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting Started with Knowledge

> Build your first knowledge-powered agent in three simple steps with this hands-on tutorial.

Ready to build your first intelligent agent? This guide will walk you through creating a knowledge-powered agent that can answer questions about your documents in just a few minutes.

## What You'll Build

By the end of this tutorial, you'll have an agent that can:

* Read and understand your documents or website content
* Answer specific questions based on that information
* Provide sources for its responses
* Search intelligently without you having to specify what to look for

## Prerequisites

<Steps>
  <Step title="Install Agno">
    ```bash theme={null}
    pip install agno
    ```
  </Step>

  <Step title="Set up your API key">
    ```bash theme={null}
    export OPENAI_API_KEY="your-api-key-here"
    ```

    <Note>This tutorial uses OpenAI, but Agno supports [many other models](/concepts/models/overview).</Note>
  </Step>
</Steps>

## Step 1: Set Up Your Knowledge Base

First, let's create a knowledge base with a vector database to store your information:

```python knowledge_agent.py theme={null}
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector
from agno.models.openai import OpenAIChat

# Create a knowledge base with PgVector
knowledge = Knowledge(
    vector_db=PgVector(
        table_name="knowledge_documents",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"
    ),
)

# Create an agent with knowledge
agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    knowledge=knowledge,
    # Enable automatic knowledge search
    search_knowledge=True,
    instructions=[
        "Always search your knowledge base before answering questions",
        "Include source references in your responses when possible"
    ]
)
```

<Accordion title="Don't have PostgreSQL? Use LanceDB instead">
  For a quick start without setting up PostgreSQL, use LanceDB which stores data locally:

  ```python theme={null}
  from agno.vectordb.lancedb import LanceDb

  # Local vector storage - no database setup required
  knowledge = Knowledge(
      vector_db=LanceDb(
          table_name="knowledge_documents",
          uri="tmp/lancedb"  # Local directory for storage
      ),
  )
  ```
</Accordion>

## Step 2: Add Your Content

Now let's add some knowledge to your agent. You can add content from various sources:

<Tabs>
  <Tab title="From Local Files">
    ```python theme={null}
    # Add a specific file
    knowledge.add_content(
        path="path/to/your/document.pdf"
    )

    # Add an entire directory
    knowledge.add_content(
        path="path/to/documents/"
    )
    ```
  </Tab>

  <Tab title="From URLs">
    ```python theme={null}
    # Add content from a website
    knowledge.add_content(
        url="https://docs.agno.com/introduction"
    )

    # Add a PDF from the web
    knowledge.add_content(
        url="https://example.com/document.pdf"
    )
    ```
  </Tab>

  <Tab title="From Text">
    ```python theme={null}
    # Add text content directly
    knowledge.add_content(
        text_content="""
        Company Policy: Remote Work Guidelines

        1. Remote work is available to all full-time employees
        2. Employees must maintain regular communication with their team
        3. Home office equipment is provided up to $1000 annually
        """
    )
    ```
  </Tab>
</Tabs>

## Step 3: Chat with Your Agent

That's it! Your agent is now ready to answer questions based on your content:

```python theme={null}
# Test your knowledge-powered agent
if __name__ == "__main__":
    # Your agent will automatically search its knowledge to answer
    agent.print_response(
        "What is the company policy on remote work?",
        stream=True
    )
```

### Complete Example

Here's the full working example:

```python knowledge_agent.py theme={null}
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.lancedb import LanceDb
from agno.models.openai import OpenAIChat

# Set up knowledge base
knowledge = Knowledge(
    vector_db=LanceDb(
        table_name="my_documents",
        uri="tmp/lancedb"
    ),
)

# Add your content
knowledge.add_content(
    text_content="""
    Agno Knowledge System

    Knowledge allows AI agents to access and search through
    domain-specific information at runtime. This enables
    dynamic few-shot learning and agentic RAG capabilities.

    Key features:
    - Automatic content chunking
    - Vector similarity search
    - Multiple data source support
    - Intelligent retrieval
    """
)

# Create your agent
agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    knowledge=knowledge,
    search_knowledge=True,
    instructions=["Always search knowledge before answering"]
)

# Chat with your agent
if __name__ == "__main__":
    agent.print_response("What is Agno Knowledge?", stream=True)
```

Run it:

```bash theme={null}
python knowledge_agent.py
```

## What Just Happened?

When you ran the code, here's what occurred behind the scenes:

1. **Content Processing**: Your text was chunked into smaller pieces and converted to vector embeddings
2. **Intelligent Search**: The agent analyzed your question and searched for relevant information
3. **Contextual Response**: The agent combined the retrieved knowledge with your question to provide an accurate answer
4. **Source Attribution**: The response is based on your specific content, not generic training data

## Next Steps: Explore Advanced Features

<CardGroup cols={2}>
  <Card title="Content Types" icon="file-lines" href="/concepts/knowledge/content_types">
    Learn about different ways to add content: files, URLs, databases, and more.
  </Card>

  <Card title="Chunking Strategies" icon="scissors" href="/concepts/knowledge/chunking/overview">
    Optimize how your content is broken down for better search results.
  </Card>

  <Card title="Vector Databases" icon="database" href="/concepts/vectordb/overview">
    Choose the right storage solution for your needs and scale.
  </Card>

  <Card title="Search Types" icon="magnifying-glass" href="/concepts/knowledge/core-concepts/search-retrieval">
    Explore different search strategies: vector, keyword, and hybrid search.
  </Card>
</CardGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Agent isn't using knowledge in responses">
    Make sure you set `search_knowledge=True` when creating your agent and consider adding explicit instructions to search the knowledge base.
  </Accordion>

  <Accordion title="Vector database connection errors">
    For local development, try LanceDB instead of PostgreSQL. For production, ensure your database connection string is correct.
  </Accordion>

  <Accordion title="Content not being found in searches">
    Your content might need better chunking. Try different chunking strategies or smaller chunk sizes for more precise retrieval.
  </Accordion>
</AccordionGroup>

<Card title="Ready for Core Concepts?" icon="graduation-cap" href="/concepts/knowledge/core-concepts/knowledge-bases">
  Dive deeper into understanding knowledge bases and how they power intelligent agents
</Card>
