completion method

Future<CactusCompletionResult> completion(
  1. List<ChatMessage> messages, {
  2. List<String> imagePaths = const [],
  3. int maxTokens = 256,
  4. double? temperature,
  5. int? topK,
  6. double? topP,
  7. List<String>? stopSequences,
  8. CactusTokenCallback? onToken,
  9. String mode = "local",
})

Implementation

Future<CactusCompletionResult> completion(
  List<ChatMessage> messages, {
  List<String> imagePaths = const [],
  int maxTokens = 256,
  double? temperature,
  int? topK,
  double? topP,
  List<String>? stopSequences,
  CactusTokenCallback? onToken,
  String mode = "local",
}) async {

  CactusCompletionResult? result;
  Exception? lastError;

  if (mode == "remote") {
    result = await _handleRemoteCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
  } else if (mode == "local") {
    result = await _handleLocalCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
  } else if (mode == "localfirst") {
    try {
      result = await _handleLocalCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
    } catch (e) {
      lastError = e is Exception ? e : Exception(e.toString());
      try {
        result = await _handleRemoteCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
      } catch (remoteError) {
        throw lastError;
      }
    }
  } else if (mode == "remotefirst") {
    try {
      result = await _handleRemoteCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
    } catch (e) {
      lastError = e is Exception ? e : Exception(e.toString());
      try {
        result = await _handleLocalCompletion(messages, imagePaths, maxTokens, temperature, topK, topP, stopSequences, onToken);
      } catch (localError) {
        throw lastError;
      }
    }
  } else {
    throw ArgumentError('Invalid mode: $mode. Must be "local", "remote", "localfirst", or "remotefirst"');
  }

  return result;
}