init method

Future<bool> init({
  1. String? modelFilename,
  2. String? chatTemplate,
  3. int contextSize = 2048,
  4. int gpuLayers = 0,
  5. int threads = 4,
  6. bool generateEmbeddings = false,
  7. CactusProgressCallback? onProgress,
  8. String? cactusToken,
})

Implementation

Future<bool> init({
  String? modelFilename,
  String? chatTemplate,
  int contextSize = 2048,
  int gpuLayers = 0,
  int threads = 4,
  bool generateEmbeddings = false,
  CactusProgressCallback? onProgress,
  String? cactusToken,
}) async {
  if (cactusToken != null) {
    setCactusToken(cactusToken);
  }

  final filename = modelFilename ?? _lastDownloadedFilename;
  if (filename == null) {
    throw ArgumentError('No model filename provided and no model was previously downloaded');
  }

  final appDocDir = await getApplicationDocumentsDirectory();
  final modelPath = '${appDocDir.path}/$filename';

  final file = File(modelPath);
  if (!await file.exists()) {
    throw ArgumentError('Model file does not exist at path: $modelPath');
  }

  final initParams = CactusInitParams(
    modelPath: modelPath,
    chatTemplate: chatTemplate,
    contextSize: contextSize,
    gpuLayers: gpuLayers,
    threads: threads,
    generateEmbeddings: generateEmbeddings,
    onInitProgress: onProgress,
  );

  try {
    _context = await CactusContext.init(initParams);
    return true;
  } catch (e) {
    CactusTelemetry.error(e, initParams);
    if (onProgress != null) {
      onProgress(null, "Initialization failed: ${e.toString()}", true);
    }
    return false;
  }
}