Knowledge Bases Tutorial

This tutorial will guide you through creating, managing, and querying knowledge bases using Dumpling AI.

What are Knowledge Bases?

Knowledge bases in Dumpling AI allow you to store, organize, and query large amounts of unstructured data. They’re powered by vector embeddings, which enable semantic search capabilities to find information based on meaning rather than just keywords.

Key use cases include:

  • Creating chatbots that can answer questions from your documentation
  • Building internal search engines for company wikis or knowledge repositories
  • Developing tools that can analyze and extract insights from large collections of text

Prerequisites

Before you begin, make sure you have:

  • A Dumpling AI account with an API key
  • Basic knowledge of HTTP requests
  • A code editor and environment for running examples (Node.js, Python, etc.)

Creating a Knowledge Base

The first step is to create a knowledge base to store your information:

const axios = require('axios');

async function createKnowledgeBase(name, description) {
  try {
    const response = await axios.post(
      'https://app.dumplingai.com/api/v1/knowledge-bases',
      {
        name: name,
        description: description
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer YOUR_API_KEY'
        }
      }
    );
    
    console.log('Knowledge Base Created:', response.data);
    return response.data.id; // Save this ID for later use
  } catch (error) {
    console.error('Error:', error.response ? error.response.data : error.message);
  }
}

// Create a new knowledge base
const kbId = await createKnowledgeBase(
  'Product Documentation',
  'Contains all product documentation for customer support'
);

Adding Content to a Knowledge Base

Once you have a knowledge base, you can add content to it:

async function addToKnowledgeBase(knowledgeBaseId, content, metadata = {}) {
  try {
    const response = await axios.post(
      'https://app.dumplingai.com/api/v1/knowledge-bases/add',
      {
        knowledgeBaseId: knowledgeBaseId,
        content: content,
        metadata: metadata
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer YOUR_API_KEY'
        }
      }
    );
    
    console.log('Content Added:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error:', error.response ? error.response.data : error.message);
  }
}

// Add content to the knowledge base
await addToKnowledgeBase(
  kbId,
  'Our product offers real-time analytics with customizable dashboards. Users can track key metrics and set up alerts for important thresholds.',
  {
    title: 'Analytics Features',
    section: 'Features',
    author: 'Product Team',
    lastUpdated: new Date().toISOString()
  }
);

Adding Documents

You can also add entire documents to your knowledge base. Dumpling AI will automatically process and chunk the document content:

async function addDocumentToKnowledgeBase(knowledgeBaseId, documentUrl, metadata = {}) {
  try {
    const response = await axios.post(
      'https://app.dumplingai.com/api/v1/knowledge-bases/add-document',
      {
        knowledgeBaseId: knowledgeBaseId,
        documentUrl: documentUrl,
        metadata: metadata
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer YOUR_API_KEY'
        }
      }
    );
    
    console.log('Document Added:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error:', error.response ? error.response.data : error.message);
  }
}

// Add a document to the knowledge base
await addDocumentToKnowledgeBase(
  kbId,
  'https://example.com/product-documentation.pdf',
  {
    title: 'Product Documentation PDF',
    version: '1.0',
    author: 'Documentation Team',
    added: new Date().toISOString()
  }
);

Querying a Knowledge Base

Now that you have content in your knowledge base, you can query it to find relevant information:

async function queryKnowledgeBase(knowledgeBaseId, query, maxResults = 5) {
  try {
    const response = await axios.post(
      'https://app.dumplingai.com/api/v1/knowledge-bases/search',
      {
        knowledgeBaseId: knowledgeBaseId,
        query: query,
        maxResults: maxResults
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer YOUR_API_KEY'
        }
      }
    );
    
    console.log('Search Results:', response.data);
    return response.data.results;
  } catch (error) {
    console.error('Error:', error.response ? error.response.data : error.message);
  }
}

// Query the knowledge base
const results = await queryKnowledgeBase(
  kbId,
  'How can I set up alerts for metrics?',
  3
);

// Display results
results.forEach((result, index) => {
  console.log(`Result ${index + 1} (Score: ${result.score}):`);
  console.log(result.content);
  console.log('Metadata:', result.metadata);
  console.log('---');
});

Building a Simple Q&A System

You can combine knowledge base queries with AI completions to build a simple Q&A system:

async function answerQuestion(knowledgeBaseId, question) {
  // First, query the knowledge base for relevant information
  const results = await queryKnowledgeBase(knowledgeBaseId, question, 3);
  
  // Extract content from results to use as context
  const context = results.map(result => result.content).join('\n\n');
  
  // Use the agent completion endpoint to generate an answer
  try {
    const response = await axios.post(
      'https://app.dumplingai.com/api/v1/agents/generate-completion',
      {
        messages: [
          {
            role: 'system',
            content: `You are a helpful assistant answering questions based on the following information. Only use this information to answer the question. If you don't know the answer, say so.\n\nContext:\n${context}`
          },
          {
            role: 'user',
            content: question
          }
        ],
        agentId: 'your_agent_id',  // Replace with your agent ID
        parseJson: false
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer YOUR_API_KEY'
        }
      }
    );
    
    console.log('Answer:', response.data.text);
    return response.data.text;
  } catch (error) {
    console.error('Error:', error.response ? error.response.data : error.message);
  }
}

// Answer a question using the knowledge base
const answer = await answerQuestion(
  kbId,
  'What kind of alerts can I set up in the dashboard?'
);

console.log('Final Answer:', answer);

Best Practices

  1. Organize your content: Group related information in the same knowledge base.
  2. Include metadata: Add detailed metadata to help with filtering and organizing results.
  3. Keep content focused: Each chunk of content should focus on a specific topic.
  4. Update regularly: Keep your knowledge base updated as information changes.
  5. Test your queries: Regularly test different types of queries to ensure your system works as expected.

Conclusion

You’ve learned how to create knowledge bases, add content, and query them using Dumpling AI. Knowledge bases are a powerful tool for building information retrieval systems, Q&A applications, and chatbots that can access your organization’s information.

Next Steps