Gemini API ব্যবহার করে কাঠামোগত আউটপুট (যেমন JSON এবং enums) তৈরি করুন৷

Gemini API ডিফল্টরূপে অসংগঠিত পাঠ্য হিসাবে প্রতিক্রিয়া প্রদান করে। যাইহোক, কিছু ব্যবহারের ক্ষেত্রে JSON এর মতো কাঠামোবদ্ধ পাঠ্য প্রয়োজন। উদাহরণস্বরূপ, আপনি অন্যান্য ডাউনস্ট্রিম কাজের জন্য প্রতিক্রিয়া ব্যবহার করতে পারেন যার জন্য একটি প্রতিষ্ঠিত ডেটা স্কিমা প্রয়োজন।

মডেলের তৈরি আউটপুট সর্বদা একটি নির্দিষ্ট স্কিমা মেনে চলে তা নিশ্চিত করতে, আপনি একটি প্রতিক্রিয়া স্কিমা সংজ্ঞায়িত করতে পারেন, যা মডেল প্রতিক্রিয়াগুলির জন্য একটি ব্লুপ্রিন্টের মতো কাজ করে৷ তারপরে আপনি কম পোস্ট-প্রসেসিং সহ মডেলের আউটপুট থেকে সরাসরি ডেটা বের করতে পারেন।

এখানে কিছু উদাহরণ আছে:

  • নিশ্চিত করুন যে একটি মডেলের প্রতিক্রিয়া বৈধ JSON তৈরি করে এবং আপনার প্রদত্ত স্কিমার সাথে সামঞ্জস্যপূর্ণ।
    উদাহরণস্বরূপ, মডেলটি রেসিপিগুলির জন্য কাঠামোগত এন্ট্রি তৈরি করতে পারে যা সর্বদা রেসিপির নাম, উপাদানগুলির তালিকা এবং পদক্ষেপগুলি অন্তর্ভুক্ত করে। তারপরে আপনি আপনার অ্যাপের UI-তে এই তথ্যটি আরও সহজে পার্স এবং প্রদর্শন করতে পারেন।

  • শ্রেণীবিন্যাস কাজের সময় একটি মডেল কীভাবে প্রতিক্রিয়া জানাতে পারে তা সীমাবদ্ধ করুন।
    উদাহরণ স্বরূপ, আপনি মডেলটি তৈরি করে এমন লেবেলের পরিবর্তে লেবেলগুলির একটি নির্দিষ্ট সেট (উদাহরণস্বরূপ, positive এবং negative এনামগুলির একটি নির্দিষ্ট সেট) সহ মডেল টীকা পাঠ রাখতে পারেন (যেটিতে good , positive , negative বা bad মতো পরিবর্তনশীলতার একটি ডিগ্রি থাকতে পারে)।

এই নির্দেশিকা আপনাকে দেখায় কিভাবে জেএসওএন আউটপুট জেনারেট করতে একটি কলে responseSchema প্রদান করে generateContent এটি শুধুমাত্র টেক্সট-ইনপুটের উপর ফোকাস করে, কিন্তু জেমিনি মাল্টিমোডাল অনুরোধের জন্য কাঠামোগত প্রতিক্রিয়াও তৈরি করতে পারে যাতে ইনপুট হিসাবে ছবি, ভিডিও এবং অডিও অন্তর্ভুক্ত থাকে।

এই পৃষ্ঠার নীচে আরও উদাহরণ রয়েছে, যেমন আউটপুট হিসাবে enum মান কীভাবে তৈরি করা যায় । আপনি কীভাবে কাঠামোগত আউটপুট তৈরি করতে পারেন তার অতিরিক্ত উদাহরণ দেখতে, Google Cloud ডকুমেন্টেশনে উদাহরণ স্কিমা এবং মডেল প্রতিক্রিয়াগুলির তালিকাটি দেখুন।

আপনি শুরু করার আগে

যদি আপনি ইতিমধ্যে না করে থাকেন, শুরু করার নির্দেশিকাটি সম্পূর্ণ করুন, যা বর্ণনা করে যে কীভাবে আপনার ফায়ারবেস প্রকল্প সেট আপ করবেন, আপনার অ্যাপকে ফায়ারবেসের সাথে সংযুক্ত করবেন, SDK যোগ করবেন, Vertex AI পরিষেবা শুরু করবেন এবং একটি GenerativeModel উদাহরণ তৈরি করবেন।

ধাপ 1 : একটি প্রতিক্রিয়া স্কিমা সংজ্ঞায়িত করুন

একটি মডেলের আউটপুটের গঠন, ক্ষেত্রের নাম এবং প্রতিটি ক্ষেত্রের জন্য প্রত্যাশিত ডেটা টাইপ নির্দিষ্ট করতে একটি প্রতিক্রিয়া স্কিমা সংজ্ঞায়িত করুন।

যখন একটি মডেল তার প্রতিক্রিয়া তৈরি করে, তখন এটি আপনার প্রম্পট থেকে ক্ষেত্রের নাম এবং প্রসঙ্গ ব্যবহার করে। আপনার অভিপ্রায় স্পষ্ট তা নিশ্চিত করতে, আমরা একটি পরিষ্কার কাঠামো, দ্ব্যর্থহীন ক্ষেত্রের নাম এবং এমনকি প্রয়োজন অনুযায়ী বর্ণনা ব্যবহার করার পরামর্শ দিই।

প্রতিক্রিয়া স্কিমা জন্য বিবেচনা

আপনার প্রতিক্রিয়া স্কিমা লেখার সময় নিম্নলিখিতগুলি মনে রাখবেন:

  • প্রতিক্রিয়া স্কিমার আকার ইনপুট টোকেন সীমার দিকে গণনা করা হয়।

  • প্রতিক্রিয়া স্কিমা বৈশিষ্ট্য নিম্নলিখিত প্রতিক্রিয়া MIME প্রকারগুলিকে সমর্থন করে:

    • application/json : প্রতিক্রিয়া স্কিমাতে সংজ্ঞায়িত হিসাবে আউটপুট JSON (গঠিত আউটপুট প্রয়োজনীয়তার জন্য দরকারী)

    • text/x.enum : রেসপন্স স্কিমাতে সংজ্ঞায়িত একটি enum মান আউটপুট করুন (শ্রেণীবিভাগের কাজের জন্য দরকারী)

  • প্রতিক্রিয়া স্কিমা বৈশিষ্ট্য নিম্নলিখিত স্কিমা ক্ষেত্র সমর্থন করে:

    enum
    items
    maxItems
    nullable
    properties
    required

    আপনি যদি একটি অসমর্থিত ক্ষেত্র ব্যবহার করেন, মডেলটি এখনও আপনার অনুরোধ পরিচালনা করতে পারে, তবে এটি ক্ষেত্রটিকে উপেক্ষা করে। উল্লেখ্য যে উপরের তালিকাটি OpenAPI 3.0 স্কিমা অবজেক্টের একটি উপসেট ( Vertex AI স্কিমা রেফারেন্স দেখুন)।

  • ডিফল্টরূপে, Firebase SDK-তে Vertex AI-এর জন্য, আপনি optionalProperties অ্যারেতে ঐচ্ছিক হিসাবে উল্লেখ না করলে সমস্ত ক্ষেত্রকে প্রয়োজনীয় বলে মনে করা হয়। এই ঐচ্ছিক ক্ষেত্রগুলির জন্য, মডেলটি ক্ষেত্রগুলি পূরণ করতে পারে বা সেগুলি এড়িয়ে যেতে পারে৷

    মনে রাখবেন যে এটিVertex AI Gemini API- এর ডিফল্ট আচরণের বিপরীত।

ধাপ 2 : JSON তৈরি করতে একটি প্রতিক্রিয়া স্কিমা সহ একটি প্রম্পট পাঠান

নিম্নলিখিত উদাহরণ দেখায় কিভাবে কাঠামোগত JSON আউটপুট তৈরি করতে হয়।

আপনি যখন GenerativeModel উদাহরণ তৈরি করেন, উপযুক্ত responseMimeType নির্দিষ্ট করুন (এই উদাহরণে, application/json ) সেইসাথে responseSchema যা আপনি মডেলটি ব্যবহার করতে চান।

সুইফট

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

কোটলিনের জন্য, এই SDK-এর পদ্ধতিগুলি হল সাসপেন্ড ফাংশন এবং একটি Coroutine স্কোপ থেকে কল করা প্রয়োজন৷
// 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

জাভার জন্য, এই SDK-এর স্ট্রিমিং পদ্ধতিগুলি প্রতিক্রিয়াশীল স্ট্রীমস লাইব্রেরি থেকে একটি 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 ডকুমেন্টেশনে উদাহরণ স্কিমা এবং মডেল প্রতিক্রিয়াগুলির তালিকাটি দেখুন।

আউটপুট হিসাবে enum মান তৈরি করুন

নিম্নলিখিত উদাহরণ দেখায় কিভাবে একটি শ্রেণীবিভাগের কাজের জন্য একটি প্রতিক্রিয়া স্কিমা ব্যবহার করতে হয়। মডেলকে তার বর্ণনার উপর ভিত্তি করে একটি সিনেমার ধরণ সনাক্ত করতে বলা হয়। আউটপুট হল একটি প্লেইন-টেক্সট enum মান যা মডেলটি প্রদত্ত প্রতিক্রিয়া স্কিমাতে সংজ্ঞায়িত মানগুলির তালিকা থেকে নির্বাচন করে।

এই কাঠামোবদ্ধ শ্রেণীবিভাগের কাজটি সম্পাদন করার জন্য, আপনাকে মডেল আরম্ভ করার সময় উপযুক্ত responseMimeType (এই উদাহরণে, text/x.enum ) এবং সেইসাথে মডেলটিকে যে responseSchema ব্যবহার করতে চান তা নির্দিষ্ট করতে হবে।

সুইফট

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

কোটলিনের জন্য, এই SDK-এর পদ্ধতিগুলি হল সাসপেন্ড ফাংশন এবং একটি Coroutine স্কোপ থেকে কল করা প্রয়োজন৷
// 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

জাভার জন্য, এই SDK-এর স্ট্রিমিং পদ্ধতিগুলি প্রতিক্রিয়াশীল স্ট্রীমস লাইব্রেরি থেকে একটি 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);

আপনার ব্যবহারের ক্ষেত্রে এবং অ্যাপের জন্য উপযুক্ত একটি মডেল এবং ঐচ্ছিকভাবে একটি অবস্থান কীভাবে চয়ন করবেন তা শিখুন।

কন্টেন্ট জেনারেশন নিয়ন্ত্রণ করার জন্য অন্যান্য বিকল্প

  • প্রম্পট ডিজাইন সম্পর্কে আরও জানুন যাতে আপনি আপনার প্রয়োজনের জন্য নির্দিষ্ট আউটপুট তৈরি করতে মডেলটিকে প্রভাবিত করতে পারেন।
  • মডেলটি কীভাবে প্রতিক্রিয়া তৈরি করে তা নিয়ন্ত্রণ করতে মডেল প্যারামিটারগুলি কনফিগার করুন। মিথুন মডেলের জন্য, এই পরামিতিগুলির মধ্যে সর্বাধিক আউটপুট টোকেন, তাপমাত্রা, topK এবং topP অন্তর্ভুক্ত রয়েছে। ইমেজেন মডেলের জন্য, এর মধ্যে রয়েছে আকৃতির অনুপাত, ব্যক্তি তৈরি, ওয়াটারমার্কিং ইত্যাদি।
  • ঘৃণাত্মক বক্তব্য এবং যৌনতাপূর্ণ বিষয়বস্তু সহ ক্ষতিকারক বলে বিবেচিত প্রতিক্রিয়া পাওয়ার সম্ভাবনা সামঞ্জস্য করতে নিরাপত্তা সেটিংস ব্যবহার করুন৷
  • মডেলের আচরণ পরিচালনা করতে সিস্টেম নির্দেশাবলী সেট করুন। এই বৈশিষ্ট্যটি একটি "প্রস্তাবনা" এর মতো যা আপনি মডেলটি শেষ ব্যবহারকারীর কাছ থেকে আরও নির্দেশাবলীর সংস্পর্শে আসার আগে যোগ করেন।


Firebase-এ Vertex AI-এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত দিন