initialize static method

Future<RagEngine> initialize({
  1. required RagConfig config,
  2. void onProgress(
    1. String status
    )?,
})

Initialize RagEngine with all dependencies.

This method handles:

  1. Copying tokenizer asset to documents directory
  2. Initializing the tokenizer
  3. Loading the ONNX embedding model
  4. 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,
  );
}