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.
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.
Every stage below is now configurable from KnowledgeConfig after the fields were wired up.
Quick Start
- 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?") - With Configuration
Use
KnowledgeConfigfor 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
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
from praisonaiagents import Agent, KnowledgeConfig
agent = Agent(
instructions="Answer from the knowledge base.",
knowledge=KnowledgeConfig(
sources=["small.pdf"],
embedder="cohere",
),
)Ollama
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.
A dict is the quickest form — tune a few keys and keep everything knowledge= already set:
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:
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:
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.
| You pass | knowledge= given? | Result |
|---|---|---|
retrieval_config={...} (dict) | yes | dict-merge — your keys override the derived keys |
retrieval_config={...} (dict) | no | dict used as-is, then resolved to RetrievalConfig |
retrieval_config=RetrievalConfig(...) | yes or no | replaces the derived config wholesale |
Every RetrievalConfig field
Full RetrievalConfig field reference
| Field | Type | Default | Notes |
|---|---|---|---|
enabled | bool | True | Whether retrieval runs |
policy | RetrievalPolicy | AUTO | AUTO / ALWAYS / NEVER |
top_k | int | 5 | Chunks to retrieve |
min_score | float | 0.0 | Minimum relevance score |
max_context_tokens | int | 4000 | Retrieved-context cap |
rerank | bool | False | Rerank retrieved chunks |
rerank_model | Optional[str] | None | "provider/model" form (e.g. "cohere/rerank-v3") |
hybrid | bool | False | Dense + keyword retrieval |
citations | bool | True | Include source citations |
citations_mode | CitationsMode | APPEND | APPEND / INLINE / HIDDEN |
model_context_window | Optional[int] | None | Auto-detected if None |
reserved_response_tokens | int | 4096 | Budget reserved for the response |
dynamic_budget | bool | True | Use dynamic token-budget calc |
strategy | str | "auto" | auto / direct / basic / hybrid / reranked / compressed / hierarchical |
compress | bool | False | Compress retrieved context |
compression_ratio | float | 0.5 | Target compression ratio |
include_glob | Optional[list] | None | Glob patterns to include |
exclude_glob | Optional[list] | None | Glob patterns to exclude |
path_filter | Optional[str] | None | Regex path filter |
chunking_strategy | str | "recursive" | Chunker type at ingest |
chunk_size | int | 512 | Target chunk size (tokens) |
chunk_overlap | int | 50 | Overlap between chunks |
vector_store_provider | str | "chroma" | chroma, mongodb, … |
vector_store_config | Dict[str, Any] | {} | Provider-specific config |
collection_name | Optional[str] | None | Collection/index name |
persist_path | str | project data dir | Resolved in __post_init__ |
embedder_provider | Optional[str] | None | Shorthand provider |
embedder_model | Optional[str] | None | Shorthand model |
embedder_config | Optional[Dict[str, Any]] | None | Full mem0 embedder dict |
auto_keywords | frozenset | question words set | Keywords that trigger retrieval in AUTO |
auto_min_length | int | 10 | Min query length for AUTO |
context_template | str | <retrieved_context>…</retrieved_context> | Template for injected context |
system_separation | bool | True | System-prompt separation for safety |
How It Works
| Phase | What happens |
|---|---|
| 1. Index | Documents are chunked and embedded into the vector store on first access |
| 2. Retrieve | User query is embedded; top-k similar chunks are returned |
| 3. Generate | Retrieved 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
| Option | Type | Default | Description | |
|---|---|---|---|---|
sources | List[str] | [] | Files, directories, or URLs to index | |
embedder | str | "openai" | Provider shorthand; "openai" means “no override”. See Use a non-OpenAI embedder. | |
embedder_config | dict | None | Full embedder dict {"provider": ..., "config": {...}} | |
chunking_strategy | str \ | ChunkingStrategy | "recursive" | Chunking method — see Chunking Strategies |
chunk_size | int | 512 | Target chunk size in tokens | |
chunk_overlap | int | 50 | Overlap between adjacent chunks | |
chunker | dict | None | Full chunker dict — alternative to the three fields above: {"type": "token", "chunk_size": 512, "chunk_overlap": 50} | |
vector_store | dict | None | Backend provider config {"provider": "chroma", "config": {"collection_name": "...", "path": "..."}} | |
retrieval_k | int | 5 | Chunks retrieved per query | |
retrieval_threshold | float | 0.0 | Minimum similarity score | |
rerank | bool | False | Rerank retrieved chunks after search | |
rerank_model | str | None | Reranker model — use "provider/model" format (e.g. "cohere/rerank-v3"); provider is required for the reranker to load | |
auto_retrieve | bool | True | Automatically inject retrieved context into the prompt | |
config | dict | None | Advanced 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.
# 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}Every knob wired up
Chunking, vector store, retrieval, and reranking are all set from one KnowledgeConfig.
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.
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.