open method

void open(
  1. String modelPath,
  2. int nCtx,
  3. {String backend = ""}
)

Initialize the context using the given model and parameters.

Implementation

void open(String modelPath, int nCtx, {String backend = ""}) {
  if (pLLm != nullptr) {
    if (pLLm.value != nullptr) {
      dllHandle.ailiaLLMDestory(pLLm.value);
    }
  }

  if (backend == "") {
    backend = _backend[1][0];
  }

  if (_currentBackend != backend) {
    if (_library != null) {
      _library!.close();
      _library = null;
    }
    List<String> backendList = getBackendList();
    for (int i = 0; i < backendList.length; i++) {
      if (backendList[i] == backend) {
        _library = _ailiaCommonGetLibrary(_backend[0][i]);
        dllHandle = ailia_llm_dart.ailiaLlmFFI(_library!);
        _currentBackend = backend;
        break;
      }
    }
    if (_library == null) {
      throw Exception("ailiaLLM backend not found");
    }
  }

  pLLm = malloc<Pointer<ailia_llm_dart.AILIALLM>>();
  pLLm.value = nullptr;

  var status = dllHandle.ailiaLLMCreate(pLLm);
  if (status != 0) {
    throw Exception("ailiaLLMCreate returned an error status $status");
  }

  if (Platform.isWindows) {
    Pointer<WChar> path = modelPath.toNativeUtf16().cast<WChar>();
    status = dllHandle.ailiaLLMOpenModelFileW(pLLm.value, path, nCtx);
    malloc.free(path);
  } else {
    Pointer<Char> path = modelPath.toNativeUtf8().cast<Char>();
    status = dllHandle.ailiaLLMOpenModelFileA(pLLm.value, path, nCtx);
    malloc.free(path);
  }
  if (status != 0) {
    throw Exception("ailiaLLMOpenModelFile returned an error status $status");
  }
}