使用模型設定控管回應

在每次呼叫模型時,您可以一併傳送模型設定,藉此控制模型生成回覆的方式。每種型號都提供不同的設定選項。

您也可以使用 Google AI Studio 測試提示和模型設定。

跳至 Gemini 設定選項 跳至 Imagen 設定選項



設定 Gemini 模型

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

本節說明如何設定搭配 Gemini 模型使用的設定,並說明各項參數

設定模型設定 (Gemini)

一般用途的設定

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

Swift

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


import FirebaseAI

// Set parameter values in a `GenerationConfig` (example values shown here)
let config = GenerationConfig(
  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

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


// ...

// Set parameter values in a `GenerationConfig` (example values shown here)
val config = generationConfig {
    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

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


// ...

// Set parameter values in a `GenerationConfig` (example values shown here)
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
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

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


// ...

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

// Set parameter values in a `GenerationConfig` (example values shown here)
const generationConfig = {
  max_output_tokens: 200,
  stop_sequences: ["red"],
  temperature: 0.9,
  top_p: 0.1,
  top_k: 16,
};

// 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` (example values shown here)
final generationConfig = GenerationConfig(
  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

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


// ...

// Set parameter values in a `GenerationConfig` (example values shown here)
var generationConfig = new GenerationConfig(
  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 的設定

參數說明 (Gemini)

以下概略說明可用的參數 (如適用)。 如要查看完整的參數和參數值清單,請參閱 Gemini Developer API 說明文件。

參數 說明 預設值
音訊時間戳記
audioTimestamp

布林值,可讓系統瞭解僅含音訊的輸入檔案的時間戳記。

僅適用於使用 generateContentgenerateContentStream 呼叫,且輸入類型為純音訊檔案的情況。

false
頻率處罰
frequencyPenalty
控制在生成的回覆中重複出現的權杖機率。
正值會懲罰在生成內容中重複出現的符記,降低重複內容的機率。
---
輸出詞元數量上限
maxOutputTokens
指定回覆中可生成的詞元數量上限。 ---
出席懲罰
presencePenalty
控制在生成的回覆中加入已出現的權杖的機率。
正值會懲罰已出現在生成內容中的符記,提高生成更多樣化內容的機率。
---
停止序列
stopSequences

指定字串清單,如果模型在回應中遇到其中一個字串,就會停止生成內容。

僅適用於使用 GenerativeModel 設定。

---
溫度
temperature
控制回覆的隨機程度。
溫度越低,模型回覆的確定性越高;溫度越高,模型回覆的內容越多元或越有創意。
視型號而定
Top-K
topK
限制生成內容中機率最高的字詞數量。
如果 Top-K 值為 1,代表下一個所選詞元應是模型詞彙表的所有詞元中可能性最高者。如果 Top-K 值為 n,代表下一個詞元應從可能性最高的 n詞元中選取 (全部依據設定的溫度)。
視型號而定
Top-P
topP
控制生成內容的多樣性。
模型會按照機率最高 (請見上方的「Top-K」) 到最低的順序選取符記,直到所選符記的機率總和等於「Top-P」值。
視型號而定
回覆模式
responseModality

指定使用 Live APIGemini 模型原生多模態輸出時的串流輸出類型,例如文字、音訊或圖片。

只有在使用 Live APILiveModel 設定,或是使用可輸出多模態內容的 Gemini 模型時,才適用這項功能。

---
語音
speechConfig

使用 Live API 時,指定用於串流音訊輸出的語音。

僅適用於使用 Live APILiveModel 設定。

Puck



設定 Imagen 模型

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

本節說明如何設定搭配 Imagen 模型使用的設定,並說明各項參數

設定模型設定 (Imagen)

這項設定會在執行個體生命週期內維持不變。如要使用其他設定,請使用該設定建立新的 ImagenModel 執行個體。

Swift

建立 ImagenModel 例項時,請在 ImagenGenerationConfig 中設定參數值。


import FirebaseAI

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

建立 ImagenModel 例項時,請在 ImagenGenerationConfig 中設定參數值。


// ...

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

建立 ImagenModel 例項時,請在 ImagenGenerationConfig 中設定參數值。


// ...

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

建立 ImagenModel 例項時,請在 ImagenGenerationConfig 中設定參數值。


// ...

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

Unity 目前尚未支援使用 Imagen,但這項功能即將推出,敬請期待!

如需各項參數的說明,請參閱本頁面下一節。

參數說明 (Imagen)

以下概略說明可用的參數 (如適用)。 如需參數及其值的完整清單,請參閱 Google Cloud 說明文件。

參數 說明 預設值
負面提示
negativePrompt
描述要從生成的圖片中省略的內容

imagen-3.0-generate-002 目前不支援這項參數。

---
結果數量
numberOfImages
每個要求傳回的生成圖片數量 Imagen 3 模型預設為一張圖片
顯示比例
aspectRatio
生成圖片的寬高比 預設為正方形 (1:1)
圖片格式
imageFormat
輸出選項,例如圖片格式 (MIME 類型) 和生成圖片的壓縮程度 預設 MIME 類型為 PNG
預設壓縮率為 75 (如果 MIME 類型設為 JPEG)
浮水印
addWatermark
是否要在生成的圖像中加入不可見的數位浮水印 (稱為 SynthID) 預設值為 Imagen 3 型號的 true
生成人物
personGeneration
是否允許模型生成人物 預設值取決於模型



控制內容生成的其他選項

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