使用模型配置来控制回答

在对模型的每次调用中,您都可以发送模型配置,以控制模型如何生成回答。每个模型都支持不同的配置选项,例如设置 maxOutputTokens 或针对思考、回答模态、图片或语音的专用配置。

在访问 Gemini 模型的大多数使用情形中,您 可以使用 GenerationConfig 配置模型。不过,如果您要配置 Gemini Live API 模型,则需要使用 LiveGenerationConfig。

本页面介绍了如何设置 Gemini 模型的配置,并 提供了 每个参数的说明。

跳转到 Gemini 配置 跳转到 Gemini Live API 配置

GenerationConfig 模型專用 Gemini

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

为大多数 Gemini 模型设置 GenerationConfig,包括通用模型、图片生成模型(“Nano Banana”模型)和文字转语音 (TTS) 模型。

该配置在 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.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
let config = GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: ["red"]
)

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .agentPlatform()).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.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
val config = generationConfig {
    maxOutputTokens = 200
    stopSequences = listOf("red")
}

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.agentPlatform()).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.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
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.agentPlatform())
                .generativeModel(
                    "GEMINI_MODEL_NAME",
                    config
                );
);

// ...

Web

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


// ...

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
const ai = getAI(firebaseApp, { backend: new AgentPlatformBackend() });

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
const generationConfig = {
  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.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
final generationConfig = GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: ["red"],
);

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.agentPlatform().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.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
var generationConfig = new GenerationConfig(
  maxOutputTokens: 200,
  stopSequences: new string[] { "red" }
);

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
// Specify the config as part of creating the `GenerativeModel` instance.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.AgentPlatform());
var model = ai.GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

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

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

LiveGenerationConfig(适用于 Gemini Live API 模型)

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

仅在使用 Gemini Live API 模型时设置 LiveGenerationConfig。

该配置在 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: .agentPlatform(location: "us-central1")).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 = Voice("FENRIR"))
}

// Specify the config as part of creating the `LiveModel` instance.
val liveModel = Firebase.ai(backend = GenerativeBackend.agentPlatform(location = "us-central1")).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(new Voice("FENRIR")));

LiveGenerationConfig config = configBuilder.build();

// Specify the config as part of creating the `LiveModel` instance.
LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.agentPlatform("us-central1")).liveModel(
          "GEMINI_LIVE_MODEL_NAME",
          config
);

// ...

Web

在初始化 LiveGenerativeModel 实例期间,设置 LiveGenerationConfig 中参数的值:


// ...

// Initialize the Agent Platform Gemini API backend service (formerly Vertex AI).
const ai = getAI(firebaseApp, { backend: new AgentPlatformBackend("us-central1") });

// 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.agentPlatform(location: 'us-central1').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.AgentPlatform(location: "us-central1")).GetLiveModel(
  modelName: "GEMINI_LIVE_MODEL_NAME",
  liveGenerationConfig: config
);

// ...

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

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



参数说明

下表简要介绍了可用的参数。某些参数仅适用于特定模型。

参数 说明 限制 默认值
音频时间戳
audioTimestamp
为仅音频输入文件启用时间戳理解功能。

布尔值

仅在使用 GenerativeModel 配置且输入类型为仅音频文件时适用。

false
输出词元数上限
maxOutputTokens

指定回答中可生成的词元数量上限。

对于 gemini-4-argon,非流式传输请求的默认输出词元数上限和最大输出词元数上限为 32,768 个词元,流式传输请求的默认输出词元数上限和最大输出词元数上限为 65,536 个词元。请注意, Firebase AI Logic 尚不支持长解码,但 此功能即将推出!

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

仅在使用 GenerativeModel 配置时适用。

不适用于 图片模型(“Nano Banana”)或 TTS 模型。

---
专用配置
思考
thinkingConfig

控制模型在生成回答时的"思考过程"。

  • 使用 思考级别控制模型可以进行的思考量。
  • 控制是否包含思考摘要。

支持的值:请参阅 思考文档

不适用于 TTS 模型。

Firebase AI Logic 尚不支持为 Live API 模型设置 thinkingConfig。

取决于模型
回答模态
responseModalities
指定输出类型(例如文本、音频或图片)。 仅在使用 必需 时适用(且为必需)。 Gemini 图片模型(“Nano Banana”模型)、TTS 模型和 Live API 模型。 ---
图片特征
imageConfig
指定生成的图片的宽高比和分辨率。

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

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

宽高比为 1:1(方形)
分辨率为 1024x1024
语音
speechConfig
指定用于音频输出的语音。 仅在使用 TTS 模型和 Live API 模型时适用。

TTS 模型必须具有此参数。
Puck



控制内容生成的其他选项