RAG Quick Start

Build retrieval-augmented agents that answer from your documents and knowledge bases

RAG agents retrieve relevant chunks from your documents before answering, grounding responses in your own knowledge.

python
from praisonaiagents import Agent

agent = Agent(
    name="Knowledge Agent",
    instructions="Answer from the provided knowledge only.",
    knowledge=["small.pdf"],
)

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

The user asks from their documents; the agent retrieves relevant chunks and grounds the reply.

Diagram

Documents

Vector DB

Question

RAG Agent

Grounded answer

Every stage below is now configurable from KnowledgeConfig after the fields were wired up.

Diagram

Chunk
chunking_strategy

Store
vector_store

Retrieve
retrieval_k

Rerank
rerank_model

Generate
auto_retrieve

Quick Start

  1. Simple Usage

    Pass file paths or directories — the agent indexes on first run, then retrieves on each query:

    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Knowledge Agent",
        instructions="Answer from the knowledge base.",
        knowledge=["small.pdf"],
    )
    
    agent.start("What is KAG in one line?")
  2. With Configuration

    Use KnowledgeConfig for vector store, chunking, and reranking:

    python
    from praisonaiagents import Agent, KnowledgeConfig
    
    agent = Agent(
        name="Knowledge Agent",
        instructions="Answer from the provided knowledge.",
        knowledge=KnowledgeConfig(
            sources=["small.pdf"],
            vector_store={
                "provider": "chroma",
                "config": {"collection_name": "praison", "path": ".praison"},
            },
            retrieval_k=5,
            rerank=True,
        ),
    )
    
    agent.start("What is KAG in one line?")

With a different embedder provider

Set embedder_config to embed with Gemini, Cohere, or a local Ollama model instead of OpenAI.

Gemini

python
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    instructions="Answer from the knowledge base.",
    knowledge=KnowledgeConfig(
        sources=["small.pdf"],
        embedder_config={
            "provider": "gemini",
            "config": {"model": "models/text-embedding-004"},
        },
    ),
)

Cohere

python
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    instructions="Answer from the knowledge base.",
    knowledge=KnowledgeConfig(
        sources=["small.pdf"],
        embedder="cohere",
    ),
)

Ollama

python
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    instructions="Answer from the knowledge base.",
    knowledge=KnowledgeConfig(
        sources=["small.pdf"],
        embedder_config={
            "provider": "ollama",
            "config": {"model": "nomic-embed-text"},
        },
    ),
)

Pass retrieval tuning directly to Agent

retrieval_config= tunes retrieval without wrapping sources in KnowledgeConfig. A dict merges on top of what knowledge= already derived; a RetrievalConfig instance replaces it wholesale.

Diagram
RetrievalAgentUserRetrievalAgentUserAgent(…, retrieval_config={“top_k”: 5, “citations”: True})Resolve config (merge over knowledge=)Top-k chunks + citationsGrounded answer

A dict is the quickest form — tune a few keys and keep everything knowledge= already set:

python
from praisonaiagents import Agent

agent = Agent(
    name="Knowledge Agent",
    instructions="Answer from the knowledge base.",
    knowledge=["small.pdf"],
    retrieval_config={"citations": True, "top_k": 5},
)

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

For the fully-typed form, pass a RetrievalConfig instance:

python
from praisonaiagents import Agent, RetrievalConfig

agent = Agent(
    name="Knowledge Agent",
    instructions="Answer from the knowledge base.",
    knowledge=["small.pdf"],
    retrieval_config=RetrievalConfig(top_k=5, citations=True, rerank=True),
)

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

Without knowledge= at all, the dict is used as-is and resolved to a RetrievalConfig:

python
from praisonaiagents import Agent

agent = Agent(
    name="Knowledge Agent",
    instructions="Answer from indexed knowledge.",
    retrieval_config={"top_k": 3},
)

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

How dict merge works

A dict patches individual keys on top of the config derived from knowledge=; a RetrievalConfig instance skips the merge and replaces it entirely.

Diagram

knowledge=KnowledgeConfig(retrieval_k=20, rerank=True)

Resolved dict

retrieval_config={top_k: 3}

{top_k: 3, rerank: True}

You passknowledge= given?Result
retrieval_config={...} (dict)yesdict-merge — your keys override the derived keys
retrieval_config={...} (dict)nodict used as-is, then resolved to RetrievalConfig
retrieval_config=RetrievalConfig(...)yes or noreplaces the derived config wholesale

Every RetrievalConfig field

Full RetrievalConfig field reference
FieldTypeDefaultNotes
enabledboolTrueWhether retrieval runs
policyRetrievalPolicyAUTOAUTO / ALWAYS / NEVER
top_kint5Chunks to retrieve
min_scorefloat0.0Minimum relevance score
max_context_tokensint4000Retrieved-context cap
rerankboolFalseRerank retrieved chunks
rerank_modelOptional[str]None"provider/model" form (e.g. "cohere/rerank-v3")
hybridboolFalseDense + keyword retrieval
citationsboolTrueInclude source citations
citations_modeCitationsModeAPPENDAPPEND / INLINE / HIDDEN
model_context_windowOptional[int]NoneAuto-detected if None
reserved_response_tokensint4096Budget reserved for the response
dynamic_budgetboolTrueUse dynamic token-budget calc
strategystr"auto"auto / direct / basic / hybrid / reranked / compressed / hierarchical
compressboolFalseCompress retrieved context
compression_ratiofloat0.5Target compression ratio
include_globOptional[list]NoneGlob patterns to include
exclude_globOptional[list]NoneGlob patterns to exclude
path_filterOptional[str]NoneRegex path filter
chunking_strategystr"recursive"Chunker type at ingest
chunk_sizeint512Target chunk size (tokens)
chunk_overlapint50Overlap between chunks
vector_store_providerstr"chroma"chroma, mongodb, …
vector_store_configDict[str, Any]{}Provider-specific config
collection_nameOptional[str]NoneCollection/index name
persist_pathstrproject data dirResolved in __post_init__
embedder_providerOptional[str]NoneShorthand provider
embedder_modelOptional[str]NoneShorthand model
embedder_configOptional[Dict[str, Any]]NoneFull mem0 embedder dict
auto_keywordsfrozensetquestion words setKeywords that trigger retrieval in AUTO
auto_min_lengthint10Min query length for AUTO
context_templatestr<retrieved_context>…</retrieved_context>Template for injected context
system_separationboolTrueSystem-prompt separation for safety

How It Works

Diagram
LLMKnowledgeStoreAgentUserLLMKnowledgeStoreAgentUserQuestionEmbed query + searchRelevant chunks (top-k)Prompt + retrieved contextGrounded answerResponse
PhaseWhat happens
1. IndexDocuments are chunked and embedded into the vector store on first access
2. RetrieveUser query is embedded; top-k similar chunks are returned
3. GenerateRetrieved context is injected before the LLM prompt; LLM answers from your data

For pre-indexed stores, pass vector config via task context or call agent.retrieve("query") directly.


Configuration Options

OptionTypeDefaultDescription
sourcesList[str][]Files, directories, or URLs to index
embedderstr"openai"Provider shorthand; "openai" means “no override”. See Use a non-OpenAI embedder.
embedder_configdictNoneFull embedder dict {"provider": ..., "config": {...}}
chunking_strategystr \ChunkingStrategy"recursive"Chunking method — see Chunking Strategies
chunk_sizeint512Target chunk size in tokens
chunk_overlapint50Overlap between adjacent chunks
chunkerdictNoneFull chunker dict — alternative to the three fields above: {"type": "token", "chunk_size": 512, "chunk_overlap": 50}
vector_storedictNoneBackend provider config {"provider": "chroma", "config": {"collection_name": "...", "path": "..."}}
retrieval_kint5Chunks retrieved per query
retrieval_thresholdfloat0.0Minimum similarity score
rerankboolFalseRerank retrieved chunks after search
rerank_modelstrNoneReranker model — use "provider/model" format (e.g. "cohere/rerank-v3"); provider is required for the reranker to load
auto_retrieveboolTrueAutomatically inject retrieved context into the prompt
configdictNoneAdvanced override — merged on top of the resolved config (does not replace it)

Install knowledge extras: pip install "praisonaiagents[knowledge]"


How config= overrides work

config={...} merges on top of the values resolved from KnowledgeConfig fields — it patches individual keys instead of replacing the whole config.

python
# Resolved by KnowledgeConfig fields:  {retrieval_k: 20, min_score: 0.4, rerank: True}
# config={"top_k": 3}                  overrides just top_k
# Final:                               {retrieval_k: 3,  min_score: 0.4, rerank: True}
Diagram

KnowledgeConfig fields

Resolved dict

config={…}

Retrieval


Every knob wired up

Chunking, vector store, retrieval, and reranking are all set from one KnowledgeConfig.

python
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    name="Research Agent",
    instructions="Answer only from the provided knowledge.",
    knowledge=KnowledgeConfig(
        sources=["research_paper.pdf"],

        # Chunking (three fields, or use chunker={...} for the same thing)
        chunking_strategy="recursive",
        chunk_size=512,
        chunk_overlap=50,

        # Vector store
        vector_store={
            "provider": "chroma",
            "config": {"collection_name": "research", "path": ".praison"},
        },

        # Retrieval
        retrieval_k=5,
        retrieval_threshold=0.35,
        auto_retrieve=True,

        # Reranking — model must be "provider/model" for the reranker to load
        rerank=True,
        rerank_model="cohere/rerank-v3",
    ),
)

agent.start("What are the main findings?")

Opting out of automatic context injection

Set auto_retrieve=False to retrieve manually instead of injecting context on every prompt.

python
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    instructions="Retrieve on your own; do not auto-inject.",
    knowledge=KnowledgeConfig(sources=["docs/"], auto_retrieve=False),
)

# Do it manually when you need it:
context = agent.retrieve("What are the deployment steps?")
answer = agent.chat_with_context("Summarise", context)

Best Practices

Index documents before querying

Pass sources via knowledge=["file.pdf"] at agent creation — first run indexes, later runs retrieve without re-indexing.

Use specific questions

Narrow questions retrieve better chunks than broad prompts like “tell me everything”.

Use persistent vector stores in production

Set vector_store with Chroma path, Qdrant, or Pinecone — avoid in-memory stores for production.

Combine RAG with tools for live data

Pair knowledge= with web tools when you need both static documents and real-time data.


Related