在每次调用模型时,您都可以发送模型配置,以控制模型如何生成回答。每个模型都支持不同的配置选项,例如设置
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
var generationConfig = new GenerationConfig(
candidateCount: 1,
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尝试提示和模型配置。参数说明
下表简要介绍了可用的参数。某些参数仅适用于特定模型。
请注意,最新的 Gemini 模型已弃用并 忽略了温度、Top-K 和 Top-P。
| 参数 | 说明 | 限制 | 默认值 |
|---|---|---|---|
音频时间戳audioTimestamp
|
为仅音频输入文件启用时间戳理解功能。 |
布尔值
仅在使用 |
false
|
候选回答数candidateCount
|
指定要返回的回答变体数量。对于每个请求, 您需要为所有候选回答的输出词元付费,但只需为输入词元支付一次费用。 |
支持的值:
仅在使用
不适用于使用 |
1
|
输出词元数上限maxOutputTokens
|
指定回答中可生成的词元数量上限。 | --- | --- |
频次惩罚frequencyPenalty
|
控制在生成的回答中包含重复出现的词元的概率。 正值会惩罚生成的文本中反复出现的词元,从而降低重复内容概率。 |
不适用于 TTS 模型。 | --- |
存在性惩罚presencePenalty
|
控制在生成的回答中包含已出现的词元的概率。 正值会惩罚生成的 文本中已存在的词元,从而增加生成更多样化 内容的概率。 |
不适用于 TTS 模型。 | --- |
停止序列stopSequences
|
指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成 内容。 |
仅在使用 不适用于 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
|
控制内容生成的其他选项
- 详细了解 提示设计 ,以便影响模型生成符合您需求的输出。
- 使用 安全设置 调整获得可能被视为 有害的回答(包括仇恨言论和露骨色情内容)的可能性。
- 设置系统指令 以引导模型的行为。此功能类似于您 在模型接收到最终用户的任何进一步指令之前添加的序言。
- 将 回答架构 与提示一起传递,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签时)。