generate method

String? generate()

Ask the model to generate the next token. This function properly handle incomplete multi-byte utf8 character.

Implementation

String? generate() {
  if (pLLm == nullptr) {
    throw Exception("ailia LLM not initialized.");
  }

  Pointer<Uint32> done = malloc<Uint32>();
  var status = dllHandle.ailiaLLMGenerate(
    pLLm.value,
    done,
  );
  int doneFlag = done.value;
  malloc.free(done);

  _contextFull = false;

  if (doneFlag == 1) {
    return null;
  }

  if (status != ailia_llm_dart.AILIA_LLM_STATUS_SUCCESS) {
    if (status == ailia_llm_dart.AILIA_LLM_STATUS_CONTEXT_FULL) {
      _contextFull = true;
      return null;
    }
    throw Exception("ailiaLLMGenerate returned an error status $status");
  }

  // Try first with gBuff which is a buffer associated to this
  // prompt instance.
  final Pointer<UnsignedInt> size = malloc<UnsignedInt>();
  dllHandle.ailiaLLMGetDeltaTextSize(pLLm.value, size);

  final Pointer<Char> byteBuffer = malloc<Char>(size.value);
  dllHandle.ailiaLLMGetDeltaText(pLLm.value, byteBuffer, size.value);

  var buffer = Uint8List(size.value - 1);
  for (var i = 0; i < size.value - 1; i++) {
    buffer[i] = byteBuffer.elementAt(i).value;
  }

  Uint8List combinedUint8List = Uint8List(_buf.length + buffer.length);
  combinedUint8List.setRange(0, _buf.length, _buf);
  combinedUint8List.setRange(
      _buf.length, _buf.length + buffer.length, buffer);
  _buf = combinedUint8List;

  malloc.free(size);
  malloc.free(byteBuffer);

  String deltaText = "";
  try {
    String text = utf8.decode(_buf);
    if (_beforeText.length != text.length) {
      deltaText = text.substring(_beforeText.length);
    }
    _beforeText = text;
  } on FormatException catch (e) {
    // unicode decode error
  }

  return deltaText;
}