@@ -24,14 +24,14 @@ const {defineSecret} = require("firebase-functions/params");
2424// [END import-params]
2525
2626// [START import-genkit]
27- // Dependencies for Genkit
27+ // Dependencies for Genkit.
2828const { gemini15Flash, googleAI} = require ( "@genkit-ai/googleai" ) ;
2929const { genkit, z} = require ( "genkit" ) ;
3030// [END import-genkit]
3131// [END imports]
3232
3333// [START define-secret]
34- // Store the Gemini API key in Cloud Secret Manager
34+ // Store the Gemini API key in Cloud Secret Manager.
3535const apiKey = defineSecret ( "GOOGLE_GENAI_API_KEY" ) ;
3636// [END define-secret]
3737
@@ -48,16 +48,19 @@ const jokeTeller = ai.defineFlow({
4848 streamSchema : z . string ( ) ,
4949} , async ( jokeType = "knock-knock" , response ) => {
5050 const prompt = `Tell me a ${ jokeType } joke.` ;
51+
52+ // Call the `generateStream()` method to
53+ // receive the `stream` async iterable.
5154 const { stream, response : aiResponse } = ai . generateStream ( prompt ) ;
5255
53- // Loop over the `stream` async iterable to
54- // send new words of the AI response
55- // to the client as they're generated
56+ // Send new words of the generative AI response
57+ // to the client as they're generated.
5658 for await ( const chunk of stream ) {
5759 response . sendChunk ( chunk . text ) ;
5860 }
5961
60- // Return the full response
62+ // Return the full generative AI response
63+ // to clients that may not support streaming.
6164 return ( await aiResponse ) . text ;
6265} ,
6366) ;
@@ -66,15 +69,15 @@ const jokeTeller = ai.defineFlow({
6669// [START trigger]
6770exports . tellJoke = onCallGenkit ( {
6871 // [START bind-secrets]
69- // bind the Gemini API key secret parameter to the function
72+ // Bind the Gemini API key secret parameter to the function.
7073 secrets : [ apiKey ] ,
7174 // [END bind-secrets]
7275 // [START auth-policy]
73- // protect your endpoint with authPolicy
74- authPolicy : ( auth ) => ! ! auth ?. token . email_verified ,
76+ // Protect your endpoint with authPolicy.
77+ // authPolicy: (auth) => !!auth?.token.email_verified,
7578 // [END auth-policy]
7679} ,
77- // pass in the genkit flow
80+ // Pass in the genkit flow.
7881jokeTeller ,
7982) ;
8083// [END trigger]
0 commit comments