फ़्लो

फ़्लो की कुछ और विशेषताएं भी होती हैं: वे पूरी तरह टाइप किए गए, स्ट्रीम किए जा सकते हैं, लोकल और रिमोट तरीके से कॉल किए जा सकते हैं. साथ ही, इन फ़ंक्शन की पूरी तरह से निगरानी की जा सकती है. Firebase Genkit, फ़्लो (दौड़ना, डीबग करना वगैरह) के साथ काम करने के लिए, सीएलआई और डेवलपर यूज़र इंटरफ़ेस (यूआई) टूल उपलब्ध कराता है.

फ़्लो तय करना

import { defineFlow } from '@genkit-ai/flow';

export const menuSuggestionFlow = defineFlow(
  {
    name: 'menuSuggestionFlow',
  },
  async (restaurantTheme) => {
    const suggestion = makeMenuItemSuggestion(restaurantTheme);

    return suggestion;
  }
);

फ़्लो के लिए इनपुट और आउटपुट स्कीमा को zod का इस्तेमाल करके तय किया जा सकता है.

import { defineFlow } from '@genkit-ai/flow';
import * as z from 'zod';

export const menuSuggestionFlow = defineFlow(
  {
    name: 'menuSuggestionFlow',
    inputSchema: z.string(),
    outputSchema: z.string(),
  },
  async (restaurantTheme) => {
    const suggestion = makeMenuItemSuggestion(input.restaurantTheme);

    return suggestion;
  }
);

स्कीमा के बारे में बताए जाने पर Genkit, इनपुट और आउटपुट के लिए स्कीमा की पुष्टि करेगा.

रनिंग फ़्लो

फ़्लो चलाने के लिए runFlow फ़ंक्शन का इस्तेमाल करें:

const response = await runFlow(menuSuggestionFlow, 'French');

फ़्लो चलाने के लिए, सीएलआई का इस्तेमाल भी किया जा सकता है:

genkit flow:run menuSuggestionFlow '"French"'

लाइव स्ट्रीम की गई

यहां एक ऐसे फ़्लो का उदाहरण दिया गया है जो फ़्लो से वैल्यू स्ट्रीम कर सकता है:

export const menuSuggestionFlow = defineFlow(
  {
    name: 'menuSuggestionFlow',
    streamSchema: z.string(),
  },
  async (restaurantTheme, streamingCallback) => {
    if (streamingCallback) {
      makeMenuItemSuggestionsAsync(restaurantTheme).subscribe((suggestion) => {
        streamingCallback(suggestion);
      });
    }
  }
);

ध्यान रखें कि streamingCallback तय नहीं हो सकता. यह सिर्फ़ तब तय किया जाता है, जब न्योता भेजने वाला क्लाइंट, स्ट्रीम किए गए रिस्पॉन्स के लिए अनुरोध कर रहा हो.

स्ट्रीमिंग मोड में फ़्लो शुरू करने के लिए, streamFlow फ़ंक्शन का इस्तेमाल करें:

const response = streamFlow(menuSuggestionFlow, 'French');

for await (const suggestion of response.stream()) {
  console.log('suggestion', suggestion);
}

अगर फ़्लो, स्ट्रीमिंग को लागू नहीं करता है, तो streamFlow, runFlow की तरह काम करेगा.

फ़्लो स्ट्रीम करने के लिए, सीएलआई का इस्तेमाल भी किया जा सकता है:

genkit flow:run menuSuggestionFlow '"French"' -s

फ़्लो डिप्लॉय करना

अगर आपको एचटीटीपी पर फ़्लो ऐक्सेस करना है, तो पहले उसे डिप्लॉय करें. Genkit, Firebase के लिए Cloud Functions और Cloud Run जैसे Express.js होस्ट के लिए इंटिग्रेशन की सुविधा ��ेता है.

डिप्लॉय किए गए फ़्लो, लोकल फ़्लो (जैसे, स्ट्रीमिंग और जांचने की क्षमता) जैसी सभी सुविधाओं के साथ काम करते हैं.

Firebase के लिए Cloud Function

'Firebase के लिए Cloud Functions' के साथ फ़्लो का इस्तेमाल करने के लिए, firebase प्लगिन का इस्तेमाल करें. इसके लिए, defineFlow को onFlow से बदलें और authPolicy को शामिल करें.

import { onFlow } from '@genkit-ai/firebase/functions';
import { firebaseAuth } from '@genkit-ai/firebase/auth';

export const menuSuggestionFlow = onFlow(
  {
    name: 'menuSuggestionFlow',
    authPolicy: firebaseAuth((user) => {
      if (!user.email_verified) {
        throw new Error("Verified email required to run flow");
      }
    }
  },
  async (restaurantTheme) => {
    // ....
  }
);

एक्सप्रेस.js

Cloud Run और मिलती-जुलती सेवाओं का इस्तेमाल करके फ़्लो डिप्लॉय करने के लिए, defineFlow का इस्तेमाल करके अपने फ़्लो तय करें और फिर startFlowsServer() को कॉल करें:

import { defineFlow, startFlowsServer } from '@genkit-ai/flow';

export const menuSuggestionFlow = defineFlow(
  {
    name: 'menuSuggestionFlow',
  },
  async (restaurantTheme) => {
    // ....
  }
);

startFlowsServer();

डिफ़ॉल्ट रूप से, startFlowsServer उन सभी फ़्लो को दिखाएगा जिन्हें आपने अपने कोड बेस में एचटीटीपी एंडपॉइंट के तौर पर तय किया है (जैसे, http://localhost:3400/menuSuggestionFlow).

आपके पास यह चुनने का विकल्प होता है कि फ़्लो सर्वर से कौनसे फ़्लो दिखाए जाएं. आपके पास कस्टम पोर्ट तय करने का विकल्प होता है. अगर सेट किया गया है, तो यह PORT एनवायरमेंट वैरिएबल का इस्तेमाल करेगा. सीओआरएस की सेटिंग भी सेट की जा सकती हैं.

import { defineFlow, startFlowsServer } from '@genkit-ai/flow';

export const flowA = defineFlow({ name: 'flowA' }, async (subject) => {
  // ....
});

export const flowB = defineFlow({ name: 'flowB' }, async (subject) => {
  // ....
});

startFlowsServer({
  flows: [flowB],
  port: 4567,
  cors: {
    origin: '*',
  },
});

फ़्लो ऑब्ज़र्वेबिलिटी

अगर तीसरे पक्ष के ऐसे SDK टूल का इस्तेमाल किया जा रहा है जो निगरानी करने के लिए नहीं बने हैं, तो हो सकता है कि आप उन्हें 'डेवलपर यूज़र इंटरफ़ेस' में एक अलग ट्रेस चरण के तौर पर देखना चाहें. आपको बस run फ़ंक्शन में कोड को रैप करना होगा.

import { defineFlow, run } from '@genkit-ai/flow';

export const menuSuggestionFlow = defineFlow(
  {
    name: 'menuSuggestionFlow',
    outputSchema: z.array(s.string()),
  },
  async (restaurantTheme) => {
    const themes = await run('find-similar-themes', async () => {
      return await findSimilarRestaurantThemes(restaurantTheme);
    });

    const suggestions = makeMenuItemSuggestions(themes);

    return suggestions;
  }
);