loadTextModel method

void loadTextModel(
  1. String modelPath, {
  2. Map<String, dynamic>? config,
})

Load a text generation model (LLM).

Uses the rac_llm_component_* API from RACommons. First creates the component handle, then loads the model.

Implementation

void loadTextModel(String modelPath, {Map<String, dynamic>? config}) {
  _ensureBackendType('llamacpp');

  // Step 1: Create the LLM component if we don't have a handle
  if (_handle == null) {
    final handlePtr = calloc<RacHandle>();
    try {
      final create = _lib.lookupFunction<RacLlmComponentCreateNative,
          RacLlmComponentCreateDart>('rac_llm_component_create');

      final result = create(handlePtr);

      if (result != RAC_SUCCESS) {
        throw NativeBackendException(
          'Failed to create LLM component: ${RacCore.getErrorMessage(result)}',
          code: result,
        );
      }

      _handle = handlePtr.value;
    } finally {
      calloc.free(handlePtr);
    }
  }

  // Step 2: Load the model
  final pathPtr = modelPath.toNativeUtf8();
  // Use filename as model ID
  final modelId = modelPath.split('/').last;
  final modelIdPtr = modelId.toNativeUtf8();
  final modelNamePtr = modelId.toNativeUtf8();

  try {
    final loadModel = _lib.lookupFunction<RacLlmComponentLoadModelNative,
        RacLlmComponentLoadModelDart>('rac_llm_component_load_model');

    final result = loadModel(_handle!, pathPtr, modelIdPtr, modelNamePtr);

    if (result != RAC_SUCCESS) {
      throw NativeBackendException(
        'Failed to load text model: ${RacCore.getErrorMessage(result)}',
        code: result,
      );
    }

    _currentModel = modelPath;
  } finally {
    calloc.free(pathPtr);
    calloc.free(modelIdPtr);
    calloc.free(modelNamePtr);
  }
}