Gemini मॉडल, डिफ़ॉल्ट रूप से जवाबों को अनस्ट्रक्चर्ड टेक्स्ट के तौर पर दिखाते हैं. हालांकि, कुछ इस्तेमाल के उदाहरणों के लिए स्ट्रक्चर्ड टेक्स्ट (जैसे, JSON या enum) की ज़रूरत होती है. उदाहरण के लिए, हो सकता है कि आपको डाउनस्ट्रीम के अन्य टास्क के लिए जवाब का इस्तेमाल करना हो. इसके लिए, तय डेटा स्कीमा की ज़रूरत होती है.
यह पक्का करने के लिए कि मॉडल से जनरेट किया गया आउटपुट हमेशा किसी खास स्कीमा के मुताबिक हो, स्कीमा तय किया जा सकता है. यह स्कीमा, मॉडल के जवाबों के लिए ब्लूप्रिंट की तरह काम करता है. इसके बाद, पोस्ट-प्रोसेसिंग कम करके, मॉडल के आउटपुट से सीधे डेटा निकाला जा सकता है.
यहां इस्तेमाल के कुछ उदाहरण दिए गए हैं:
यह पक्का करें कि मॉडल का जवाब, मान्य JSON फ़ॉर्मैट में हो और आपके दिए गए स्कीमा के मुताबिक हो.
उदाहरण के लिए, मॉडल, रेसिपी के लिए स्ट्रक्चर्ड एंट्री जनरेट कर सकता है. इनमें हमेशा रेसिपी का नाम, सामग्री की सूची, और बनाने के तरीके शामिल होते हैं. इसके बाद, इस जानकारी को अपने ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) में आसानी से पार्स और दिखाया जा सकता है.वर्गीकरण के टास्क के दौरान, मॉडल के जवाब देने के तरीके पर पाबंदी लगाएं.
उदाहरण के लिए, मॉडल से टेक्स्ट को लेबल के किसी खास सेट (जैसे, enum का कोई खास सेट, जैसे किpositiveऔरnegative) से एनोटेट कराया जा सकता है. इसके बजाय, मॉडल से जनरेट किए गए लेबल (जिनमेंgood,positive,negativeयाbadजैसी अलग-अलग वैल्यू हो सकती हैं) का इस्तेमाल किया जा सकता है.
इस पेज पर, Android ऐप्लिकेशन के लिए हाइब्रिड अनुभवों में स्ट्रक्चर्ड आउटपुट (जैसे, JSON और enum) जनरेट करने का तरीका बताया गया है.
JSON फ़ॉर्मैट में आउटपुट पर जाएं enum फ़ॉर्मैट में आउटपुट पर जाएं
स्ट्रक्चर्ड आउटपुट के लिए कॉन्फ़िगरेशन
उपयोगकर्ता के डिवाइस पर और क्लाउड पर होस्ट किए गए अनुमान, दोनों के लिए स्ट्रक्चर्ड आउटपुट (जैसे, JSON और enum) जनरेट किया जा सकता है.
स्ट्रक्चर्ड आउटपुट जनरेट करने के लिए, अपना स्कीमा सीधे
generateObject() को पास करें.
स्कीमा की ज़रूरतें, कॉन्फ़िगर किए गए इन्फ़रेंस मोड पर निर्भर करती हैं:
उपयोगकर्ता के डिवाइस पर और हाइब्रिड अनुमान (
ONLY_ON_DEVICE,PREFER_ON_DEVICE, औरPREFER_IN_CLOUD) के लिए:- इसके लिए, KSP प्रोसेसर के साथ Kotlin
data classपर@Generableएनोटेशन का इस्तेमाल करना ज़रूरी है. मैन्युअल स्कीमा और सीधेenum classएनोटेशन काम नहीं करते. - जब इन्फ़रेंस डिवाइस पर चलता है, तो एसडीके, ML Kit Prompt API का इस्तेमाल करके, स्कीमा को डिवाइस पर मौजूद मॉडल के लिए पाबंदियों में अपने-आप बदल देता है.
- अगर हाइब्रिड अनुरोध, क्लाउड इन्फ़रेंस पर वापस आता है, तो एसडीके अपने-आप
सेट कर देता है
responseMimeTypeकोapplication/jsonपर. साथ ही, स्कीमा को पास कर देता है क्लाउड पर होस्ट किए गए Gemini मॉडल को.
- इसके लिए, KSP प्रोसेसर के साथ Kotlin
सिर्फ़ क्लाउड इन्फ़रेंस (
ONLY_IN_CLOUD) के लिए:- यह
@Generableएनोटेशन (सुझाया गया) और मैन्युअल स्कीमा (जिन्हेंJsonSchemaहेल्पर तरीकों का इस्तेमाल करके बनाया जाता है), दोनों के साथ काम करता है. - एसडीके,
responseMimeTypeकोapplication/jsonपर अपने-आप सेट कर देता है और स्कीमा को क्लाउड पर होस्ट किए गए Gemini मॉडल को पास कर देता है.
- यह
शुरू करने से पहले
|
अपने Gemini API प्रोवाइडर पर क्लिक करके, इस पेज पर प्रोवाइडर के हिसाब से कॉन्टेंट और कोड देखें. |
स्ट्रक्चर्ड आउटपुट जनरेट करने से पहले, पक्का करें कि आपने यह सेटअप पूरा कर लिया हो:
हाइब्रिड अनुभव बनाने के लिए, शुरू करने से जुड़ी गाइड पूरी करें. इसमें Firebase प्रोजेक्ट सेट अप करने, डिवाइस पर मौजूद मॉडल डाउनलोड करने, और App Checkको कॉन्फ़िगर करने के बारे में बताया गया है.
Kotlin Symbol Processing (KSP) प्लगिन को कॉन्फ़िगर करें और अपने ऐप्लिकेशन में Firebase AI KSP डिपेंडेंसी जोड़ें.
अपनी मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (जैसे,
<project>/<app-module>/build.gradle.kts) में, KSP प्लगिन, Kotlin Serialization प्लगिन, और ज़रूरी डिपेंडेंसी जोड़ें: क्लाउड पर होस्ट किए गए मॉडल के साथ स्टैंडर्डplugins { // ... other plugins id("com.google.gms.google-services") id("com.google.devtools.ksp") version "LATEST_VERSION" id("org.jetbrains.kotlin.plugin.serialization") version "LATEST_VERSION" } dependencies { // ... other androidx dependencies // Add the dependencies for the Firebase AI Logic and App Check libraries. implementation("com.google.firebase:firebase-ai:18.0.0") implementation("com.google.firebase:firebase-ai-ondevice:16.0.0-beta06") implementation("com.google.firebase:firebase-appcheck-debug:20.0.0") // Add the Firebase AI KSP processor for schema generation. ksp("com.google.firebase:firebase-ai-ksp-processor:17.0.0") // (Optional) Add kotlinx.serialization JSON library for object decoding. implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:LATEST_VERSION") }
generateContent()का इस्तेमाल किया जा सकता है.
JSON फ़ॉर्मैट में आउटपुट पर जाएं enum फ़ॉर्मैट में आउटपुट पर जाएं
JSON फ़ॉर्मैट में आउटपुट
यहां दिए गए उदाहरण,
सामान्य JSON फ़ॉर्मैट में आउटपुट के उदाहरण
को हाइब्रिड इन्फ़रेंस (उदाहरण के लिए, PREFER_ON_DEVICE) के हिसाब से ढालते हैं.
इन उदाहरणों के लिए, मॉडल, फ़ैंटेसी कहानी के लिए किरदारों की प्रोफ़ाइल की सूची जनरेट करता है. इसमें नाम, उम्र, प्रजाति, और वैकल्पिक ऐक्सेसरी जैसे स्ट्रक्चर्ड एट्रिब्यूट शामिल होते हैं.
जवाब के स्कीमा को इनमें से किसी भी तरीके से तय किया जा सकता है:
KSP एनोटेशन (
@Generableऔर@Guide)- ये सभी इन्फ़रेंस मोड के लिए काम करते हैं. साथ ही, डिवाइस पर और हाइब्रिड इन्फ़रेंस (खास तौर पर,
ONLY_ON_DEVICE,PREFER_ON_DEVICE, औरPREFER_IN_CLOUD) के लिए इनका इस्तेमाल करना ज़रूरी है. - कंपाइल करने के दौरान स्कीमा को अपने-आप जनरेट करने और
getObject()का इस्तेमाल करके, जवाबों को सीधे मज़बूती से टाइप किए गए ऑब्जेक्ट में डीसीरियलाइज़ करने के लिए, Kotlin डेटा क्लास तय करें.
- ये सभी इन्फ़रेंस मोड के लिए काम करते हैं. साथ ही, डिवाइस पर और हाइब्रिड इन्फ़रेंस (खास तौर पर,
मैन्युअल
JsonSchemaहेल्पर तरीके- ये सिर्फ़ क्लाउड-आधारित इन्फ़रेंस (खास तौर पर,
ONLY_IN_CLOUD) के लिए काम करते हैं. - KSP प्रोसेसर का इस्तेमाल किए बिना, कोड में मैन्युअल तरीके से
JsonSchemaबनाएं. साथ ही,response.response.textसे रॉ JSON स्ट्रिंग पढ़ें.
- ये सिर्फ़ क्लाउड-आधारित इन्फ़रेंस (खास तौर पर,
पहला उदाहरण: KSP के साथ @Generable और @Guide एनोटेशन का इस्तेमाल करना
@Serializable और @Generable से एनोटेट की गई Kotlin data class तय करें.
मॉडल को ब्यौरे, वैल्यू बाउंड या आइटम की पाबंदियां देने के लिए, प्रॉपर्टी पर @Guide एनोटेशन का इस्तेमाल करें.
यह तरीका सभी इन्फ़रेंस मोड के लिए काम करता है. साथ ही, उपयोगकर्ता के डिवाइस पर और हाइब्रिड अनुभवों (खास तौर पर, ONLY_ON_DEVICE, PREFER_ON_DEVICE, और PREFER_IN_CLOUD) के लिए इसका इस्तेमाल करना ज़रूरी है.
|
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें. उस सेक्शन में, आपको चुने गए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर प्रोवाइडर के हिसाब से कॉन्टेंट दिखे. |
import com.google.firebase.Firebase
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.InferenceMode
import com.google.firebase.ai.OnDeviceConfig
import com.google.firebase.ai.annotations.Generable
import com.google.firebase.ai.annotations.Guide
import com.google.firebase.ai.ai
import kotlinx.serialization.Serializable
// Define data classes representing the schema, annotated with @Serializable and @Generable.
// You can provide descriptions on @Generable and @Guide to guide the model's output.
@Serializable
@Generable(description = "A character profile for a fantasy story")
data class Character(
val name: String,
val age: Int,
val species: String,
// Use @Guide to add property descriptions, value bounds (minimum/maximum), or formats.
// Properties with default values or nullable types are treated as optional in the schema.
@Guide(description = "An accessory the character wears or carries")
val accessory: String? = null
) {
// An empty companion object is required for KSP to generate the firebaseAISchema() extension.
companion object
}
@Serializable
@Generable(description = "A list of character profiles")
data class CharacterList(
// Use minItems or maxItems to specify collection size bounds for the model.
@Guide(description = "List of characters", minItems = 1)
val characters: List<Character>
) {
// An empty companion object is required for KSP to generate the firebaseAISchema() extension.
companion object
}
// Initialize the Gemini Developer API backend service.
// Create a GenerativeModel instance configured for hybrid inference (like PREFER_ON_DEVICE).
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "CLOUD_MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.INFERENCE_MODE)
)
// Obtain the schema generated by KSP via the firebaseAISchema() extension.
val schema = CharacterList.firebaseAISchema()
val prompt = "Create profiles for some characters for a fantasy story."
// Generate the structured object (the SDK applies the schema to on-device or cloud models).
val response = model.generateObject(schema, prompt)
// Access the strongly-typed deserialized object directly via getObject().
val characterList: CharacterList? = response.getObject()
characterList?.characters?.forEach { character ->
println("Name: ${character.name}, Species: ${character.species}, Age: ${character.age}")
println("Accessory: ${character.accessory ?: "None"}")
}
दूसरा उदाहरण: मैन्युअल JsonSchema हेल्पर तरीकों का इस्तेमाल करना
अगर आपका ऐप्लिकेशन सिर्फ़ क्लाउड-आधारित अनुमान (खास तौर पर,
ONLY_IN_CLOUD) का इस्तेमाल कर रहा है, तो Firebase AI Logic Firebase AI Logic SDK टूल से मिले हेल्पर तरीकों का इस्तेमाल करके, मैन्युअल तरीके से
JsonSchema
बनाया जा सकता है.
|
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें. उस सेक्शन में, आपको चुने गए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर प्रोवाइडर के हिसाब से कॉन्टेंट दिखे. |
import com.google.firebase.Firebase
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.InferenceMode
import com.google.firebase.ai.OnDeviceConfig
import com.google.firebase.ai.ai
import com.google.firebase.ai.type.JsonSchema
// Define the schema manually using JsonSchema helper methods.
// Properties are required by default unless specified in optionalProperties.
val jsonSchema = JsonSchema.obj(
properties = mapOf(
"characters" to JsonSchema.array(
items = JsonSchema.obj(
properties = mapOf(
"name" to JsonSchema.string(),
"accessory" to JsonSchema.string(),
"age" to JsonSchema.integer(),
"species" to JsonSchema.string()
),
optionalProperties = listOf("accessory")
)
)
)
)
// Initialize the Gemini Developer API backend service.
// Manual schemas are only supported for cloud-based inference (specifically, ONLY_IN_CLOUD).
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "CLOUD_MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.ONLY_IN_CLOUD)
)
// Call generateObject() with the manual schema and prompt.
val response = model.generateObject(
jsonSchema,
"Create profiles for some characters for a fantasy story."
)
// Access the generated JSON string conforming to the schema from response.response.text.
println(response.response.text)
Enum फ़ॉर्मैट में आउटपुट
यहां दिए गए उदाहरण,
enum फ़ॉर्मैट में सामान्य आउटपुट के उदाहरण
को हाइब्रिड इन्फ़रेंस (उदाहरण के लिए, PREFER_ON_DEVICE) के हिसाब से ढालते हैं.
इन उदाहरणों के लिए, मॉडल, फ़िल्म के ब्यौरे को, अनुमति वाले विकल्पों (drama, comedy या documentary) की पहले से तय की गई सूची में से कोई एक शैली चुनकर कैटगरी में बांटता है.
जवाब के स्कीमा को इनमें से किसी भी तरीके से तय किया जा सकता है:
-
- ये सभी इन्फ़रेंस मोड के लिए काम करते हैं. साथ ही, डिवाइस पर और हाइब्रिड इन्फ़रेंस (खास तौर पर,
ONLY_ON_DEVICE,PREFER_ON_DEVICE, औरPREFER_IN_CLOUD) के लिए इनका इस्तेमाल करना ज़रूरी है. - स्कीमा को अपने-आप जनरेट करने और
getObject()का इस्तेमाल करके, जवाबों को सीधे मज़बूती से टाइप किए गए ऑब्जेक्ट में डीसीरियलाइज़ करने के लिए, Kotlindata classमें रैप किया गया enum तय करें.
- ये सभी इन्फ़रेंस मोड के लिए काम करते हैं. साथ ही, डिवाइस पर और हाइब्रिड इन्फ़रेंस (खास तौर पर,
मैन्युअल
JsonSchemaहेल्पर तरीके- ये सिर्फ़ क्लाउड-आधारित इन्फ़रेंस (खास तौर पर,
ONLY_IN_CLOUD) के लिए काम करते हैं. - KSP प्रोसेसर का इस्तेमाल किए बिना, मैन्युअल तरीके से enum
JsonSchemaबनाएंJsonSchema.enumeration(). साथ ही,response.response.textसे चुनी गई स्ट्रिंग पढ़ें.
- ये सिर्फ़ क्लाउड-आधारित इन्फ़रेंस (खास तौर पर,
पहला उदाहरण: KSP के साथ @Generable एनोटेशन का इस्तेमाल करना
अनुमति वाली वैल्यू को दिखाने वाली enum class तय करें. साथ ही, इसे @Serializable और @Generable से एनोटेट की गई Kotlin data class में रैप करें.
यह तरीका सभी इन्फ़रेंस मोड के लिए काम करता है. साथ ही, उपयोगकर्ता के डिवाइस पर और हाइब्रिड अनुभवों (खास तौर पर, ONLY_ON_DEVICE, PREFER_ON_DEVICE, और PREFER_IN_CLOUD) के लिए इसका इस्तेमाल करना ज़रूरी है.
|
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें. उस सेक्शन में, आपको चुने गए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर प्रोवाइडर के हिसाब से कॉन्टेंट दिखे. |
import com.google.firebase.Firebase
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.InferenceMode
import com.google.firebase.ai.OnDeviceConfig
import com.google.firebase.ai.annotations.Generable
import com.google.firebase.ai.annotations.Guide
import com.google.firebase.ai.ai
import kotlinx.serialization.Serializable
// Define an enum class representing the allowed options.
@Serializable
enum class FilmGenre {
DRAMA,
COMEDY,
DOCUMENTARY
}
// Wrap the enum in a data class annotated with @Serializable and @Generable.
// On-device inference requires an @Generable data class.
// Direct enum annotations are not supported on-device.
@Serializable
@Generable(description = "The classification result for the film")
data class FilmClassification(
@Guide(description = "The genre of the film")
val genre: FilmGenre
) {
// An empty companion object is required for KSP to generate the firebaseAISchema() extension.
companion object
}
// Initialize the Gemini Developer API backend service.
// Create a GenerativeModel instance configured for hybrid inference (like PREFER_ON_DEVICE).
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "CLOUD_MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.INFERENCE_MODE)
)
// Obtain the schema generated by KSP via the firebaseAISchema() extension.
val schema = FilmClassification.firebaseAISchema()
val 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 object (the SDK applies the schema to on-device or cloud models).
val response = model.generateObject(schema, prompt)
// Access the strongly-typed deserialized object and enum value directly via getObject().
val classification: FilmClassification? = response.getObject()
val genre: FilmGenre? = classification?.genre
println("Selected genre: $genre")
दूसरा उदाहरण: मैन्युअल JsonSchema हेल्पर तरीकों का इस्तेमाल करना
अगर आपका ऐप्लिकेशन सिर्फ़ क्लाउड-आधारित अनुमान (खास तौर पर,
ONLY_IN_CLOUD) का इस्तेमाल कर रहा है, तो Firebase AI Logic Firebase AI Logic SDK टूल से मिले हेल्पर तरीकों का इस्तेमाल करके, मैन्युअल तरीके से enum
JsonSchema
बनाया जा सकता है.
|
इस सैंपल को आज़माने से पहले, अपने प्रोजेक्ट और ऐप्लिकेशन को सेट अप करने के लिए, इस गाइड का
शुरू करने से पहले सेक्शन पूरा करें. उस सेक्शन में, आपको चुने गए Gemini API प्रोवाइडर के लिए एक बटन पर भी क्लिक करना होगा, ताकि आपको इस पेज पर प्रोवाइडर के हिसाब से कॉन्टेंट दिखे. |
import com.google.firebase.Firebase
import com.google.firebase.ai.type.GenerativeBackend
import com.google.firebase.ai.InferenceMode
import com.google.firebase.ai.OnDeviceConfig
import com.google.firebase.ai.ai
import com.google.firebase.ai.type.JsonSchema
// Define an enum schema with allowed string values and a description.
val enumSchema = JsonSchema.enumeration(
values = listOf("drama", "comedy", "documentary"),
description = "The genre of the film"
)
// Initialize the Gemini Developer API backend service.
// Manual schemas are only supported for cloud-based inference (specifically, ONLY_IN_CLOUD).
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "CLOUD_MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.ONLY_IN_CLOUD)
)
val 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.
"""
// Call generateObject() with the enum schema and prompt.
val response = model.generateObject(enumSchema, prompt)
// Access the selected enum value string from response.response.text.
println(response.response.text)
सुझाव/राय देना या शिकायत करना के बारे में अपने अनुभव के साथ Firebase AI Logic