在每次呼叫模型時,您可以一併傳送模型設定,藉此控制模型生成回覆的方式。每種型號提供不同的設定選項。
您也可以使用 Google AI Studio 測試提示和模型設定。設定 Gemini 模型
| 按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 | 
本節說明如何設定搭配 Gemini 模型使用的設定,並說明各項參數。
設定模型設定 (Gemini)
一般用途的設定
這項設定會在執行個體生命週期內維持不變。如要使用其他設定,請使用該設定建立新的 GenerativeModel 執行個體。
Swift
建立 GenerativeModel 執行個體時,請在 GenerationConfig 中設定參數值。
import FirebaseAI
// 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
建立 GenerativeModel 執行個體時,請在 GenerationConfig 中設定參數值。
// ...
// 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
建立 GenerativeModel 執行個體時,請在 GenerationConfig 中設定參數值。
// ...
// 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
建立 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.
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
在建立 GenerativeModel 執行個體時,設定 GenerationConfig 中的參數值。
// ...
// 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
建立 GenerativeModel 執行個體時,請在 GenerationConfig 中設定參數值。
// ...
// 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
在 LiveGenerativeModel 執行個體初始化期間,設定 liveGenerationConfig 中的參數值:
import FirebaseAI
// ...
// 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"),
)
// Initialize the Vertex AI Gemini API backend service
// Specify the config as part of creating the `LiveGenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_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
}
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_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.setResponseModalities(ResponseModality.AUDIO);
configBuilder.setSpeechConfig(new SpeechConfig(Voices.FENRIR));
configBuilder.setTemperature(0.9f);
configBuilder.setTopK(16);
configBuilder.setTopP(0.1f);
LiveGenerationConfig config = configBuilder.build();
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
LiveModelFutures model = LiveModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                    "GEMINI_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 generationConfig = {
  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 model = getLiveGenerativeModel(ai, {
  model: "GEMINI_MODEL_NAME",
  generationConfig,
});
// ...
Dart
在建立 LiveModel 例項時,設定 LiveGenerationConfig 中的參數值。
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
final generationConfig = LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: [ResponseModalities.audio],
  speechConfig: SpeechConfig(voiceName: 'Fenrir'),
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
);
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
final model = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_MODEL_NAME',
  config: generationConfig,
);
// ...
Unity
在建立 LiveModel 例項時,設定 LiveGenerationConfig 中的參數值。
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
var liveGenerationConfig = new LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: new [] { ResponseModality.Audio },
  speechConfig: SpeechConfig.UsePrebuiltVoice("Fenrir"),
  temperature: 0.9f,
  topK: 16,
  topP: 0.1f
);
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetLiveModel(
  modelName: "GEMINI_MODEL_NAME",
  liveGenerationConfig: liveGenerationConfig
);
如需各個參數的說明,請參閱本頁的下一節。
參數說明 (Gemini)
以下概略說明可用的參數 (如適用)。 如要查看參數及其值的完整清單,請參閱 Gemini Developer API 說明文件。
| 參數 | 說明 | 預設值 | 
|---|---|---|
| 音訊時間戳記 audioTimestamp | 布林值,可讓系統瞭解僅含音訊的輸入檔案的時間戳記。 僅適用於使用  | false | 
| 候選人數 candidateCount | 指定要傳回的回覆變體數量。每次要求時,系統會針對所有候選項目的輸出權杖收費,但只會針對輸入權杖收費一次。 支援的值: 僅適用於使用  | 1 | 
| 頻率懲罰 frequencyPenalty | 控制在生成的回覆中重複出現的權杖機率。 正值會懲罰在生成內容中重複出現的符記,降低重複內容的機率。 | --- | 
| 輸出詞元數量上限 maxOutputTokens | 指定回覆中可生成的詞元數量上限。 | --- | 
| 缺席處罰 presencePenalty | 控制在生成的回覆中加入已出現的權杖的機率。 正值會懲罰已出現在生成內容中的符記,提高生成更多樣化內容的機率。 | --- | 
| 停止序列 stopSequences | 指定字串清單,如果模型在回應中遇到其中一個字串,就會停止生成內容。 僅適用於使用  | --- | 
| 溫度 temperature | 控制回覆的隨機程度。 溫度越低,模型生成的回覆越具確定性;溫度越高,模型生成的回覆越多元或有創意。 | 視機型而定 | 
| Top-K topK | 限制生成內容中機率最高的字詞數量。 如果 Top-K 值為 1,代表下一個所選詞元是模型詞彙表的所有詞元中可能性最高者。如果 Top-K 值為n,則代表模型會依據設定的溫度參數,從可能性最高的 n 個詞元中選取下一個詞元。 | 視機型而定 | 
| Top-P topP | 控制生成內容的多樣性。 模型會按照機率最高 (請見上方的「Top-K」) 到最低的順序選取符記,直到所選符記的機率總和等於「Top-P」值。 | 視機型而定 | 
| 回覆方式 responseModality | 指定使用 Live API 或 Gemini 模型原生多模態輸出時的串流輸出類型,例如文字、音訊或圖片。 只有在使用 Live API 和  | --- | 
| 語音 speechConfig | 指定使用 Live API 時,串流音訊輸出內容所用的語音。 僅適用於使用 Live API 和  | 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
建立 ImagenModel 例項時,請在 ImagenGenerationConfig 中設定參數值。
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 | 描述要從生成的圖片中省略的內容 
 | --- | 
| 結果數量 numberOfImages | 每個要求傳回的生成圖片數量 | 預設為一張圖片 | 
| 顯示比例 aspectRatio | 生成圖片的寬高比 | 預設為正方形 (1:1) | 
| 圖片格式 imageFormat | 輸出選項,例如圖片格式 (MIME 類型) 和生成圖片的壓縮程度 | 預設 MIME 類型為 PNG 預設壓縮率為 75 (如果 MIME 類型設為 JPEG) | 
| 浮水印 addWatermark | 是否要在生成的圖片中加入不可見的數位浮水印 (稱為 SynthID) | 預設值為 true | 
| 生成人物 personGeneration | 是否允許模型生成人物 | 預設值取決於模型 | 
| 加入安全屬性 includeSafetyAttributes | 是否要針對未經過濾的輸入和輸出內容,在回覆中啟用安全屬性清單的四捨五入負責任 AI 分數 安全屬性類別:
           | 預設值為 false | 
控制內容生成作業的其他選項
- 進一步瞭解提示設計,以便影響模型,生成符合您需求的輸出內容。
- 使用安全性設定,調整收到可能有害回應的機率,包括仇恨言論和煽情露骨內容。
- 設定系統指令,引導模型行為。這項功能就像前言,您可以在模型接收任何來自使用者的進一步指令前新增前言。
- 連同提示傳遞回覆結構定義,指定特定輸出結構定義。這項功能最常用於產生 JSON 輸出內容,但也可以用於分類工作 (例如想讓模型使用特定標籤)。