Skip to content

Commit

Permalink
feat: start vertex ai support
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 21, 2024
1 parent e79b662 commit f5c58a0
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 12 deletions.
3 changes: 2 additions & 1 deletion auth_flutter/lib/auth_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:tekartik_firebase_auth/auth.dart';

import 'auth_flutter.dart';
import 'src/auth_flutter.dart' as auth_flutter;

export 'package:tekartik_firebase_auth/auth.dart';
Expand All @@ -9,7 +10,7 @@ export 'src/auth_flutter.dart' show FirebaseAuthFlutterExtension;
export 'src/google_auth.dart' show GoogleAuthProvider;

/// The flutter auth service
AuthService get authServiceFlutter => auth_flutter.authService;
FirebaseAuthService get authServiceFlutter => auth_flutter.authService;

/// The flutter auth service
@Deprecated('Use authServiceFlutter')
Expand Down
7 changes: 6 additions & 1 deletion auth_flutter/lib/auth_flutter_api.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export 'auth_flutter.dart';
export 'src/auth_flutter_api.dart'
show AuthFlutter, AuthServiceFlutter, AuthSignInOptionsWeb;
show
AuthFlutter,
AuthServiceFlutter,
AuthSignInOptionsWeb,
FirebaseAuthServiceFlutter,
FirebaseAuthFlutter;
11 changes: 8 additions & 3 deletions auth_flutter/lib/src/auth_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:tekartik_firebase/firebase_mixin.dart';
import 'package:tekartik_firebase_auth/src/auth_mixin.dart';
import 'package:tekartik_firebase_auth_flutter/auth_flutter.dart';
import 'package:tekartik_firebase_auth_flutter/src/google_auth.dart';
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'
import 'package:tekartik_firebase_flutter/firebase_flutter.dart'
as firebase_flutter;

import 'import.dart' as common;
Expand All @@ -17,7 +17,7 @@ class AuthServiceFlutterImpl
with common.FirebaseProductServiceMixin<FirebaseAuth>, AuthServiceMixin
implements AuthServiceFlutter {
@override
Auth auth(common.App app) {
FirebaseAuthFlutter auth(common.App app) {
return getInstance(app, () {
assert(app is firebase_flutter.FirebaseAppFlutter,
'invalid firebase app type');
Expand Down Expand Up @@ -118,11 +118,12 @@ class _UserFlutterImpl implements User, UserInfoWithIdToken {
/// Flutter impl
class AuthFlutterImpl
with FirebaseAppProductMixin<FirebaseAuth>, FirebaseAuthMixin
implements AuthFlutter {
implements FirebaseAuthFlutter {
/// The service
final AuthServiceFlutter serviceFlutter;

/// The native instance
/// Prefer using nativeInstance
final native.FirebaseAuth nativeAuth;

StreamSubscription? _onAuthStateChangedSubscription;
Expand Down Expand Up @@ -273,6 +274,10 @@ class AuthFlutterImpl

@override
FirebaseAuthService get service => serviceFlutter;

/// Prefer nativeInstance
@override
native.FirebaseAuth get nativeInstance => nativeAuth;
}

/// Flutter impl
Expand Down
17 changes: 15 additions & 2 deletions auth_flutter/lib/src/auth_flutter_api.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart' as native;
import 'package:tekartik_firebase_auth/auth.dart';

/// Browser sign in options
Expand All @@ -16,11 +17,23 @@ class AuthSignInOptionsWeb implements AuthSignInOptions {
}
}

/// compat
typedef AuthFlutter = FirebaseAuthFlutter;

/// Browser sign in result
abstract class AuthFlutter implements Auth {
abstract class FirebaseAuthFlutter implements Auth {
/// Sign in with popup
Future<User?> googleSignIn();

/// The native instance
native.FirebaseAuth get nativeInstance;
}

/// compat
typedef AuthServiceFlutter = FirebaseAuthServiceFlutter;

/// Auth service for flutter
abstract class AuthServiceFlutter implements AuthService {}
abstract class FirebaseAuthServiceFlutter implements FirebaseAuthService {
@override
FirebaseAuthFlutter auth(App app);
}
6 changes: 5 additions & 1 deletion firebase_flutter/lib/firebase_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import 'src/firebase_flutter.dart';
export 'package:tekartik_firebase/firebase.dart';

export 'src/firebase_flutter.dart'
show FirebaseFlutter, FirebaseFlutterExtension, FirebaseAppFlutterExtension;
show
FirebaseFlutter,
FirebaseFlutterExtension,
FirebaseAppFlutterExtension,
FirebaseAppFlutter;

/// Flutter async for initialization
/// Compat
Expand Down
2 changes: 1 addition & 1 deletion firestore_flutter/lib/src/firestore_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:cloud_firestore/cloud_firestore.dart' as native;
import 'package:flutter/foundation.dart';
import 'package:path/path.dart';
import 'package:tekartik_firebase/firebase_mixin.dart';
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'; // ignore: implementation_imports
import 'package:tekartik_firebase_flutter/firebase_flutter.dart';

import 'aggregate_query_flutter.dart';
import 'document_snapshot_flutter.dart';
Expand Down
2 changes: 1 addition & 1 deletion functions_call_flutter/lib/src/functions_call_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:cloud_functions/cloud_functions.dart' as native;
import 'package:tekartik_common_utils/common_utils_import.dart';
import 'package:tekartik_firebase/firebase_mixin.dart';
// ignore: implementation_imports
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'
import 'package:tekartik_firebase_flutter/firebase_flutter.dart'
show FirebaseAppFlutter;
import 'package:tekartik_firebase_functions/firebase_functions.dart';
import 'package:tekartik_firebase_functions_call/functions_call.dart';
Expand Down
3 changes: 2 additions & 1 deletion repo_support/tool/run_ci.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Future<void> main() async {
'firestore_flutter',
'firebase_flutter',
'auth_flutter',
'functions_call_flutter'
'functions_call_flutter',
'vertex_ai_flutter',
]) {
await packageRunCi(join(topDir, dir));
}
Expand Down
2 changes: 1 addition & 1 deletion storage_flutter/lib/src/storage_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:typed_data';

import 'package:firebase_storage/firebase_storage.dart' as native;
import 'package:tekartik_firebase/firebase_mixin.dart';
import 'package:tekartik_firebase_flutter/src/firebase_flutter.dart'; // ignore: implementation_imports
import 'package:tekartik_firebase_flutter/firebase_flutter.dart'; // ignore: implementation_imports
import 'package:tekartik_firebase_storage/src/common/storage_service_mixin.dart'; // ignore: implementation_imports
import 'package:tekartik_firebase_storage/storage.dart';
import 'package:tekartik_firebase_storage/utils/link.dart';
Expand Down
32 changes: 32 additions & 0 deletions vertex_ai_flutter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
build/
.metadata
.flutter-plugins
.flutter-plugins-dependencies
14 changes: 14 additions & 0 deletions vertex_ai_flutter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# firebase_vertex_ai_flutter

Flutter vertex ai

## Setup

In `pubspec.yaml`:
```yaml
tekartik_firebase_vertex_ai_flutter:
git:
url: https://github.com/tekartik/firebase_flutter
path: vertex_ai_flutter
ref: dart3a
```
4 changes: 4 additions & 0 deletions vertex_ai_flutter/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:tekartik_lints_flutter/package.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
14 changes: 14 additions & 0 deletions vertex_ai_flutter/lib/src/vertex_ai_api_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:firebase_vertexai/firebase_vertexai.dart' as fb;
import 'package:tekartik_firebase_vertex_ai/vertex_ai.dart';

/// Response flutter
class VaiGenerateContentResponseFlutter implements VaiGenerateContentResponse {
/// Native response
final fb.GenerateContentResponse nativeResponse;

/// Constructor
VaiGenerateContentResponseFlutter(this.nativeResponse);

@override
String? get text => nativeResponse.text;
}
9 changes: 9 additions & 0 deletions vertex_ai_flutter/lib/src/vertex_ai_constant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// See https://cloud.google.com/vertex-ai/docs/general/locations#europe

/// Paris
const vertexAiLocationParis = 'europe-west9';

// See https://firebase.google.com/docs/vertex-ai/gemini-models#available-model-names

/// Gemini 1.5 flash
const vertexAiModelGemini1dot5Flash = 'gemini-1.5-flash';
68 changes: 68 additions & 0 deletions vertex_ai_flutter/lib/src/vertex_ai_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'package:firebase_vertexai/firebase_vertexai.dart' as fb;
import 'package:tekartik_firebase/firebase_mixin.dart';
import 'package:tekartik_firebase_auth_flutter/auth_flutter.dart';
import 'package:tekartik_firebase_flutter/firebase_flutter.dart';
import 'package:tekartik_firebase_vertex_ai/vertex_ai.dart';
import 'package:tekartik_firebase_vertex_ai_flutter/src/vertex_ai_model_flutter.dart';

/// Flutter service
final FirebaseVertexAiServiceFlutter firebaseVertexAiServiceFlutter =
_FirebaseVertexAiServiceFlutter();

/// Flutter service interface
abstract class FirebaseVertexAiServiceFlutter
implements FirebaseVertexAiService {
/// Optionnal auth service
///
factory FirebaseVertexAiServiceFlutter(
{FirebaseAuthService? authService, String? location}) {
assert(authService is FirebaseAuthServiceFlutter?,
'authService should be a FirebaseAuthServiceFlutter');
return _FirebaseVertexAiServiceFlutter(
authServiceFlutter: authService, location: location);
}
}

class _FirebaseVertexAiServiceFlutter
with FirebaseProductServiceMixin<FirebaseVertexAi>
implements FirebaseVertexAiServiceFlutter {
final String? location;
final FirebaseAuthService? authServiceFlutter;

_FirebaseVertexAiServiceFlutter({this.authServiceFlutter, this.location});
@override
FirebaseVertexAiFlutter vertexAi(App app) {
return getInstance(app, () {
var appFlutter = app as FirebaseAppFlutter;
var nativeAuth = authServiceFlutter?.auth(app).nativeInstance;
var fbVertexAi = fb.FirebaseVertexAI.instanceFor(
app: appFlutter.nativeInstance!,
auth: nativeAuth,
location: location);
return _FirebaseVertexAiFlutter(this, appFlutter, fbVertexAi);
});
}
}

class _FirebaseVertexAiFlutter
with FirebaseAppProductMixin<FirebaseVertexAi>
implements FirebaseVertexAiFlutter {
final FirebaseVertexAiServiceFlutter serviceFlutter;
final FirebaseAppFlutter appFlutter;
final fb.FirebaseVertexAI fbVertexAi;

_FirebaseVertexAiFlutter(
this.serviceFlutter, this.appFlutter, this.fbVertexAi);

@override
FirebaseApp get app => appFlutter;

@override
VaiGenerativeModel generativeModel({required String model}) {
var nativeModel = fbVertexAi.generativeModel(model: model);
return VaiGenerativeModelFlutter(this, nativeModel);
}
}

/// Flutter service
abstract class FirebaseVertexAiFlutter implements FirebaseVertexAi {}
49 changes: 49 additions & 0 deletions vertex_ai_flutter/lib/src/vertex_ai_model_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:firebase_vertexai/firebase_vertexai.dart' as fb;
import 'package:tekartik_firebase_vertex_ai/vertex_ai.dart';
import 'package:tekartik_firebase_vertex_ai_flutter/src/vertex_ai_api_flutter.dart';
import 'package:tekartik_firebase_vertex_ai_flutter/src/vertex_ai_flutter.dart';

extension on Iterable<VaiContent> {
Iterable<fb.Content> toNative() {
return map((e) => e.toNative());
}
}

extension on VaiContent {
fb.Content toNative() {
return fb.Content(role, parts.map((e) => e.toNative()).toList());
}
}

extension on VaiContentPart {
fb.Part toNative() {
var part = this;
if (part is VaiContentTextPart) {
return fb.TextPart(part.text);
} else if (part is VaiContentDataPart) {
return fb.DataPart(part.mimeType, part.bytes);
} else {
throw 'Unsupported part $part (${part.runtimeType})';
}
}
}

/// Flutter impl
class VaiGenerativeModelFlutter implements VaiGenerativeModel {
/// Vertex AI
final FirebaseVertexAiFlutter vertexAiFlutter;

/// The native instance
final fb.GenerativeModel nativeInstance;

/// Constructor
VaiGenerativeModelFlutter(this.vertexAiFlutter, this.nativeInstance);

@override
Future<VaiGenerateContentResponse> generateContent(
Iterable<VaiContent> prompt) async {
var nativeResponse =
await nativeInstance.generateContent(prompt.toNative());
return VaiGenerateContentResponseFlutter(nativeResponse);
}
}
4 changes: 4 additions & 0 deletions vertex_ai_flutter/lib/vertex_ai_flutter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'package:tekartik_firebase_vertex_ai/vertex_ai.dart';
export 'src/vertex_ai_constant.dart';
export 'src/vertex_ai_flutter.dart'
show FirebaseVertexAiServiceFlutter, FirebaseVertexAiFlutter;
Loading

0 comments on commit f5c58a0

Please sign in to comment.