在每次调用模型时,您都可以随附发送模型配置,以控制模型如何生成回答。每种型号都提供不同的配置选项。
您还可以使用 Google AI Studio 尝试不同的提示和模型配置。跳转到 Gemini 配置选项 跳转到 Imagen 配置选项
配置 Gemini 模型
| 点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 | 
本部分介绍如何设置配置以用于 Gemini 模型,并说明每个参数。
设置模型配置 (Gemini)
适用于一般使用场景的配置
该配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 GenerativeModel 实例。
Swift
在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。
import FirebaseAI
// 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 实例时,设置 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 实例时,设置 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 实例时,设置 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 实例时,设置 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
在初始化 LiveGenerativeModel 实例期间,设置 liveGenerationConfig 中参数的值:
import FirebaseAI
// ...
// 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"),
)
// Initialize the Vertex AI Gemini API backend service
// Specify the config as part of creating the `LiveGenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_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
}
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_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.setResponseModalities(ResponseModality.AUDIO);
configBuilder.setSpeechConfig(new SpeechConfig(Voices.FENRIR));
configBuilder.setTemperature(0.9f);
configBuilder.setTopK(16);
configBuilder.setTopP(0.1f);
LiveGenerationConfig config = configBuilder.build();
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
LiveModelFutures model = LiveModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                    "GEMINI_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 generationConfig = {
  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 model = getLiveGenerativeModel(ai, {
  model: "GEMINI_MODEL_NAME",
  generationConfig,
});
// ...
Dart
在创建 LiveModel 实例时,设置 LiveGenerationConfig 中参数的值。
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
final generationConfig = LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: [ResponseModalities.audio],
  speechConfig: SpeechConfig(voiceName: 'Fenrir'),
  temperature: 0.9,
  topP: 0.1,
  topK: 16,
);
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
final model = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_MODEL_NAME',
  config: generationConfig,
);
// ...
Unity
在创建 LiveModel 实例时,设置 LiveGenerationConfig 中参数的值。
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here)
var liveGenerationConfig = new LiveGenerationConfig(
  maxOutputTokens: 200,
  responseModalities: new [] { ResponseModality.Audio },
  speechConfig: SpeechConfig.UsePrebuiltVoice("Fenrir"),
  temperature: 0.9f,
  topK: 16,
  topP: 0.1f
);
// Initialize the Gemini Developer API backend service
// Specify the config as part of creating the `LiveModel` instance
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetLiveModel(
  modelName: "GEMINI_MODEL_NAME",
  liveGenerationConfig: liveGenerationConfig
);
您可以在本页的下一部分中找到每个参数的说明。
参数说明 (Gemini)
以下是可用参数的简要概览(如适用)。 您可以在 Gemini Developer API 文档中找到参数及其值的完整列表。
| 参数 | 说明 | 默认值 | 
|---|---|---|
| 音频时间戳 audioTimestamp | 一个布尔值,用于为仅音频输入文件启用时间戳理解功能。 仅在使用  | false | 
| 候选人数量 candidateCount | 指定要返回的响应变体数量。对于每个请求,您需要为所有候选词元的输出词元付费,但只需为输入词元支付一次费用。 支持的值: 仅在使用  | 1 | 
| 频次惩罚 frequencyPenalty | 控制在生成的回答中包含重复出现的 token 的概率。 正值会惩罚生成的文本中反复出现的 token,从而降低重复内容概率。 | --- | 
| 输出词元数上限 maxOutputTokens | 指定回答中可生成的 token 数量上限。 | --- | 
| 存在性惩罚 presencePenalty | 控制在生成的回答中包含已出现的 token 的概率。 正值会惩罚生成的文本中已存在的 token,从而增加生成更多样化内容的概率。 | --- | 
| 停止序列 stopSequences | 指定一个字符串列表,告知模型在回答中遇到其中一个字符串时,停止生成内容。 仅在使用  | --- | 
| 温度 temperature | 控制回答的随机性。 较低的温度会产生更具确定性的回答,而较高的温度会产生更具多样性或创造性的回答。 | 取决于模型 | 
| Top-K topK | 限制生成的内容中使用的最高概率字词的数量。 如果 top-K 值设为 1,表示下一个所选 token 应是模型词汇表中所有 token 中概率最高的 token;如果 top-K 值设为n,表示下一个 token 应从概率最高的 n 个 token 中选择(所有这些都基于设置的温度)。 | 取决于模型 | 
| Top-P topP | 控制生成内容的多样性。 系统会按照概率从最高(见上文中的 top-K)到最低的顺序选择 token,直到所选 token 的概率总和等于 top-P 的值。 | 取决于模型 | 
| 回答模态 responseModality | 指定使用 Live API 或 Gemini 模型提供的原生多模态输出时,流式输出的类型,例如文本、音频或图片。 仅在使用 Live API 和  | --- | 
| 语音 speechConfig | 指定使用 Live API 时用于流式音频输出的语音。 仅在使用 Live API 和  | Puck | 
配置 Imagen 模型
| 点击您的 Imagen API 提供商,以查看此页面上特定于提供商的内容和代码。 | 
本部分介绍如何设置配置以用于 Imagen 模型,并说明每个参数。
设置模型配置 (Imagen)
该配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 ImagenModel 实例。
Swift
在创建 ImagenModel 实例时,设置 ImagenGenerationConfig 中参数的值。
import FirebaseAI
// 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 输出,但也可用于分类任务(例如,当您希望模型使用特定标签时)。