generateText method

Future<GeminiAIHttpResponse> generateText({
  1. required String query,
  2. required String apiKey,
  3. required GenerationConfig? config,
  4. required List<SafetySettings>? safetySettings,
  5. String model = 'gemini-pro',
})

Implementation

Future<GeminiAIHttpResponse> generateText({
  required String query,
  required String apiKey,
  required GenerationConfig? config,
  required List<SafetySettings>? safetySettings,
  String model = 'gemini-pro',
}) async {
  try {
    final response = await _dio.post(
      '${Constants.geminiAIEndpoint}/$model:generateContent',
      queryParameters: {'key': apiKey},
      data: {
        "contents": [
          {
            "parts": [
              {"text": query}
            ]
          }
        ],
        "safetySettings": _convertSafetySettings(safetySettings ?? []),
        "generationConfig": config?.toJson(),
      },
    );

    return GeminiAIHttpResponse.fromJson(response.data);
  } catch (error) {
    throw _handleError(error);
  }
}