Skip to content

Commit

Permalink
feat: add generationconfig support for vertex ai
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Oct 23, 2024
1 parent 51744c9 commit 42001a4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
51 changes: 50 additions & 1 deletion vertex_ai_flutter/lib/src/vertex_ai_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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';
import 'package:tekartik_common_utils/list_utils.dart';

/// Flutter service
final FirebaseVertexAiServiceFlutter firebaseVertexAiServiceFlutter =
Expand Down Expand Up @@ -62,10 +63,58 @@ class _FirebaseVertexAiFlutter
VaiGenerativeModel generativeModel(
{String? model, GenerationConfig? generationConfig}) {
model ??= vertexAiModelGemini1dot5Flash;
var nativeModel = fbVertexAi.generativeModel(model: model);
var nativeModel = fbVertexAi.generativeModel(
model: model,
generationConfig: generationConfig?.toFbGenerationConfig());
return VaiGenerativeModelFlutter(this, nativeModel);
}
}

/// Flutter service
abstract class FirebaseVertexAiFlutter implements FirebaseVertexAi {}

extension on SchemaType {
fb.SchemaType toFbSchemaType() {
switch (this) {
case SchemaType.object:
return fb.SchemaType.object;
case SchemaType.array:
return fb.SchemaType.array;
case SchemaType.integer:
return fb.SchemaType.integer;
case SchemaType.boolean:
return fb.SchemaType.boolean;
case SchemaType.string:
return fb.SchemaType.string;
case SchemaType.number:
return fb.SchemaType.number;
}
}
}

extension on Schema {
fb.Schema toFbSchema() {
return fb.Schema(type.toFbSchemaType(),
items: items?.toFbSchema(),
format: format,
description: description,
enumValues: enumValues,
nullable: nullable,
properties:
properties?.map((key, value) => MapEntry(key, value.toFbSchema())),
optionalProperties: optionalProperties);
}
}

extension on GenerationConfig {
fb.GenerationConfig toFbGenerationConfig() {
return fb.GenerationConfig(
candidateCount: candidateCount,
maxOutputTokens: maxOutputTokens,
temperature: temperature,
topP: topP,
topK: topK,
responseMimeType: responseMimeType,
responseSchema: responseSchema?.toFbSchema());
}
}
4 changes: 4 additions & 0 deletions vertex_ai_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ dependencies:
url: https://github.com/tekartik/firebase.dart
ref: dart3a
path: firebase
tekartik_common_utils:
git:
url: https://github.com/tekartik/common_utils.dart
ref: dart3a
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 42001a4

Please sign in to comment.