使用模型設定控管回應

在每次呼叫模型時,您可以一併傳送模型設定,藉此控制模型生成回覆的方式。每個模型支援不同的設定選項,例如設定 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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
let config = GenerationConfig(
  candidateCount: 1,
  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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
val config = generationConfig {
    candidateCount = 1
    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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.candidateCount = 1;
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
const generationConfig = {
  candidate_count: 1,
  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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
final generationConfig = GenerationConfig(
  candidateCount: 1,
  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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
var generationConfig = new GenerationConfig(
  candidateCount: 1,
  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 = Voices.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 例項時,設定 a 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(Voices.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 例項時,設定 a 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 例項時,設定 a 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 嘗試各種提示和模型設定。



參數說明

下表列出可用的參數,並提供概要總覽。部分參數僅適用於特定模型。

請注意,temperature、top-K 和 top-P 已淘汰,最新版 Gemini 模型會忽略這些參數。

參數 說明 限制 預設值
音訊時間戳記
audioTimestamp
可瞭解僅含音訊的輸入檔案的時間戳記。

布林值

僅適用於使用 GenerativeModel 設定,且輸入類型為純音訊檔案的情況。

false
候選人數
candidateCount
指定要傳回的回覆變體數量。每次要求時,系統會針對所有候選內容的輸出權杖收費,但只會針對輸入權杖收費一次。

支援的值:1 - 8 (包含頭尾)

僅適用於使用 generateContent 的情況。

使用 generateContentStream、文字轉語音 (TTS) 模型或 Live API 模型時,不適用這項設定。

1
輸出詞元數量上限
maxOutputTokens
指定回覆中可生成的詞元數量上限。 --- ---
頻率懲罰
frequencyPenalty

控制在生成的回覆中重複出現的權杖機率。

正值會懲罰在生成的內容中重複出現的符記,降低重複內容的機率。

不適用於 TTS 模型。 ---
存在感處罰
presencePenalty

控制在生成的回覆中加入已出現的權杖的機率。

正值會懲罰已出現在生成內容中的符記,提高生成更多樣化內容的機率。

不適用於 TTS 模型。 ---
停止序列
stopSequences
指定字串清單,如果模型在回應中遇到其中一個字串,就會停止生成內容。

僅適用於使用 GenerativeModel 設定。

不適用於 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 輸出內容,但也可以用於分類工作 (例如想讓模型使用特定標籤)。