Skip to content

Commit

Permalink
feat(vertex): Add auth support in the vertexai (#12797)
Browse files Browse the repository at this point in the history
* Add auth support in the vertexai

* Update the example model name

* revert to the previous color theme
  • Loading branch information
cynthiajoan committed May 21, 2024
1 parent 8f16fd0 commit 3241c0b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ class _ChatWidgetState extends State<ChatWidget> {

initFirebase().then((value) {
_model = FirebaseVertexAI.instance.generativeModel(
model: 'gemini-1.5-pro-preview-0409',
model: 'gemini-1.5-flash-preview-0514',
);
_functionCallModel = FirebaseVertexAI.instance.generativeModel(
model: 'gemini-1.5-pro-preview-0409',
model: 'gemini-1.5-flash-preview-0514',
tools: [
Tool(functionDeclarations: [exchangeRateTool]),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:firebase_app_check/firebase_app_check.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart'
show FirebasePluginPlatform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
{required this.app,
required this.options,
required this.location,
this.appCheck})
this.appCheck,
this.auth})
: super(app.name, 'plugins.flutter.io/firebase_vertexai');

/// The [FirebaseApp] for this current [FirebaseVertexAI] instance.
Expand All @@ -35,6 +36,9 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
/// https://firebase.google.com/docs/app-check
FirebaseAppCheck? appCheck;

/// The optional [FirebaseAuth] for this current [FirebaseVertexAI] instance.
FirebaseAuth? auth;

/// Configuration parameters for sending requests to the backend.
RequestOptions options;

Expand All @@ -57,6 +61,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
static FirebaseVertexAI instanceFor({
FirebaseApp? app,
FirebaseAppCheck? appCheck,
FirebaseAuth? auth,
RequestOptions? options,
String? location,
}) {
Expand All @@ -72,7 +77,11 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
location ??= _defaultLocation;

FirebaseVertexAI newInstance = FirebaseVertexAI._(
app: app, options: options, location: location, appCheck: appCheck);
app: app,
options: options,
location: location,
appCheck: appCheck,
auth: auth);
_cachedInstances[app.name] = newInstance;

return newInstance;
Expand Down Expand Up @@ -100,6 +109,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
model: model,
app: app,
appCheck: appCheck,
auth: auth,
location: location,
safetySettings: safetySettings,
generationConfig: generationConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class GenerativeModel {
required String location,
required FirebaseApp app,
FirebaseAppCheck? appCheck,
FirebaseAuth? auth,
List<SafetySetting>? safetySettings,
GenerationConfig? generationConfig,
List<Tool>? tools,
Expand All @@ -51,7 +52,7 @@ final class GenerativeModel {
model: _normalizeModelName(model),
apiKey: app.options.apiKey,
baseUri: _vertexUri(app, location),
requestHeaders: _appCheckToken(appCheck),
requestHeaders: _firebaseTokens(appCheck, auth),
safetySettings: safetySettings != null
? safetySettings
.map((setting) => setting._toGoogleAISafetySetting())
Expand Down Expand Up @@ -90,16 +91,22 @@ final class GenerativeModel {
}
}

static FutureOr<Map<String, String>> Function() _appCheckToken(
FirebaseAppCheck? appCheck) {
static FutureOr<Map<String, String>> Function() _firebaseTokens(
FirebaseAppCheck? appCheck, FirebaseAuth? auth) {
return () async {
Map<String, String> headers = {};
// Override the client name in Google AI SDK
headers['x-goog-api-client'] = 'gl-dart/flutter fire/$packageVersion';
if (appCheck != null) {
final token = await appCheck.getToken();
if (token != null) {
headers['X-Firebase-AppCheck'] = token;
final appCheckToken = await appCheck.getToken();
if (appCheckToken != null) {
headers['X-Firebase-AppCheck'] = appCheckToken;
}
}
if (auth != null) {
final idToken = await auth.currentUser?.getIdToken();
if (idToken != null) {
headers['Authorization'] = idToken;
}
}
return headers;
Expand Down
1 change: 1 addition & 0 deletions packages/firebase_vertexai/firebase_vertexai/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:

dependencies:
firebase_app_check: ^0.2.2+6
firebase_auth: ^4.19.5
firebase_core: ^2.31.1
firebase_core_platform_interface: ^5.0.0
flutter:
Expand Down

0 comments on commit 3241c0b

Please sign in to comment.