เรียกใช้เวิร์กโฟลว์ Genkit จากแอป

Cloud Functions for Firebase มีเมธอด onCallGenkit ที่ช่วยให้คุณสร้างฟังก์ชันที่เรียกใช้ได้ด้วยการดำเนินการ Genkit (โฟลว์) เรียกใช้ฟังก์ชันเหล่านี้ได้ด้วย genkit/beta/client หรือ SDK ไคลเอ็นต์ Cloud Functions ซึ่งจะเพิ่มข้อมูลการตรวจสอบสิทธิ์โดยอัตโนมัติ

ก่อนเริ่มต้น

  • คุณควรคุ้นเคยกับแนวคิดโฟลว์ของ Genkit และวิธีเขียน วิธีการในหน้านี้ถือว่าคุณได้กําหนดขั้นตอนบางส่วนที่ต้องการติดตั้งใช้งานแล้ว
  • การดำเนินการนี้มีประโยชน์ แต่ไม่จำเป็นต้องทำหากคุณเคยใช้ Cloud Functions for Firebase มาก่อน

ตั้งค่าโปรเจ็กต์ Firebase

  1. สร้างโปรเจ็กต์ Firebase ใหม่โดยใช้คอนโซล Firebase หรือเลือกโปรเจ็กต์ที่มีอยู่

  2. อัปเกรดโปรเจ็กต์เป็นแพ็กเกจ Blaze ซึ่งจําเป็นสําหรับการติดตั้งใช้งาน Cloud Functions ในเวอร์ชันที่ใช้งานจริง

  3. ติดตั้ง Firebase CLI

  4. เข้าสู่ระบบด้วย Firebase CLI โดยทำดังนี้

    firebase login
    firebase login --reauth # alternative, if necessary
    firebase login --no-localhost # if running in a remote shell
  5. สร้างไดเรกทอรีโปรเจ็กต์ใหม่

    export PROJECT_ROOT=~/tmp/genkit-firebase-project1
    mkdir -p $PROJECT_ROOT
  6. เริ่มต้นโปรเจ็กต์ Firebase ในไดเรกทอรีโดยทำดังนี้

    cd $PROJECT_ROOT
    firebase 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/v2/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 (AI ของ Google)

  1. ตรวจสอบว่า AI ของ Google พร้อมให้บริการในภูมิภาคของคุณ

  2. สร้างคีย์ API สำหรับ Gemini API โดยใช้ Google AI Studio

  3. จัดเก็บคีย์ API ใน Secret Manager ของ Cloud โดยทำดังนี้

    firebase functions:secrets:set GOOGLE_GENAI_API_KEY

    ขั้นตอนนี้สำคัญเพื่อป้องกันไม่ให้คีย์ API ของคุณรั่วไหลโดยไม่ตั้งใจ ซึ่งจะมอบสิทธิ์เข้าถึงบริการที่มีการเรียกเก็บเงิน

    ดูข้อมูลเพิ่มเติมเกี่ยวกับการจัดการข้อมูลลับได้ที่จัดเก็บและเข้าถึงข้อมูลการกําหนดค่าที่มีความละเอียดอ่อน

  4. แก้ไข 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 จะจัดเก็บไว้ใน Secret Manager ของ Cloud และพร้อมใช้งานจากสภาพแวดล้อม Cloud Functions

Gemini (Vertex AI)

  1. ในคอนโซลระบบคลาวด์ ให้เปิดใช้ Vertex AI API สําหรับโปรเจ็กต์ Firebase

  2. ในหน้า 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/v2/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_ROOT
firebase deploy --only functions