在对模型的每次调用中,您都可以发送模型配置,以控制模型如何生成回答。每个模型都支持不同的配置选项,例如设置 maxOutputTokens 或针对思考、回答模态、图片、语音或语音活动检测 (VAD) 的专门配置。
在访问 Gemini 模型的大多数用例中,您可以使用 GenerationConfig 配置模型。但是,如果您要配置 Gemini Live API 模型,则应使用 LiveGenerationConfig。
本页面介绍了如何为 Gemini 模型设置配置,并提供了可用参数表。
跳转到 Gemini 配置 跳转到 Gemini Live API 配置
GenerationConfig(适用于 Gemini 模型)
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
为大多数 Gemini 生成模型设置 GenerationConfig,包括通用模型、图片生成 (Nano Banana) 模型和文字转语音 (TTS) 模型。
该配置会在 GenerativeModel 实例的生命周期内保持不变。如果您想使用其他配置,请创建并使用具有其他配置的新实例。
以下示例展示了一个非常基本的模型配置。如需了解其他选项(尤其是专用模型的选项),请参阅本页面后面的可用参数表。
Swift
在创建 GenerativeModel 实例的过程中,在 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 实例的过程中,在 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 实例的过程中,在 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 实例的过程中,在 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',
generationConfig: generationConfig,
);
// ...
Unity
在创建 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
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
);
LiveGenerationConfig(适用于 Gemini Live API 模型)
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
仅在使用 Gemini Live API 模型时设置 LiveGenerationConfig。
该配置会在 LiveModel 实例的生命周期内保持不变。如果您想使用其他配置,请创建并使用具有其他配置的新实例。
以下示例展示了一个非常基本的模型配置。如需了解其他选项(例如配置语音活动检测 [VAD]),请参阅本页面下文中的可用参数表。
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
);
// ...
可用的模型参数
下表简要概述了可用于模型配置的参数。某些参数仅适用于特定模型。
| 参数 | 说明 | 限制 | 默认值 |
|---|---|---|---|
音频时间戳audioTimestamp
|
为仅音频输入文件启用时间戳理解功能。 |
布尔值
仅在使用 |
false
|
输出 token 数量上限maxOutputTokens
|
指定回答中可生成的词元数量上限。
对于 |
--- | --- |
停止序列stopSequences
|
指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成内容。 |
仅在使用 不适用于 Nano Banana 或 TTS 模型。 |
--- |
| 专用配置 | |||
思考型thinkingConfig
|
控制模型在生成回答时的“思考过程”。
|
支持的值:请参阅思考文档 不适用于 TTS 模型。
Firebase AI Logic尚不支持为 Live API 模型设置 |
取决于模型 |
回答模态responseModalities
|
指定输出类型(例如文本、音频或图片)。 | 仅在使用 Nano Banana、TTS 和 Live API 模型时适用(且必需)。 | --- |
图片特征imageConfig
|
指定生成图片的宽高比和分辨率。 |
支持的值:请参阅配置图片生成 仅在使用 Nano Banana 模型时适用。 |
1:1 宽高比(方形) 1024x1024 分辨率 |
语音speechConfig
|
指定用于音频输出的语音。 |
仅在使用 TTS 模型和 Live API 模型时适用。 TTS 模型必须具有此参数。 |
Puck
|
语音活动检测 (VAD)realtimeInputConfig
|
配置语音活动检测 (VAD)、中断处理和轮次上下文覆盖范围,或停用自动 VAD。 |
支持的值:请参阅配置 VAD 仅在使用 Live API 模型时适用。 |
已启用 VAD 请参阅各项设置的默认值。 |
其他内容生成控制选项
- 详细了解提示设计,以便影响模型生成符合您特定需求的输出。
- 使用安全设置可调整收到可能被视为有害的回答(包括仇恨言论和露骨色情内容)的可能性。
- 设置系统指令,以引导模型的行为。此功能就像一段预置指令,在模型接收到最终用户的进一步指令之前添加。
- 将响应 schema 与提示一起传递,以指定特定的输出 schema。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签或标记时)。