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

# RAG Agent

> Build Retrieval-Augmented Generation agents with knowledge bases

# RAG Agent

Build agents that can retrieve and use information from knowledge bases to provide accurate, grounded responses.

## Installation

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
npm install praisonai-ts
```

## Quick Start

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { Agent, createMemoryVectorStore } from 'praisonai-ts';

// Create a vector store for RAG
const vectorStore = createMemoryVectorStore();

// Add documents to the knowledge base
await vectorStore.addDocuments([
  { id: '1', content: 'PraisonAI is an AI agent framework', metadata: { source: 'docs' } },
  { id: '2', content: 'Agents can use tools to accomplish tasks', metadata: { source: 'docs' } },
]);

// Create an agent with RAG capabilities
const agent = new Agent({
  name: 'RAGAgent',
  instructions: 'You answer questions using the knowledge base. Always cite sources.',
  knowledge: vectorStore,
});

const response = await agent.chat('What is PraisonAI?');
console.log(response);
```

## Configuration Options

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { Agent, createPineconeStore } from 'praisonai-ts';

const vectorStore = await createPineconeStore({
  apiKey: process.env.PINECONE_API_KEY!,
  index: 'my-knowledge-base',
  namespace: 'docs',
});

const agent = new Agent({
  name: 'RAGAgent',
  instructions: 'You are a helpful assistant with access to a knowledge base.',
  knowledge: vectorStore,
  ragConfig: {
    topK: 5,                    // Number of results to retrieve
    minScore: 0.7,              // Minimum similarity score
    includeMetadata: true,      // Include document metadata
    rerank: true,               // Enable reranking
    citationFormat: 'inline',   // Citation format: inline, footnote, none
  },
});
```

## Vector Store Options

### Memory Vector Store (Development)

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createMemoryVectorStore } from 'praisonai-ts';

const store = createMemoryVectorStore();
```

### Pinecone

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createPineconeStore } from 'praisonai-ts';

const store = await createPineconeStore({
  apiKey: process.env.PINECONE_API_KEY!,
  index: 'my-index',
  namespace: 'my-namespace',
});
```

### Weaviate

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createWeaviateStore } from 'praisonai-ts';

const store = await createWeaviateStore({
  host: process.env.WEAVIATE_HOST!,
  apiKey: process.env.WEAVIATE_API_KEY,
  className: 'Documents',
});
```

### Qdrant

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createQdrantStore } from 'praisonai-ts';

const store = await createQdrantStore({
  url: process.env.QDRANT_URL!,
  apiKey: process.env.QDRANT_API_KEY,
  collectionName: 'my-collection',
});
```

### ChromaDB

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createChromaStore } from 'praisonai-ts';

const store = await createChromaStore({
  path: './chroma-data',
  collectionName: 'my-collection',
});
```

## Adding Documents

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// Add single document
await vectorStore.addDocument({
  id: 'doc-1',
  content: 'Document content here',
  metadata: { source: 'manual', category: 'guide' },
});

// Add multiple documents
await vectorStore.addDocuments([
  { id: 'doc-2', content: 'First document', metadata: {} },
  { id: 'doc-3', content: 'Second document', metadata: {} },
]);

// Add from file (PDF, TXT, MD)
await vectorStore.addFromFile('./document.pdf');
```

## Reranking

Enable reranking for better retrieval quality:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { Agent, createCohereReranker } from 'praisonai-ts';

const reranker = createCohereReranker({
  apiKey: process.env.COHERE_API_KEY!,
  model: 'rerank-english-v3.0',
});

const agent = new Agent({
  name: 'RAGAgent',
  instructions: 'Answer questions using the knowledge base.',
  knowledge: vectorStore,
  reranker: reranker,
});
```

## Graph RAG

For complex relationships between documents:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createGraphRAG } from 'praisonai-ts';

const graphRAG = await createGraphRAG({
  vectorStore: vectorStore,
  extractEntities: true,
  extractRelationships: true,
});

const agent = new Agent({
  name: 'GraphRAGAgent',
  instructions: 'Answer questions using the knowledge graph.',
  knowledge: graphRAG,
});
```

## Best Practices

1. **Chunk documents appropriately** - Split large documents into meaningful chunks
2. **Use metadata** - Add source, date, category metadata for filtering
3. **Enable reranking** - Improves retrieval quality for complex queries
4. **Set appropriate topK** - Balance between context and relevance
5. **Use citations** - Always cite sources for transparency

## Environment Variables

| Variable           | Required      | Description            |
| ------------------ | ------------- | ---------------------- |
| `OPENAI_API_KEY`   | Yes           | For embeddings and LLM |
| `PINECONE_API_KEY` | For Pinecone  | Pinecone API key       |
| `COHERE_API_KEY`   | For reranking | Cohere API key         |

## Related

* [Knowledge Base](/docs/js/knowledge-base) - Managing knowledge bases
* [Graph RAG](/docs/js/graph-rag) - Graph-based RAG
* [Embeddings](/docs/js/embeddings) - Embedding models
