initializeModel method

Future<void> initializeModel({
  1. CactusInitParams? params,
})

Implementation

Future<void> initializeModel({final CactusInitParams? params}) async {
  if (!Telemetry.isInitialized) {
    await Telemetry.init(CactusTelemetry.telemetryToken);
  }

  final model = params?.model?? _lastInitializedModel ?? defaultInitParams.model;
  final modelPath = '${(await getApplicationDocumentsDirectory()).path}/models/$model';

  final result = await CactusContext.initContext(modelPath, ((params?.contextSize) ?? defaultInitParams.contextSize)!);
  _handle = result.$1;

  if(_handle == null && !await _isModelDownloaded(model)) {
    debugPrint('Failed to initialize model context with model at $modelPath, trying to download the model first.');
    await downloadModel(model: model);
    return initializeModel(params: params);
  }

  if(Telemetry.isInitialized) {
    Telemetry.instance?.logInit(_handle != null, model, result.$2);
  }

  if(_handle == null) {
    throw Exception('Failed to initialize model context with model at $modelPath');
  }
  _lastInitializedModel = model;
}