Skip to main content

RAG Agent

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

Installation

npm install praisonai-ts

Quick Start

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

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)

import { createMemoryVectorStore } from 'praisonai-ts';

const store = createMemoryVectorStore();

Pinecone

import { createPineconeStore } from 'praisonai-ts';

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

Weaviate

import { createWeaviateStore } from 'praisonai-ts';

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

Qdrant

import { createQdrantStore } from 'praisonai-ts';

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

ChromaDB

import { createChromaStore } from 'praisonai-ts';

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

Adding Documents

// 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:
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:
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

VariableRequiredDescription
OPENAI_API_KEYYesFor embeddings and LLM
PINECONE_API_KEYFor PineconePinecone API key
COHERE_API_KEYFor rerankingCohere API key