setPrompt method

void setPrompt(
  1. List<Map<String, dynamic>> messages
)

Set the prompt to be process by the model. The prompt will be formatted according to the selected format. messages must be an array of object with two string properties named 'role' and 'content'.

Implementation

void setPrompt(List<Map<String, dynamic>> messages) {
  if (pLLm == nullptr) {
    throw Exception("ailia LLM not initialized.");
  }

  // Allocate an array of ailia_llm_chat_message_t and initialize it
  // with the messages data.
  final messagesPtr =
      calloc<ailia_llm_dart.AILIALLMChatMessage>(messages.length);

  try {
    for (var i = 0; i < messages.length; i++) {
      if (!messages[i].containsKey("content")) {
        throw Exception("missing 'content' property");
      }
      if (!messages[i].containsKey("role")) {
        throw Exception("missing 'role' property");
      }

      final content = messages[i]['content'] as String;
      final role = messages[i]['role'] as String;
      final p = messagesPtr[i];

      p.content = content.toNativeUtf8().cast<Char>();
      p.role = role.toNativeUtf8().cast<Char>();
    }

    _contextFull = false;
    _buf = Uint8List(0);
    _beforeText = "";

    int status =
        dllHandle.ailiaLLMSetPrompt(pLLm.value, messagesPtr, messages.length);
    if (status != ailia_llm_dart.AILIA_LLM_STATUS_SUCCESS) {
      if (status == ailia_llm_dart.AILIA_LLM_STATUS_CONTEXT_FULL) {
        _contextFull = true;
        return;
      }
      throw Exception("ailiaLLMSetPrompt returned an error status $status");
    }
  } finally {
    // free string
    for (var i = 0; i < messages.length; i++) {
      final p = messagesPtr[i];
      if (p.content != nullptr) {
        malloc.free(p.content);
      }
      if (p.role != nullptr) {
        malloc.free(p.role);
      }
    }
    malloc.free(messagesPtr);
  }
}