モデル構成を使用してレスポンスを制御する

モデルへの呼び出しごとに、モデルがどのようにレスポンスを生成するかを制御するモデル構成を送信できます。各モデルは、maxOutputTokens の設定や、思考、レスポンス モダリティ、画像、音声用の特殊な構成など、さまざまな構成オプションをサポートしています。

Gemini モデルにアクセスするほとんどのユースケースでは、GenerationConfig を使用してモデルを構成します。ただし、Gemini Live API モデルを構成する場合は、LiveGenerationConfig を使用します。

このページでは、Gemini モデルの構成を設定する方法と、各パラメータの説明について説明します。

Gemini 構成に移動 Gemini Live API 構成に移動

Gemini モデルの GenerationConfig

Gemini API プロバイダをクリックして、このページでプロバイダ固有のコンテンツとコードを表示します。

汎用モデル、画像生成モデル(「Nano Banana」モデル)、テキスト読み上げ(TTS)モデルなど、ほとんどの Gemini モデルに GenerationConfig を設定します。

構成は 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 を使用して、プロンプトとモデル構成を試すことができます。

Gemini Live API モデルの LiveGenerationConfig

Gemini API プロバイダをクリックして、このページでプロバイダ固有のコンテンツとコードを表示します。

LiveGenerationConfig は、Gemini Live API モデルを使用する場合にのみ設定します。

構成は 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 インスタンスの作成の一環として、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 インスタンスの作成の一環として、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(スクエア)
解像度 1,024 x 1,024
音声(音声)
speechConfig
音声出力に使用する音声を指定します。 TTS モデルと Live API モデルを使用する場合にのみ適用されます。

TTS モデルに必要です。
Puck



コンテンツ生成を制御するその他のオプション

  • プロンプト設計について学習し、ニーズに合った出力を生成するようにモデルに影響を与えます。
  • 安全に関する設定を使用して、ヘイトスピーチや性的描写が露骨なコンテンツなど、有害とみなされる可能性のあるレスポンスが生成される可能性を調整します。
  • システム指示を設定して、モデルの動作を制御します。この機能は、エンドユーザーからの詳細な指示がモデルに伝達される前に追加する前文のようなものです。
  • プロンプトとともにレスポンス スキーマを渡して、特定の出力スキーマを指定します。この機能は、JSON 出力を生成する場合に最もよく使用されますが、分類タスク(モデルに特定のラベルやタグを使用させる場合など)にも使用できます。