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:
Retrieve relevant information from provided documents
Augment the model’s knowledge with this specific information
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.
You can create a new agent with RAG knowledge in two steps:
Copy
Ask AI
// Step 1: Upload a documentconst file = /* your file object */;const uploadResult = await truffle.uploadRAGFile(file);// Step 2: Create an agent with the document IDconst 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});
Once you’ve created or updated an agent with RAG capabilities, you can query it normally:
Copy
Ask AI
// The agent will now reference information from your documentconst result = await agent.run('Summarize the key information from the document.');console.log(result);// You can also use chat for interactive conversations about document contentconst chat = agent.chat();const response = await chat.send('What are the main topics covered in the document?');console.log(response);
Here’s a complete example of creating a knowledge base agent:
Copy
Ask AI
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;}
Document Preparation: For best results, ensure your documents are well-structured and contain relevant information.
Specific Instructions: When creating agents that use RAG, include specific instructions about how to leverage the document knowledge.
Document Size: There are limits to document size and processing. Larger documents may take longer to process.
Multiple Documents: You can upload multiple documents and associate different documents with different agents based on their needs.
Environment Compatibility: Remember that RAG file uploads have different implementations in browser and Node.js environments. Use the appropriate format for your environment.