Cloud Functions for Firebase มีเมธอด onCallGenkit ที่ช่วยให้คุณสร้าง
ฟังก์ชันที่เรียกใช้ได้ด้วยการดำเนินการ Genkit (โฟลว์)
โดยฟังก์ชันเหล่านี้สามารถเรียกใช้ได้ด้วย genkit/beta/client หรือ a
Cloud Functions for Firebase Client SDK ซึ่ง
จะเพิ่มข้อมูลการตรวจสอบสิทธิ์โดยอัตโนมัติ
ก่อนเริ่มต้น
- คุณควรคุ้นเคยกับแนวคิดเรื่อง Genkit โฟลว์ และวิธี เขียนโฟลว์ โดยคำแนะนำในหน้านี้จะถือว่าคุณได้กำหนดโฟลว์บางอย่างที่ต้องการทำให้ใช้งานได้แล้ว
- หากคุณเคยใช้ Cloud Functions for Firebase มาก่อน ก็จะเป็นประโยชน์ แต่ไม่จำเป็น
ตั้งค่าโปรเจ็กต์ Firebase
สร้างโปรเจ็กต์ Firebase ใหม่โดยใช้คอนโซล Firebase หรือ เลือกโปรเจ็กต์ที่มีอยู่
อัปเกรดโปรเจ็กต์เป็นแพ็กเกจการกำหนดราคา Blaze แบบจ่ายตามการใช้งาน ซึ่งจำเป็นสำหรับ Cloud Functionsการทำให้ใช้งานได้จริง
ติดตั้ง Firebase CLI
Cloud Shellเข้าสู่ระบบ 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; }, );
หากต้องการทำให้โฟลว์เช่นนี้ใช้งานได้ ให้รวมโฟลว์ไว้ใน onCallGenkit ซึ่งมีอยู่ใน firebase-functions/https เมธอดตัวช่วยนี้มีฟีเจอร์ทั้งหมดของ
ฟังก์ชันที่เรียกใช้ได้ และรองรับ
ทั้งการตอบกลับแบบสตรีมมิงและการตอบกลับแบบ 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)
สร้างคีย์ API สำหรับ Gemini Developer API โดยใช้ Google AI Studio
จัดเก็บคีย์ 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 ให้เปิดใช้ Vertex AI API สำหรับโปรเจ็กต์ Firebase
ในหน้า IAM ตรวจสอบว่าได้มอบบทบาท ผู้ใช้ Vertex AI ให้กับบัญชีบริการเริ่มต้นของ Compute แล้ว
ข้อมูลลับเดียวที่คุณต้องตั้งค่าสำหรับบทแนะนำนี้คือข้อมูลลับสำหรับผู้ให้บริการโมเดล แต่โดยทั่วไปแล้ว คุณต้องทำสิ่งที่คล้ายกันสำหรับแต่ละบริการที่โฟลว์ใช้
(ไม่บังคับ) เพิ่มการบังคับใช้ 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