initialize method

  1. @override
Future<bool> initialize({
  1. String? modelPath,
})
override

Loads the local model.

Call once at application startup. Returns true if the model loaded successfully.

Implementation

@override
Future<bool> initialize({String? modelPath}) async {
  if (isReady && modelPath == _loadedModelPath) return true;
  if (_isInitializing) return false;

  _isInitializing = true;
  final path = modelPath ?? '';

  if (path.isEmpty || !File(path).existsSync()) {
    SintSentinel.logger.e('LocalLlamaProvider: Cannot initialize, model file does not exist at: "$path"');
    _isInitializing = false;
    return false;
  }

  try {
    SintSentinel.logger.i('LocalLlamaProvider: Optimizing local LLM inference context via HardwareProfiler...');

    // Perform hardware profiling to optimize settings
    final profiler = const HardwareProfiler();
    final profile = await profiler.detect();

    SintSentinel.logger.i('LocalLlamaProvider: Detected hardware tier: ${profile.tier.name.toUpperCase()} '
        '(RAM: ${profile.totalRamGB}GB, Cores: ${profile.cpuCores}, GPU: ${profile.gpuBackend.name.toUpperCase()})');

    // Dispose existing engine if any
    await dispose();

    // Setup backend
    final backend = llama.LlamaBackend();
    _engine = llama.LlamaEngine(backend);

    SintSentinel.logger.i('LocalLlamaProvider: Loading model GGUF: "$path" into in-process runtime...');

    // Load model into engine
    await _engine!.loadModel(path);
    _loadedModelPath = path;

    SintSentinel.logger.i('LocalLlamaProvider: Loaded model successfully! In-process inference engine is active.');
    _isInitializing = false;
    return true;
  } catch (e, st) {
    SintSentinel.logger.e('LocalLlamaProvider: Initialization failed: $e', error: e, stackTrace: st);
    await dispose();
    _isInitializing = false;
    return false;
  }
}