مدلهای Gemini بهطور پیشفرض پاسخها را بهصورت نوشتار بدون ساختار برمیگردانند. بااینحال، برخیاز موارد استفاده به نوشتار ساختاریافته (مانند JSON یا enums) نیاز دارند. برای مثال، ممکن است از پاسخ برای کارهای پاییندستی دیگری استفاده کنید که به طرحواره دادههای تثبیتشده نیاز دارند.
برای اطمینان از اینکه برونداد تولیدشده مدل همیشه از طرحواره خاصی پیروی میکند، میتوانید طرحوارهای تعریف کنید که مانند طرحی برای پاسخهای مدل عمل میکند. سپس میتوانید مستقیماً دادهها را از خروجی مدل با پسپردازش کمتر استخراج کنید.
در اینجا چند نمونه از موارد استفاده آورده شده است:
مطمئن شوید که پاسخ مدل JSON معتبر تولید میکند و با طرحواره ارائهشده شما مطابقت دارد.
برای مثال، مدل میتواند ورودیهای ساختاریافتهای برای دستورهای پخت تولید کند که همیشه شامل نام دستور پخت، فهرست مواد اولیه، و مراحل باشد. سپس میتوانید این اطلاعات را راحتتر در میانای کاربر برنامهتان تجزیه و نمایش دهید.محدود کردن نحوه پاسخ دادن مدل درطول تکالیف طبقهبندی.
برای مثال، میتوانید از مدل بخواهید نوشتار را با مجموعه خاصی از برچسبها (مثلاً مجموعه خاصی از شمارشیها مانندpositiveوnegative) حاشیهنویسی کند، نه برچسبهایی که مدل تولید میکند (که ممکن است درجاتی از تنوع مانندgood،positive،negative، یاbadداشته باشند).
این صفحه نحوه تولید خروجی ساختاریافته (مانند JSON و شمارشیها) در تجربههای ترکیبی برای برنامههای وب را شرح میدهد.
پرش به خروجی JSON پرش به خروجی enum
پیکربندی برای خروجی ساختاریافته
تولید خروجی ساختاریافته (مانند JSON و شمارشیها) برای استنتاج بااستفاده از مدلهای میزبانیشده در فضای ابری و مدلهای روی دستگاه پشتیبانی میشود.
حالتهای استنتاج ترکیبی: هر دو
inCloudParamsوonDeviceParamsرا پیکربندی کنید تا مدل با خروجی ساختاریافته پاسخ دهد، صرفنظر از اینکه استنتاج در فضای ابری اجرا میشود یا در دستگاه:برای مدلهای دروندستگاهی:
responseConstraintرا درonDeviceParamsبااستفاده از طرحواره ایجادشده با روشهای کمکیSchemaیا طرحواره JSON ساده مشخص کنید.برای مدلهای میزبانیشده در فضای ابری:
responseMimeType(application/jsonبرای JSON یاtext/x.enumبرای شمارشیها) وresponseSchemaرا درinCloudParamsمشخص کنید.
حالتهای استنتاج غیرترکیبی: فقط از پیکربندی قابلاجرای شرحدادهشده در بالا استفاده کنید.
قبل از شروع
|
روی ارائهدهنده Gemini API خود کلیک کنید تا محتوا و کد مختص ارائهدهنده را در این صفحه ببینید. |
مطمئن شوید که راهنمای شروع به کار برای ساختن تجربههای ترکیبی را تکمیل کردهاید.
پرش به خروجی JSON پرش به خروجی enum
خروجی JSON
نمونههای زیر
نمونه خروجی JSON عمومی
را برای تطبیق با استنتاج ترکیبی (برای مثال، PREFER_ON_DEVICE) سازگار میکنند.
در سناریوی این مثالها، مدل فهرستی از نمایههای شخصیت برای یک داستان فانتزی با مشخصههای ساختاریافته مانند نام، سن، گونه، و لوازم جانبی اختیاری تولید میکند.
میتوانید طرحوارههای پاسخ خود را بااستفاده از یکی از رویکردهای زیر تعریف کنید:
روشهای کمکی
SchemaFirebase (توصیهشده): از روشهای کمکی (مانندSchema.object()وSchema.enumString()) برای نوشتن طرحوارههای مختصر و فشرده مستقیماً در کد خود بدون نیاز به کد تکراری اضافی استفاده کنید.طرح JSON ساده: اگر ازقبل تعاریف طرح موجود دارید، طرحها را در پلاتفرمها یا سرویسهای پسبک همرسانی میکنید، یا طرحها را از فایلهای JSON وارد میکنید، از شیء «طرح JSON» استاندارد استفاده کنید.
مثال ۱: استفاده از روشهای کمکی Firebase Schema
این مثال از روشهای کمکی Schema (مانند Schema.object،
Schema.array، Schema.string، و Schema.number) که توسط
کیت توسعه نرمافزار Firebase AI Logic ارائه شده است برای تعریف طرحواره شیء استفاده میکند.
|
قبلاز امتحان کردن این نمونه، بخش
قبلاز شروع این راهنما را تکمیل کنید
تا پروژه و برنامهتان را راهاندازی کنید. در آن بخش، روی دکمهای برای ارائهدهنده انتخابیتان 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) سازگار میکنند.
در سناریوی این مثالها، مدل شرح فیلم را با انتخاب یک ژانر از فهرست ازپیشتعریفشده گزینههای مجاز (drama، comedy، یا documentary) طبقهبندی میکند.
میتوانید طرحوارههای پاسخ خود را بااستفاده از یکی از رویکردهای زیر تعریف کنید:
روشهای کمکی
SchemaFirebase (توصیهشده): از روشهای کمکی (مانندSchema.object()وSchema.enumString()) برای نوشتن طرحوارههای مختصر و فشرده مستقیماً در کد خود بدون نیاز به کد تکراری اضافی استفاده کنید.طرح JSON ساده: اگر ازقبل تعاریف طرح موجود دارید، طرحها را در پلاتفرمها یا سرویسهای پسبک همرسانی میکنید، یا طرحها را از فایلهای JSON وارد میکنید، از شیء «طرح JSON» استاندارد استفاده کنید.
مثال ۱: استفاده از روشهای کمکی Firebase Schema
این مثال از روش کمکی Schema.enumString ارائهشده توسط
کیت توسعه نرمافزار Firebase AI Logic برای تعریف مقادیر شمارشی مجاز استفاده میکند.
|
قبلاز امتحان کردن این نمونه، بخش
قبلاز شروع این راهنما را تکمیل کنید
تا پروژه و برنامهتان را راهاندازی کنید. در آن بخش، روی دکمهای برای ارائهدهنده انتخابیتان 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 ساده
این مثال طرحواره enum را فقط بااستفاده از 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 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 بازخورد ارائه دهید