-
Notifications
You must be signed in to change notification settings - Fork 25
feat(google_genai): add Gemma model family support #257
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
4d553fe
d650849
580248a
bca46f9
f6b8aab
6d01305
0145b05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import 'package:schemantic/schemantic.dart'; | |
|
|
||
| import 'aggregation.dart'; | ||
| import 'api_client.dart'; | ||
| import 'gemma.dart'; | ||
| import 'generated/generativelanguage.dart' as gcl; | ||
| import 'model.dart'; | ||
|
|
||
|
|
@@ -40,11 +41,16 @@ final commonModelInfo = ModelInfo( | |
| abstract class CommonGoogleGenPlugin extends GenkitPlugin { | ||
| Future<GenerativeLanguageBaseClient> getApiClient([String? requestApiKey]); | ||
|
|
||
| Model createModel(String modelName, SchemanticType customOptions) { | ||
| Model createModel( | ||
| String modelName, | ||
| SchemanticType customOptions, { | ||
| ModelInfo? modelInfo, | ||
| }) { | ||
| final isGemma = isGemmaModelName(modelName); | ||
| return Model( | ||
| name: '$name/$modelName', | ||
| customOptions: customOptions, | ||
| metadata: {'model': commonModelInfo.toJson()}, | ||
| metadata: {'model': (modelInfo ?? commonModelInfo).toJson()}, | ||
| fn: (req, ctx) async { | ||
| gcl.GenerationConfig generationConfig; | ||
| List<gcl.SafetySetting>? safetySettings; | ||
|
|
@@ -74,9 +80,17 @@ abstract class CommonGoogleGenPlugin extends GenkitPlugin { | |
| ); | ||
| toolConfig = toGeminiToolConfig(options.functionCallingConfig); | ||
| } else { | ||
| final options = req.config == null | ||
| ? GeminiOptions() | ||
| : GeminiOptions.$schema.parse(req.config!); | ||
| final GeminiOptions options; | ||
| if (isGemma) { | ||
| final gemmaOptions = req.config == null | ||
| ? GemmaOptions() | ||
| : GemmaOptions.$schema.parse(req.config!); | ||
| options = gemmaToGeminiOptions(gemmaOptions); | ||
| } else { | ||
| options = req.config == null | ||
| ? GeminiOptions() | ||
| : GeminiOptions.$schema.parse(req.config!); | ||
| } | ||
| apiKey = options.apiKey; | ||
| generationConfig = toGeminiSettings( | ||
| options, | ||
|
|
@@ -98,9 +112,12 @@ abstract class CommonGoogleGenPlugin extends GenkitPlugin { | |
| final systemMessage = req.messages | ||
| .where((m) => m.role == Role.system) | ||
| .firstOrNull; | ||
| final messages = req.messages | ||
| final nonSystemMessages = req.messages | ||
| .where((m) => m.role != Role.system) | ||
| .toList(); | ||
| final messages = isGemma | ||
| ? stripReasoningParts(nonSystemMessages) | ||
| : nonSystemMessages; | ||
|
CorieW marked this conversation as resolved.
Comment on lines
+110
to
+112
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While |
||
|
|
||
| final generateRequest = gcl.GenerateContentRequest( | ||
| contents: toGeminiContent(messages), | ||
|
|
@@ -189,6 +206,15 @@ abstract class CommonGoogleGenPlugin extends GenkitPlugin { | |
| return createEmbedder(name); | ||
| } | ||
| if (actionType == 'model') { | ||
| if (isGemmaModelName(name)) { | ||
| return createModel( | ||
| name, | ||
| GemmaOptions.$schema, | ||
| modelInfo: isGemma3ModelName(name) | ||
| ? gemma3ModelInfo | ||
| : commonGemmaModelInfo, | ||
| ); | ||
| } | ||
| if (name.contains('-tts')) { | ||
| return createModel(name, GeminiTtsOptions.$schema); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'package:genkit/plugin.dart'; | ||
|
|
||
| import 'model.dart'; | ||
|
|
||
| final commonGemmaModelInfo = ModelInfo( | ||
| supports: { | ||
| 'multiturn': true, | ||
| 'media': true, | ||
| 'tools': true, | ||
| 'toolChoice': true, | ||
| 'systemRole': true, | ||
| 'constrained': 'no-tools', | ||
| }, | ||
| ); | ||
|
|
||
| final gemma3ModelInfo = ModelInfo( | ||
| supports: {...?commonGemmaModelInfo.supports, 'systemRole': false}, | ||
| ); | ||
|
|
||
| bool isGemmaModelName(String name) => name.startsWith('gemma-'); | ||
|
|
||
| bool isGemma3ModelName(String name) => | ||
| name.startsWith('gemma-3-') || name.startsWith('gemma-3n-'); | ||
|
|
||
| /// Strips parts that the Gemma API rejects in history: reasoning parts | ||
| /// and any text/tool parts whose metadata carries a `thoughtSignature`. | ||
| /// Messages that become empty after filtering are dropped. | ||
| List<Message> stripReasoningParts(List<Message> messages) { | ||
| return messages | ||
| .map( | ||
| (m) => Message( | ||
| role: m.role, | ||
| content: m.content | ||
| .where( | ||
| (p) => | ||
| !p.isReasoning && p.metadata?['thoughtSignature'] == null, | ||
| ) | ||
| .toList(), | ||
| metadata: m.metadata, | ||
| ), | ||
| ) | ||
| .where((m) => m.content.isNotEmpty) | ||
| .toList(); | ||
| } | ||
|
|
||
| /// Maps a [GemmaOptions] config to its [GeminiOptions] equivalent so the | ||
| /// shared `toGeminiSettings`/`toGeminiTools`/etc. helpers can be reused. | ||
| /// Every Gemma field has an identical Gemini twin; the only schema-level | ||
| /// difference is the tighter `temperature` cap on Gemma. | ||
| GeminiOptions gemmaToGeminiOptions(GemmaOptions o) { | ||
| return GeminiOptions( | ||
| apiKey: o.apiKey, | ||
| safetySettings: o.safetySettings, | ||
| codeExecution: o.codeExecution, | ||
| functionCallingConfig: o.functionCallingConfig, | ||
| thinkingConfig: o.thinkingConfig, | ||
| responseModalities: o.responseModalities, | ||
| googleSearch: o.googleSearch, | ||
| fileSearch: o.fileSearch, | ||
| temperature: o.temperature, | ||
| topP: o.topP, | ||
| topK: o.topK, | ||
| candidateCount: o.candidateCount, | ||
| stopSequences: o.stopSequences, | ||
| maxOutputTokens: o.maxOutputTokens, | ||
| responseMimeType: o.responseMimeType, | ||
| responseLogprobs: o.responseLogprobs, | ||
| logprobs: o.logprobs, | ||
| presencePenalty: o.presencePenalty, | ||
| frequencyPenalty: o.frequencyPenalty, | ||
| seed: o.seed, | ||
| speechConfig: o.speechConfig, | ||
| ); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.