completion method
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;
}