Gemini मॉडल, डिफ़ॉल्ट रूप से जवाबों को बिना किसी स्ट्रक्चर वाले टेक्स्ट के तौर पर दिखाते हैं. हालांकि, इस्तेमाल के कुछ मामलों में स्ट्रक्चर्ड टेक्स्ट (जैसे, JSON या enum) की ज़रूरत होती है. उदाहरण के लिए, हो सकता है कि आपको जवाब का इस्तेमाल अन्य डाउनस्ट्रीम टास्क के लिए करना हो. इसके लिए, एक तय डेटा स्कीमा की ज़रूरत होती है.
यह पक्का करने के लिए कि मॉडल से जनरेट किया गया आउटपुट हमेशा किसी खास स्कीमा के मुताबिक हो, स्कीमा तय की जा सकती है. यह स्कीमा, मॉडल के जवाबों के लिए ब्लूप्रिंट की तरह काम करती है. इसके बाद, मॉडल के आउटपुट से सीधे तौर पर डेटा निकाला जा सकता है. इसके लिए, पोस्ट-प्रोसेसिंग की ज़रूरत कम पड़ती है.
यहां इस्तेमाल के कुछ उदाहरण दिए गए हैं:
पक्का करें कि मॉडल का जवाब, मान्य JSON फ़ॉर्मैट में हो और आपके दिए गए स्कीमा के मुताबिक हो.
उदाहरण के लिए, मॉडल ऐसी रेसिपी के लिए स्ट्रक्चर्ड एंट्री जनरेट कर सकता है जिनमें हमेशा रेसिपी का नाम, सामग्री की सूची, और बनाने का तरीका शामिल होता है. इसके बाद, इस जानकारी को आसानी से पार्स किया जा सकता है और इसे अपने ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) में दिखाया जा सकता है.यह तय करना कि क्लासिफ़िकेशन के टास्क के दौरान मॉडल कैसे जवाब दे सकता है.
उदाहरण के लिए, मॉडल को टेक्स्ट की व्याख्या करने के लिए, लेबल का कोई खास सेट दिया जा सकता है. जैसे, एनम का कोई खास सेट, जैसे किpositiveऔरnegative. ऐसा करने से, मॉडल के जनरेट किए गए लेबल के बजाय, आपके दिए गए लेबल का इस्तेमाल किया जाएगा. मॉडल के जनरेट किए गए लेबल में अलग-अलग तरह के लेबल शामिल हो सकते हैं. जैसे,good,positive,negativeयाbad.
इस पेज पर, वेब ऐप्लिकेशन के लिए हाइब्रिड अनुभवों में स्ट्रक्चर्ड आउटपुट (जैसे कि JSON और enum) जनरेट करने का तरीका बताया गया है.
JSON आउटपुट पर जाएं enum आउटपुट पर जाएं
स्ट्रक्चर्ड आउटपुट के लिए कॉन्फ़िगरेशन
स्ट्रक्चर्ड आउटपुट (जैसे कि JSON और enum) जनरेट करने की सुविधा, क्लाउड पर होस्ट किए गए और उपयोगकर्ता के डिवाइस पर मौजूद, दोनों तरह के मॉडल का इस्तेमाल करके अनुमान लगाने के लिए उपलब्ध है.
हाइब्रिड इन्फ़्रेंस मोड:
inCloudParamsऔरonDeviceParams, दोनों को कॉन्फ़िगर करें, ताकि मॉडल स्ट्रक्चर्ड आउटपुट दे सके. इससे कोई फ़र्क़ नहीं पड़ता कि इन्फ़्रेंस, क्लाउड में चलता है या डिवाइस पर:उपयोगकर्ता के डिवाइस पर मौजूद मॉडल के लिए:
onDeviceParamsमेंresponseConstraintकी जानकारी दें. इसके लिए,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 आउटपुट
यहां दिए गए उदाहरणों में, सामान्य enum आउटपुट के उदाहरण को हाइब्रिड इन्फ़्रेंस (जैसे, PREFER_ON_DEVICE) के हिसाब से बदला गया है.
इन उदाहरणों में, मॉडल ने फ़िल्म की जानकारी को इस तरह से कैटगरी में बांटा है कि उसने पहले से तय की गई सूची में से सिर्फ़ एक शैली चुनी है. जैसे, drama, comedy या documentary.
इनमें से किसी भी तरीके का इस्तेमाल करके, जवाब के स्कीमा तय किए जा सकते हैं:
Firebase
Schemaहेल्पर तरीके (सुझाया गया): हेल्पर तरीकों (जैसे किSchema.object()औरSchema.enumString()) का इस्तेमाल करके, अपने कोड में सीधे तौर पर छोटे और कम शब्दों वाले स्कीमा लिखें. इसके लिए, आपको अतिरिक्त बॉयलरप्लेट की ज़रूरत नहीं होगी.प्लेन JSON स्कीमा: अगर आपके पास पहले से स्कीमा की परिभाषाएं मौजूद हैं, अलग-अलग प्लैटफ़ॉर्म या बैकएंड सेवाओं के साथ स्कीमा शेयर करने हैं या JSON फ़ाइलों से स्कीमा इंपोर्ट करने हैं, तो स्टैंडर्ड JSON स्कीमा ऑब्जेक्ट का इस्तेमाल करें.
पहला उदाहरण: Firebase Schema हेल्पर मेथड का इस्तेमाल करना
इस उदाहरण में, Firebase AI Logic SDK की ओर से उपलब्ध कराए गए Schema.enumString हेल्पर तरीके का इस्तेमाल करके, अनुमति वाली enum वैल्यू तय की गई हैं.
|
इस सैंपल को आज़माने से पहले, इस गाइड के शुरू करने से पहले सेक्शन में दिया गया तरीका अपनाकर, अपना प्रोजेक्ट और ऐप्लिकेशन सेट अप करें. उस सेक्शन में, आपको अपनी चुनी गई 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 के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें