Skip to content

Refactor: Replace Gemini and Vertex SDKs with firebase_ai #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions example/lib/gemini/gemini.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart';
import 'package:google_generative_ai/google_generative_ai.dart';
// import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart'; // Will be replaced by firebase_ai
// import 'package:google_generative_ai/google_generative_ai.dart'; // Will be replaced by firebase_ai

import '../gemini_api_key.dart';

Expand All @@ -26,10 +26,12 @@ class ChatPage extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text(App.title)),
body: LlmChatView(
provider: GeminiProvider(
model: GenerativeModel(model: 'gemini-2.0-flash', apiKey: geminiApiKey),
),
),
// body: LlmChatView(
// provider: GeminiProvider(
// model: GenerativeModel(model: 'gemini-2.0-flash', apiKey: geminiApiKey),
// ),
// ),
// TODO: Instantiate LlmChatView with the new firebase_ai provider
body: const Center(child: Text('Gemini provider will be implemented here.')),
);
}
20 changes: 11 additions & 9 deletions example/lib/vertex/vertex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// found in the LICENSE file.

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_vertexai/firebase_vertexai.dart';
// import 'package:firebase_vertexai/firebase_vertexai.dart'; // Will be replaced by firebase_ai
import 'package:flutter/material.dart';
import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart';
// import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart'; // Will be replaced by firebase_ai

// from `flutterfire config`: https://firebase.google.com/docs/flutter/setup
import '../firebase_options.dart';
Expand All @@ -31,12 +31,14 @@ class ChatPage extends StatelessWidget {
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text(App.title)),
body: LlmChatView(
provider: VertexProvider(
model: FirebaseVertexAI.instance.generativeModel(
model: 'gemini-2.0-flash',
),
),
),
// body: LlmChatView(
// provider: VertexProvider(
// model: FirebaseVertexAI.instance.generativeModel(
// model: 'gemini-2.0-flash',
// ),
// ),
// ),
// TODO: Instantiate LlmChatView with the new firebase_ai provider
body: const Center(child: Text('Vertex AI provider will be implemented here.')),
);
}
3 changes: 1 addition & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies:
flutter_ai_toolkit:
path: ..
cupertino_icons: ^1.0.8
google_generative_ai: ^0.4.3
shared_preferences: ^2.3.2
url_launcher: ^6.3.0
go_router: ^14.2.8
Expand All @@ -26,7 +25,7 @@ dependencies:
future_builder_ex: ^5.0.0
split_view: ^3.2.1
firebase_core: ^3.13.0
firebase_vertexai: ^1.5.0
firebase_ai: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down
148 changes: 148 additions & 0 deletions lib/src/providers/implementations/firebase_ai_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2024 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:firebase_ai/firebase_ai.dart';
import 'package:flutter/foundation.dart';

import '../interface/attachments.dart';
import '../interface/chat_message.dart';
import '../interface/llm_provider.dart';
import '../interface/message_origin.dart';

class FirebaseAIProvider with ChangeNotifier implements LlmProvider {
final GenerativeModel _model;
List<ChatMessage> _history = [];

FirebaseAIProvider({required GenerativeModel model, Iterable<ChatMessage>? history}) : _model = model {
if (history != null) {
_history = history.toList();
}
}

@override
List<ChatMessage> get history => _history;

@override
set history(List<ChatMessage> newHistory) {
_history = newHistory;
notifyListeners();
}

List<Part> _convertAttachmentsToParts(Iterable<Attachment> attachments) {
final parts = <Part>[];
for (final attachment in attachments) {
if (attachment is ImageAttachmentBytes) {
parts.add(DataPart(attachment.mimeType, attachment.bytes));
} else if (attachment is ImageAttachmentUrl) {
// Assuming the firebase_ai SDK handles URL fetching or requires bytes.
// For now, let's log a warning if we encounter a URL, as direct URL support in Part might vary.
// Or, if the SDK expects a specific URI format (like gs://), that needs to be handled.
// Based on firebase_ai, it seems to prefer bytes directly.
// If URLs need to be fetched first, that's an additional step not directly part of this conversion.
debugPrint('ImageAttachmentUrl currently not fully supported for direct conversion to Part. Please provide bytes.');
// parts.add(TextPart('Image at ${attachment.url}')); // Placeholder
}
// Add other attachment type conversions if necessary
}
return parts;
}

Content _convertChatMessageToContent(ChatMessage message) {
final parts = <Part>[TextPart(message.prompt)];
parts.addAll(_convertAttachmentsToParts(message.attachments));
// The firebase_ai Content object has a 'role' which can be 'user' or 'model'.
// We need to map MessageOrigin to this.
final role = message.origin == MessageOrigin.user ? 'user' : 'model';
return Content(role, parts);
}

List<Content> _convertMessagesToContent(Iterable<ChatMessage> messages) {
return messages.map((message) => _convertChatMessageToContent(message)).toList();
}

@override
Stream<String> sendMessageStream(String prompt, {Iterable<Attachment>? attachments}) {
final userMessageAttachments = attachments?.toList() ?? [];
final userMessage = ChatMessage(
prompt: prompt,
origin: MessageOrigin.user,
attachments: userMessageAttachments,
timestamp: DateTime.now(),
);
_history.add(userMessage);
notifyListeners();

final currentHistoryForModel = _convertMessagesToContent(_history.toList());

final controller = StreamController<String>();
final responseBuffer = StringBuffer();

_model.generateContentStream(currentHistoryForModel).listen(
(response) {
final text = response.text;
if (text != null) {
responseBuffer.write(text);
controller.add(text);
}
},
onError: (error) {
// Potentially add an error message to history or handle differently
_history.add(ChatMessage(
prompt: "Error: ${error.toString()}",
origin: MessageOrigin.system,
timestamp: DateTime.now()));
notifyListeners();
controller.addError(error);
controller.close();
},
onDone: () {
if (responseBuffer.isNotEmpty) {
_history.add(ChatMessage(
prompt: responseBuffer.toString(),
origin: MessageOrigin.llm,
timestamp: DateTime.now(),
));
notifyListeners();
}
controller.close();
},
);

return controller.stream;
}

@override
Stream<String> generateStream(String prompt, {Iterable<Attachment>? attachments}) {
final parts = <Part>[TextPart(prompt)];
if (attachments != null) {
parts.addAll(_convertAttachmentsToParts(attachments));
}

// For generateStream, we typically don't pass history, just the current prompt.
// The firebase_ai SDK expects a List<Content>. For a single prompt,
// this would be a single Content object with role 'user'.
final content = [Content('user', parts)];

final controller = StreamController<String>();

_model.generateContentStream(content).listen(
(response) {
final text = response.text;
if (text != null) {
controller.add(text);
}
},
onError: (error) {
controller.addError(error);
controller.close();
},
onDone: () {
controller.close();
},
);
return controller.stream;
}
}
166 changes: 0 additions & 166 deletions lib/src/providers/implementations/gemini_provider.dart

This file was deleted.

Loading
Loading