initialize static method
Initialize the RAG engine. Call once in main().
This will:
- Initialize the Rust library (FFI)
- Load the tokenizer from assets
- Load the ONNX embedding model
- Initialize the SQLite database
Parameters:
tokenizerAsset- Path to tokenizer.json in assets (e.g.,'assets/tokenizer.json')modelAsset- Path to ONNX model file in assets (e.g.,'assets/model.onnx')databaseName- SQLite database file name (default:'rag.sqlite')maxChunkChars- Maximum characters per chunk (default: 500)overlapChars- Overlap between chunks for context continuity (default: 50)embeddingIntraOpNumThreads- Precise thread count for ONNX (e.g.,1for minimal CPU). Mutually exclusive withthreadLevel.threadLevel- High-level thread usage:low(~20%),medium(~40%),high(~80%). Mutually exclusive withembeddingIntraOpNumThreads.deferIndexWarmup- If true, returns before BM25/HNSW warmup completes. Use isIndexReady or warmupFuture to gate search quality.onProgress- Callback for initialization progress updates
Thread Configuration:
Choose ONE of the following:
threadLevel: ThreadUseLevel.medium- Simple, recommended for most appsembeddingIntraOpNumThreads: 2- Fine-grained control
⚠️ Setting BOTH will throw an AssertionError.
Example:
await MobileRag.initialize(
tokenizerAsset: 'assets/tokenizer.json',
modelAsset: 'assets/model.onnx',
threadLevel: ThreadUseLevel.medium, // Recommended
onProgress: (status) => print(status),
);
Implementation
static Future<void> initialize({
required String tokenizerAsset,
required String modelAsset,
String? databaseName,
int maxChunkChars = 500,
int overlapChars = 50,
int? embeddingIntraOpNumThreads,
ThreadUseLevel? threadLevel,
bool deferIndexWarmup = false,
void Function(String status)? onProgress,
}) async {
if (_instance != null) {
onProgress?.call('Already initialized');
return;
}
_engine = await RagEngine.initialize(
config: RagConfig.fromAssets(
tokenizerAsset: tokenizerAsset,
modelAsset: modelAsset,
databaseName: databaseName,
maxChunkChars: maxChunkChars,
overlapChars: overlapChars,
embeddingIntraOpNumThreads: embeddingIntraOpNumThreads,
threadLevel: threadLevel,
deferIndexWarmup: deferIndexWarmup,
),
onProgress: onProgress,
);
_instance = MobileRag._();
}