使用模型設定控管回應

在每次呼叫模型時,您可以一併傳送模型設定,藉此控制模型生成回覆的方式。每個模型支援不同的設定選項,例如設定 maxOutputTokens 或專門用於思考、回應模式、圖片或語音的設定。

在大多數用途中,存取 Gemini 模型時,您會使用 GenerationConfig 設定模型。不過,如果您要設定 Gemini Live API 模型,則使用 LiveGenerationConfig。

本頁說明如何設定 Gemini 模型的設定,並提供各項參數的說明。

跳至 Gemini 設定 跳至 Gemini Live API 設定

GenerationConfig,適用於 Gemini 模型

按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。

為大多數 Gemini 模型設定 GenerationConfig,包括一般用途模型、圖像生成模型 (「Nano Banana」模型) 和文字轉語音 (TTS) 模型。

設定會在 GenerativeModel 例項的生命週期內維持不變。如要使用其他設定,請建立並使用具有不同設定的新執行個體。

Swift

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


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

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// 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

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// 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

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// 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

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// 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

在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。


// ...

// 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

在 LiveModel 例項的初始化期間,設定 liveGenerationConfig 中的參數值:


// ...

// 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

在建立 LiveModel 例項時,設定 LiveGenerationConfig 中的參數值。


// ...

// 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.agentPlatform()).liveModel(
    modelName = "GEMINI_LIVE_MODEL_NAME",
    generationConfig = config
)

// ...

Java

在建立 LiveModel 例項時,設定 LiveGenerationConfig 中的參數值。


// ...

// 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

在 LiveGenerativeModel 例項的初始化期間,設定 LiveGenerationConfig 中的參數值:


// ...

// 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

在建立 LiveGenerativeModel 例項時,設定 LiveGenerationConfig 中的參數值。


// ...

// 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

在建立 LiveModel 例項時,設定 LiveGenerationConfig 中的參數值。


// ...

// 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
指定回覆中可生成的詞元數量上限。 --- ---
停止序列
stopSequences
指定字串清單,如果模型在回應中遇到其中一個字串,就會停止生成內容。

僅適用於使用 GenerativeModel 設定。

不適用於 圖像模型 (「Nano Banana」) 或 TTS 模型。

---
專門設定
思考中
thinkingConfig

控制模型生成回覆時的「思考過程」。

  • 使用思考程度 (Gemini 3.x 和後續模型),控制模型可進行的思考量。
  • 控管是否要納入想法摘要。

支援的值:請參閱思考文件

不適用於 TTS 模型。

Firebase AI Logic目前不支援為 Live API 模型設定 thinkingConfig。

視型號而定
回覆模式
responseModalities
指定輸出類型 (例如文字、音訊或圖片)。 僅適用於使用 Gemini 圖像模型 (「Nano Banana」模型)、TTS 模型和 Live API 模型時。 ---
圖片特徵
imageConfig
指定生成圖像的顯示比例和解析度。

支援的值:請參閱「設定圖片生成」一文

僅適用於使用Gemini圖像模型 (「Nano Banana」模型)。

顯示比例 1:1 (正方形)
解析度 1024x1024
語音
speechConfig
指定用於音訊輸出的語音。 僅適用於 TTS 模型和 Live API 模型。

TTS 模型必須提供這項資訊。
Puck



控制內容生成作業的其他選項

  • 進一步瞭解提示設計,讓模型生成符合您需求的輸出內容。
  • 使用安全性設定 調整獲得可能有害回覆的機率,包括仇恨言論和情色露骨內容。
  • 設定系統指令,引導模型行為。這項功能就像前言,您可以在模型收到使用者的任何進一步指示前新增前言。
  • 連同提示傳遞回覆結構定義,指定特定輸出內容結構定義。這項功能最常用於產生 JSON 輸出內容,但也可以用於分類工作 (例如,當您希望模型使用特定標籤)。