Gemini API ডিফল্ট হিসেবে উত্তরকে আনস্ট্রাকচার্ড টেক্সট হিসেবে রিটার্ন করে। তবে, কিছু ব্যবহারের ক্ষেত্রে JSON-এর মতো স্ট্রাকচার্ড টেক্সট প্রয়োজন। যেমন, আপনি হয়ত অন্যান্য ডাউনস্ট্রিম টাস্কের জন্য উত্তর ব্যবহার করছেন যার জন্য প্রতিষ্ঠিত ডেটা স্কিমা প্রয়োজন।
মডেলের তৈরি করা আউটপুট যাতে সবসময় একটি নির্দিষ্ট স্কিমা মেনে চলে তা নিশ্চিত করতে, আপনি একটি উত্তর স্কিমা নির্ধারণ করতে পারেন, যা মডেলের উত্তরের ব্লুপ্রিন্ট হিসেবে কাজ করে। আপনি তারপরে পোস্ট-প্রসেসিং কম করেই মডেলের আউটপুট থেকে সরাসরি ডেটা এক্সট্র্যাক্ট করতে পারবেন।
কয়েকটি উদাহরণ দেখুন:
নিশ্চিত করুন যে মডেলের দেওয়া উত্তরে সঠিক JSON আছে এবং সেটি আপনার দেওয়া স্কিমা মেনে চলছে।
যেমন, মডেলটি রেসিপির জন্য স্ট্রাকচার্ড এন্ট্রি তৈরি করতে পারে যেগুলিতে সবসময় রেসিপির নাম, উপকরণের তালিকা ও ধাপ অন্তর্ভুক্ত থাকে। এরপরে আপনি আরও সহজে এই তথ্য পার্স করতে এবং আপনার অ্যাপের UI-তে দেখাতে পারবেন।শ্রেণীবদ্ধকরণ টাস্ক চলাকালীন মডেল কীভাবে উত্তর দিতে পারে তা সীমিত করুন।
যেমন, মডেলের তৈরি করা লেবেলের (যেগুলিgood,positive,negativeবাbad-এর মতো পরিবর্তনশীল হতে পারে) পরিবর্তে, আপনি মডেলকে নির্দিষ্ট লেবেলের সেট (যেমন,positiveএবংnegative-এর মতো নির্দিষ্ট এনুমের সেট) দিয়ে টেক্সট অ্যানোটেট করতে পারেন।
এই গাইড আপনাকে দেখায় যে কীভাবে generateContent-এ কল করার সময় responseSchema
প্রদান করে JSON আউটপুট জেনারেট করতে হয়। এটি শুধুমাত্র টেক্সট ইনপুটের উপর ফোকাস করে, তবে Gemini
মাল্টিমোডাল অনুরোধের ক্ষেত্রে স্ট্রাকচার্ড উত্তরও দিতে পারে, যার মধ্যে ইনপুট হিসেবে ছবি, ভিডিও ও অডিও থাকে।
এই পৃষ্ঠার নিচে আরও উদাহরণ দেওয়া আছে, যেমন কীভাবে আউটপুট হিসেবে এনুম ভ্যালু জেনারেট করতে হয়।
শুরু করার আগে
|
এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট কন্টেন্ট ও কোড দেখতে আপনার Gemini API প্রদানকারীর আইকনে ক্লিক করুন। |
আপনি এখনও না করে থাকলে,
শুরু করার নির্দেশিকা সম্পূর্ণ করুন, যা বর্ণনা করে যে কীভাবে
আপনার Firebase প্রোজেক্ট সেট-আপ করতে হয়, Firebase-এর সাথে আপনার অ্যাপ কানেক্ট করতে হয়, SDK যোগ করতে হয়,
আপনার বেছে নেওয়া Gemini API প্রদানকারীর জন্য ব্যাকএন্ড পরিষেবা চালু করতে হয় এবং
GenerativeModel ইনস্ট্যান্স তৈরি করতে হয়।
আপনার প্রম্পট পরীক্ষা ও ইটারেট করার জন্য, আমরা Google AI Studio ব্যবহার করার সাজেশন দিই।
এই ক্ষমতা কাজ করে এমন মডেল
gemini-3.1-pro-previewgemini-3.8-flash(এবং এর আগেরgemini-3.7-flash,gemini-3.6-flashওgemini-3.5-flash)gemini-3.5-flash-lite(এবং পুরনোgemini-3.1-flash-lite)
সাধারণ-ব্যবহারের Gemini 2.5 মডেলগুলিতে এই কেপেবিলিটি কাজ করে, তবে সেগুলি সবকটিই বাতিল করা হয়েছে।
ধাপ ১: উত্তরের স্কিমা নির্ধারণ করুন
মডেলের আউটপুটের স্ট্রাকচার, ফিল্ডের নাম এবং প্রতিটি ফিল্ডের জন্য প্রত্যাশিত ডেটার ধরন নির্দিষ্ট করতে, একটি উত্তর স্কিমা নির্ধারণ করুন।
কোনও মডেল উত্তর তৈরি করার সময়, আপনার প্রম্পট থেকে ফিল্ডের নাম ও প্রসঙ্গ ব্যবহার করে। আপনার উদ্দেশ্য যে স্পষ্ট তা নিশ্চিত করতে, আমরা স্পষ্ট স্ট্রাকচার, দ্ব্যর্থহীন ফিল্ডের নাম এবং প্রয়োজন অনুযায়ী বিবরণ ব্যবহার করার সাজেশন দিই।
উত্তর স্কিমা সংক্রান্ত বিবেচ্য বিষয়
আপনার উত্তর স্কিমা লেখার সময় নিম্নলিখিত বিষয়গুলি মনে রাখবেন:
উত্তর স্কিমার সাইজ ইনপুট টোকেন সীমার মধ্যে পড়ে।
উত্তর স্কিমা ফিচারে নিম্নলিখিত উত্তর MIME ধরন কাজ করে:
application/json: উত্তরের স্কিমায় যেভাবে JSON আউটপুটকে সংজ্ঞায়িত করা হয়েছে, সেইভাবে আউটপুট দাও (স্ট্রাকচার্ড আউটপুটের প্রয়োজনীয়তার ক্ষেত্রে সহায়ক)text/x.enum: উত্তরের স্কিমায় যেভাবে সংজ্ঞায়িত করা হয়েছে সেইভাবে এনুম ভ্যালু আউটপুট করো (শ্রেণীবদ্ধ করার টাস্কের জন্য উপযোগী)
উত্তর স্কিমা ফিচারে নিম্নলিখিত স্কিমা ফিল্ড কাজ করে:
enum
items
maxItems
nullable
properties
required
আপনি কাজ করে না এমন ফিল্ড ব্যবহার করলেও, মডেলটি আপনার অনুরোধ প্রসেস করতে পারবে, তবে সেটি ফিল্ডটি উপেক্ষা করবে। মনে রাখবেন, উপরের তালিকাটি OpenAPI 3.0 স্কিমা অবজেক্টের একটি সাবসেট।
ডিফল্ট হিসেবে, Firebase AI Logic SDK-এর ক্ষেত্রে, সব ফিল্ডকে প্রয়োজনীয় হিসেবে বিবেচনা করা হয়, যদি না আপনি
optionalPropertiesঅ্যারেতে সেগুলিকে ঐচ্ছিক হিসেবে উল্লেখ করেন। এইসব ঐচ্ছিক ফিল্ডের জন্য, মডেল ফিল্ড পূরণ করতে বা সেগুলি এড়িয়ে যেতে পারে। মনে রাখবেন, আপনি যদি সরাসরি দুটি Gemini API পরিষেবা প্রদানকারীর সার্ভার SDK বা API ব্যবহার করেন, তাহলে এটি তাদের ডিফল্ট আচরণের বিপরীত।
ধাপ ২: আপনার উত্তর স্কিমা ব্যবহার করে JSON আউটপুট তৈরি করুন
|
এই নমুনা চেষ্টা করার আগে, আপনার প্রজেক্ট ও অ্যাপ সেট-আপ করতে
এই গাইডের শুরু করার আগে বিভাগটি সম্পূর্ণ করুন।
সেই বিভাগে, আপনার বেছে নেওয়া Gemini API প্রদানকারীর জন্য একটি বোতামেও ক্লিক করবেন যাতে আপনি এই পৃষ্ঠায় প্রদানকারী-নির্দিষ্ট কন্টেন্ট দেখতে পান। |
নিচে দেওয়া উদাহরণ থেকে কীভাবে স্ট্রাকচার্ড JSON আউটপুট তৈরি করতে হয় তা জানতে পারবেন।
আপনি GenerativeModel ইনস্ট্যান্স তৈরি করার সময়, উপযুক্ত
responseMimeType (এই উদাহরণে, application/json) এবং
responseSchema উল্লেখ করুন যা আপনি চান যে মডেলটি ব্যবহার করুক।
Swift
import FirebaseAILogic
// 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_MODEL_NAME",
// 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_MODEL_NAME",
// 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 */ "<var>GEMINI_MODEL_NAME</var>",
/* 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_MODEL_NAME",
// 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_MODEL_NAME',
// 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_MODEL_NAME",
// 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 FirebaseAILogic
// 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_MODEL_NAME",
// 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_MODEL_NAME",
// 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 */ "<var>GEMINI_MODEL_NAME</var>",
/* 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_MODEL_NAME",
// 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_MODEL_NAME',
// 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_MODEL_NAME",
// 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.");
আপনার ব্যবহারিক ক্ষেত্র ও অ্যাপের জন্য উপযুক্ত মডেল কীভাবে বেছে নিতে হয় তা জানুন।
কন্টেন্ট জেনারেশন কন্ট্রোল করার অন্যান্য বিকল্প
- প্রম্পট ডিজাইন সম্পর্কে আরও জানুন যাতে আপনার প্রয়োজন অনুযায়ী আউটপুট তৈরি করার জন্য আপনি মডেলকে প্রভাবিত করতে পারেন।
- মডেল কীভাবে উত্তর তৈরি করবে তা নিয়ন্ত্রণ করতে, মডেল প্যারামিটার কনফিগার করুন, যেমন সর্বাধিক আউটপুট টোকেন, পুনরাবৃত্ত আউটপুট টোকেনের সম্ভাবনা ইত্যাদি।
- নিরাপত্তা সেটিংস ব্যবহার করে এমন উত্তর পাওয়ার সম্ভাবনা অ্যাডজাস্ট করুন যা ঘৃণাত্মক বক্তব্য ও যৌনতাপূর্ণ কন্টেন্ট সহ ক্ষতিকারক বলে বিবেচিত হতে পারে।
- মডেলের আচরণ নিয়ন্ত্রণ করতে সিস্টেম নির্দেশাবলী সেট করুন। এই ফিচারটি হল একটি ভূমিকা যা আপনি মডেলটি ব্যবহারকারীর থেকে কোনও আরও নির্দেশাবলী পাওয়ার আগে যোগ করেন।
Firebase AI Logic-এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত জানান