আপনি জেমিনি মডেলগুলিকে এমনভাবে কনফিগার করতে পারেন যাতে একটি প্রদত্ত JSON স্কিমা মেনে চলে এমন প্রতিক্রিয়া তৈরি করা যায়। এই ক্ষমতাটি পূর্বাভাসযোগ্য এবং বিশ্লেষণযোগ্য ফলাফলের নিশ্চয়তা দেয়, ফর্ম্যাট এবং টাইপ-নিরাপত্তা নিশ্চিত করে, প্রত্যাখ্যানের প্রোগ্রাম্যাটিক সনাক্তকরণ সক্ষম করে এবং প্রম্পটিং সহজ করে।
বিস্তৃত অ্যাপ্লিকেশনের জন্য কাঠামোগত আউটপুট ব্যবহার আদর্শ:
- ডেটা এক্সট্রাকশন: অসংগঠিত টেক্সট থেকে নির্দিষ্ট তথ্য বের করুন, যেমন একটি ইনভয়েস থেকে নাম, তারিখ এবং পরিমাণ বের করা।
- কাঠামোগত শ্রেণীবিভাগ: পাঠ্যকে পূর্বনির্ধারিত বিভাগে শ্রেণীবদ্ধ করুন এবং কাঠামোগত লেবেল বরাদ্দ করুন, যেমন অনুভূতি এবং বিষয় অনুসারে গ্রাহক প্রতিক্রিয়া শ্রেণীবদ্ধ করা।
- এজেন্টিক ওয়ার্কফ্লো: এমন স্ট্রাকচার্ড ডেটা তৈরি করুন যা অন্যান্য টুল বা API কল করতে ব্যবহার করা যেতে পারে, যেমন একটি গেমের জন্য একটি ক্যারেক্টার শিট তৈরি করা বা একটি ফর্ম পূরণ করা।
REST API-তে JSON স্কিমা সমর্থন করার পাশাপাশি, Python এবং JavaScript-এর জন্য Google GenAI SDK গুলি যথাক্রমে Pydantic এবং Zod ব্যবহার করে অবজেক্ট স্কিমা সংজ্ঞায়িত করা সহজ করে তোলে। নীচের উদাহরণটি দেখায় যে কীভাবে কোডে সংজ্ঞায়িত স্কিমার সাথে সামঞ্জস্যপূর্ণ অসংগঠিত পাঠ্য থেকে তথ্য বের করা যায়।
এই উদাহরণটি দেখায় কিভাবে object , array , string , এবং integer এর মতো মৌলিক JSON Schema প্রকার ব্যবহার করে টেক্সট থেকে স্ট্রাকচার্ড ডেটা বের করা যায়।
পাইথন
from google import genai
from pydantic import BaseModel, Field
from typing import List, Optional
class Ingredient(BaseModel):
name: str = Field(description="Name of the ingredient.")
quantity: str = Field(description="Quantity of the ingredient, including units.")
class Recipe(BaseModel):
recipe_name: str = Field(description="The name of the recipe.")
prep_time_minutes: Optional[int] = Field(description="Optional time in minutes to prepare the recipe.")
ingredients: List[Ingredient]
instructions: List[str]
client = genai.Client()
prompt = """
Please extract the recipe from the following text.
The user wants to make delicious chocolate chip cookies.
They need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,
1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,
3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.
For the best part, they'll need 2 cups of semisweet chocolate chips.
First, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,
baking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar
until light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry
ingredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons
onto ungreased baking sheets and bake for 9 to 11 minutes.
"""
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=prompt,
config={
"response_mime_type": "application/json",
"response_json_schema": Recipe.model_json_schema(),
},
)
recipe = Recipe.model_validate_json(response.text)
print(recipe)
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const ingredientSchema = z.object({
name: z.string().describe("Name of the ingredient."),
quantity: z.string().describe("Quantity of the ingredient, including units."),
});
const recipeSchema = z.object({
recipe_name: z.string().describe("The name of the recipe."),
prep_time_minutes: z.number().optional().describe("Optional time in minutes to prepare the recipe."),
ingredients: z.array(ingredientSchema),
instructions: z.array(z.string()),
});
const ai = new GoogleGenAI({});
const prompt = `
Please extract the recipe from the following text.
The user wants to make delicious chocolate chip cookies.
They need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,
1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,
3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.
For the best part, they'll need 2 cups of semisweet chocolate chips.
First, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,
baking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar
until light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry
ingredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons
onto ungreased baking sheets and bake for 9 to 11 minutes.
`;
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseJsonSchema: zodToJsonSchema(recipeSchema),
},
});
const recipe = recipeSchema.parse(JSON.parse(response.text));
console.log(recipe);
যাও
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
prompt := `
Please extract the recipe from the following text.
The user wants to make delicious chocolate chip cookies.
They need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,
1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,
3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.
For the best part, they'll need 2 cups of semisweet chocolate chips.
First, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,
baking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar
until light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry
ingredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons
onto ungreased baking sheets and bake for 9 to 11 minutes.
`
config := &genai.GenerateContentConfig{
ResponseMIMEType: "application/json",
ResponseJsonSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"recipe_name": map[string]any{
"type": "string",
"description": "The name of the recipe.",
},
"prep_time_minutes": map[string]any{
"type": "integer",
"description": "Optional time in minutes to prepare the recipe.",
},
"ingredients": map[string]any{
"type": "array",
"items": map[string]any{
"type": "object",
"properties": map[string]any{
"name": map[string]any{
"type": "string",
"description": "Name of the ingredient.",
},
"quantity": map[string]any{
"type": "string",
"description": "Quantity of the ingredient, including units.",
},
},
"required": []string{"name", "quantity"},
},
},
"instructions": map[string]any{
"type": "array",
"items": map[string]any{"type": "string"},
},
},
"required": []string{"recipe_name", "ingredients", "instructions"},
},
}
result, err := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text(prompt),
config,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text())
}
বিশ্রাম
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[
{ "text": "Please extract the recipe from the following text.\nThe user wants to make delicious chocolate chip cookies.\nThey need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,\n1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,\n3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.\nFor the best part, they will need 2 cups of semisweet chocolate chips.\nFirst, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,\nbaking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar\nuntil light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry\ningredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons\nonto ungreased baking sheets and bake for 9 to 11 minutes." }
]
}],
"generationConfig": {
"responseMimeType": "application/json",
"responseJsonSchema": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
"description": "The name of the recipe."
},
"prep_time_minutes": {
"type": "integer",
"description": "Optional time in minutes to prepare the recipe."
},
"ingredients": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string", "description": "Name of the ingredient."},
"quantity": { "type": "string", "description": "Quantity of the ingredient, including units."}
},
"required": ["name", "quantity"]
}
},
"instructions": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["recipe_name", "ingredients", "instructions"]
}
}
}'
উদাহরণ প্রতিক্রিয়া:
{
"recipe_name": "Delicious Chocolate Chip Cookies",
"ingredients": [
{
"name": "all-purpose flour",
"quantity": "2 and 1/4 cups"
},
{
"name": "baking soda",
"quantity": "1 teaspoon"
},
{
"name": "salt",
"quantity": "1 teaspoon"
},
{
"name": "unsalted butter (softened)",
"quantity": "1 cup"
},
{
"name": "granulated sugar",
"quantity": "3/4 cup"
},
{
"name": "packed brown sugar",
"quantity": "3/4 cup"
},
{
"name": "vanilla extract",
"quantity": "1 teaspoon"
},
{
"name": "large eggs",
"quantity": "2"
},
{
"name": "semisweet chocolate chips",
"quantity": "2 cups"
}
],
"instructions": [
"Preheat the oven to 375°F (190°C).",
"In a small bowl, whisk together the flour, baking soda, and salt.",
"In a large bowl, cream together the butter, granulated sugar, and brown sugar until light and fluffy.",
"Beat in the vanilla and eggs, one at a time.",
"Gradually beat in the dry ingredients until just combined.",
"Stir in the chocolate chips.",
"Drop by rounded tablespoons onto ungreased baking sheets and bake for 9 to 11 minutes."
]
}
স্ট্রিমিং
আপনি স্ট্রাকচার্ড আউটপুট স্ট্রিম করতে পারেন, যা আপনাকে সম্পূর্ণ আউটপুট সম্পূর্ণ হওয়ার জন্য অপেক্ষা না করেই প্রতিক্রিয়াটি তৈরি হওয়ার সাথে সাথে প্রক্রিয়াকরণ শুরু করতে দেয়। এটি আপনার অ্যাপ্লিকেশনের অনুভূত কর্মক্ষমতা উন্নত করতে পারে।
স্ট্রিম করা অংশগুলি বৈধ আংশিক JSON স্ট্রিং হবে, যা চূড়ান্ত, সম্পূর্ণ JSON অবজেক্ট তৈরি করতে সংযুক্ত করা যেতে পারে।
পাইথন
from google import genai
from pydantic import BaseModel, Field
from typing import Literal
class Feedback(BaseModel):
sentiment: Literal["positive", "neutral", "negative"]
summary: str
client = genai.Client()
prompt = "The new UI is incredibly intuitive and visually appealing. Great job. Add a very long summary to test streaming!"
response_stream = client.models.generate_content_stream(
model="gemini-2.5-flash",
contents=prompt,
config={
"response_mime_type": "application/json",
"response_json_schema": Feedback.model_json_schema(),
},
)
for chunk in response_stream:
print(chunk.candidates[0].content.parts[0].text)
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const ai = new GoogleGenAI({});
const prompt = "The new UI is incredibly intuitive and visually appealing. Great job! Add a very long summary to test streaming!";
const feedbackSchema = z.object({
sentiment: z.enum(["positive", "neutral", "negative"]),
summary: z.string(),
});
const stream = await ai.models.generateContentStream({
model: "gemini-2.5-flash",
contents: prompt,
config: {
responseMimeType: "application/json",
responseJsonSchema: zodToJsonSchema(feedbackSchema),
},
});
for await (const chunk of stream) {
console.log(chunk.candidates[0].content.parts[0].text)
}
JSON স্কিমা সমর্থন
একটি JSON অবজেক্ট তৈরি করতে, জেনারেশন কনফিগারেশনে response_mime_type application/json এ সেট করুন এবং একটি response_json_schema প্রদান করুন। স্কিমাটি অবশ্যই একটি বৈধ JSON স্কিমা হতে হবে যা পছন্দসই আউটপুট ফর্ম্যাট বর্ণনা করে।
এরপর মডেলটি এমন একটি প্রতিক্রিয়া তৈরি করবে যা প্রদত্ত স্কিমার সাথে মিলে একটি সিনট্যাক্টিক্যালি বৈধ JSON স্ট্রিং হবে। স্ট্রাকচার্ড আউটপুট ব্যবহার করার সময়, মডেলটি স্কিমার কীগুলির মতো একই ক্রমে আউটপুট তৈরি করবে।
জেমিনির স্ট্রাকচার্ড আউটপুট মোড JSON স্কিমা স্পেসিফিকেশনের একটি উপসেট সমর্থন করে।
নিম্নলিখিত type মানগুলি সমর্থিত:
-
string: টেক্সটের জন্য। -
number: ভাসমান-বিন্দু সংখ্যার জন্য। -
integer: পূর্ণসংখ্যার জন্য। -
boolean: সত্য/মিথ্যা মানের জন্য। -
object: কী-মান জোড়া সহ স্ট্রাকচার্ড ডেটার জন্য। -
array: আইটেমের তালিকার জন্য। -
null: একটি প্রপার্টি null করার জন্য, টাইপ অ্যারেতে"null"অন্তর্ভুক্ত করুন (যেমন,{"type": ["string", "null"]})।
এই বর্ণনামূলক বৈশিষ্ট্যগুলি মডেলটিকে পরিচালনা করতে সাহায্য করে:
-
title: একটি সম্পত্তির সংক্ষিপ্ত বিবরণ। -
description: একটি সম্পত্তির দীর্ঘ এবং আরও বিস্তারিত বর্ণনা।
প্রকার-নির্দিষ্ট বৈশিষ্ট্য
object মানগুলির জন্য:
-
properties: এমন একটি অবজেক্ট যেখানে প্রতিটি কী একটি প্রোপার্টির নাম এবং প্রতিটি মান সেই প্রোপার্টির জন্য একটি স্কিমা। -
required: স্ট্রিংগুলির একটি অ্যারে, কোন বৈশিষ্ট্যগুলি বাধ্যতামূলক তা তালিকাভুক্ত করে। -
additionalProperties:propertiesতালিকাভুক্ত নয় এমন বৈশিষ্ট্যগুলি অনুমোদিত কিনা তা নিয়ন্ত্রণ করে। এটি একটি বুলিয়ান বা স্কিমা হতে পারে।
string মানগুলির জন্য:
-
enum: শ্রেণীবদ্ধকরণ কাজের জন্য সম্ভাব্য স্ট্রিংগুলির একটি নির্দিষ্ট সেট তালিকাভুক্ত করে। -
format: স্ট্রিং এর জন্য একটি সিনট্যাক্স নির্দিষ্ট করে, যেমনdate-time,date,time।
number এবং integer মানের জন্য:
-
enum: সম্ভাব্য সংখ্যাসূচক মানের একটি নির্দিষ্ট সেট তালিকাভুক্ত করে। -
minimum: সর্বনিম্ন অন্তর্ভুক্ত মান। -
maximum: সর্বাধিক অন্তর্ভুক্ত মান।
array মানগুলির জন্য:
-
items: অ্যারের সকল আইটেমের জন্য স্কিমা নির্ধারণ করে। -
prefixItems: প্রথম N আইটেমের জন্য স্কিমার একটি তালিকা সংজ্ঞায়িত করে, যা টুপলের মতো কাঠামোর জন্য অনুমতি দেয়। -
minItems: অ্যারেতে থাকা আইটেমের সর্বনিম্ন সংখ্যা। -
maxItems: অ্যারেতে আইটেমের সর্বাধিক সংখ্যা।
মডেল সাপোর্ট
নিম্নলিখিত মডেলগুলি কাঠামোগত আউটপুট সমর্থন করে:
| মডেল | স্ট্রাকচার্ড আউটপুট |
|---|---|
| জেমিনি ২.৫ প্রো | ✔️ |
| জেমিনি 2.5 ফ্ল্যাশ | ✔️ |
| জেমিনি ২.৫ ফ্ল্যাশ-লাইট | ✔️ |
| জেমিনি ২.০ ফ্ল্যাশ | ✔️* |
| জেমিনি ২.০ ফ্ল্যাশ-লাইট | ✔️* |
* মনে রাখবেন যে Gemini 2.0-এর পছন্দের কাঠামো নির্ধারণের জন্য JSON ইনপুটের মধ্যে একটি স্পষ্ট propertyOrdering list প্রয়োজন। আপনি এই রান্নার বইতে একটি উদাহরণ খুঁজে পেতে পারেন।
স্ট্রাকচার্ড আউটপুট বনাম ফাংশন কলিং
স্ট্রাকচার্ড আউটপুট এবং ফাংশন কলিং উভয়ই JSON স্কিমা ব্যবহার করে, তবে তারা বিভিন্ন উদ্দেশ্যে কাজ করে:
| বৈশিষ্ট্য | প্রাথমিক ব্যবহারের ক্ষেত্রে |
|---|---|
| স্ট্রাকচার্ড আউটপুট | ব্যবহারকারীর কাছে চূড়ান্ত প্রতিক্রিয়া ফর্ম্যাট করা। যখন আপনি মডেলের উত্তরটি একটি নির্দিষ্ট ফর্ম্যাটে রাখতে চান (যেমন, একটি ডাটাবেসে সংরক্ষণের জন্য একটি ডকুমেন্ট থেকে ডেটা বের করা) তখন এটি ব্যবহার করুন। |
| ফাংশন কলিং | কথোপকথনের সময় পদক্ষেপ নেওয়া। যখন মডেল আপনাকে কোনও কাজ সম্পাদন করতে বলবে (যেমন, "বর্তমান আবহাওয়া জানুন") তখন এটি ব্যবহার করুন, যখন তারা চূড়ান্ত উত্তর দেওয়ার আগে। |
সেরা অনুশীলন
- স্পষ্ট বর্ণনা: প্রতিটি বৈশিষ্ট্য কী প্রতিনিধিত্ব করে সে সম্পর্কে মডেলকে স্পষ্ট নির্দেশনা প্রদানের জন্য আপনার স্কিমার
descriptionক্ষেত্রটি ব্যবহার করুন। মডেলের আউটপুট পরিচালনার জন্য এটি অত্যন্ত গুরুত্বপূর্ণ। - শক্তিশালী টাইপিং: যখনই সম্ভব নির্দিষ্ট প্রকার (
integer,string,enum) ব্যবহার করুন। যদি কোনও প্যারামিটারে বৈধ মানের সীমিত সেট থাকে, তাহলে একটিenumব্যবহার করুন। - প্রম্পট ইঞ্জিনিয়ারিং: আপনার প্রম্পটে স্পষ্টভাবে বলুন যে আপনি মডেলটি কী করতে চান। উদাহরণস্বরূপ, "টেক্সট থেকে নিম্নলিখিত তথ্যগুলি বের করুন..." অথবা "প্রদত্ত স্কিমা অনুসারে এই প্রতিক্রিয়াটিকে শ্রেণীবদ্ধ করুন..."।
- বৈধকরণ: স্ট্রাকচার্ড আউটপুট সিনট্যাক্টিকভাবে সঠিক JSON এর নিশ্চয়তা দিলেও, এটি মানগুলি শব্দার্থিকভাবে সঠিক কিনা তার নিশ্চয়তা দেয় না। আপনার অ্যাপ্লিকেশন কোড ব্যবহার করার আগে সর্বদা চূড়ান্ত আউটপুট যাচাই করুন।
- ত্রুটি পরিচালনা: আপনার অ্যাপ্লিকেশনে শক্তিশালী ত্রুটি পরিচালনা প্রয়োগ করুন যাতে মডেলের আউটপুট, স্কিমা-সম্মত হলেও, আপনার ব্যবসায়িক যুক্তির প্রয়োজনীয়তা পূরণ নাও করতে পারে এমন ক্ষেত্রে সুন্দরভাবে পরিচালনা করা যায়।
সীমাবদ্ধতা
- স্কিমা সাবসেট: JSON স্কিমা স্পেসিফিকেশনের সমস্ত বৈশিষ্ট্য সমর্থিত নয়। মডেলটি অসমর্থিত বৈশিষ্ট্যগুলিকে উপেক্ষা করে।
- স্কিমার জটিলতা: API খুব বড় বা গভীরভাবে নেস্টেড স্কিমা প্রত্যাখ্যান করতে পারে। যদি আপনি ত্রুটির সম্মুখীন হন, তাহলে সম্পত্তির নাম ছোট করে, নেস্টিং কমিয়ে, অথবা সীমাবদ্ধতার সংখ্যা সীমিত করে আপনার স্কিমা সহজ করার চেষ্টা করুন।