از پیکربندی مدل برای کنترل پاسخ‌ها استفاده کنید

در هر فراخوانی به مدل، می‌توانید پیکربندی مدل را ارسال کنید تا نحوه تولید پاسخ توسط مدل را کنترل کنید. هر مدل از گزینه‌های پیکربندی مختلفی پشتیبانی می‌کند، مثل تنظیم maxOutputTokens یا پیکربندی‌های تخصصی برای فکر کردن، شیوه‌های پاسخ، تصاویر، یا گفتار.

برای اکثر موارد استفاده هنگام دسترسی به مدل Gemini، مدل را بااستفاده از GenerationConfig پیکربندی می‌کنید. اما اگر درحال پیکربندی مدل Gemini Live API هستید، از LiveGenerationConfig استفاده می‌کنید.

این صفحه نحوه تنظیم پیکربندی برای مدل‌های Gemini را نشان می‌دهد و توضیحی درباره هر پارامتر ارائه می‌دهد.

رفتن به پیکربندی Gemini رفتن به پیکربندی Gemini Live API

‫GenerationConfig برای مدل‌های Gemini

روی ارائه‌دهنده Gemini API خود کلیک کنید تا محتوا و کد مخصوص ارائه‌دهنده را در این صفحه مشاهده کنید.

برای اکثر مدل‌های Gemini، ازجمله مدل‌های عمومی، مدل‌های تولید تصویر («مدل‌های Nano Banana»)، و مدل‌های تبدیل نوشتار به گفتار (TTS)، GenerationConfig تنظیم کنید.

پیکربندی برای طول عمر نمونه GenerativeModel حفظ می‌شود. اگر می‌خواهید از پیکربندی دیگری استفاده کنید، نمونه جدیدی با پیکربندی متفاوت ایجاد و استفاده کنید.

Swift

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


import FirebaseAILogic

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
let config = GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: ["red"]
)

// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: config
)

// ...

Kotlin

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
val config = generationConfig {
    maxOutputTokens = 200
    stopSequences = listOf("red")
}

// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    generationConfig = config
)

// ...

Java

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.maxOutputTokens = 200;
configBuilder.stopSequences = List.of("red");

GenerationConfig config = configBuilder.build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                    "GEMINI_MODEL_NAME",
                    config
                );
);

// ...

Web

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


// ...

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
const generationConfig = {
  maxOutputTokens: 200,
  stopSequences: ["red"],
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME",  generationConfig });

// ...

Dart

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
final generationConfig = GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: ["red"],
);

// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

مقادیر پارامترها را در GenerationConfig به‌عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید.


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
var generationConfig = new GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: new string[] { "red" }
);

// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

می‌توانید شرح هر پارامتر را در بخش بعدی این صفحه پیدا کنید.

بااستفاده از Google AI Studio می‌توانید پیام‌واره‌ها و پیکربندی‌های مدل را آزمایش کنید.

‫LiveGenerationConfig برای Gemini Live API مدل

روی ارائه‌دهنده Gemini API خود کلیک کنید تا محتوا و کد مخصوص ارائه‌دهنده را در این صفحه مشاهده کنید.

فقط هنگام استفاده از مدل‌های Gemini Live API، LiveGenerationConfig تنظیم کنید.

پیکربندی برای طول عمر نمونه LiveModel حفظ می‌شود. اگر می‌خواهید از پیکربندی دیگری استفاده کنید، نمونه جدیدی با پیکربندی متفاوت ایجاد و استفاده کنید.

Swift

مقادیر پارامترها را در liveGenerationConfig درطول مقداردهی اولیه نمونه LiveModel تنظیم کنید:


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
let config = LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: [.audio],
  speech: SpeechConfig(voiceName: "Fenrir"),
)

// Specify the config as part of creating the `liveModel` instance.
let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_MODEL_NAME",
  generationConfig: config
)

// ...

Kotlin

مقادیر پارامترها را در LiveGenerationConfig به‌عنوان بخشی از ایجاد نمونه LiveModel تنظیم کنید.


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
val config = liveGenerationConfig {
    maxOutputTokens = 200
    responseModality = ResponseModality.AUDIO
    speechConfig = SpeechConfig(voice = Voice("FENRIR"))
}

// Specify the config as part of creating the `LiveModel` instance.
val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_MODEL_NAME",
    generationConfig = config
)

// ...

Java

مقادیر پارامترها را در LiveGenerationConfig به‌عنوان بخشی از ایجاد نمونه LiveModel تنظیم کنید.


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder();
configBuilder.setMaxOutputTokens(200);
configBuilder.setResponseModality(ResponseModality.AUDIO);

configBuilder.setSpeechConfig(new SpeechConfig(new Voice("FENRIR")));

LiveGenerationConfig config = configBuilder.build();

// Specify the config as part of creating the `LiveModel` instance.
LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
          "GEMINI_LIVE_MODEL_NAME",
          config
);

// ...

Web

مقادیر پارامترها را در LiveGenerationConfig درطول مقداردهی اولیه نمونه LiveGenerativeModel تنظیم کنید:


// ...

// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
const liveGenerationConfig = {
  maxOutputTokens: 200,
  responseModalities: [ResponseModality.AUDIO],
  speechConfig: {
    voiceConfig: {
      prebuiltVoiceConfig: { voiceName: "Fenrir" },
    },
  },
};

// Specify the config as part of creating the `LiveGenerativeModel` instance.
const liveModel = getLiveGenerativeModel(ai, {
  model: "GEMINI_LIVE_MODEL_NAME",
  liveGenerationConfig,
});

// ...

Dart

مقادیر پارامترها را در LiveGenerationConfig به‌عنوان بخشی از ایجاد نمونه LiveGenerativeModel تنظیم کنید.


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
final config = LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: [ResponseModalities.audio],
  speechConfig: SpeechConfig(voiceName: 'Fenrir'),
);

// Specify the config as part of creating the `liveGenerativeModel` instance.
final liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_MODEL_NAME',
  liveGenerationConfig: config,
);

// ...

Unity

مقادیر پارامترها را در LiveGenerationConfig به‌عنوان بخشی از ایجاد نمونه LiveModel تنظیم کنید.


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here).
var config = new LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: new[] { ResponseModality.Audio },
  speechConfig: SpeechConfig.UsePrebuiltVoice("Fenrir")
);

// Specify the config as part of creating the `LiveModel` instance.
var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
  modelName: "GEMINI_LIVE_MODEL_NAME",
  liveGenerationConfig: config
);

// ...

می‌توانید شرح هر پارامتر را در بخش بعدی این صفحه پیدا کنید.

می‌توانید بااستفاده از Google AI Studio، پیام‌واره‌ها و پیکربندی‌های مدل را آزمایش کنید.



شرح پارامترها

جدول زیر نمای کلی سطح بالایی از پارامترهای دردسترس است. برخی‌از پارامترها فقط برای مدل‌های خاصی قابل‌اعمال هستند.

پارامتر شرح محدودیت‌ها مقدار پیش‌فرض
مُهر زمان صدا
audioTimestamp
درک مُهر زمان را برای فایل‌های ورودی فقط صوتی فعال می‌کند.

بولی

فقط زمانی قابل‌اعمال است که از پیکربندی GenerativeModel استفاده شود و نوع ورودی فایل فقط صوتی باشد.

false
بیشینه داده‌واحدهای برونداد
maxOutputTokens

حداکثر تعداد نشانه‌هایی را که می‌توان در پاسخ تولید کرد مشخص می‌کند.

برای gemini-4-argon، حد پیش‌فرض و حداکثر داده‌واحد برونداد برای درخواست‌های غیرجاری‌سازی 32,768 داده‌واحد و برای درخواست‌های جاری‌سازی 65,536 داده‌واحد است. توجه داشته باشید که Firebase AI Logic هنوز از رمزگشایی طولانی پشتیبانی نمی‌کند، اما این ویژگی به‌زودی ارائه می‌شود!

--- ---
توقف توالی‌ها
stopSequences
فهرستی از رشته‌ها را مشخص می‌کند که به مدل می‌گوید اگر یکی از رشته‌ها در پاسخ وجود داشت، تولید محتوا را متوقف کند.

فقط هنگام استفاده از پیکربندی GenerativeModel قابل‌اجرا است.

برای مدل‌های تصویر («Nano Banana») یا مدل‌های تبدیل نوشتار به گفتار قابل‌اجرا نیست.

---
پیکربندی‌های تخصصی
درحال فکر کردن
thinkingConfig

«فرایند تفکر» مدل‌ها را هنگام تولید پاسخ کنترل می‌کند.

  • بااستفاده از سطح اندیشیدن، میزان اندیشیدن مدل را کنترل کنید.
  • کنترل کنید که آیا خلاصه‌های فکری گنجانده شود یا نه.

مقادیر پشتیبانی‌شده: به اسناد تفکر مراجعه کنید

برای مدل‌های «تبدیل نوشتار به گفتار» کاربرد ندارد.

‫Firebase AI Logic هنوز از تنظیم thinkingConfig برای مدل‌های Live API پشتیبانی نمی‌کند.

بسته به مدل
روش‌های پاسخ
responseModalities
نوع خروجی (مانند نوشتار، صدا، یا تصویر) را مشخص می‌کند. فقط هنگام استفاده از Gemini مدل‌های تصویر (مدل‌های «Nano Banana»)، مدل‌های TTS، و مدل‌های Live API قابل‌اعمال (و الزامی) است. ---
مشخصات تصویر
imageConfig
نسبت ابعادی و وضوح تصاویر تولیدشده را مشخص می‌کند.

مقادیر پشتیبانی‌شده: به پیکربندی تولید تصویر مراجعه کنید

فقط درصورت استفاده از مدل‌های Gemini تصویر («Nano Banana» مدل‌ها) قابل‌اعمال است.

نسبت ابعادی ۱:۱ (مربع)
وضوح ۱۰۲۴x۱۰۲۴
گفتار (صدا)
speechConfig
صدای مورداستفاده برای خروجی صوتی را مشخص می‌کند. فقط هنگام استفاده از مدل‌های «تبدیل نوشتار به گفتار» و مدل‌های Live API قابل‌اعمال است.

برای مدل‌های TTS لازم است.
Puck



گزینه‌های دیگر برای کنترل تولید محتوا

  • درباره طراحی پیام‌واره بیشتر بدانید تا بتوانید بر مدل تأثیر بگذارید تا خروجی خاصی را برای نیازهایتان تولید کند.
  • از تنظیمات ایمنی برای تنظیم احتمال دریافت پاسخ‌هایی که ممکن است مضر درنظر گرفته شوند، ازجمله سخنان مشوق عداوت و تنفر و محتوای جنسی صریح، استفاده کنید.
  • دستورالعمل‌های سیستم را تنظیم کنید تا رفتار مدل را هدایت کنید. این ویژگی مانند مقدمه‌ای است که قبل‌از اینکه مدل درمعرض دستورالعمل‌های بیشتر ازسوی کاربر نهایی قرار گیرد، اضافه می‌کنید.
  • یک طرح‌واره پاسخ را همراه با پیام‌واره ارسال کنید تا طرح‌واره برونداد خاصی را مشخص کنید. این ویژگی بیشتر هنگام تولید خروجی JSON استفاده می‌شود، اما می‌توان از آن برای تکالیف طبقه‌بندی نیز استفاده کرد (مثلاً وقتی می‌خواهید مدل از برچسب‌ها یا برچسب‌های خاصی استفاده کند).