Cloud Functions for Firebase 具有 onCallGenkit 方法,可讓您使用 Genkit 動作 (流程) 建立可呼叫函式。您可以使用 genkit/beta/client 或 Cloud Functions for Firebase Client SDK 呼叫這些函式,系統會自動新增驗證資訊。
事前準備
- 您應熟悉 Genkit 流程的概念,以及如何編寫流程。本頁操作說明假設您已定義要部署的流程。
- 如果您曾使用過 Cloud Functions for Firebase,會很有幫助,但並非必要。
設定 Firebase 專案
使用 Firebase 控制台建立新的 Firebase 專案,或選擇現有專案。
將專案升級至即付即用 Blaze 定價方案,這是 Cloud Functions 正式環境部署作業的必要條件。
安裝 Firebase CLI。
登入 Firebase CLI:
firebase loginfirebase login --reauth # alternative, if necessaryfirebase login --no-localhost # if running in a remote shell建立新的專案目錄:
export PROJECT_ROOT=~/tmp/genkit-firebase-project1mkdir -p $PROJECT_ROOT在目錄中初始化 Firebase 專案:
cd $PROJECT_ROOTfirebase init functions
本頁其餘部分假設您選擇以 JavaScript 編寫函式。
將流程包裝在 onCallGenkit 中
設定 Firebase 專案並在其中初始化 Cloud Functions 後,您可以在專案的 functions 目錄中複製或編寫流程定義。以下是示範流程:
const ai = genkit({ plugins: [googleAI()], model: gemini15Flash, }); const jokeTeller = ai.defineFlow({ name: "jokeTeller", inputSchema: z.string().nullable(), outputSchema: z.string(), streamSchema: z.string(), }, async (jokeType = "knock-knock", {sendChunk}) => { const prompt = `Tell me a ${jokeType} joke.`; // Call the `generateStream()` method to // receive the `stream` async iterable. const {stream, response: aiResponse} = ai.generateStream(prompt); // Send new words of the generative AI response // to the client as they are generated. for await (const chunk of stream) { sendChunk(chunk.text); } // Return the full generative AI response // to clients that may not support streaming. return (await aiResponse).text; }, );
如要部署這類流程,請使用 firebase-functions/https 中的 onCallGenkit 包裝。這個輔助方法具備可呼叫函式的所有功能,並自動支援串流和 JSON 回應。
const {onCallGenkit} = require("firebase-functions/https");
exports.tellJoke = onCallGenkit({ // Bind the Gemini API key secret parameter to the function. secrets: [apiKey], }, // Pass in the genkit flow. jokeTeller, );
將 API 憑證提供給已部署的流程
部署完成後,流程需要驗證所依賴的遠端服務。大多數流程至少需要憑證,才能存取所用的模型 API 服務。
在本範例中,請根據您選擇的模型供應商,執行下列其中一項操作:
Gemini (Google AI)
使用 Google AI Studio 為 Gemini Developer API 產生 API 金鑰。
將 API 金鑰儲存在 Google Cloud Secret Manager 中:
firebase functions:secrets:set GOOGLE_GENAI_API_KEY這個步驟很重要,可避免不小心洩漏 API 金鑰,導致他人存取可能需要付費的服務。
如要進一步瞭解如何管理密鑰,請參閱「儲存及存取機密設定資訊」。
編輯
src/index.js,並在現有匯入項目後方加入以下內容:const {defineSecret} = require("firebase-functions/params");
// Store the Gemini API key in Cloud Secret Manager. const apiKey = defineSecret("GOOGLE_GENAI_API_KEY");
接著,在可呼叫函式定義中,宣告函式需要存取這個密鑰值:
// Bind the Gemini API key secret parameter to the function. secrets: [apiKey],
現在,當您部署這個函式時,API 金鑰會儲存在 Google Cloud Secret Manager 中,並可從 Cloud Functions 環境存取。
Gemini (Vertex AI)
在 Google Cloud 控制台中,為 Firebase 專案啟用 Vertex AI API。
在「IAM」頁面,確認預設運算服務帳戶已取得 Vertex AI 使用者角色。
在本教學課程中,您只需要為模型供應商設定密鑰,但一般來說,您必須為流程使用的每項服務執行類似的操作。
(選用) 新增Firebase App Check強制執行
Firebase App Check 會使用原生認證,驗證只有您的應用程式呼叫 API。onCallGenkit 方法支援以宣告方式強制執行 App Check。
export const generatePoem = onCallGenkit({
enforceAppCheck: true,
// Optional. Makes App Check tokens only usable once. This adds extra security
// at the expense of slowing down your app to generate a token for every API
// call
consumeAppCheckToken: true,
}, generatePoemFlow);
設定 CORS (跨源資源共享)
使用 cors 選項,控管哪些來源可以存取函式。
根據預設,可呼叫函式會設定 CORS,允許來自所有來源的要求。如要允許部分 (而非全部) 跨源要求,請傳遞應允許的特定網域或規則運算式清單。例如:
export const tellJoke = onCallGenkit({
cors: 'mydomain.com',
}, jokeTeller);
完整範例
完成本指南所述的所有變更後,可部署的流程會類似以下範例:
const {onCallGenkit} = require("firebase-functions/https"); const {defineSecret} = require("firebase-functions/params"); // Dependencies for Genkit. const {gemini15Flash, googleAI} = require("@genkit-ai/googleai"); const {genkit, z} = require("genkit"); // Store the Gemini API key in Cloud Secret Manager. const apiKey = defineSecret("GOOGLE_GENAI_API_KEY"); const ai = genkit({ plugins: [googleAI()], model: gemini15Flash, }); const jokeTeller = ai.defineFlow({ name: "jokeTeller", inputSchema: z.string().nullable(), outputSchema: z.string(), streamSchema: z.string(), }, async (jokeType = "knock-knock", {sendChunk}) => { const prompt = `Tell me a ${jokeType} joke.`; // Call the `generateStream()` method to // receive the `stream` async iterable. const {stream, response: aiResponse} = ai.generateStream(prompt); // Send new words of the generative AI response // to the client as they are generated. for await (const chunk of stream) { sendChunk(chunk.text); } // Return the full generative AI response // to clients that may not support streaming. return (await aiResponse).text; }, ); exports.tellJoke = onCallGenkit({ // Bind the Gemini API key secret parameter to the function. secrets: [apiKey], }, // Pass in the genkit flow. jokeTeller, );
將流程部署至 Firebase
使用 onCallGenkit 定義流程後,您可以像部署其他函式一樣部署流程:
cd $PROJECT_ROOTfirebase deploy --only functions