generateText method

  1. @override
Future<String?> generateText(
  1. String prompt, {
  2. String? sessionId,
  3. int? maxTokens,
  4. double? temperature,
  5. double? topP,
})
override

Implementation

@override
Future<String?> generateText(
  String prompt, {
  String? sessionId,
  int? maxTokens,
  double? temperature,
  double? topP,
}) async {
  if (prompt.trim().isEmpty) {
    throw AppleFoundationException(
      'Prompt cannot be empty',
      code: 'INVALID_PROMPT',
    );
  }

  if (maxTokens != null && maxTokens <= 0) {
    throw AppleFoundationException(
      'maxTokens must be greater than 0',
      code: 'INVALID_MAX_TOKENS',
    );
  }

  if (temperature != null && (temperature < 0.0 || temperature > 2.0)) {
    throw AppleFoundationException(
      'temperature must be between 0.0 and 2.0',
      code: 'INVALID_TEMPERATURE',
    );
  }

  if (topP != null && (topP < 0.0 || topP > 1.0)) {
    throw AppleFoundationException(
      'topP must be between 0.0 and 1.0',
      code: 'INVALID_TOP_P',
    );
  }

  try {
    final String? response =
        await _invokeMethodWithTimeout<String>('generateText', {
          'prompt': prompt,
          if (sessionId != null) 'sessionId': sessionId,
          if (maxTokens != null) 'maxTokens': maxTokens,
          if (temperature != null) 'temperature': temperature,
          if (topP != null) 'topP': topP,
        });
    return response;
  } catch (e) {
    _logError('generateText', e);
    throw AppleFoundationException(
      'Failed to generate text: ${e.toString()}',
      code: 'GENERATE_TEXT_FAILED',
      details: {
        'prompt': prompt,
        'sessionId': sessionId,
        'maxTokens': maxTokens,
        'temperature': temperature,
        'topP': topP,
      },
    );
  }
}