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

# Knowledge Base (RAG)

> Give Agents access to your documents and data

Give your Agents access to documents, FAQs, and data. Agents automatically search the knowledge base to answer questions accurately.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    Agent[Agent] --> KB[Knowledge Base]
    KB --> Answer([Answer])

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff

    class Agent,KB agent
    class Answer tool
    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff

```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import { Agent, createKnowledgeBase } from 'praisonai';

    // Create knowledge base with your documents
    const kb = createKnowledgeBase();
    await kb.add({ id: 'faq1', content: 'Our return policy allows returns within 30 days.' });
    await kb.add({ id: 'faq2', content: 'Shipping takes 3-5 business days.' });
    await kb.add({ id: 'faq3', content: 'Contact support at help@example.com.' });

    // Agent uses knowledge base to answer questions
    const agent = new Agent({
      name: 'Support Agent',
      instructions: 'Answer customer questions using the knowledge base.',
      knowledgeBase: kb
    });

    await agent.chat('What is your return policy?');
    // Agent searches KB and responds: "Our return policy allows returns within 30 days."

    await agent.chat('How long does shipping take?');
    // Agent responds: "Shipping takes 3-5 business days."
    ```
  </Step>

  <Step title="With Configuration">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    const kb = createKnowledgeBase({ embeddings: 'openai/text-embedding-3-small' });

    const agent = new Agent({
      name: 'Support Agent',
      instructions: 'Answer customer questions using the knowledge base.',
      knowledgeBase: kb,
      verbose: true,
    });
    ```
  </Step>
</Steps>

## Agent with Document Loading

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

const kb = createKnowledgeBase();

// Load multiple documents
await kb.addBatch([
  { id: 'product-1', content: 'Product A: $99, wireless headphones with 20hr battery' },
  { id: 'product-2', content: 'Product B: $149, noise-canceling headphones with 30hr battery' },
  { id: 'product-3', content: 'Product C: $199, premium headphones with 40hr battery and ANC' }
]);

const salesAgent = new Agent({
  name: 'Sales Agent',
  instructions: 'Help customers find the right product based on their needs.',
  knowledgeBase: kb
});

await salesAgent.chat('I need headphones with long battery life');
// Agent searches and recommends Product C with 40hr battery
```

## Multi-Agent Shared Knowledge

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

// Shared knowledge base for the team
const companyKB = createKnowledgeBase();
await companyKB.addBatch([
  { id: 'policy-1', content: 'Vacation policy: 20 days PTO per year' },
  { id: 'policy-2', content: 'Remote work: Hybrid model, 3 days in office' },
  { id: 'tech-1', content: 'Tech stack: TypeScript, React, PostgreSQL' }
]);

// HR Agent uses company knowledge
const hrAgent = new Agent({
  name: 'HR Assistant',
  instructions: 'Answer HR and policy questions.',
  knowledgeBase: companyKB
});

// Tech Agent uses same knowledge
const techAgent = new Agent({
  name: 'Tech Assistant',
  instructions: 'Answer technical questions about our stack.',
  knowledgeBase: companyKB
});

await hrAgent.chat('How many vacation days do I get?');
await techAgent.chat('What database do we use?');
```

## Agent with RAG Tool

Give Agent explicit control over knowledge search:

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

const kb = createKnowledgeBase();
await kb.add({ id: 'doc1', content: 'Important company information...' });

// Tool to search knowledge base
const searchKBTool = createTool({
  name: 'search_knowledge',
  description: 'Search the knowledge base for relevant information',
  parameters: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'Search query' }
    },
    required: ['query']
  },
  execute: async ({ query }) => {
    const results = await kb.search(query, 3);
    return results.map(r => r.document.content).join('\n');
  }
});

const agent = new Agent({
  name: 'Research Agent',
  instructions: 'Use search_knowledge to find information before answering.',
  tools: [searchKBTool]
});

await agent.chat('What do you know about the company?');
```

## Document Operations

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// Get document
const doc = kb.get('doc1');

// Delete document
kb.delete('doc1');

// List all documents
const allDocs = kb.list();

// Get count
console.log('Documents:', kb.size);

// Clear all
kb.clear();
```

## Custom Embedding Provider

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

const customEmbedder: EmbeddingProvider = {
  async embed(text: string): Promise<number[]> {
    // Your embedding logic
    return await callEmbeddingAPI(text);
  },
  async embedBatch(texts: string[]): Promise<number[][]> {
    return Promise.all(texts.map(t => this.embed(t)));
  }
};

const kb = new KnowledgeBase({
  embeddingProvider: customEmbedder,
  similarityThreshold: 0.7,
  maxResults: 5
});
```

## Related

<CardGroup cols={2}>
  <Card title="Knowledge Base CLI" icon="terminal" href="/docs/js/knowledge-base-cli">
    CLI knowledge commands
  </Card>

  <Card title="Vector Stores" icon="database" href="/docs/js/vector-stores">
    Scalable document storage
  </Card>
</CardGroup>
