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

モデルへの呼び出しごとに、モデル構成を送信して、モデルがどのようにレスポンスを生成するかを制御できます。各モデルには、異なる構成オプションが用意されています。

Google AI Studio を使用して、プロンプトとモデル構成を試すこともできます。 Google AI Studio.

Gemini の構成オプションに移動 Imagen の構成オプションに移動 (非推奨)



Gemini モデルを構成する

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

このセクションでは、 構成を設定する方法と、 Geminiモデルで使用するための 各パラメータの説明について説明します。

モデル構成を設定する(Gemini

一般的な Gemini ユースケースの構成

構成はインスタンスの有効期間中維持されます。別の構成を使用する場合は、その構成で新しい 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.
let config = GenerationConfig(
  candidateCount: 1,
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
  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.
val config = generationConfig {
    candidateCount = 1
    maxOutputTokens = 200
    stopSequences = listOf("red")
    temperature = 0.9f
    topK = 16
    topP = 0.1f
}

// 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.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.candidateCount = 1;
configBuilder.maxOutputTokens = 200;
configBuilder.stopSequences = List.of("red");
configBuilder.temperature = 0.9f;
configBuilder.topK = 16;
configBuilder.topP = 0.1f;

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.
const generationConfig = {
  candidate_count: 1,
  maxOutputTokens: 200,
  stopSequences: ["red"],
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
};

// 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.
final generationConfig = GenerationConfig(
  candidateCount: 1,
  maxOutputTokens: 200,
  stopSequences: ["red"],
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
);

// 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.
var generationConfig = new GenerationConfig(
  candidateCount: 1,
  maxOutputTokens: 200,
  stopSequences: new string[] { "red" },
  temperature: 0.9f,
  topK: 16,
  topP: 0.1f
);

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

各パラメータの説明 については、このページの次のセクションをご覧ください。

Gemini Live API の構成

構成はインスタンスの有効期間中維持されます。別の構成を使用する場合は、その構成で新しい LiveModel インスタンスを作成します。

Swift

LiveModel インスタンスの初期化時に liveGenerationConfig のパラメータ値を設定します。


// ...

// Set parameter values in a `LiveGenerationConfig` (example values shown here)
let config = LiveGenerationConfig(
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
  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)
    temperature = 0.9f
    topK = 16
    topP = 0.1f
}

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

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));
configBuilder.setTemperature(0.9f);
configBuilder.setTopK(16);
configBuilder.setTopP(0.1f);

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


// ...

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" },
    },
  },
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
};

// 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'),
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
);

// 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"),
  temperature: 0.9f,
  topK: 16,
  topP: 0.1f
);

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

// ...

各パラメータの説明 については、このページの次のセクションをご覧ください。

パラメータの説明(Gemini

使用可能なパラメータの概要は次のとおりです。 パラメータとその値の完全なリストについては、 Gemini Developer APIドキュメントをご覧ください。

パラメータ 説明 デフォルト値
音声タイムスタンプ
audioTimestamp

音声のみの入力 ファイルでタイムスタンプの認識を有効にするブール値。

`generateContent` または generateContent または generateContentStream 呼び出しを使用し、入力タイプが 音声のみのファイルの場合にのみ適用されます。

false
候補数
candidateCount

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

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

generateContent と最新の Gemini モデルを使用する場合にのみ適用されます。Live API モデルと generateContentStream は対象外です。

1
頻度のペナルティ
frequencyPenalty
生成されたレスポンスに繰り返し出現するトークンを含める確率を制御します。
値が正の場合は、生成された コンテンツに繰り返し出現するトークンにペナルティが課されるため、コンテンツが繰り返される確率は低下します。
---
最大出力トークン
maxOutputTokens
レスポンスで生成できるトークンの最大数を指定します。 ---
プレゼンス ペナルティ
presencePenalty
生成されたレスポンスにすでに表示されているトークンを含める確率を制御します。
値が正の場合は、生成されたコンテンツ内の既存のトークンにペナルティが課されるため、より多様なコンテンツが生成される確率は高くなります。
---
停止シーケンス
stopSequences

レスポンスでいずれかの文字列が検出された場合に、コンテンツの生成を停止するようモデルに指示する文字列のリストを指定します。

`GenerativeModel` 構成を使用する場合にのみ適用されます。 GenerativeModel

---
温度
temperature
レスポンスのランダム性を制御します。
温度が低いほど、より決定的なレスポンスが生成され、温度が高いほど、より多様で創造的なレスポンスが生成されます。
モデルによって異なります
Top-K
topK
生成されたコンテンツで使用される確率の高い単語の数を制限します。
Top-K 値が 1 の場合、次に選択されるトークンは、モデルの語彙内のすべてのトークンで 最も確率の高いものである必要があります。Top-K 値が n の場合は、最も確率の高い上位 n 個のトークンから次のトークンを選択する必要があります (すべて設定された Temperature に基づきます)。
モデルによって異なります
Top-P
topP
生成されたコンテンツの多様性を制御します。
トークンは、確率の合計が Top-P 値に等しくなるまで、確率の高いもの(上記の Top-K を参照)から低いものへと選択されます。
モデルによって異なります
レスポンス モダリティ
responseModality

Gemini モデルによる Live API またはネイティブ マルチモーダル出力を使用する場合のストリーミング出力のタイプ(テキスト、音声、画像など)を指定します。 Gemini

Live API モデルを使用する場合、またはマルチモーダル出力をサポートする Gemini モデルを使用する場合にのみ適用されます。

---
音声
speechConfig

Live API を使用する場合にストリーミング オーディオ出力に使用する音声を指定します。Live API

Live API モデルを使用する場合にのみ適用されます。

Puck



Imagen モデルを構成する

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

このセクションでは、 構成を設定する方法と、 Imagenモデルで使用する 各パラメータの説明について説明します。

モデル構成を設定する(Imagen

構成はインスタンスの有効期間中維持されます。別の構成を使用する場合は、その構成で新しい ImagenModel インスタンスを作成します。

Swift

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


import FirebaseAILogic

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
let config = ImagenGenerationConfig(
  negativePrompt: "frogs",
  numberOfImages: 2,
  aspectRatio: .landscape16x9,
  imageFormat: .jpeg(compressionQuality: 100),
  addWatermark: false
)

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

// ...

Kotlin

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


// ...

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
val config = ImagenGenerationConfig {
    negativePrompt = "frogs",
    numberOfImages = 2,
    aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9,
    imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100),
    addWatermark = false
}

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

// ...

Java

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


// ...

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
ImagenGenerationConfig config = new ImagenGenerationConfig.Builder()
    .setNegativePrompt("frogs")
    .setNumberOfImages(2)
    .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9)
    .setImageFormat(ImagenImageFormat.jpeg(100))
    .setAddWatermark(false)
    .build();

// Specify the config as part of creating the `ImagenModel` instance
ImagenModelFutures model = ImagenModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .imagenModel(
                    "IMAGEN_MODEL_NAME",
                    config
                );
);

// ...

Web

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


// ...

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

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
const generationConfig = {
  negativePrompt: "frogs",
  numberOfImages: 2,
  aspectRatio: ImagenAspectRatio.LANDSCAPE_16x9,
  imageFormat: ImagenImageFormat.jpeg(100),
  addWatermark: false
};

// Specify the config as part of creating the `ImagenModel` instance
const model = getImagenModel(ai, { model: "IMAGEN_MODEL_NAME", generationConfig });

// ...

Dart

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


// ...

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
final generationConfig = ImagenGenerationConfig(
  negativePrompt: 'frogs',
  numberOfImages: 2,
  aspectRatio: ImagenAspectRatio.landscape16x9,
  imageFormat: ImagenImageFormat.jpeg(compressionQuality: 100)
  addWatermark: false
);

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

// ...

Unity

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


using Firebase.AI;

// Set parameter values in a `ImagenGenerationConfig` (example values shown here)
var config = new ImagenGenerationConfig(
  numberOfImages: 2,
  aspectRatio: ImagenAspectRatio.Landscape16x9,
  imageFormat: ImagenImageFormat.Jpeg(100)
);

// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `ImagenModel` instance
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetImagenModel(
  modelName: "imagen-4.0-generate-001",
  generationConfig: config
);

// ...

各パラメータの説明 については、このページの次のセクションをご覧ください。

パラメータの説明(Imagen

使用可能なパラメータの概要は次のとおりです。 パラメータとその値の 完全なリストについては、 Google Cloudドキュメントをご覧ください。

パラメータ 説明 デフォルト値
ネガティブ プロンプト
negativePrompt
生成される画像から除外する内容の説明

このパラメータは、 imagen-3.0-generate-002 ではまだサポートされていません。

---
結果の件数
numberOfImages
リクエストごとに返される生成画像の数 デフォルトは 1 画像
アスペクト比
aspectRatio
生成画像の幅と高さの比 デフォルトはスクエア(1:1)
画像形式
imageFormat
生成された画像の画像形式(MIME タイプ)や圧縮レベルなどの出力オプション デフォルトの MIME タイプは PNG
デフォルトの圧縮率は 75(MIME タイプが JPEG に設定されている場合)
透かし
addWatermark
生成された画像に目に見えないデジタル透かし( SynthID)を追加するかどうか デフォルトは true
人物生成
personGeneration
モデルによる人物の画像生成を許可するかどうか デフォルトはモデルによって異なります
安全性属性を含める
includeSafetyAttributes
フィルタリングされていない入力と出力のレスポンスで、安全性 属性のリストに対して丸められた責任ある AI スコアを有効にするかどうか

安全性属性のカテゴリ: "Death, Harm & Tragedy", "Firearms & Weapons", "Hate", "Health", "Illicit Drugs", "Politics", "Porn", "Religion & Belief", "Toxic", "Violence", "Vulgarity", "War & Conflict".

デフォルトは false



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