genkit_vertexai 1.1.0
genkit_vertexai: ^1.1.0 copied to clipboard
Vertex AI plugin for Genkit Dart.
// 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 'dart:io';
import 'package:genkit/genkit.dart';
import 'package:genkit_vertexai/genkit_vertexai.dart';
void main(List<String> args) async {
final ai = Genkit(
plugins: [
vertexAI(
projectId: Platform.environment['GCLOUD_PROJECT'],
// `global` serves the most models; any other location works too if
// the model is available there.
location: Platform.environment['GCLOUD_LOCATION'] ?? 'global',
),
],
);
// --- Basic Generate Flow ---
ai.defineFlow(
name: 'basicGenerate',
inputSchema: .string(defaultValue: 'Hello Genkit for Dart!'),
outputSchema: .string(),
fn: (input, context) async {
final response = await ai.generate(
model: vertexAI.gemini('gemini-3.8-flash'),
prompt: input,
);
return response.text;
},
);
// --- Image Generation Flow (Nano Banana) ---
ai.defineFlow(
name: 'imageGenerator',
inputSchema: .string(
defaultValue:
'A cozy reading nook with a window seat on a rainy afternoon.',
),
outputSchema: Media.$schema,
fn: (prompt, _) async {
final response = await ai.generate(
model: vertexAI.geminiImage('gemini-nano-banana-2.1'),
prompt: prompt,
config: GeminiImageOptions(
imageConfig: GeminiImageConfig(aspectRatio: '9:16', imageSize: '1K'),
),
);
if (response.media == null) {
throw Exception('No image generated');
}
return response.media!;
},
);
// --- Music Generation Flow (Lyria) ---
ai.defineFlow(
name: 'musicClip',
inputSchema: .string(
defaultValue: 'A cheerful acoustic folk song with guitar and harmonica.',
),
outputSchema: .map(.string(), .dynamicSchema()),
fn: (prompt, _) async {
final response = await ai.generate(
model: vertexAI.lyria('lyria-3-clip-preview'),
prompt: prompt,
// Lyria is only served on `global`, whatever the plugin's location.
config: LyriaOptions(location: 'global'),
);
if (response.media == null) {
throw Exception('No music generated');
}
// The music, plus the lyrics or a caption as text.
return {'lyrics': response.text, 'audio': response.media!.toJson()};
},
);
// --- Embedding Flow ---
ai.defineFlow(
name: 'embedding',
inputSchema: .string(defaultValue: 'Hello Genkit'),
outputSchema: .list(.doubleSchema()),
fn: (input, _) async {
final embeddings = await ai.embed(
embedder: vertexAI.textEmbedding('gemini-embedding-001'),
documents: [
DocumentData(content: [TextPart(text: input)]),
],
);
return embeddings.first.embedding;
},
);
}