RAG Quick Start

Learn how to configure retrieval behavior for AI agents with knowledge bases.

Diagram

Query

Input

RAG Agent

Vector DB

Knowledge Task

Output

A knowledge-centric workflow where RAG (Retrieval Augmented Generation) agents interact with vector databases to store and retrieve information efficiently, enabling sophisticated question-answering and information retrieval capabilities.

Quick Start

  1. Install Package

    Install PraisonAI Agents with knowledge support:

    bash
    pip install "praisonaiagents[knowledge]"
  2. Set API Key

    Set your OpenAI API key:

    bash
    export OPENAI_API_KEY=xxxxx
  3. Create Script

    Create a new file app.py:

    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Knowledge Agent",
        instructions="You answer questions based on the provided knowledge.",
        knowledge=["small.pdf"], # Indexing
    )
    
    agent.start("What is KAG in one line?") # Retrieval

Data Indexing and Retrieval Agents

Diagram

Agents

Vector Store

Vector DB

Input

PDF

TXT

MD

Agent 1

Agent 2

Agent 3

The simplest way to create a knowledge-based agent is without any configuration:

python
from praisonaiagents import Agent

agent = Agent(
    name="Knowledge Agent",
    instructions="You answer questions based on the provided knowledge.",
    knowledge=["small.pdf"] # Indexing
)

agent.start("What is KAG in one line?") # Retrieval

Advanced Configuration

For more control over the knowledge base, you can specify a configuration:

python
from praisonaiagents import Agent

config = {
    "vector_store": {
        "provider": "chroma",
        "config": {
            "collection_name": "praison",
            "path": ".praison",
        }
    }
}

agent = Agent(
    name="Knowledge Agent",
    instructions="You answer questions based on the provided knowledge.",
    knowledge={
        "sources": ["small.pdf"],
        **config
    }
)

agent.start("What is KAG in one line?") # Retrieval

Multi-Agent Knowledge System

For more complex scenarios, you can create a knowledge-based system with multiple agents:

python
from praisonaiagents import Agent, Task, AgentTeam
import logging
import os

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# Define the configuration for the Knowledge instance
config = {
    "vector_store": {
        "provider": "chroma",
        "config": {
            "collection_name": "praison",
            "path": ".praison",
        }
    }
}

# Create an agent with knowledge capabilities
knowledge_agent = Agent(
    name="KnowledgeAgent",
    role="Information Specialist",
    goal="Store and retrieve knowledge efficiently",
    backstory="Expert in managing and utilizing stored knowledge",
    knowledge={
        "sources": ["sample.pdf"],
        **config
    }
)

# Define a task for the agent
knowledge_task = Task(
    name="knowledge_task",
    description="Who is Mervin Praison?",
    expected_output="Answer to the question",
    agent=knowledge_agent # Agent
)

# Create and start the agents
agents = AgentTeam(
    agents=[knowledge_agent],
    tasks=[knowledge_task],
    process="sequential",
    memory={"user_id": "user1"} # User ID
)

# Start execution
result = agents.start() # Retrieval

Retrieval Agents

  1. Install Package

    Retrieval uses a vector store, so install the knowledge extra:

    bash
    pip install "praisonaiagents[knowledge]"
  2. Set API Key

    Set your OpenAI API key as an environment variable in your terminal:

    bash
    export OPENAI_API_KEY=your_api_key_here
  3. Create a file

    Create a new file rag_agent.py with the basic setup:

    python
    from praisonaiagents import Agent, Task, AgentTeam
    
    # Define the configuration for the Knowledge instance
    config = {
        "vector_store": {
            "provider": "chroma",
            "config": {
                "collection_name": "praison",
                "path": ".praison"
            }
        }
    }
    
    # Create an agent
    rag_agent = Agent(
        name="RAG Agent",
        role="Information Specialist",
        goal="Retrieve knowledge efficiently",
        llm="gpt-4o-mini"
    )
    
    # Define a task for the agent
    rag_task = Task(
        name="RAG Task",
        description="What is KAG?",
        expected_output="Answer to the question",
        agent=rag_agent,
        context=[config] # Retrieval : Vector Database provided as context
    )
    
    # Build Agents
    agents = AgentTeam(
        agents=[rag_agent],
        tasks=[rag_task],
        memory={"user_id": "user1"}
    )
    
    # Start Agents
    agents.start()
  4. Start Agents

    Type this in your terminal to run your agents:

    bash
    python rag_agent.py

Adding Knowledge to RAG Agents

python
from praisonaiagents import Agent

config = {
    "vector_store": {
        "provider": "chroma",
        "config": {
            "collection_name": "praison",
            "path": ".praison",
        }
    }
}

agent = Agent(
    name="Knowledge Agent",
    instructions="You answer questions based on the provided knowledge.",
    knowledge={
        "sources": ["small.pdf"],
        **config
    }
)

agent.start("What is KAG in one line?") # Retrieval

Understanding RAG Agents

What are RAG Agents?

RAG (Retrieval Augmented Generation) agents enable: – Efficient knowledge retrieval – Semantic search capabilities – Persistent knowledge storage – Context-aware responses

Features

Troubleshooting

Next Steps