वेब ऐप्लिकेशन में हाइब्रिड अनुभवों के लिए स्ट्रक्चर्ड आउटपुट जनरेट करना

Gemini मॉडल, डिफ़ॉल्ट रूप से जवाबों को बिना किसी स्ट्रक्चर वाले टेक्स्ट के तौर पर दिखाते हैं. हालांकि, कुछ मामलों में स्ट्रक्चर्ड टेक्स्ट (जैसे, JSON या enum) की ज़रूरत होती है. उदाहरण के लिए, हो सकता है कि आपको जवाब का इस्तेमाल, डाउनस्ट्रीम के ऐसे अन्य कामों के लिए करना हो जिनके लिए डेटा स्कीमा का होना ज़रूरी है.

यह पक्का करने के लिए कि मॉडल से जनरेट किया गया आउटपुट हमेशा किसी खास स्कीमा के मुताबिक हो, स्कीमा तय की जा सकती है. यह स्कीमा, मॉडल के जवाबों के लिए ब्लूप्रिंट की तरह काम करती है. इसके बाद, मॉडल के आउटपुट से सीधे तौर पर डेटा निकाला जा सकता है. इसके लिए, पोस्ट-प्रोसेसिंग की ज़रूरत कम पड़ती है.

यहां इस्तेमाल के कुछ उदाहरण दिए गए हैं:

  • पक्का करें कि मॉडल का जवाब, मान्य JSON फ़ॉर्मैट में हो और आपके दिए गए स्कीमा के मुताबिक हो.
    उदाहरण के लिए, मॉडल ऐसी रेसिपी के लिए स्ट्रक्चर्ड एंट्री जनरेट कर सकता है जिनमें हमेशा रेसिपी का नाम, सामग्री की सूची, और तरीका शामिल होता है. इसके बाद, इस जानकारी को अपने ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) में आसानी से पार्स और दिखाया जा सकता है.

  • यह तय करना कि कैटगरी तय करने के टास्क के दौरान, मॉडल कैसे जवाब दे सकता है.
    उदाहरण के लिए, मॉडल से टेक्स्ट को लेबल के किसी खास सेट (जैसे, positive और negative जैसे एनम का कोई खास सेट) के साथ एनोटेट कराया जा सकता है. ऐसा मॉडल के जनरेट किए गए लेबल के बजाय किया जा सकता है. मॉडल के जनरेट किए गए लेबल में अलग-अलग तरह के लेबल शामिल हो सकते हैं, जैसे कि good, positive, negative या bad.

इस पेज पर, वेब ऐप्लिकेशन के लिए हाइब्रिड अनुभवों में स्ट्रक्चर्ड आउटपुट (जैसे कि JSON और enum) जनरेट करने का तरीका बताया गया है.

JSON आउटपुट पर जाएं एनम आउटपुट पर जाएं

स्ट्रक्चर्ड आउटपुट के लिए कॉन्फ़िगरेशन

क्लाउड पर होस्ट किए गए और उपयोगकर्ता के डिवाइस पर मौजूद, दोनों तरह के मॉडल का इस्तेमाल करके अनुमान लगाने के लिए, स्ट्रक्चर्ड आउटपुट (जैसे कि JSON और enum) जनरेट किया जा सकता है.

  • हाइब्रिड इन्फ़्रेंस मोड: inCloudParams और onDeviceParams, दोनों को कॉन्फ़िगर करें, ताकि मॉडल स्ट्रक्चर्ड आउटपुट दे सके. इससे कोई फ़र्क़ नहीं पड़ता कि इन्फ़्रेंस, क्लाउड में चलता है या डिवाइस पर:

    • उपयोगकर्ता के डिवाइस पर मौजूद मॉडल के लिए: onDeviceParams में responseConstraint की जानकारी दें. इसके लिए, Schema हेल्पर मेथड से बनाया गया स्कीमा या सामान्य JSON स्कीमा इस्तेमाल करें.

    • क्लाउड पर होस्ट किए गए मॉडल के लिए: inCloudParams में responseMimeType (JSON के लिए application/json या enum के लिए text/x.enum) और responseSchema की जानकारी दें.

  • नॉन-हाइब्रिड इन्फ़रेंस मोड: सिर्फ़ ऊपर बताए गए कॉन्फ़िगरेशन का इस्तेमाल करें.

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

इस पेज पर, सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट और कोड देखने के लिए, Gemini API पर क्लिक करें.

पक्का करें कि आपने हाइब्रिड अनुभव बनाने के लिए शुरुआती गाइड को पूरा कर लिया हो.


JSON आउटपुट पर जाएं एनम आउटपुट पर जाएं

JSON आउटपुट

यहां दिए गए उदाहरणों में, JSON फ़ॉर्मैट में आउटपुट देने वाले सामान्य उदाहरण को हाइब्रिड इन्फ़्रेंस (उदाहरण के लिए, PREFER_ON_DEVICE) के हिसाब से बदला गया है.

इन उदाहरणों में, मॉडल एक काल्पनिक कहानी के लिए, किरदार की प्रोफ़ाइलों की सूची जनरेट करता है. इसमें नाम, उम्र, प्रजाति, और वैकल्पिक ऐक्सेसरी जैसे स्ट्रक्चर्ड एट्रिब्यूट शामिल होते हैं.

जवाब के स्कीमा को इनमें से किसी भी तरीके से तय किया जा सकता है:

  • Firebase Schema हेल्पर मेथड (सुझाया गया): हेल्पर मेथड (जैसे कि Schema.object() और Schema.enumString()) का इस्तेमाल करके, अपने कोड में सीधे तौर पर छोटे और कम शब्दों वाले स्कीमा लिखें. इसके लिए, आपको अतिरिक्त बॉयलरप्लेट की ज़रूरत नहीं होगी.

  • प्लेन JSON स्कीमा: अगर आपके पास पहले से मौजूद स्कीमा की परिभाषाएं हैं, तो स्टैंडर्ड JSON स्कीमा ऑब्जेक्ट का इस्तेमाल करें. इसके अलावा, अगर आपको अलग-अलग प्लैटफ़ॉर्म या बैकएंड सेवाओं के साथ स्कीमा शेयर करने हैं या JSON फ़ाइलों से स्कीमा इंपोर्ट करने हैं, तो भी इसका इस्तेमाल करें.

पहला उदाहरण: Firebase Schema हेल्पर के तरीकों का इस्तेमाल करना

इस उदाहरण में, ऑब्जेक्ट स्कीमा को तय करने के लिए, Firebase AI Logic SDK टूल की ओर से उपलब्ध कराए गए Schema हेल्पर मेथड (जैसे कि Schema.object, Schema.array, Schema.string, और Schema.number) का इस्तेमाल किया गया है.

इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें.
उस सेक्शन में, आपको सेवा देने वाली कंपनी Gemini API के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

import { initializeApp } from "firebase/app";
import {
  getAI,
  getGenerativeModel,
  GoogleAIBackend,
  InferenceMode,
  Schema
} from "firebase/ai";

// TODO(developer): Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp.
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Define a schema using the `Schema` helper methods.
// Optional properties are specified in optionalProperties.
const jsonSchema = Schema.object({
  properties: {
    characters: Schema.array({
      items: Schema.object({
        properties: {
          name: Schema.string(),
          age: Schema.number(),
          species: Schema.string(),
          accessory: Schema.string()
        },
        optionalProperties: ["accessory"]
      })
    })
  }
});

// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
  mode: InferenceMode.INFERENCE_MODE,
  // For cloud-hosted models, specify MIME type and response schema.
  inCloudParams: {
    model: "CLOUD_MODEL_NAME",
    generationConfig: {
      responseMimeType: "application/json",
      responseSchema: jsonSchema
    }
  },
  // For on-device models, pass the schema as the response constraint.
  onDeviceParams: {
    promptOptions: {
      responseConstraint: jsonSchema
    }
  }
});

const prompt = "Create profiles for some characters for a fantasy story.";

// Generate the structured output.
const result = await model.generateContent(prompt);

// Access the generated JSON string conforming to the schema from response.text().
console.log(result.response.text());

// Parse the JSON string into a JavaScript object.
console.log(JSON.parse(result.response.text()));

दूसरा उदाहरण: सामान्य JSON स्कीमा का इस्तेमाल करना

इस उदाहरण में, स्कीमा को सिर्फ़ JSON का इस्तेमाल करके तय किया गया है.

इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें.
उस सेक्शन में, आपको सेवा देने वाली कंपनी Gemini API के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

import { initializeApp } from "firebase/app";
import {
  getAI,
  getGenerativeModel,
  GoogleAIBackend,
  InferenceMode
} from "firebase/ai";

// TODO(developer): Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp.
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Define the schema as a plain JSON object.
// Properties are required by default unless omitted from the required array.
const jsonSchema = {
  type: "object",
  properties: {
    characters: {
      type: "array",
      items: {
        type: "object",
        properties: {
          name: {
            type: "string",
            nullable: false
          },
          age: {
            type: "number",
            nullable: false
          },
          species: {
            type: "string",
            nullable: false
          },
          accessory: {
            type: "string",
            nullable: true
          }
        },
        nullable: false,
        required: [
          "name",
          "age",
          "species"
        ]
      },
      nullable: false
    }
  },
  nullable: false,
  required: [
    "characters"
  ]
};

// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
  mode: InferenceMode.INFERENCE_MODE,
  // For cloud-hosted models, specify MIME type and response schema.
  inCloudParams: {
    model: "CLOUD_MODEL_NAME",
    generationConfig: {
      responseMimeType: "application/json",
      responseSchema: jsonSchema
    }
  },
  // For on-device models, pass the schema as the response constraint.
  onDeviceParams: {
    promptOptions: {
      responseConstraint: jsonSchema
    }
  }
});

const prompt = "Create profiles for some characters for a fantasy story.";

// Generate the structured output.
const result = await model.generateContent(prompt);

// Access the generated JSON string conforming to the schema from response.text().
console.log(result.response.text());

// Parse the JSON string into a JavaScript object.
console.log(JSON.parse(result.response.text()));

Enum आउटपुट

यहां दिए गए उदाहरणों में, सामान्य एनम आउटपुट के उदाहरण को हाइब्रिड इन्फ़्रेंस (उदाहरण के लिए, PREFER_ON_DEVICE) के हिसाब से बदला गया है.

इन उदाहरणों में, मॉडल ने फ़िल्म की जानकारी को कैटगरी में बांटा है. इसके लिए, उसने अनुमति वाले विकल्पों की पहले से तय की गई सूची में से एक शैली चुनी है (drama, comedy या documentary).

जवाब के स्कीमा को इनमें से किसी भी तरीके से तय किया जा सकता है:

  • Firebase Schema हेल्पर मेथड (सुझाया गया): हेल्पर मेथड (जैसे कि Schema.object() और Schema.enumString()) का इस्तेमाल करके, अपने कोड में सीधे तौर पर छोटे और कम शब्दों वाले स्कीमा लिखें. इसके लिए, आपको अतिरिक्त बॉयलरप्लेट की ज़रूरत नहीं होगी.

  • प्लेन JSON स्कीमा: अगर आपके पास पहले से मौजूद स्कीमा की परिभाषाएं हैं, तो स्टैंडर्ड JSON स्कीमा ऑब्जेक्ट का इस्तेमाल करें. इसके अलावा, अगर आपको अलग-अलग प्लैटफ़ॉर्म या बैकएंड सेवाओं के साथ स्कीमा शेयर करने हैं या JSON फ़ाइलों से स्कीमा इंपोर्ट करने हैं, तो भी इसका इस्तेमाल करें.

पहला उदाहरण: Firebase Schema हेल्पर के तरीकों का इस्तेमाल करना

इस उदाहरण में, अनुमति वाली enum वैल्यू तय करने के लिए, Firebase AI Logic एसडीके के ज़रिए उपलब्ध कराए गए Schema.enumString हेल्पर तरीके का इस्तेमाल किया गया है.

इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें.
उस सेक्शन में, आपको सेवा देने वाली कंपनी Gemini API के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

import { initializeApp } from "firebase/app";
import {
  getAI,
  getGenerativeModel,
  GoogleAIBackend,
  InferenceMode,
  Schema
} from "firebase/ai";

// TODO(developer): Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp.
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Define an enum schema using the `Schema` helper method with allowed string values.
const enumSchema = Schema.enumString({
  enum: ["drama", "comedy", "documentary"]
});

// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
  mode: InferenceMode.INFERENCE_MODE,
  // For cloud-hosted models, specify MIME type and response schema.
  inCloudParams: {
    model: "CLOUD_MODEL_NAME",
    generationConfig: {
      responseMimeType: "text/x.enum",
      responseSchema: enumSchema
    }
  },
  // For on-device models, pass the enum schema as the response constraint.
  onDeviceParams: {
    promptOptions: {
      responseConstraint: enumSchema
    }
  }
});

const prompt = `The film aims to educate and inform viewers about real-life
subjects, events, or people. It offers a factual record of a particular topic
by combining interviews, historical footage, and narration. The primary purpose
of a film is to present information and provide insights into various aspects
of reality.`;

// Generate the structured enum output.
const result = await model.generateContent(prompt);

// Access the selected enum value string from response.text().
console.log(result.response.text());

दूसरा उदाहरण: सामान्य JSON स्कीमा का इस्तेमाल करना

इस उदाहरण में, सिर्फ़ JSON का इस्तेमाल करके enum स्कीमा को तय किया गया है.

इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें.
उस सेक्शन में, आपको सेवा देने वाली कंपनी Gemini API के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर सेवा देने वाली कंपनी के हिसाब से कॉन्टेंट दिखे.

import { initializeApp } from "firebase/app";
import {
  getAI,
  getGenerativeModel,
  GoogleAIBackend,
  InferenceMode
} from "firebase/ai";

// TODO(developer): Replace the following with your app's Firebase configuration.
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp.
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Define the enum schema as a plain JSON object.
const enumSchema = {
  type: "string",
  enum: ["drama", "comedy", "documentary"]
};

// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
  mode: InferenceMode.INFERENCE_MODE,
  // For cloud-hosted models, specify MIME type and response schema.
  inCloudParams: {
    model: "CLOUD_MODEL_NAME",
    generationConfig: {
      responseMimeType: "text/x.enum",
      responseSchema: enumSchema
    }
  },
  // For on-device models, pass the enum schema as the response constraint.
  onDeviceParams: {
    promptOptions: {
      responseConstraint: enumSchema
    }
  }
});

const prompt = `The film aims to educate and inform viewers about real-life
subjects, events, or people. It offers a factual record of a particular topic
by combining interviews, historical footage, and narration. The primary purpose
of a film is to present information and provide insights into various aspects
of reality.`;

// Generate the structured enum output.
const result = await model.generateContent(prompt);

// Access the selected enum value string from response.text().
console.log(result.response.text());


Firebase AI Logic के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें