What is RAG?

Retrieval Augmented Generation (RAG) is a technique that enhances AI language models by allowing them to access and utilize external knowledge not included in their training data. Rather than relying solely on information the model learned during training, RAG enables the model to:

  1. Retrieve relevant information from provided documents
  2. Augment the model’s knowledge with this specific information
  3. Generate accurate and contextually relevant responses based on the combined knowledge

This approach offers several key benefits:

  • Customized Knowledge: Empower your AI agents with domain-specific knowledge from your own documents
  • Reduced Hallucinations: Improve accuracy by grounding responses in factual, retrievable content
  • Up-to-date Information: Use the latest information without retraining the underlying model
  • Private Data Handling: Process and utilize information that isn’t in the public domain

For example, you can build:

  • An interactive resume assistant that answers questions about your experience and skills during job interviews
  • A product documentation bot that helps customers navigate your software features
  • A legal document assistant that clarifies terms and implications from contracts
  • A research companion that discusses specific academic papers and their methodologies
  • A knowledge base for your company’s internal procedures and policies

The Truffle AI SDK makes implementing RAG simple, allowing you to upload documents, process them automatically, and create agents that can leverage this knowledge when responding to queries.

How RAG Works in Truffle AI

  1. Document Upload: You upload a document (PDF, DOCX, TXT, etc.) to the Truffle AI platform
  2. Processing: The document is processed, converted to text, and split into manageable chunks
  3. Embedding Creation: Each chunk is converted into vector embeddings that capture semantic meaning
  4. Storage: These embeddings are stored and associated with your agent
  5. Retrieval: When a query is received, the system finds the most relevant document sections
  6. Generation: The AI model generates a response that incorporates this specific knowledge

Implementing RAG in Your Applications

Uploading RAG Files

You can upload documents to be processed and made available for your agents:

// Upload a document file
const uploadResult = await truffle.uploadRAGFile(file);
console.log(`Document uploaded with ID: ${uploadResult.documentId}`);

Browser Environments

In browser environments, you can use standard HTML file input elements:

// Browser file input
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', async (event) => {
  const file = event.target.files[0]; // Standard File object
  const uploadResult = await truffle.uploadRAGFile(file);
  console.log(`Document ID: ${uploadResult.documentId}`);
});

Node.js Environments

In Node.js environments, you’ll need to create a file-like object:

const fs = require('fs');
const path = require('path');

// Read a file using Node.js fs module
const filePath = path.join(__dirname, 'documents', 'knowledge.txt');
const fileBuffer = fs.readFileSync(filePath);

// Create a file-like object for Node.js
const nodeFile = {
  data: fileBuffer,  // Buffer containing file data
  name: 'knowledge.txt',
  type: 'text/plain',
  size: fileBuffer.length
};

// Upload the file
const uploadResult = await truffle.uploadRAGFile(nodeFile);
console.log(`Document ID: ${uploadResult.documentId}`);

Creating RAG-Enabled Agents

Method 1: Create a New Agent with RAG

You can create a new agent with RAG knowledge in two steps:

// Step 1: Upload a document
const file = /* your file object */;
const uploadResult = await truffle.uploadRAGFile(file);

// Step 2: Create an agent with the document ID
const agent = await truffle.deployAgent({
  name: 'Knowledge Assistant',
  instruction: 'You are a helpful assistant that provides information based on the provided documents.',
  model: 'gpt-4o-mini',
  documentId: uploadResult.documentId  // Include the document ID
});

Method 2: Update an Existing Agent with RAG

You can also add RAG capabilities to an existing agent:

// Upload a document
const file = /* your file object */;
const uploadResult = await truffle.uploadRAGFile(file);

// Load an existing agent
const agent = await truffle.loadAgent('your-agent-id');

// Update the agent with the document ID
await agent.update({
  documentId: uploadResult.documentId
});

Querying RAG-Enabled Agents

Once you’ve created or updated an agent with RAG capabilities, you can query it normally:

// The agent will now reference information from your document
const result = await agent.run('Summarize the key information from the document.');
console.log(result);

// You can also use chat for interactive conversations about document content
const chat = agent.chat();
const response = await chat.send('What are the main topics covered in the document?');
console.log(response);

Supported File Types

The following file types are supported for RAG:

  • PDF (.pdf)
  • Microsoft Word (.doc, .docx)
  • Text files (.txt)
  • Markdown (.md)

Example: Document Knowledge Assistant

Here’s a complete example of creating a knowledge base agent:

import { TruffleAI } from 'truffle-ai';
import fs from 'fs';
import path from 'path';

async function createDocumentAssistant() {
  const truffle = new TruffleAI('your-api-key');
  
  // For Node.js: Create a file-like object
  const filePath = path.join(__dirname, 'documents', 'product-manual.pdf');
  const fileBuffer = fs.readFileSync(filePath);
  const file = {
    data: fileBuffer,
    name: 'product-manual.pdf',
    type: 'application/pdf',
    size: fileBuffer.length
  };
  
  // For Browser: Use a standard File object
  // const file = document.getElementById('fileInput').files[0];
  
  // Upload the document
  console.log('Uploading document...');
  const uploadResult = await truffle.uploadRAGFile(file);
  console.log(`Document ID: ${uploadResult.documentId}`);
  
  // Create an agent with the document knowledge
  const agent = await truffle.deployAgent({
    name: 'Product Support',
    instruction: `You are a product support specialist who:
      - Answers questions based on the product manual
      - Provides accurate technical information
      - Explains features and troubleshooting steps
      - Maintains a helpful and clear communication style`,
    model: 'gpt-4o-mini',
    documentId: uploadResult.documentId
  });
  
  // Test the agent with product-specific questions
  const chat = agent.chat();
  const responses = await Promise.all([
    chat.send('How do I set up this product?'),
    chat.send('What are the system requirements?'),
    chat.send('I'm having trouble with feature X. How do I fix it?')
  ]);
  
  responses.forEach((response, i) => {
    console.log(`Q${i+1} Response:`, response);
  });
  
  return agent;
}

Best Practices

  1. Document Preparation: For best results, ensure your documents are well-structured and contain relevant information.

  2. Specific Instructions: When creating agents that use RAG, include specific instructions about how to leverage the document knowledge.

  3. Document Size: There are limits to document size and processing. Larger documents may take longer to process.

  4. Multiple Documents: You can upload multiple documents and associate different documents with different agents based on their needs.

  5. Environment Compatibility: Remember that RAG file uploads have different implementations in browser and Node.js environments. Use the appropriate format for your environment.