在每次调用模型时,您都可以随附发送模型配置,以控制模型如何生成回答。每种模型都支持不同的配置选项,例如设置 maxOutputTokens 或用于思考、回答模态、图片或语音的专用配置。
在大多数用例中,当访问 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 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.
// 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 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.
// 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.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.
// 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 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.
// 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 Gemini Developer API backend service.
// 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
);
您可以在本页的下一部分中找到每个参数的说明。
您可以使用 Google AI 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: .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.agentPlatform()).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 中参数的值:
// ...
// 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 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
);
// ...
您可以在本页的下一部分中找到每个参数的说明。
您可以使用 Google AI Studio 尝试不同的提示和模型配置。参数说明
下表简要介绍了可用的参数。某些形参仅适用于特定模型。
| 参数 | 说明 | 限制 | 默认值 |
|---|---|---|---|
音频时间戳audioTimestamp
|
为仅音频输入文件启用时间戳理解功能。 |
布尔值
仅在以下情况下适用:使用 |
false
|
输出 token 数上限maxOutputTokens
|
指定可在回答中生成的令牌数量上限。 | --- | --- |
停止序列stopSequences
|
指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成内容。 |
仅在使用 不适用于图片模型(“Nano Banana”)或 TTS 模型。 |
--- |
| 专用配置 | |||
思考型thinkingConfig
|
控制模型在生成回答时的“思考过程”。
|
支持的值:请参阅思考文档 不适用于 TTS 模型。
Firebase AI Logic 尚不支持为 Live API 模型设置 |
取决于模型 |
响应模态responseModalities
|
指定输出类型(例如文本、音频或图片)。 | 仅在使用 Gemini 图片模型(“Nano Banana”模型)、TTS 模型和 Live API 模型时适用(且为必需)。 | --- |
图片特征imageConfig
|
指定所生成图片的宽高比和分辨率。 |
支持的值:请参阅配置图片生成 仅在使用 Gemini 图片模型(“Nano Banana”模型)时适用。 |
1:1 宽高比(方形) 1024x1024 分辨率 |
语音speechConfig
|
指定用于音频输出的语音。 |
仅在使用 TTS 模型和 Live API 模型时适用。 对于 TTS 模型是必需的。 |
Puck
|
控制内容生成的其他选项
- 详细了解提示设计,以便影响模型生成符合您需求的输出内容。
- 使用安全设置调整获得可能被视为有害的回答(包括仇恨言论和露骨色情内容)的可能性。
- 设置系统指令,以引导模型的行为。此功能就像一段序言,在模型接收到最终用户的进一步指令之前添加。
- 传递响应 schema以及提示,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签时)。