37 lines
921 B
Dart
37 lines
921 B
Dart
import 'package:firebase_ai/firebase_ai.dart';
|
|
|
|
class GeminiService {
|
|
static late GenerativeModel _model;
|
|
|
|
/// Initializes Gemini Developer API
|
|
static Future<void> initialize() async {
|
|
_model = FirebaseAI.googleAI().generativeModel(
|
|
model: 'gemini-2.5-flash',
|
|
);
|
|
}
|
|
|
|
/// Generate text from a text prompt
|
|
static Future<String> generateText(String prompt) async {
|
|
try {
|
|
final content = [Content.text(prompt)];
|
|
|
|
final response = await _model.generateContent(content);
|
|
|
|
return response.text ?? "No response text found.";
|
|
} catch (e) {
|
|
return "Error: $e";
|
|
}
|
|
}
|
|
|
|
/// Stream text response (optional)
|
|
static Stream<String> streamText(String prompt) async* {
|
|
final content = [Content.text(prompt)];
|
|
|
|
final stream = _model.generateContentStream(content);
|
|
|
|
await for (final chunk in stream) {
|
|
if (chunk.text != null) yield chunk.text!;
|
|
}
|
|
}
|
|
}
|