<!-- source: https://deepwiki.com/MervinPraison/PraisonAI/4.3-rag-system -->

# RAG System

- README.md
- api.md
- src/praisonai/README.md

## Purpose and Scope

This document explains the Retrieval Augmented Generation (RAG) system in PraisonAI, which enables agents to retrieve and use information from external documents and knowledge bases. The RAG pipeline processes documents through chunking, embedding, vector storage, retrieval, and reranking to provide agents with relevant context.

For general knowledge management (adding documents, managing sources), see [Knowledge Management](/MervinPraison/PraisonAI/4.2-knowledge-management). For memory systems (conversation history, user preferences), see [Memory Management](/MervinPraison/PraisonAI/4.1-memory-management). For context window management (compression, summarization), see [Context Management](/MervinPraison/PraisonAI/4.4-context-management).

## RAG Pipeline Architecture

The RAG system implements a complete retrieval pipeline that transforms user queries into relevant document context.

### End-to-End Pipeline

```

```

**Pipeline Stages:**

Sources: [README.md113](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L113-L113) [README.md278-285](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L278-L285)

## Agent Integration

The RAG system integrates with agents through the consolidated `knowledge` parameter and dedicated retrieval methods.

### Knowledge Parameter

```

```

**Precedence Ladder:** `Instance > Config > Array > Dict > String > Bool > Default`

Sources: [examples/consolidated_params/basic_knowledge.py1-18](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/basic_knowledge.py#L1-L18) [examples/consolidated_params/README.md23](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/README.md?plain=1#L23-L23)

### Retrieval Methods

```

```

**Method Signatures:**

Sources: [api.md60-63](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L60-L63)

## Chunking System

Chunking splits documents into segments suitable for embedding and retrieval.

### Chunking Strategies

```

```

**Strategy Selection:**

**Default Configuration:**

- Chunk size: 1000 characters
- Overlap: 200 characters
- Metadata: source file, page number, chunk index

Sources: [README.md280](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L280-L280)

## Embedding System

The embedding system converts text chunks into dense vector representations.

### Embedding Architecture

```

```

**Embedding Functions:**

**Signature:** `embedding(input: Union[str, List[str]], model: str = 'text-embedding-3-small', dimensions: Optional[int] = None, ...)`

Sources: [api.md141-145](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L141-L145)

### Supported Embedding Models

```

```

**Model Dimensions:**

Sources: [api.md145](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L145-L145)

## Vector Store Backends

PraisonAI supports 6+ vector database backends for storing and retrieving embeddings.

### Vector Store Architecture

```

```

**Backend Comparison:**

**Configuration Example:**

```

```

Sources: [README.md281](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L281-L281)

## Retrieval Strategies

The retrieval system supports multiple strategies for finding relevant chunks.

### Retrieval Strategy Types

```

```

**Strategy Details:**

**Retrieval Parameters:**

Sources: [README.md282](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L282-L282)

### Index Types

```

```

**Index Algorithms:**

Sources: [README.md284](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L284-L284)

## Reranking System

Reranking improves retrieval precision by reordering candidates using more sophisticated models.

### Reranking Architecture

```

```

**Reranker Comparison:**

**Configuration Example:**

```

```

Sources: [README.md283](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L283-L283)

### Reranking Parameters

Sources: [README.md283](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L283-L283)

## Citations and Sources

The RAG system tracks source documents and provides citations for retrieved information.

### Citation Flow

```

```

**RAGResult Structure:**

```

```

**Citation Metadata Fields:**

Sources: [api.md61](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L61-L61)

## Query Engines

Advanced query processing for complex retrieval scenarios.

### Query Engine Types

```

```

**Query Engine Comparison:**

**Configuration:**

```

```

Sources: [README.md285](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L285-L285)

## Configuration Reference

Complete configuration options for the RAG system.

### Knowledge Configuration

```

```

**Full Configuration Example:**

```

```

Sources: [examples/consolidated_params/basic_knowledge.py1-18](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/basic_knowledge.py#L1-L18) [examples/consolidated_params/README.md1-96](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/README.md?plain=1#L1-L96)

### Configuration Precedence

The knowledge parameter follows the standard precedence ladder:

```
Instance > Config > Array > Dict > String > Bool > Default
```

**Examples by precedence:**

```

```

Sources: [examples/consolidated_params/README.md8-9](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/README.md?plain=1#L8-L9)

## Usage Examples

### Basic RAG with Agent

```

```

Sources: [examples/consolidated_params/basic_knowledge.py10-18](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/basic_knowledge.py#L10-L18)

### Explicit Retrieval

```

```

Sources: [api.md60-63](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L60-L63)

### Advanced RAG Configuration

```

```

Sources: [api.md25](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L25-L25)

### Multi-Document RAG

```

```

Sources: [examples/consolidated_params/basic_knowledge.py10-18](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/basic_knowledge.py#L10-L18)

### RAG with Citations

```

```

Sources: [api.md61](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L61-L61)

## Integration with Other Systems

### RAG + Memory Integration

```

```

The RAG system works alongside memory to provide both document context and conversation history.

```

```

Sources: [README.md274-275](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L274-L275)

### RAG + Context Management

Context management can compress retrieved chunks when token limits are reached.

```

```

Sources: [examples/consolidated_params/basic_context.py1-18](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/basic_context.py#L1-L18)

## Performance Considerations

### Optimization Strategies

### Scaling Recommendations

```

```

**Scaling Guidelines:**

Sources: [README.md281](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L281-L281)

## Troubleshooting

### Common Issues

### Debugging RAG

```

```

Sources: [api.md63](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L63-L63) [examples/consolidated_params/README.md35](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/examples/consolidated_params/README.md?plain=1#L35-L35)

## Related CLI Commands

The CLI provides commands for managing knowledge and testing RAG:

```

```

Sources: [README.md86](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L86-L86) [api.md248-251](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/api.md?plain=1#L248-L251)
