अपने ऐप्लिकेशन से Genkit फ़्लो को ट्रिगर करना

Firebase के लिए Cloud Functions में onCallGenkit तरीका है. इसकी मदद से Genkit ऐक्शन (फ़्लो) के साथ कॉल किया जा सकने वाला फ़ंक्शन बनाया जा सकता है. इन फ़ंक्शन को genkit/beta/client या Cloud Functions के क्लाइंट एसडीके टूल, की मदद से कॉल किया जा सकता है. ये टूल, पुष्टि करने से जुड़ी जानकारी अपने-आप जोड़ देते हैं.

शुरू करने से पहले

Firebase प्रोजेक्ट सेट अप करना

  1. Firebase console का इस्तेमाल करके, कोई नया Firebase प्रोजेक्ट बनाएं या कोई मौजूदा प्रोजेक्ट चुनें.

  2. प्रोजेक्ट को ब्लेज़ प्लान में अपग्रेड करें. 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 में रैप करना

Cloud Functions के साथ कोई Firebase प्रोजेक्ट सेट अप करने के बाद, प्रोजेक्ट की 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,
);

डिप्लॉय किए गए फ़्लो के लिए, एपीआई क्रेडेंशियल उपलब्ध कराना

डिप्लॉय किए जाने के बाद, आपके फ़्लो को उन सभी रिमोट सेवाओं के साथ पुष्टि करने के लिए किसी तरीके की ज़रूरत होती है जिन पर वे निर्भर करते हैं. ज़्यादातर फ़्लो को, कम से कम उस मॉडल एपीआई सेवा को ऐक्सेस करने के लिए क्रेडेंशियल की ज़रूरत होती है जिसका वे इस्तेमाल करते हैं.

इस उदाहरण के लिए, चुने गए मॉडल उपलब्ध कराने वाली कंपनी के हिसाब से, इनमें से कोई एक काम करें:

Gemini (Google AI)

  1. पक्का करें कि आपके इलाके में Google AI उपलब्ध हो.

  2. एपीआई पासकोड जनरेट करें Google AI Studio का इस्तेमाल करके, Gemini API के लिए.

  3. अपना एपीआई पासकोड, Cloud Secret Manager में सेव करें:

    firebase functions:secrets:set GOOGLE_GENAI_API_KEY

    यह चरण ज़रूरी है, ताकि गलती से आपका एपीआई पासकोड लीक न हो. इससे, संभावित तौर पर मीटर की गई सेवा को ऐक्सेस किया जा सकता है.

    सीक्रेट मैनेज करने के बारे में ज़्यादा जानने के लिए, संवेदनशील कॉन्फ़िगरेशन की जानकारी सेव करना और उसे ऐक्सेस करना लेख पढ़ें.

  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],

अब, इस फ़ंक्शन को डिप्लॉय करने पर, आपका एपीआई पासकोड Cloud Secret Manager में सेव हो जाएगा. साथ ही, यह Cloud Functions एनवायरमेंट से उपलब्ध होगा.

Gemini (Vertex AI)

  1. Cloud Console में, अपने Firebase प्रोजेक्ट के लिए Vertex AI API चालू करें.

  2. IAM पेज पर, पक्का करें कि डिफ़ॉल्ट कंप्यूट सेवा खाते को Vertex AI उपयोगकर्ता की भूमिका दी गई हो.

इस ट्यूटोरियल के लिए, आपको सिर्फ़ मॉडल उपलब्ध कराने वाली कंपनी के लिए सीक्रेट सेट अप करना होगा. हालांकि, आम तौर पर, आपको अपने फ़्लो में इस्तेमाल की जाने वाली हर सेवा के लिए ऐसा ही करना होगा.

(ज़रूरी नहीं) App Check लागू करना

Firebase App Check, नेटिव अटेस्टेशन का इस्तेमाल करके यह पुष्टि करता है कि आपका एपीआई सिर्फ़ आपके ऐप्लिकेशन से कॉल किया जा रहा है. 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 विकल्प का इस्तेमाल करके, यह कंट्रोल करें कि किन ऑरिजिन से आपके फ़ंक्शन को ऐक्सेस किया जा सकता है.

डिफ़ॉल्ट रूप से, कॉल किए जा सकने वाले फ़ंक्शन में सीओआरएस कॉन्फ़िगर होता है, ताकि सभी ऑरिजिन से अनुरोध किए जा सकें. क्रॉस-ऑरिजिन के कुछ अनुरोधों को अनुमति देने के लिए, लेकिन सभी को नहीं, खास डोमेन या रेगुलर एक्सप्रेशन की सूची पास करें जिन्हें अनुमति दी जानी चाहिए. उदाहरण के लिए:

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