generateChatbotService static method
Implementation
static String generateChatbotService(String projectName) {
return '''
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:$projectName/features/chatbot/models/chat_message_model.dart';
import 'package:$projectName/core/constants/app_constants.dart';
class ChatbotService {
final Dio _dio = Dio();
Future<String> sendMessage(List<ChatMessageModel> messages) async {
try {
final response = await _dio.post(
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=\${AppConstants.geminiApiKey}",
data: {
"contents": messages.map((e) => e.toMap()).toList(),
"generationConfig": {
"temperature": 0.9,
"topK": 1,
"topP": 1,
"maxOutputTokens": 2048,
"stopSequences": []
},
"safetySettings": [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
}
]
},
);
if (response.statusCode != null &&
response.statusCode! >= 200 &&
response.statusCode! < 300) {
return response
.data['candidates'].first['content']['parts'].first['text'];
}
throw Exception('Failed to get response from Gemini API');
} catch (e) {
log('Chatbot Service Error: \$e');
rethrow;
}
}
}
''';
}