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

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

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

यहां कुछ उदाहरण दिए गए हैं:

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

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

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

JSON आउटपुट पर जाएं enum आउटपुट पर जाएं

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

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

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

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

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

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

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

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

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


JSON आउटपुट पर जाएं enum आउटपुट पर जाएं

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) को शामिल करने के लिए, सामान्य enum आउटपुट के उदाहरण के हिसाब से बनाए गए हैं.

इन उदाहरणों में, मॉडल किसी फ़िल्म की जानकारी को कैटगरी में बांटता है. इसके लिए, वह पहले से तय की गई सूची में से कोई एक शैली चुनता है. जैसे, 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 के साथ अपने अनुभव के बारे में सुझाव/राय देना या शिकायत करना