diff --git a/src/init/features/genkit/index.ts b/src/init/features/genkit/index.ts index 9a2ac77d09f..8b9302d3130 100644 --- a/src/init/features/genkit/index.ts +++ b/src/init/features/genkit/index.ts @@ -382,7 +382,23 @@ export async function genkitSetup( default: true, })) ) { - generateSampleFile(modelOptions[model].plugin, plugins, projectDir, genkitInfo.templateVersion); + logger.info( + "Telemetry data can be used to monitor and gain insights into your AI features. There may be a cost associated with using this feature. See https://firebase.google.com/docs/genkit/observability/telemetry-collection.", + ); + const enableTelemetry = + options.nonInteractive || + (await confirm({ + message: "Would like you to enable telemetry collection?", + default: true, + })); + + generateSampleFile( + modelOptions[model].plugin, + plugins, + projectDir, + genkitInfo.templateVersion, + enableTelemetry, + ); } } @@ -507,6 +523,7 @@ function generateSampleFile( configPlugins: string[], projectDir: string, templateVersion: string, + enableTelemetry: boolean, ): void { let modelImport = ""; if (modelPlugin && pluginToInfo[modelPlugin].model) { @@ -534,6 +551,7 @@ function generateSampleFile( ? pluginToInfo[modelPlugin].model || pluginToInfo[modelPlugin].modelStr || "" : "'' /* TODO: Set a model. */", ), + enableTelemetry, ); logLabeledBullet("genkit", "Generating sample file"); try { @@ -622,7 +640,7 @@ async function updatePackageJson(nonInteractive: boolean, projectDir: string): P } } -function renderConfig(pluginNames: string[], template: string): string { +function renderConfig(pluginNames: string[], template: string, enableTelemetry: boolean): string { const imports = pluginNames .map((pluginName) => generateImportStatement(pluginToInfo[pluginName].imports, pluginName)) .join("\n"); @@ -631,7 +649,8 @@ function renderConfig(pluginNames: string[], template: string): string { " /* Add your plugins here. */"; return template .replace("$GENKIT_CONFIG_IMPORTS", imports) - .replace("$GENKIT_CONFIG_PLUGINS", plugins); + .replace("$GENKIT_CONFIG_PLUGINS", plugins) + .replace("$GENKIT_ENABLE_TELEMETRY", enableTelemetry.toString()); } function generateImportStatement(imports: string, name: string): string { diff --git a/templates/genkit/firebase.1.0.0.template b/templates/genkit/firebase.1.0.0.template index 4b0e939d73f..21c3755240b 100644 --- a/templates/genkit/firebase.1.0.0.template +++ b/templates/genkit/firebase.1.0.0.template @@ -1,5 +1,6 @@ // Import the Genkit core libraries and plugins. import {genkit, z} from "genkit"; +import {enableFirebaseTelemetry} from "@genkit-ai/firebase"; $GENKIT_CONFIG_IMPORTS $GENKIT_MODEL_IMPORT @@ -21,6 +22,13 @@ $GENKIT_CONFIG_PLUGINS ], }); +// The Firebase telemetry plugin exports a combination of metrics, traces, and logs to Google Cloud +// Observability. See https://firebase.google.com/docs/genkit/observability/telemetry-collection. +const enableTelemetry = $GENKIT_ENABLE_TELEMETRY; +if (enableTelemetry) { + enableFirebaseTelemetry(); +} + // Define a simple flow that prompts an LLM to generate menu suggestions. const menuSuggestionFlow = ai.defineFlow({ name: "menuSuggestionFlow",