Gemini মডেল সাধারণত আনস্ট্রাকচার্ড টেক্সট হিসেবে উত্তর দেয়। তবে, কিছু ব্যবহারের ক্ষেত্রে স্ট্রাকচার্ড টেক্সট (যেমন JSON বা enum) প্রয়োজন হয়। যেমন, আপনি হয়ত অন্যান্য ডাউনস্ট্রিম টাস্কের জন্য উত্তর ব্যবহার করছেন যার জন্য একটি প্রতিষ্ঠিত ডেটা স্কিমা প্রয়োজন।
মডেলের জেনারেট করা আউটপুট যাতে সবসময় নির্দিষ্ট স্কিমা মেনে চলে তা নিশ্চিত করতে, আপনি একটি স্কিমা নির্দিষ্ট করতে পারেন, যা মডেলের উত্তরের জন্য ব্লুপ্রিন্টের মতো কাজ করে। আপনি তারপরে কম পোস্ট-প্রসেসিং সহ মডেলের আউটপুট থেকে সরাসরি ডেটা এক্সট্র্যাক্ট করতে পারবেন।
এখানে কিছু উদাহরণ দেওয়া হল:
নিশ্চিত করুন যে মডেলের উত্তর সঠিক JSON তৈরি করে এবং আপনার প্রদান করা স্কিমা মেনে চলে।
যেমন, মডেলটি রেসিপির জন্য স্ট্রাকচার্ড এন্ট্রি তৈরি করতে পারে যাতে সবসময় রেসিপির নাম, উপকরণের তালিকা এবং ধাপগুলি অন্তর্ভুক্ত থাকে। আপনি আরও সহজে এই তথ্য পার্স করতে এবং আপনার অ্যাপের UI-তে দেখাতে পারবেন।শ্রেণিবিভাগ সংক্রান্ত টাস্কের সময় মডেল কীভাবে উত্তর দিতে পারে তা সীমাবদ্ধ করুন।
যেমন, মডেলের তৈরি করা লেবেলের (যেগুলির মধ্যেgood,positive,negativeবাbad-এর মতো পরিবর্তনশীলতা থাকতে পারে) পরিবর্তে, আপনি মডেলকে নির্দিষ্ট সেট লেবেল (যেমন,positiveওnegative-এর মতো নির্দিষ্ট সেট enum) দিয়ে টেক্সট অ্যানোটেট করতে বলতে পারেন।
এই পৃষ্ঠায় বর্ণনা করা হয়েছে যে কীভাবে ওয়েব অ্যাপের জন্য আপনার হাইব্রিড অভিজ্ঞতায় স্ট্রাকচার্ড আউটপুট (যেমন JSON এবং enum) জেনারেট করতে হয়।
JSON আউটপুটে যান enum আউটপুটে যান
স্ট্রাকচার্ড আউটপুটের জন্য কনফিগারেশন
ক্লাউড-হোস্টেড এবং অন-ডিভাইস মডেল, দুটির ক্ষেত্রেই ইনফারেন্সের জন্য স্ট্রাকচার্ড আউটপুট (যেমন JSON এবং enum) তৈরি করা যায়।
হাইব্রিড ইনফারেন্স মোড: দুটিই কনফিগার করুন
inCloudParamsএবংonDeviceParamsযাতে ইনফারেন্স ক্লাউডে বা অন-ডিভাইসে রান করুক না কেন, মডেলটি স্ট্রাকচার্ড আউটপুটের সাথে উত্তর দেয়:অন-ডিভাইস মডেলের জন্য:
Schemaহেল্পার পদ্ধতি ব্যবহার করে তৈরি করা স্কিমা অথবা সাধারণ JSON স্কিমা ব্যবহার করেonDeviceParams-এresponseConstraintউল্লেখ করুন।ক্লাউড-হোস্টেড মডেলের জন্য:
responseMimeType(JSON-এর জন্যapplication/jsonবা enum-এর জন্যtext/x.enum) এবংinCloudParams-এresponseSchemaনির্দিষ্ট করুন।
নন-হাইব্রিড ইনফারেন্স মোড: শুধুমাত্র উপরে বর্ণিত প্রযোজ্য কনফিগারেশন ব্যবহার করুন।
শুরু করার আগে
|
এই পৃষ্ঠায় পরিষেবা প্রদানকারী-নির্দিষ্ট কন্টেন্ট ও কোড দেখতে আপনার Gemini API পরিষেবা প্রদানকারীর উপর ক্লিক করুন। |
আপনি হাইব্রিড অভিজ্ঞতা তৈরি করার জন্য শুরু করার নির্দেশিকা সম্পূর্ণ করেছেন কিনা তা নিশ্চিত করুন।
JSON আউটপুটে যান enum আউটপুটে যান
JSON আউটপুট
নিম্নলিখিত উদাহরণগুলি হাইব্রিড ইনফারেন্স (যেমন, PREFER_ON_DEVICE)
অ্যাকোমোডেট করার জন্য
সাধারণ JSON আউটপুট উদাহরণ অ্যাডাপ্ট করে।
এইসব উদাহরণের ক্ষেত্রে, মডেলটি একটি কাল্পনিক গল্পের জন্য চরিত্রের প্রোফাইলের একটি তালিকা তৈরি করে, যার মধ্যে নাম, বয়স, প্রজাতি এবং ঐচ্ছিক অ্যাক্সেসরির মতো স্ট্রাকচার্ড অ্যাট্রিবিউট থাকে।
আপনি নিম্নলিখিত যেকোনও একটি পদ্ধতি ব্যবহার করে আপনার রেসপন্স স্কিমা সংজ্ঞায়িত করতে পারেন:
Firebase
Schemaহেল্পার মেথড (সাজেস্ট করা হয়েছে): হেল্পার মেথড (যেমনSchema.object()এবংSchema.enumString()) ব্যবহার করে অতিরিক্ত বয়লারপ্লেট ছাড়াই সরাসরি আপনার কোডে সংক্ষিপ্ত, কম্প্যাক্ট স্কিমা লিখুন।প্লেন JSON স্কিমা: আপনার কাছে আগে থেকেই স্কিমা সংজ্ঞা থাকলে, প্ল্যাটফর্ম বা ব্যাকএন্ড পরিষেবা জুড়ে স্কিমা শেয়ার করলে বা JSON ফাইল থেকে স্কিমা ইমপোর্ট করলে, স্ট্যান্ডার্ড JSON স্কিমা অবজেক্ট ব্যবহার করুন।
উদাহরণ ১: Firebase Schema হেল্পার পদ্ধতি ব্যবহার করা
এই উদাহরণে অবজেক্ট স্কিমা সংজ্ঞায়িত করতে Firebase AI Logic SDK-এর মাধ্যমে প্রদান করা
Schema সহায়ক পদ্ধতি (যেমন Schema.object,
Schema.array, Schema.string এবং Schema.number) ব্যবহার করা হয়।
|
এই স্যাম্পেল ব্যবহার করার আগে, আপনার প্রোজেক্ট ও অ্যাপ সেট-আপ করতে
এই গাইডের শুরু করার আগে বিভাগটি
সম্পূর্ণ করুন। সেই বিভাগে, আপনার বেছে নেওয়া Gemini API প্রোভাইডারের জন্য একটি বোতামে ক্লিক করতে হবে যাতে আপনি এই পৃষ্ঠায় প্রোভাইডার-নির্দিষ্ট কন্টেন্ট দেখতে পান। |
import { initializeApp } from "firebase/app";
import {
getAI,
getGenerativeModel,
GoogleAIBackend,
InferenceMode,
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() });
// Define a schema using the `Schema` helper methods.
// Optional properties are specified in optionalProperties.
const jsonSchema = Schema.object({
properties: {
characters: Schema.array({
items: Schema.object({
properties: {
name: Schema.string(),
age: Schema.number(),
species: Schema.string(),
accessory: Schema.string()
},
optionalProperties: ["accessory"]
})
})
}
});
// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
mode: InferenceMode.INFERENCE_MODE,
// For cloud-hosted models, specify MIME type and response schema.
inCloudParams: {
model: "CLOUD_MODEL_NAME",
generationConfig: {
responseMimeType: "application/json",
responseSchema: jsonSchema
}
},
// For on-device models, pass the schema as the response constraint.
onDeviceParams: {
promptOptions: {
responseConstraint: jsonSchema
}
}
});
const prompt = "Create profiles for some characters for a fantasy story.";
// Generate the structured output.
const result = await model.generateContent(prompt);
// Access the generated JSON string conforming to the schema from response.text().
console.log(result.response.text());
// Parse the JSON string into a JavaScript object.
console.log(JSON.parse(result.response.text()));
উদাহরণ ২: প্লেন JSON স্কিমা ব্যবহার করা
এই উদাহরণে শুধু JSON ব্যবহার করে স্কিমা নির্দিষ্ট করা হয়েছে।
|
এই স্যাম্পেল ব্যবহার করার আগে, আপনার প্রোজেক্ট ও অ্যাপ সেট-আপ করতে
এই গাইডের শুরু করার আগে বিভাগটি
সম্পূর্ণ করুন। সেই বিভাগে, আপনার বেছে নেওয়া Gemini API প্রোভাইডারের জন্য একটি বোতামে ক্লিক করতে হবে যাতে আপনি এই পৃষ্ঠায় প্রোভাইডার-নির্দিষ্ট কন্টেন্ট দেখতে পান। |
import { initializeApp } from "firebase/app";
import {
getAI,
getGenerativeModel,
GoogleAIBackend,
InferenceMode
} 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() });
// Define the schema as a plain JSON object.
// Properties are required by default unless omitted from the required array.
const jsonSchema = {
type: "object",
properties: {
characters: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
nullable: false
},
age: {
type: "number",
nullable: false
},
species: {
type: "string",
nullable: false
},
accessory: {
type: "string",
nullable: true
}
},
nullable: false,
required: [
"name",
"age",
"species"
]
},
nullable: false
}
},
nullable: false,
required: [
"characters"
]
};
// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
mode: InferenceMode.INFERENCE_MODE,
// For cloud-hosted models, specify MIME type and response schema.
inCloudParams: {
model: "CLOUD_MODEL_NAME",
generationConfig: {
responseMimeType: "application/json",
responseSchema: jsonSchema
}
},
// For on-device models, pass the schema as the response constraint.
onDeviceParams: {
promptOptions: {
responseConstraint: jsonSchema
}
}
});
const prompt = "Create profiles for some characters for a fantasy story.";
// Generate the structured output.
const result = await model.generateContent(prompt);
// Access the generated JSON string conforming to the schema from response.text().
console.log(result.response.text());
// Parse the JSON string into a JavaScript object.
console.log(JSON.parse(result.response.text()));
Enum আউটপুট
নিম্নলিখিত উদাহরণগুলি হাইব্রিড ইনফারেন্স (যেমন, PREFER_ON_DEVICE) অ্যাকোমোডেট করার জন্য
সাধারণ enum আউটপুট উদাহরণ
অ্যাডাপ্ট করে।
এই উদাহরণগুলির ক্ষেত্রে, মডেলটি অনুমোদিত বিকল্পগুলির একটি পূর্বনির্ধারিত তালিকা থেকে একটি একক জেনার বেছে নিয়ে একটি ফিল্মের বিবরণকে শ্রেণীবদ্ধ করে
(drama, comedy, বা documentary)।
আপনি নিম্নলিখিত যেকোনও একটি পদ্ধতি ব্যবহার করে আপনার রেসপন্স স্কিমা সংজ্ঞায়িত করতে পারেন:
Firebase
Schemaহেল্পার মেথড (সাজেস্ট করা হয়েছে): হেল্পার মেথড (যেমনSchema.object()এবংSchema.enumString()) ব্যবহার করে অতিরিক্ত বয়লারপ্লেট ছাড়াই সরাসরি আপনার কোডে সংক্ষিপ্ত, কম্প্যাক্ট স্কিমা লিখুন।প্লেন JSON স্কিমা: আপনার কাছে আগে থেকেই স্কিমা সংজ্ঞা থাকলে, প্ল্যাটফর্ম বা ব্যাকএন্ড পরিষেবা জুড়ে স্কিমা শেয়ার করলে বা JSON ফাইল থেকে স্কিমা ইমপোর্ট করলে, স্ট্যান্ডার্ড JSON স্কিমা অবজেক্ট ব্যবহার করুন।
উদাহরণ ১: Firebase Schema হেল্পার পদ্ধতি ব্যবহার করা
এই উদাহরণে অনুমোদিত enum ভ্যালু নির্ধারণ করতে Firebase AI Logic SDK-এর মাধ্যমে প্রদান করা Schema.enumString হেল্পার পদ্ধতি ব্যবহার করা হয়।
|
এই স্যাম্পেল ব্যবহার করার আগে, আপনার প্রোজেক্ট ও অ্যাপ সেট-আপ করতে
এই গাইডের শুরু করার আগে বিভাগটি
সম্পূর্ণ করুন। সেই বিভাগে, আপনার বেছে নেওয়া Gemini API প্রোভাইডারের জন্য একটি বোতামে ক্লিক করতে হবে যাতে আপনি এই পৃষ্ঠায় প্রোভাইডার-নির্দিষ্ট কন্টেন্ট দেখতে পান। |
import { initializeApp } from "firebase/app";
import {
getAI,
getGenerativeModel,
GoogleAIBackend,
InferenceMode,
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() });
// Define an enum schema using the `Schema` helper method with allowed string values.
const enumSchema = Schema.enumString({
enum: ["drama", "comedy", "documentary"]
});
// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
mode: InferenceMode.INFERENCE_MODE,
// For cloud-hosted models, specify MIME type and response schema.
inCloudParams: {
model: "CLOUD_MODEL_NAME",
generationConfig: {
responseMimeType: "text/x.enum",
responseSchema: enumSchema
}
},
// For on-device models, pass the enum schema as the response constraint.
onDeviceParams: {
promptOptions: {
responseConstraint: enumSchema
}
}
});
const 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 enum output.
const result = await model.generateContent(prompt);
// Access the selected enum value string from response.text().
console.log(result.response.text());
উদাহরণ ২: প্লেন JSON স্কিমা ব্যবহার করা
এই উদাহরণে শুধুমাত্র JSON ব্যবহার করে enum স্কিমা নির্ধারণ করা হয়েছে।
|
এই স্যাম্পেল ব্যবহার করার আগে, আপনার প্রোজেক্ট ও অ্যাপ সেট-আপ করতে
এই গাইডের শুরু করার আগে বিভাগটি
সম্পূর্ণ করুন। সেই বিভাগে, আপনার বেছে নেওয়া Gemini API প্রোভাইডারের জন্য একটি বোতামে ক্লিক করতে হবে যাতে আপনি এই পৃষ্ঠায় প্রোভাইডার-নির্দিষ্ট কন্টেন্ট দেখতে পান। |
import { initializeApp } from "firebase/app";
import {
getAI,
getGenerativeModel,
GoogleAIBackend,
InferenceMode
} 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() });
// Define the enum schema as a plain JSON object.
const enumSchema = {
type: "string",
enum: ["drama", "comedy", "documentary"]
};
// Create a GenerativeModel instance configured to use a hybrid inference mode (like PREFER_ON_DEVICE).
const model = getGenerativeModel(ai, {
mode: InferenceMode.INFERENCE_MODE,
// For cloud-hosted models, specify MIME type and response schema.
inCloudParams: {
model: "CLOUD_MODEL_NAME",
generationConfig: {
responseMimeType: "text/x.enum",
responseSchema: enumSchema
}
},
// For on-device models, pass the enum schema as the response constraint.
onDeviceParams: {
promptOptions: {
responseConstraint: enumSchema
}
}
});
const 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 enum output.
const result = await model.generateContent(prompt);
// Access the selected enum value string from response.text().
console.log(result.response.text());
Firebase AI Logic-এর সাথে আপনার অভিজ্ঞতা সম্পর্কে মতামত জানান