Llama constructor

Llama(
  1. String modelPath, {
  2. String? mmprojPath,
  3. ModelParams? modelParams,
  4. ContextParams? contextParams,
  5. SamplerParams? samplerParams,
  6. bool verbose = false,
})

Creates a new Llama instance.

Implementation

Llama(
  String modelPath, {
  String? mmprojPath,
  ModelParams? modelParams,
  ContextParams? contextParams,
  SamplerParams? samplerParams,
  bool verbose = false,
}) {
  try {
    _verbose = verbose;
    _status = LlamaStatus.loading;

    _initializeLlama(
        modelPath, mmprojPath, modelParams, contextParams, samplerParams);

    contextParams ??= ContextParams();
    _contextParams = contextParams;
    _nPredict = contextParams.nPredict;

    _validateConfiguration();

    var nativeContextParams = contextParams.get();
    try {
      batch = lib.llama_batch_init(nativeContextParams.n_batch, 0, 1);

      for (int i = 0; i < nativeContextParams.n_batch; i++) {
        final seqIdPtr = calloc<llama_seq_id>();
        seqIdPtr.value = 0;
        _batchSeqIds.add(seqIdPtr);
      }
    } catch (e) {
      if (_smpl != nullptr) lib.llama_sampler_free(_smpl);
      if (context.address != 0) lib.llama_free(context);
      if (model.address != 0) lib.llama_free_model(model);
      if (_mctx != nullptr) lib.mtmd_free(_mctx);

      for (final ptr in _batchSeqIds) {
        calloc.free(ptr);
      }
      _batchSeqIds.clear();

      throw LlamaException('Failed to initialize batch', e);
    }

    _isInitialized = true;
    _status = LlamaStatus.ready;
  } catch (e) {
    _status = LlamaStatus.error;
    _isDisposed = true;
    throw LlamaException('Failed to initialize Llama', e);
  }
}