使用模型配置来控制回答

在每次调用模型时,您都可以随调用一起发送模型配置,以控制模型如何生成回答。每种型号都提供不同的配置选项。

您还可以使用 Google AI Studio 尝试不同的提示和模型配置。

跳转到 Gemini 配置选项 跳转到 Imagen 配置选项



配置 Gemini 模型

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

本部分将介绍如何设置配置以用于 Gemini 模型,并说明每个参数

设置模型配置 (Gemini)

适用于一般使用场景的配置

该配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 GenerativeModel 实例。

Swift

在创建 GenerativeModel 实例时,设置 GenerationConfig 中参数的值。


import FirebaseAI

// Set parameter values in a `GenerationConfig` (example values shown here)
let config = GenerationConfig(
  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` (example values shown here)
val config = generationConfig {
    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` (example values shown here)
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
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` (example values shown here)
const generationConfig = {
  max_output_tokens: 200,
  stop_sequences: ["red"],
  temperature: 0.9,
  top_p: 0.1,
  top_k: 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` (example values shown here)
final generationConfig = GenerationConfig(
  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` (example values shown here)
var generationConfig = new GenerationConfig(
  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 的配置

参数说明 (Gemini)

以下是可用参数的简要概览(如适用)。 您可以在 Gemini Developer API 文档中找到参数及其值的完整列表

参数 说明 默认值
音频时间戳
audioTimestamp

一个布尔值,用于为仅音频输入文件启用时间戳理解功能。

仅在使用 generateContentgenerateContentStream 调用且输入类型为仅音频文件时适用。

false
频次惩罚
frequencyPenalty
控制在生成的回答中包含重复出现的 token 的概率。
正值会惩罚生成的文本中反复出现的 token,从而降低重复内容概率。
---
输出词元数上限
maxOutputTokens
指定回答中可生成的 token 数量上限。 ---
存在惩罚
presencePenalty
控制在生成的回答中包含已出现的 token 的概率。
正值会惩罚生成的文本中已存在的 token,从而增加生成更多样化内容的概率。
---
停止序列
stopSequences

指定一个字符串列表,告知模型在响应中遇到其中一个字符串时,停止生成内容。

仅在使用 GenerativeModel 配置时适用。

---
温度
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 APIGemini 模型提供的原生多模态输出时,流式输出的类型,例如文本、音频或图片。

仅当使用 Live APILiveModel 配置时,或者当使用能够进行多模态输出的 Gemini 模型时适用。

---
语音
speechConfig

指定使用 Live API 时用于流式音频输出的语音。

仅在使用 Live APILiveModel 配置时适用。

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

Unity 尚不支持使用 Imagen,但请稍后再回来看看!

您可以在本页的下一部分中找到每个参数的说明

参数说明 (Imagen)

以下是可用参数的简要概览(如适用)。您可以在 Google Cloud 文档中找到参数及其值的完整列表

参数 说明 默认值
负面提示
negativePrompt
有关您要在生成的图片中省略的内容的说明

imagen-3.0-generate-002 尚不支持此参数。

---
结果数量
numberOfImages
每个请求返回的生成图片数量 默认值为 1 张图片(适用于 Imagen 3 型号)
宽高比
aspectRatio
生成图片的宽高比 默认值为方形 (1:1)
图片格式
imageFormat
输出选项,例如图片格式 (MIME 类型) 和生成的图片的压缩级别 默认 MIME 类型为 PNG
默认压缩率为 75(如果 MIME 类型设置为 JPEG)
水印
addWatermark
是否向生成的图片添加不可见的数字水印(称为 SynthID 对于 Imagen 3 型号,默认值为 true
人称生成
personGeneration
是否允许模型生成人物 默认值取决于模型



控制内容生成的其他选项

  • 详细了解提示设计,以便影响模型生成符合您需求的输出内容。
  • 使用安全设置调整获得可能被视为有害的回答(包括仇恨言论和露骨色情内容)的可能性。
  • 设置系统指令,以引导模型的行为。此功能类似于您在模型接触到来自最终用户的任何进一步指令之前添加的序言。
  • 传递回答架构以及提示,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签时)。