initialize static method
Initialize RagEngine with all dependencies.
This method handles:
- Copying tokenizer asset to documents directory
- Initializing the tokenizer
- Loading the ONNX embedding model
- Initializing the RAG database
config - Configuration containing asset paths and options.
onProgress - Optional callback for initialization status updates.
Example:
final rag = await RagEngine.initialize(
config: RagConfig.fromAssets(
tokenizerAsset: 'assets/tokenizer.json',
modelAsset: 'assets/model.onnx',
),
onProgress: (status) => setState(() => _status = status),
);
Implementation
static Future<RagEngine> initialize({
required RagConfig config,
void Function(String status)? onProgress,
}) async {
// 1. Get app documents directory
final dir = await getApplicationDocumentsDirectory();
final dbPath = "${dir.path}/${config.databaseName ?? 'rag.sqlite'}";
final tokenizerPath = "${dir.path}/tokenizer.json";
// 2. Copy and initialize tokenizer
onProgress?.call('Initializing tokenizer...');
await _copyAssetToFile(config.tokenizerAsset, tokenizerPath);
await initTokenizer(tokenizerPath: tokenizerPath);
final vocabSize = getVocabSize();
// 3. Load ONNX embedding model
onProgress?.call('Loading embedding model...');
final modelBytes = await rootBundle.load(config.modelAsset);
await EmbeddingService.init(modelBytes.buffer.asUint8List());
// 4. Initialize RAG service
onProgress?.call('Initializing database...');
final ragService = SourceRagService(
dbPath: dbPath,
maxChunkChars: config.maxChunkChars,
overlapChars: config.overlapChars,
);
await ragService.init();
onProgress?.call('Ready!');
return RagEngine._(
ragService: ragService,
dbPath: dbPath,
vocabSize: vocabSize,
);
}