genkit_firebase_ai 0.4.0
genkit_firebase_ai: ^0.4.0 copied to clipboard
Firebase AI plugin for Genkit Dart.
Firebase AI plugin for Genkit Dart.
Building with a coding agent? Install the Genkit Dart skill first.
npx skills add genkit-ai/skills --skill developing-genkit-dartIt teaches your agent the current Genkit Dart APIs and common gotchas. Source, manual install and skills for other languages: genkit-ai/skills.
Usage #
import 'package:genkit/genkit.dart';
import 'package:genkit_firebase_ai/genkit_firebase_ai.dart';
void main() async {
// Initialize Genkit with the Firebase AI plugin
final ai = Genkit(plugins: [firebaseAI()]);
// Generate text
final response = await ai.generate(
model: firebaseAI.gemini('gemini-flash-latest'),
prompt: 'Tell me a joke about a developer.',
);
print(response.text);
}
Configuration #
You can optionally pass in FirebaseApp and FirebaseAiProvider instances,
and the useLimitedUseAppCheckTokens flag when initializing the plugin. App
Check and Auth are read from the FirebaseApp.
final firebasePlugin = firebaseAI(
app: Firebase.app('my-app'),
provider: FirebaseAiProvider.agentPlatform(),
useLimitedUseAppCheckTokens: true,
);
final ai = Genkit(plugins: [firebasePlugin]);
Set location (for example 'us-central1') for Agent Platform Live models
such as gemini-live-2.5-flash-native-audio, which are not available on the
default global location, or for data residency.
Migrating from earlier versions:
FirebaseAiProvider.vertexAIis nowFirebaseAiProvider.agentPlatform. It defaultslocationtoglobal, notus-central1. Passlocation: 'us-central1'to keep the old behavior.- The
appCheckandauthparameters offirebaseAIare removed. The Firebase AI SDK reads App Check and Auth from theFirebaseApponly when it first builds itsFirebaseAIinstance, and it caches that instance per app (and per location foragentPlatform). It only finds services registered withFirebaseAppCheck.instanceFor(app:)andFirebaseAuth.instanceFor(app:)(or.instancefor the default app). So set up App Check and Auth for that app before the first model call. For the same reason,useLimitedUseAppCheckTokenshas no effect if the app already created aFirebaseAIinstance for the same Firebase app and location. Instances created for a different app thanapp:are no longer used.
To route requests through your own http.Client (a proxy, logging, retries,
or a mock in tests), pass httpClient. Live models connect over a WebSocket
and do not use it. The plugin never closes a client you provide, so close it
yourself when you are done:
import 'package:http/http.dart' as http;
final httpClient = http.Client();
final ai = Genkit(plugins: [firebaseAI(httpClient: httpClient)]);
// ...
httpClient.close();
Tool Calling #
// Define a tool
ai.defineTool(
name: 'getWeather',
description: 'Get the weather for a location',
inputSchema: WeatherToolInput.$schema,
fn: (input, context) async {
return .response('The weather in ${input.location} is 75 and sunny.');
},
);
// Generate with tools
final response = await ai.generate(
model: firebaseAI.gemini('gemini-flash-latest'),
prompt: 'What is the weather in Boston?',
toolNames: ['getWeather'],
);