mobile_rag_engine library
Mobile RAG Engine
A high-performance, on-device RAG (Retrieval-Augmented Generation) engine for Flutter. Run semantic search completely offline on iOS and Android.
Quick Start (Recommended)
import 'package:mobile_rag_engine/mobile_rag_engine.dart';
```dart
import 'package:mobile_rag_engine/mobile_rag_engine.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize (just 1 line!)
await MobileRag.initialize(
tokenizerAsset: 'assets/tokenizer.json',
modelAsset: 'assets/model.onnx',
);
runApp(const MyApp());
}
// Add documents
await MobileRag.instance.addDocument('Your long document text here');
await MobileRag.instance.rebuildIndex();
// Search with LLM context assembly
final result = await MobileRag.instance.search('query', tokenBudget: 2000);
final prompt = MobileRag.instance.formatPrompt('query', result);
// Send prompt to LLM
Advanced Usage (Low-Level API)
For fine-grained control, use the individual services directly:
await initTokenizer(tokenizerPath: 'path/to/tokenizer.json');
await EmbeddingService.init(modelBytes);
final rag = SourceRagService(dbPath: 'path/to/rag.db');
await rag.init();
Classes
- AssembledContext
- Assembled context ready for LLM consumption.
- ContextBuilder
- Builds optimized context for LLM prompts.
- EmbeddingService
- Dart-based embedding service Rust tokenizer + Flutter ONNX Runtime combination
- MobileRag
- Singleton facade for Mobile RAG Engine.
- RagConfig
- Configuration options for RagEngine initialization.
- RagEngine
- Unified RAG engine with simplified initialization.
- RagSearchResult
- Search result with assembled context.
- SourceAddResult
- Result of adding a source document with automatic chunking.
- SourceRagService
- High-level service for source-based RAG operations.
Enums
- ChunkingStrategy
- Chunking strategy for document processing.
- ContextStrategy
- Strategy for selecting and ordering chunks.
Functions
-
detectChunkingStrategy(
String? filePath) → ChunkingStrategy - Detect the appropriate chunking strategy based on file extension.
-
extractTextFromDocument(
{required List< int> fileBytes}) → Future<String> - Auto-detect document type and extract text Uses magic bytes to determine file format
-
extractTextFromDocx(
{required List< int> fileBytes}) → Future<String> - Extract text content from a DOCX file (bytes)
-
extractTextFromPdf(
{required List< int> fileBytes}) → Future<String> - Extract text content from a PDF file (bytes) Uses page-by-page extraction for safe page number removal and hyphenation handling