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

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

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

  • คุณควรคุ้นเคยกับแนวคิดของโฟลว์ 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/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)

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

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

    firebase functions:secrets:set GOOGLE_GENAI_API_KEY

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

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

  3. แก้ไข 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)

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

  2. ในหน้า IAM ให้ตรวจสอบว่าบัญชีบริการ Compute เริ่มต้นได้รับบทบาทผู้ใช้ 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_ROOT
firebase deploy --only functions