checkAPIValidity method
- String apiKey,
- BuildContext context
Validates the provided API key by sending a test query to the Gemini API.
apiKey
is the API key to be validated.
context
is the BuildContext used for localization.
Returns an empty string if the API key is valid, otherwise returns an error message localized for the current context.
Implementation
Future<String> checkAPIValidity(String apiKey, BuildContext context) async {
final testQuery = 'Hello';
try {
final llm = ChatGoogleGenerativeAI(
apiKey: apiKey,
defaultOptions: ChatGoogleGenerativeAIOptions(
model: 'gemini-1.0-pro',
),
);
// ignore: unused_local_variable
final response = await llm.invoke(PromptValue.string(testQuery));
return '';
} catch (e) {
return AppLocalizations.of(context)!
.aiGenerationAPIGemini_error1(e.toString());
}
}