|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START complete-example] |
| 18 | +// [START imports] |
| 19 | +// [START import-trigger] |
| 20 | +const {onCallGenkit} = require("firebase-functions/v2/https"); |
| 21 | +// [END import-trigger] |
| 22 | +// [START import-params] |
| 23 | +const {defineSecret} = require("firebase-functions/params"); |
| 24 | +// [END import-params] |
| 25 | + |
| 26 | +// [START import-genkit] |
| 27 | +// Dependencies for Genkit. |
| 28 | +const {gemini15Flash, googleAI} = require("@genkit-ai/googleai"); |
| 29 | +const {genkit, z} = require("genkit"); |
| 30 | +// [END import-genkit] |
| 31 | +// [END imports] |
| 32 | + |
| 33 | +// [START define-secret] |
| 34 | +// Store the Gemini API key in Cloud Secret Manager. |
| 35 | +const apiKey = defineSecret("GOOGLE_GENAI_API_KEY"); |
| 36 | +// [END define-secret] |
| 37 | + |
| 38 | +// [START flow] |
| 39 | +const ai = genkit({ |
| 40 | + plugins: [googleAI()], |
| 41 | + model: gemini15Flash, |
| 42 | +}); |
| 43 | + |
| 44 | +const jokeTeller = ai.defineFlow({ |
| 45 | + name: "jokeTeller", |
| 46 | + inputSchema: z.string().nullable(), |
| 47 | + outputSchema: z.string(), |
| 48 | + streamSchema: z.string(), |
| 49 | +}, async (jokeType = "knock-knock", response) => { |
| 50 | + const prompt = `Tell me a ${jokeType} joke.`; |
| 51 | + |
| 52 | + // Call the `generateStream()` method to |
| 53 | + // receive the `stream` async iterable. |
| 54 | + const {stream, response: aiResponse} = ai.generateStream(prompt); |
| 55 | + |
| 56 | + // Send new words of the generative AI response |
| 57 | + // to the client as they're generated. |
| 58 | + for await (const chunk of stream) { |
| 59 | + response.sendChunk(chunk.text); |
| 60 | + } |
| 61 | + |
| 62 | + // Return the full generative AI response |
| 63 | + // to clients that may not support streaming. |
| 64 | + return (await aiResponse).text; |
| 65 | +}, |
| 66 | +); |
| 67 | +// [END flow] |
| 68 | + |
| 69 | +// [START trigger] |
| 70 | +exports.tellJoke = onCallGenkit({ |
| 71 | + // [START bind-secrets] |
| 72 | + // Bind the Gemini API key secret parameter to the function. |
| 73 | + secrets: [apiKey], |
| 74 | + // [END bind-secrets] |
| 75 | + // [START auth-policy] |
| 76 | + // Protect your endpoint with authPolicy. |
| 77 | + // authPolicy: (auth) => !!auth?.token.email_verified, |
| 78 | + // [END auth-policy] |
| 79 | +}, |
| 80 | +// Pass in the genkit flow. |
| 81 | +jokeTeller, |
| 82 | +); |
| 83 | +// [END trigger] |
| 84 | +// [END complete-example] |
0 commit comments