使用模型配置来控制回答

在每次调用模型时,您都可以发送模型配置来控制模型如何生成回答。每个模型都提供不同的配置选项。

您还可以使用 Google AI Studio尝试提示和模型配置。

跳转到 Gemini 配置选项 跳转到 Imagen 配置选项 (已废弃)



配置 Gemini 模型

本部分将向您展示如何 设置配置以与 Gemini模型搭配使用,并提供 每个参数的说明

设置模型配置 (Gemini)

Gemini Live API

适用于一般 Gemini 用例的配置

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容 和代码。

配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 GenerativeModel 实例。

Swift

在创建 GenerativeModel 实例时,设置 a GenerationConfig 中参数的值。


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,
  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 实例时,设置 a 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")
}

// 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 实例时,设置 a 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");

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 实例时,设置 a 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"],
};

// 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"],
);

// 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 实例时,设置 a 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" }
);

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

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 中参数的值:


// ...

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

// ...

您可以在本页面的下一部分中找到每个参数的说明

参数说明 (Gemini)

下面简要介绍了可用的参数(如果适用)。如需查看参数及其值的完整列表,请参阅您选择的 Gemini API 提供方的文档:Gemini Developer APIVertex AI Gemini API

参数 说明 默认值
音频时间戳
audioTimestamp

一个布尔值,用于为仅音频输入 文件启用时间戳理解功能。

仅在使用 generateContentgenerateContentStream 调用且输入类型为仅音频文件时适用。

false
候选回答数量
candidateCount

指定要返回的回答变体数量。对于每个 请求,您需要为所有候选回答的输出词元付费, 但只需为输入词元支付一次费用。

支持的值:1 - 8(全包含)

仅在使用 generateContent 和 最新的 Gemini 模型时适用。
使用 generateContentStreamLive API 模型时不适用。

1
频次惩罚
frequencyPenalty
控制在生成的回答中包含重复出现的词元的概率。
正值会惩罚生成的 文本中反复出现的词元,从而降低重复内容概率。
---
输出词元数上限
maxOutputTokens
指定回答中可生成的词元数量上限。 ---
存在性惩罚
presencePenalty
控制在生成的回答中包含已出现的词元的概率。
正值会惩罚生成的文本中已存在的词元,从而增加生成更多样化内容的概率。
---
停止序列
stopSequences

指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成 内容。

仅在使用 GenerativeModel 配置时适用。

---
回答模态
responseModalities

指定输出类型(如文本、音频或图片)。

仅在使用 Gemini 图片模型 (如“Nano Banana”模型)或 Live API 模型时适用。

---
语音
speechConfig

指定用于音频输出的语音。

仅在使用音频生成模型时适用, 例如 Live API 模型。

Puck
图片特征
imageConfig

指定生成的图片的宽高比和分辨率。

支持的值:请参阅 配置图片生成

仅在使用 Gemini 图片模型 (如“Nano Banana”模型)时适用。

1:1 宽高比(方形)
1024x1024 分辨率

支持以下参数;但是, 不建议为 Gemini 3.x 及更高版本模型设置这些参数。 如需获得更具确定性的回答,请为您的特定用例定义包含明确规则的系统指令。

参数 说明 默认值
温度
temperature
控制回答的随机程度。
温度越低,回答的确定性越高;温度越高,回答的多样性或创造性越高。
取决于模型
Top-K
topK
限制在 生成的内容中使用的概率最高的字词数量。
如果 Top-K 值为 1,表示下一个所选词元应是 概率最高的词元(在模型词汇表的所有词元中); 如果 Top-K 值为 n,表示下一个词元应从 n 个概率最高的词元中选择 (全部基于设置的温度)。
取决于模型
Top-P
topP
控制生成内容的多样性。
系统会按照概率从最高(见上文的 Top-K)到最低的顺序选择词元,直到所选词元的概率总和等于 Top-P 的值。
取决于模型



配置 Imagen 模型

点击您的 Imagen API 提供商,以查看此页面上特定于提供商的内容 和代码。

本部分将向您展示如何 设置配置以与 Imagen模型搭配使用,并提供 每个参数的说明

设置模型配置 (Imagen)

配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 ImagenModel 实例。

Swift

在创建 ImagenModel 实例时,设置 ImagenGenerationConfig 中参数的值。


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

在创建 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
有关您要在生成的图片中省略的内容的说明

imagen-3.0-generate-002 尚不支持此参数。

---
结果数量
numberOfImages
为每个请求返回的生成的图片数量 默认值为一张图片
宽高比
aspectRatio
生成的图片的宽高比 默认值为方形 (1:1)
图片格式
imageFormat
输出选项,例如生成的图片的图片格式(MIME 类型)和 压缩级别 默认 MIME 类型为 PNG
默认压缩级别为 75(如果 MIME 类型设置为 JPEG)
水印
addWatermark
是否为生成的图片添加不可见的数字水印(称为 SynthID 默认值为 true
人像生成
personGeneration
是否允许模型生成人物 默认值取决于模型
包含安全属性
includeSafetyAttributes
是否针对未经过滤的输入和输出在回答中启用安全 属性列表的四舍五入 Responsible AI 分数

安全属性类别: "Death, Harm & Tragedy", "Firearms & Weapons", "Hate", "Health", "Illicit Drugs", "Politics", "Porn", "Religion & Belief", "Toxic", "Violence", "Vulgarity", "War & Conflict".

默认值为 false



控制内容生成的其他选项