Retrieval Augmented Generation (RAG)
Enhance your agents with custom document knowledge using RAG
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:
- 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.
How RAG Works in Truffle AI
- Document Upload: You upload a document (PDF, DOCX, TXT, etc.) to the Truffle AI platform
- Processing: The document is processed, converted to text, and split into manageable chunks
- Embedding Creation: Each chunk is converted into vector embeddings that capture semantic meaning
- Storage: These embeddings are stored and associated with your agent
- Retrieval: When a query is received, the system finds the most relevant document sections
- 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:
Browser Environments
In browser environments, you can use standard HTML file input elements:
Node.js Environments
In Node.js environments, you’ll need to create a file-like object:
Creating RAG-Enabled Agents
Method 1: Create a New Agent with RAG
You can create a new agent with RAG knowledge in two steps:
Method 2: Update an Existing Agent with RAG
You can also add RAG capabilities to an existing agent:
Querying RAG-Enabled Agents
Once you’ve created or updated an agent with RAG capabilities, you can query it normally:
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:
Best Practices
-
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.