Gemini API का इस्तेमाल करके, स्ट्रक्चर्ड आउटपुट (जैसे, JSON और एनम) जनरेट करना

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

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

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

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

  • यह तय करना कि किसी मॉडल को, कैटगरी तय करने के टास्क के दौरान किस तरह के जवाब देने चाहिए.
    उदाहरण के लिए, मॉडल के जनरेट किए गए लेबल के बजाय, टेक्स्ट पर एनोटेशन करने के लिए, मॉडल को लेबल के किसी खास सेट का इस्तेमाल करने के लिए कहा जा सकता है. जैसे, positive और negative जैसे एनम का कोई खास सेट.positivenegativegoodbad

इस गाइड में, generateContent को कॉल करते समय responseSchema का इस्तेमाल करके, JSON आउटपुट जनरेट करने का तरीका बताया गया है. यह सिर्फ़ टेक्स्ट इनपुट पर फ़ोकस करता है. हालांकि, Gemini, मल्टीमोडल अनुरोधों के लिए भी स्ट्रक्चर्ड जवाब दे सकता है. इन अनुरोधों में, इमेज, वीडियो, और ऑडियो को इनपुट के तौर पर शामिल किया जाता है.

इस पेज पर सबसे नीचे, आउटपुट के तौर पर वैल्यू जनरेट करने जैसे और उदाहरण दिए गए हैं.

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

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

अगर आपने अब तक ऐसा नहीं किया है, तो शुरू करने से जुड़ी गाइड पढ़ें. इसमें, Firebase प्रोजेक्ट सेट अप करने, अपने ऐप्लिकेशन को Firebase से कनेक्ट करने, SDK टूल जोड़ने, चुने गए Gemini API प्रोवाइडर के लिए बैकएंड सेवा को शुरू करने, और GenerativeModel इंस्टेंस बनाने का तरीका बताया गया है.

हमारा सुझाव है कि अपने प्रॉम्प्ट की जांच करने और उन पर बार-बार काम करने के लिए, Google AI Studio का इस्तेमाल करें. इससे, जनरेट किया गया कोड स्निपेट भी मिल सकता है.

पहला चरण: जवाब का स्कीमा तय करना

मॉडल के आउटपुट के स्ट्रक्चर, फ़ील्ड के नाम, और हर फ़ील्ड के लिए उम्मीद के मुताबिक डेटा टाइप की जानकारी देने के लिए, रिस्पॉन्स स्कीमा तय करें.

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

रिस्पॉन्स स्कीमा के लिए ध्यान देने वाली बातें

रिस्पॉन्स स्कीमा लिखते समय इन बातों का ध्यान रखें:

  • रिस्पॉन्स स्कीमा का साइज़, इनपुट टोकन की सीमा में गिना जाता है.

  • रिस्पॉन्स स्कीमा की सुविधा, इन रिस्पॉन्स MIME टाइप के साथ काम करती है:

    • application/json: रिस्पॉन्स स्कीमा में बताए गए तरीके के हिसाब से JSON आउटपुट करना (स्ट्रक्चर्ड आउटपुट की ज़रूरी शर्तों के लिए मददगार)

    • text/x.enum: रिस्पॉन्स स्कीमा में बताई गई वैल्यू के तौर पर, एक enum वैल्यू दिखाएं (वर्गीकरण के टास्क के लिए मददगार)

  • रिस्पॉन्स स्कीमा की सुविधा, इन स्कीमा फ़ील्ड के साथ काम करती है:

    enum
    items
    maxItems
    nullable
    properties
    required

    अगर किसी ऐसे फ़ील्ड का इस्तेमाल किया जाता है जो काम नहीं करता, तो मॉडल आपके अनुरोध को मैनेज कर सकता है. हालांकि, वह उस फ़ील्ड को अनदेखा कर देता है. ध्यान दें कि ऊपर दी गई सूची, OpenAPI 3.0 स्कीमा ऑब्जेक्ट का सबसेट है.

  • डिफ़ॉल्ट रूप से, Firebase AI Logic SDKs के लिए, सभी फ़ील्ड को ज़रूरी माना जाता है. हालांकि, अगर आपने उन्हें optionalProperties ऐरे में 'ज़रूरी नहीं है' के तौर पर सेट किया है, तो ऐसा नहीं होगा. इन वैकल्पिक फ़ील्ड के लिए, मॉडल फ़ील्ड में अपने-आप जानकारी भर सकता है या उन्हें स्किप कर सकता है. ध्यान दें कि अगर सीधे तौर पर इन दोनों Gemini API प्रोवाइडर के सर्वर SDK टूल या उनके एपीआई का इस्तेमाल किया जाता है, तो यह इनके डिफ़ॉल्ट तरीके से बिलकुल उलट है.

दूसरा चरण: अपने रिस्पॉन्स स्कीमा का इस्तेमाल करके, JSON आउटपुट जनरेट करना

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

नीचे दिए गए उदाहरण में, स्ट्रक्चर्ड JSON आउटपुट जनरेट करने का तरीका बताया गया है.

GenerativeModel इंस्टेंस बनाते समय, सही responseMimeType (इस उदाहरण में, application/json) के साथ-साथ वह responseSchema भी बताएं जिसका इस्तेमाल मॉडल को करना है.

Swift


import FirebaseAI

// 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 Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel(
            /* modelName */ "gemini-2.0-flash",
            /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

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 { getAI, getGenerativeModel, GoogleAIBackend, 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() });

// 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"],
      }),
    }),
  }
});

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, {
  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_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.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'],
      );


// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.googleAI().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);

Unity


using Firebase;
using Firebase.AI;

// Provide a JSON schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
var jsonSchema = Schema.Object(
  properties: new System.Collections.Generic.Dictionary<string, Schema> {
    { "characters", Schema.Array(
      items: Schema.Object(
        properties: new System.Collections.Generic.Dictionary<string, Schema> {
          { "name", Schema.String() },
          { "age", Schema.Int() },
          { "species", Schema.String() },
          { "accessory", Schema.Enum(new string[] { "hat", "belt", "shoes" }) },
        },
        optionalProperties: new string[] { "accessory" }
      )
    ) },
  }
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
var model = FirebaseAI.DefaultInstance.GetGenerativeModel(
  modelName: "gemini-2.0-flash",
  // In the generation config, set the `responseMimeType` to `application/json`
  // and pass the JSON schema object into `responseSchema`.
  generationConfig: new GenerationConfig(
    responseMimeType: "application/json",
    responseSchema: jsonSchema
  )
);

var prompt = "For use in a children's card game, generate 10 animal-based characters.";

var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल चुनने का तरीका जानें.

कुछ और उदाहरण

स्ट्रक्चर्ड आउटपुट का इस्तेमाल और उसे जनरेट करने के कुछ और उदाहरण यहां दिए गए हैं.

आउटपुट के तौर पर, एनम की वैल्यू जनरेट करना

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

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

स्ट्रक्चर्ड क्लासिफ़िकेशन का यह टास्क पूरा करने के लिए, आपको मॉडल को शुरू करने के दौरान सही responseMimeType (इस उदाहरण में, text/x.enum) के साथ-साथ वह responseSchema भी बताना होगा जिसका इस्तेमाल मॉडल को करना है.

Swift


import FirebaseAI

// 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 Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).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 Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
        .generativeModel(
            /* modelName */ "gemini-2.0-flash",
            /* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);

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 { getAI, getGenerativeModel, GoogleAIBackend, 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() });

// 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"],
});

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, {
  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_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.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']);

// Initialize FirebaseApp
await Firebase.initializeApp(
  options: DefaultFirebaseOptions.currentPlatform,
);

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.googleAI().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);

Unity


using Firebase;
using Firebase.AI;

// Provide an enum schema object using a standard format.
// Later, pass this schema object into `responseSchema` in the generation config.
var enumSchema = Schema.Enum(new string[] { "drama", "comedy", "documentary" });

// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
var model = FirebaseAI.DefaultInstance.GetGenerativeModel(
  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: new GenerationConfig(
    responseMimeType: "text/x.enum",
    responseSchema: enumSchema
  )
);

var 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.
";

var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");

अपने इस्तेमाल के उदाहरण और ऐप्लिकेशन के हिसाब से सही मॉडल चुनने का तरीका जानें.

कॉन्टेंट जनरेशन को कंट्रोल करने के अन्य विकल्प

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


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