在每次调用模型时,您都可以发送模型配置来控制模型如何生成回答。每个模型都提供不同的配置选项。
您还可以使用 Google AI Studio尝试提示和模型配置。跳转到 Gemini 配置选项 跳转到 Imagen 配置选项 (已废弃)
配置 Gemini 模型
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
本部分将向您介绍如何 设置配置以用于 Gemini 模型,并提供 每个参数的说明。
设置模型配置 (Gemini)
适用于一般 Gemini 用例的配置
配置在实例的生命周期内保持不变。如果您想使用其他配置,请使用该配置创建一个新的 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,
temperature: 0.9,
topP: 0.1,
topK: 16,
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")
temperature = 0.9f
topK = 16
topP = 0.1f
}
// 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");
configBuilder.temperature = 0.9f;
configBuilder.topK = 16;
configBuilder.topP = 0.1f;
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"],
temperature: 0.9,
topP: 0.1,
topK: 16,
};
// 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"],
temperature: 0.9,
topP: 0.1,
topK: 16,
);
// 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" },
temperature: 0.9f,
topK: 16,
topP: 0.1f
);
// 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 的配置
配置在实例的生命周期内保持不变。如果您想使用其他配置,请使用该配置创建一个新的 LiveModel 实例。
Swift
在初始化 LiveModel 实例期间,设置
liveGenerationConfig
中参数的值:
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
let config = LiveGenerationConfig(
temperature: 0.9,
topP: 0.1,
topK: 16,
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)
temperature = 0.9f
topK = 16
topP = 0.1f
}
// 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));
configBuilder.setTemperature(0.9f);
configBuilder.setTopK(16);
configBuilder.setTopP(0.1f);
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" },
},
},
temperature: 0.9,
topP: 0.1,
topK: 16,
};
// 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'),
temperature: 0.9,
topP: 0.1,
topK: 16,
);
// 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"),
temperature: 0.9f,
topK: 16,
topP: 0.1f
);
// 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 Developer API
| 参数 | 说明 | 默认值 |
|---|---|---|
音频时间戳audioTimestamp
|
一个布尔值,用于为仅音频输入 文件启用时间戳理解功能。 仅在使用 |
false |
候选回答数candidateCount
|
指定要返回的回答变体数量。对于每个 请求,您需要为所有候选回答的输出词元付费, 但只需为输入词元支付一次费用。 支持的值: 仅在使用 |
1 |
频次惩罚frequencyPenalty
|
控制在生成的回答中包含重复出现的词元的概率。
正值会惩罚生成的 文本中反复出现的词元,从而降低重复内容概率。 |
--- |
输出词元数上限maxOutputTokens |
指定回答中可生成的词元数量上限。 | --- |
存在性惩罚presencePenalty
|
控制在生成的回答中包含已出现的词元的概率。
正值会惩罚生成的文本中已存在的词元,从而增加生成更多样化内容的概率。 |
--- |
停止序列stopSequences
|
指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成 内容。 仅在使用
|
--- |
温度temperature
|
控制回答的随机程度。 温度越低,回答的确定性越高;温度越高,回答的多样性或创造性越高。 |
取决于模型 |
Top-KtopK
|
限制在
生成的内容中使用的概率最高的字词数量。 如果 Top-K 值为 1,表示下一个所选词元应是
模型词汇表的所有词元中概率最高的词元;
如果 Top-K 值为 n,则表示下一个词元应
从 n 个概率最高的词元(均基于设置的温度)中
选择。
|
取决于模型 |
Top-PtopP
|
控制生成内容的多样性。 系统会按照概率从最高(见上文的 Top-K)到最低的顺序选择词元,直到所选词元的概率总和等于 Top-P 的值。 |
取决于模型 |
回答模态responseModality
|
指定在使用 Live API 或 Gemini 模型使用原生多模态输出时串流输出的类型,例如文本、音频或图片。 仅在使用 Live API 模型或使用 能够进行多模态输出的 Gemini 模型时适用。 |
--- |
语音speechConfig
|
指定在使用 Live API 时用于流式音频输出的语音。 仅在使用 Live API 模型时适用。 |
Puck |
配置 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
|
有关您要在生成的图片中排除的内容的说明
|
--- |
结果数量
numberOfImages
|
为每个请求返回的生成图片数量 | 默认值为一张图片 |
宽高比
aspectRatio
|
生成图片的宽高比 | 默认值为方形 (1:1) |
图片格式
imageFormat
|
输出选项,例如生成图片的图片格式(MIME 类型)和 压缩级别 | 默认 MIME 类型为 PNG 默认压缩级别为 75(如果 MIME 类型设置为 JPEG) |
水印
addWatermark
|
是否向生成的图片添加不可见的数字水印(称为 SynthID) | 默认值为 true
|
人像生成
personGeneration
|
是否允许模型生成人物 | 默认值取决于模型 |
包含安全属性
includeSafetyAttributes
|
是否针对未经过滤的输入和输出在回答中启用安全
属性列表的四舍五入 Responsible AI 分数 安全属性类别:
|
默认值为 false |
控制内容生成的其他选项
- 详细了解 提示设计 ,以便影响模型生成符合您需求的输出。
- 使用 安全设置 调整收到可能被视为 有害的回答(包括仇恨言论和露骨色情内容)的可能性。
- 设置系统指令 以引导模型的行为。此功能就像一段序言,在模型接收到最终用户的进一步指令之前添加。
- 将 回答架构 与提示一起传递,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签或标记时)。