Skip to main content

Agent Knowledge Base

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

Agent with Knowledge Base

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."

Agent with Document Loading

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

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

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

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
});