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

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

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

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

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

GenerationConfig モデルの Gemini

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

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

構成は、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.
// 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

GenerationConfig のパラメータの値を、GenerativeModel インスタンスの作成の一部として設定します。


// ...

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

GenerationConfig のパラメータの値を、GenerativeModel インスタンスの作成の一部として設定します。


// ...

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

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

GenerationConfig のパラメータの値を、 インスタンスの作成の一部GenerativeModelとして設定します。


// ...

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

GenerationConfig のパラメータの値を、GenerativeModel インスタンスの作成の一部として設定します。


// ...

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

LiveGenerationConfig モデル用 Gemini Live API

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

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

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



パラメータの説明

次の表に、使用可能なパラメータの概要を示します。一部のパラメータは、特定のモデルにのみ適用されます。

temperature、top-K、top-P は非推奨であり、 最新の Gemini モデルでは無視されます。

パラメータ 説明 制限事項 デフォルト値
音声タイムスタンプ
audioTimestamp
音声のみの入力ファイルでタイムスタンプの認識を有効にします。

ブール値

`GenerativeModel` 構成を使用し、入力タイプが音声のみのファイルの場合にのみ適用されます。

false
候補数
candidateCount
返すレスポンスのバリエーションの数を指定します。リクエストごとに、 すべての候補の出力トークンが課金されますが、入力トークンは 1 回のみ課金されます。

サポートされる値: 18(それぞれの値を含む)

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
出力のタイプ(テキスト、音声、画像など)を指定します。 GeminiGemini Image モデル(「Nano Banana」」モデル)、TTS モデル、 Live APILive API モデル を使用する場合に のみ適用されます(必須)。 ---
画像特性
imageConfig
生成される画像のアスペクト比と解像度を指定します。

サポートされる値: 画像生成を構成する をご覧ください

Gemini Image モデル(「Nano Banana」 モデル)を使用する場合にのみ適用されます。

アスペクト比 1:1(スクエア)
解像度 1,024 x 1,024
音声
speechConfig
音声出力に使用する音声を指定します。 TTS モデルと Live API モデルを使用する場合にのみ適用されます。

TTS モデルでは必須です。
Puck





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

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