initialize static method

Future<void> initialize({
  1. required String tokenizerAsset,
  2. required String modelAsset,
  3. String? databaseName,
  4. int maxChunkChars = 500,
  5. int overlapChars = 50,
  6. int? embeddingIntraOpNumThreads,
  7. ThreadUseLevel? threadLevel,
  8. bool deferIndexWarmup = false,
  9. void onProgress(
    1. String status
    )?,
})

Initialize the RAG engine. Call once in main().

This will:

  1. Initialize the Rust library (FFI)
  2. Load the tokenizer from assets
  3. Load the ONNX embedding model
  4. 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., 1 for minimal CPU). Mutually exclusive with threadLevel.
  • threadLevel - High-level thread usage: low (~20%), medium (~40%), high (~80%). Mutually exclusive with embeddingIntraOpNumThreads.
  • 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 apps
  • embeddingIntraOpNumThreads: 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._();
}