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