Gemini API, डिफ़ॉल्ट रूप से जवाबों को बिना स्ट्रक्चर वाले टेक्स्ट के तौर पर दिखाता है. हालांकि, कुछ इस्तेमाल के उदाहरणों के लिए, JSON जैसे स्ट्रक्चर्ड टेक्स्ट की ज़रूरत होती है. उदाहरण के लिए, हो सकता है कि आप जवाब का इस्तेमाल, डाउनस्ट्रीम के उन अन्य टास्क के लिए कर रहे हों जिनके लिए डेटा स्कीमा की ज़रूरत होती है.
यह पक्का करने के लिए कि मॉडल का जनरेट किया गया आउटपुट हमेशा किसी खास स्कीमा के मुताबिक हो, रिस्पॉन्स स्कीमा तय किया जा सकता है. यह मॉडल के रिस्पॉन्स के लिए ब्लूप्रिंट की तरह काम करता है. इसके बाद, पोस्ट-प्रोसेसिंग के कम चरण के साथ, मॉडल के आउटपुट से सीधे डेटा निकाला जा सकता है.
यहां कुछ उदाहरण दिए गए हैं:
पक्का करें कि मॉडल का रिस्पॉन्स, मान्य JSON फ़ॉर्मैट में हो और आपके दिए गए स्कीमा के मुताबिक हो.
उदाहरण के लिए, यह मॉडल उन रेसिपी के लिए स्ट्रक्चर्ड एंट्री जनरेट कर सकता है जिनमें हमेशा रेसिपी का नाम, सामग्री की सूची, और बनाने का तरीका शामिल होता है. इसके बाद, इस जानकारी को अपने ऐप्लिकेशन के यूज़र इंटरफ़ेस (यूआई) में आसानी से पार्स और दिखाया जा सकता है.यह तय करना कि किसी मॉडल को, डेटा को अलग-अलग कैटगरी में बांटने के टास्क के दौरान किस तरह के जवाब देने हैं.
उदाहरण के लिए, मॉडल के जनरेट किए गए लेबल के बजाय, टेक्स्ट पर एनोटेशन करने के लिए, मॉडल को लेबल के किसी खास सेट का इस्तेमाल करने के लिए कहा जा सकता है. जैसे,positive
औरnegative
जैसे एनम का कोई खास सेट.positive
negative
good
bad
इस गाइड में, generateContent
को कॉल करते समय responseSchema
का इस्तेमाल करके, JSON आउटपुट जनरेट करने का तरीका बताया गया है. यह सिर्फ़ टेक्स्ट इनपुट पर फ़ोकस करता है. हालांकि, Gemini, मल्टीमोडल अनुरोधों के लिए भी स्ट्रक्चर्ड जवाब दे सकता है. इन अनुरोधों में, इमेज, वीडियो, और ऑडियो को इनपुट के तौर पर शामिल किया जाता है.
इस पेज पर सबसे नीचे, आउटपुट के तौर पर एनम वैल्यू जनरेट करने जैसे और उदाहरण दिए गए हैं. स्ट्रक्चर्ड आउटपुट जनरेट करने के अन्य उदाहरण देखने के लिए, Google Cloud दस्तावेज़ में उदाहरण के तौर पर दिए गए स्कीमा और मॉडल रिस्पॉन्स की सूची देखें.
शुरू करने से पहले
अगर आपने अब तक ऐसा नहीं किया है, तो शुरू करने से जुड़ी गाइड पढ़ें. इसमें, Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, SDK टूल जोड़ने, Vertex AI सेवा को शुरू करने, और GenerativeModel
इंस्टेंस बनाने का तरीका बताया गया है.
पहला चरण: जवाब का स्कीमा तय करना
मॉडल के आउटपुट के स्ट्रक्चर, फ़ील्ड के नाम, और हर फ़ील्ड के लिए उम्मीद के मुताबिक डेटा टाइप की जानकारी देने के लिए, रिस्पॉन्स स्कीमा तय करें.
जब कोई मॉडल जवाब जनरेट करता है, तो वह आपके प्रॉम्प्ट में मौजूद फ़ील्ड के नाम और कॉन्टेक्स्ट का इस्तेमाल करता है. हमारा सुझाव है कि आपके इंटेंट के बारे में साफ़ तौर पर पता चलने के लिए, ज़रूरत के हिसाब से साफ़ तौर पर जानकारी देने वाले स्ट्रक्चर, फ़ील्ड के ऐसे नामों, और ब्यौरों का इस्तेमाल करें जिनसे यह साफ़ तौर पर पता चलता हो कि वे किस डेटा से जुड़े हैं.
रिस्पॉन्स स्कीमा के लिए ध्यान देने वाली बातें
रिस्पॉन्स स्कीमा लिखते समय इन बातों का ध्यान रखें:
रिस्पॉन्स स्कीमा का साइज़, इनपुट टोकन की सीमा में गिना जाता है.
रिस्पॉन्स स्कीमा की सुविधा, इन रिस्पॉन्स MIME टाइप के साथ काम करती है:
application/json
: रिस्पॉन्स स्कीमा में बताए गए तरीके के हिसाब से JSON आउटपुट करना (स्ट्रक्चर्ड आउटपुट की ज़रूरी शर्तों के लिए मददगार)text/x.enum
: रिस्पॉन्स स्कीमा में बताई गई वैल्यू के तौर पर, एक enum वैल्यू दिखाएं (वर्गीकरण के टास्क के लिए मददगार)
रिस्पॉन्स स्कीमा की सुविधा, इन स्कीमा फ़ील्ड के साथ काम करती है:
enum
items
maxItems
nullable
properties
required
अगर किसी ऐसे फ़ील्ड का इस्तेमाल किया जाता है जो काम नहीं करता, तो मॉडल आपके अनुरोध को मैनेज कर सकता है. हालांकि, वह उस फ़ील्ड को अनदेखा कर देता है. ध्यान दें कि ऊपर दी गई सूची, OpenAPI 3.0 स्कीमा ऑब्जेक्ट का सबसेट है. Vertex AI स्कीमा का रेफ़रंस देखें.
डिफ़ॉल्ट रूप से, Vertex AI in Firebase SDKs के लिए, सभी फ़ील्ड को ज़रूरी माना जाता है. हालांकि, अगर आपने उन्हें
optionalProperties
ऐरे में 'ज़रूरी नहीं है' के तौर पर सेट किया है, तो ऐसा नहीं होगा. इन वैकल्पिक फ़ील्ड के लिए, मॉडल फ़ील्ड में अपने-आप जानकारी भर सकता है या उन्हें स्किप कर सकता है.ध्यान दें कि यह Vertex AI Gemini API के लिए डिफ़ॉल्ट व्यवहार से उलट है.
दूसरा चरण: JSON जनरेट करने के लिए, रिस्पॉन्स स्कीमा के साथ प्रॉम्प्ट भेजना
नीचे दिए गए उदाहरण में, स्ट्रक्चर्ड JSON आउटपुट जनरेट करने का तरीका बताया गया है.
GenerativeModel
इंस्टेंस बनाते समय, सही responseMimeType
(इस उदाहरण में, application/json
) के साथ-साथ वह responseSchema
भी बताएं जिसका इस्तेमाल मॉडल को करना है.
Swift
import FirebaseVertexAI
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
let jsonSchema = Schema.object(
properties: [
"characters": Schema.array(
items: .object(
properties: [
"name": .string(),
"age": .integer(),
"species": .string(),
"accessory": .enumeration(values: ["hat", "belt", "shoes"]),
],
optionalProperties: ["accessory"]
)
),
]
)
// Initialize the Vertex AI service and the generative model.
let model = VertexAI.vertexAI().generativeModel(
modelName: "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
generationConfig: GenerationConfig(
responseMIMEType: "application/json",
responseSchema: jsonSchema
)
)
let prompt = "For use in a children's card game, generate 10 animal-based characters."
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")
Kotlin
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
val jsonSchema = Schema.obj(
mapOf("characters" to Schema.array(
Schema.obj(
mapOf(
"name" to Schema.string(),
"age" to Schema.integer(),
"species" to Schema.string(),
"accessory" to Schema.enumeration(listOf("hat", "belt", "shoes")),
),
optionalProperties = listOf("accessory")
)
))
)
// Initialize the Vertex AI service and the generative model.
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
generationConfig = generationConfig {
responseMimeType = "application/json"
responseSchema = jsonSchema
})
val prompt = "For use in a children's card game, generate 10 animal-based characters."
val response = generativeModel.generateContent(prompt)
print(response.text)
Java
Java के लिए, इस SDK टूल में स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी सेPublisher
टाइप दिखाते हैं.
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
Schema jsonSchema = Schema.obj(
/* properties */
Map.of(
"characters", Schema.array(
/* items */ Schema.obj(
/* properties */
Map.of("name", Schema.str(),
"age", Schema.numInt(),
"species", Schema.str(),
"accessory",
Schema.enumeration(
List.of("hat", "belt", "shoes")))
))),
List.of("accessory"));
// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "application/json";
configBuilder.responseSchema = jsonSchema;
GenerationConfig generationConfig = configBuilder.build();
// Initialize the Vertex AI service and the generative model.
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
/* modelName */ "gemini-2.0-flash",
/* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
Content content = new Content.Builder()
.addText("For use in a children's card game, generate 10 animal-based characters.")
.build();
// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
response,
new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
},
executor);
Web
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel, Schema } from "firebase/vertexai";
// 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 Vertex AI service.
const vertexAI = getVertexAI(firebaseApp);
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
const jsonSchema = Schema.object({
properties: {
characters: Schema.array({
items: Schema.object({
properties: {
name: Schema.string(),
accessory: Schema.string(),
age: Schema.number(),
species: Schema.string(),
},
optionalProperties: ["accessory"],
}),
}),
}
});
// Initialize the generative model.
const model = getGenerativeModel(vertexAI, {
model: "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
generationConfig: {
responseMimeType: "application/json",
responseSchema: jsonSchema
},
});
let prompt = "For use in a children's card game, generate 10 animal-based characters.";
let result = await model.generateContent(prompt)
console.log(result.response.text());
Dart
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
final jsonSchema = Schema.object(
properties: {
'characters': Schema.array(
items: Schema.object(
properties: {
'name': Schema.string(),
'age': Schema.integer(),
'species': Schema.string(),
'accessory':
Schema.enumString(enumValues: ['hat', 'belt', 'shoes']),
},
),
),
},
optionalProperties: ['accessory'],
);
await Firebase.initializeApp();
// Initialize the Vertex AI service and the generative model.
final model =
FirebaseVertexAI.instance.generativeModel(
model: 'gemini-2.0-flash',
// In the generation config, set the `responseMimeType` to `application/json`
// and pass the JSON schema object into `responseSchema`.
generationConfig: GenerationConfig(
responseMimeType: 'application/json', responseSchema: jsonSchema));
final prompt = "For use in a children's card game, generate 10 animal-based characters.";
final response = await model.generateContent([Content.text(prompt)]);
print(response.text);
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल और जगह चुनने का तरीका जानें.
कुछ और उदाहरण
स्ट्रक्चर्ड आउटपुट का इस्तेमाल और उसे जनरेट करने के तरीके के बारे में ज़्यादा उदाहरण देखने के लिए, Google Cloud दस्तावेज़ में उदाहरण के तौर पर दिए गए स्कीमा और मॉडल रिस्पॉन्स की सूची देखें.
आउटपुट के तौर पर, एनम की वैल्यू जनरेट करना
यहां दिए गए उदाहरण में, किसी कैटगरी में बांटने के टास्क के लिए, रिस्पॉन्स स्कीमा का इस्तेमाल करने का तरीका बताया गया है. मॉडल से, मूवी की जानकारी के आधार पर उसकी शैली की पहचान करने के लिए कहा जाता है. आउटपुट, एक प्लैन-टेक्स्ट वाली वैल्यू होती है. मॉडल, दी गई वैल्यू की सूची से यह वैल्यू चुनता है. यह सूची, दिए गए रिस्पॉन्स स्कीमा में दी गई होती है.
स्ट्रक्चर्ड क्लासिफ़िकेशन का यह टास्क पूरा करने के लिए, आपको मॉडल को शुरू करने के दौरान सही responseMimeType
(इस उदाहरण में, text/x.enum
) के साथ-साथ वह responseSchema
भी बताना होगा जिसका इस्तेमाल मॉडल को करना है.
Swift
import FirebaseVertexAI
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
let enumSchema = Schema.enumeration(values: ["drama", "comedy", "documentary"])
// Initialize the Vertex AI service and the generative model.
let model = VertexAI.vertexAI().generativeModel(
modelName: "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the enum schema object into `responseSchema`.
generationConfig: GenerationConfig(
responseMIMEType: "text/x.enum",
responseSchema: enumSchema
)
)
let 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.
"""
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")
Kotlin
Kotlin के लिए, इस SDK टूल में मौजूद मैथड, सस्पेंड फ़ंक्शन हैं. इन्हें कोरूटीन स्कोप से कॉल किया जाना चाहिए.// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
val enumSchema = Schema.enumeration(listOf("drama", "comedy", "documentary"))
// Initialize the Vertex AI service and the generative model.
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the enum schema object into `responseSchema`.
generationConfig = generationConfig {
responseMimeType = "text/x.enum"
responseSchema = enumSchema
})
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.
"""
val response = generativeModel.generateContent(prompt)
print(response.text)
Java
Java के लिए, इस SDK टूल में मौजूद स्ट्रीमिंग के तरीके, Reactive Streams लाइब्रेरी सेPublisher
टाइप दिखाते हैं.
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
Schema enumSchema = Schema.enumeration(List.of("drama", "comedy", "documentary"));
// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the enum schema object into `responseSchema`.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "text/x.enum";
configBuilder.responseSchema = enumSchema;
GenerationConfig generationConfig = configBuilder.build();
// Initialize the Vertex AI service and the generative model.
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
/* modelName */ "gemini-2.0-flash",
/* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
String 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.";
Content content = new Content.Builder().addText(prompt).build();
// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
response,
new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
},
executor);
Web
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel, Schema } from "firebase/vertexai";
// 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 Vertex AI service.
const vertexAI = getVertexAI(firebaseApp);
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
const enumSchema = Schema.enumString({
enum: ["drama", "comedy", "documentary"],
});
// Initialize the generative model.
const model = getGenerativeModel(vertexAI, {
model: "gemini-2.0-flash",
// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the JSON schema object into `responseSchema`.
generationConfig: {
responseMimeType: "text/x.enum",
responseSchema: enumSchema,
},
});
let 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.`;
let result = await model.generateContent(prompt);
console.log(result.response.text());
Dart
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
final enumSchema = Schema.enumString(enumValues: ['drama', 'comedy', 'documentary']);
await Firebase.initializeApp();
// Initialize the Vertex AI service and the generative model.
final model =
FirebaseVertexAI.instance.generativeModel(
model: 'gemini-2.0-flash',
// In the generation config, set the `responseMimeType` to `text/x.enum`
// and pass the enum schema object into `responseSchema`.
generationConfig: GenerationConfig(
responseMimeType: 'text/x.enum', responseSchema: enumSchema));
final 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.
""";
final response = await model.generateContent([Content.text(prompt)]);
print(response.text);
अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल और जगह चुनने का तरीका जानें.
कॉन्टेंट जनरेशन को कंट्रोल करने के अन्य विकल्प
- प्रॉम्प्ट डिज़ाइन के बारे में ज़्यादा जानें, ताकि आप अपनी ज़रूरतों के हिसाब से आउटपुट जनरेट करने के लिए, मॉडल पर असर डाल सकें.
- मॉडल के जवाब जनरेट करने के तरीके को कंट्रोल करने के लिए, मॉडल पैरामीटर को कॉन्फ़िगर करें. Gemini मॉडल के लिए, इन पैरामीटर में ज़्यादा से ज़्यादा आउटपुट टोकन, तापमान, टॉपK, और टॉपP शामिल हैं. Imagen मॉडल के लिए, इनमें आसपेक्ट रेशियो, व्यक्ति जनरेशन, वॉटरमार्किंग वगैरह शामिल हैं.
- सुरक्षा सेटिंग का इस्तेमाल करके, ऐसे जवाबों की संभावना को कम करें जिन्हें नुकसान पहुंचाने वाला माना जा सकता है. इनमें नफ़रत फैलाने वाली भाषा और साफ़ तौर पर सेक्शुअल ऐक्ट दिखाने वाला कॉन्टेंट शामिल है.
- मॉडल के व्यवहार को कंट्रोल करने के लिए, सिस्टम के निर्देश सेट करें. यह सुविधा, "प्रीऐब्सटेंस" की तरह है. इसे मॉडल को असली उपयोगकर्ता से मिलने वाले निर्देशों के ज़रिए इस्तेमाल करने से पहले जोड़ा जाता है.
Vertex AI in Firebase के साथ अपने अनुभव के बारे में सुझाव/राय दें या शिकायत करें