本页介绍了以下混合体验配置选项:
请确保您已完成构建混合体验入门指南。
设置推理模式
入门指南中的示例使用 PREFER_ON_DEVICE 模式,但此模式只是四种可用的推理模式之一。
以下是可用的推理模式:
PREFER_ON_DEVICE:尝试使用设备端模型(如果可用且支持相应类型的请求)。否则,在设备上记录错误,然后自动回退到云托管模型。Kotlin
val config = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE)Java
InferenceMode mode = InferenceMode.PREFER_ON_DEVICE; OnDeviceConfig config = new OnDeviceConfig(mode);ONLY_ON_DEVICE:尝试使用设备端模型(如果可用且支持相应类型的请求)。否则,抛出异常。Kotlin
val config = OnDeviceConfig(mode = InferenceMode.ONLY_ON_DEVICE)Java
InferenceMode mode = InferenceMode.ONLY_ON_DEVICE; OnDeviceConfig config = new OnDeviceConfig(mode);PREFER_IN_CLOUD:如果设备在线且模型可用,则尝试使用云托管模型。如果设备处于离线状态,则回退到设备端模型。在所有其他失败情况下,抛出异常。Kotlin
val config = OnDeviceConfig(mode = InferenceMode.PREFER_IN_CLOUD)Java
InferenceMode mode = InferenceMode.PREFER_IN_CLOUD; OnDeviceConfig config = new OnDeviceConfig(mode);ONLY_IN_CLOUD:如果设备在线且模型可用,则尝试使用云托管模型。否则,抛出异常。Kotlin
val config = OnDeviceConfig(mode = InferenceMode.ONLY_IN_CLOUD)Java
InferenceMode mode = InferenceMode.ONLY_IN_CLOUD; OnDeviceConfig config = new OnDeviceConfig(mode);
确定是使用了设备端推理还是云端推理
如果您的推理模式为 PREFER_ON_DEVICE 或 PREFER_IN_CLOUD,那么了解给定请求使用了哪种模式可能会很有帮助。此信息由每个响应的 inferenceSource 属性提供。
当您访问此属性时,返回的值将为 ON_DEVICE 或 IN_CLOUD。
Kotlin
// ...
print("You used: ${result.response.inferenceSource}")
print(result.response.text)
Java
// ...
System.out.println("You used: " + result.getResponse().getInferenceSource());
System.out.println(result.getResponse().getText());
指定要使用的模型
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
您可以在创建 generativeModel 实例时指定要使用的模型(Kotlin | Java)。
指定云托管模型:
如果您的推理模式为
PREFER_ON_DEVICE、PREFER_IN_CLOUD或ONLY_IN_CLOUD,则必须明确指定要使用的云托管模型。SDK 没有默认的云托管模型。查找所有受支持的云端托管 Gemini 模型的模型名称。
指定设备端模型:
如果您的推理模式为
PREFER_ON_DEVICE、PREFER_IN_CLOUD或ONLY_ON_DEVICE,则可以选择性地在onDeviceConfig中指定要使用的设备端模型的“类别”。类别是发布阶段和性能特征的组合。支持的类别值如下所列。
AICore 会自动选择符合指定类别条件且受设备支持的设备端模型。例如,如果您指定PREVIEW,而设备是 Pixel 9,则系统可能会自动选择 Gemini Nano 4 Full [预览版] (nano-v4-full)。STABLE:最新的稳定版设备端模型。经过全面测试,可在消费类设备上使用。
例如 Gemini Nano 3 (
nano-v3) 或 Gemini Nano 2 (nano-v2)。如果未指定
OnDeviceModelOption,则设备端模型的默认设置。
PREVIEW:最新的预览版设备端模型,具有完整的性能。专为提供更强大的推理能力和处理复杂任务而设计。
例如,Gemini Nano 4 Full [预览版](
nano-v4-full,基于 Gemma 4 E4B)。
PREVIEW_FAST:最新的预览版快速设备端模型。经过优化,可实现最高速度和更低延迟时间。
例如,Gemini Nano 4 Fast [预览版](
nano-v4-fast,基于 Gemma 4 E2B)。
Kotlin
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
// Specify a cloud-hosted model.
// Required for `PREFER_ON_DEVICE`, `PREFER_IN_CLOUD`, and `ONLY_IN_CLOUD` inference modes.
modelName = "CLOUD_MODEL_NAME",
onDeviceConfig = OnDeviceConfig(
mode = InferenceMode.INFERENCE_MODE,
// (Optional) Specify an on-device model category.
// AICore will auto-select an on-device model based on this category.
// If not specified, AICore will auto-select the default stable on-device model.
modelOption = OnDeviceModelOption.ON-DEVICE_MODEL_CATEGORY)
)
Java
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
// Specify a cloud-hosted model.
// Required for `PREFER_ON_DEVICE`, `PREFER_IN_CLOUD`, and `ONLY_IN_CLOUD` inference modes.
"CLOUD_MODEL_NAME",
/* config = */ null,
/* safetySettings = */ null,
/* tools = */ null,
/* toolConfig = */ null,
/* systemInstruction = */ null,
/* requestOptions = */ new RequestOptions(),
new OnDeviceConfig(
/* mode = */ InferenceMode.INFERENCE_MODE,
/* maxOutputTokens = */ null,
/* temperature = */ null,
/* topK = */ null,
/* seed = */ null,
/* candidateCount = */ 1,
// (Optional) Specify an on-device model category.
// AICore will auto-select an on-device model based on this category.
// If not specified, AICore will auto-select the default stable on-device model.
/* modelOption = */ OnDeviceModelOption.ON-DEVICE_MODEL_CATEGORY)
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
使用模型配置来控制回答
|
点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
在向模型发送的每个请求中,您都可以随附模型配置,以控制模型如何生成回答。云端托管模型和设备端模型的配置方式不同,提供的配置选项也不同:
该配置在实例的整个生命周期内都会保持不变。如果您想使用其他配置,请使用该配置创建新的 GenerativeModel 实例。
以下示例展示了如何为云端托管模型和设备端模型设置配置,以便在设置 PREFER_ON_DEVICE 推理模式时使用:
Kotlin
// Config for cloud-hosted model
val generationConfig = generationConfig {
maxOutputTokens = 200
stopSequences = listOf("red")
}
// Config for on-device model
val onDeviceConfig = OnDeviceConfig(
mode = InferenceMode.PREFER_ON_DEVICE,
maxOutputTokens = 200
)
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "CLOUD_MODEL_NAME",
generationConfig = generationConfig,
onDeviceConfig = onDeviceConfig
)
Java
// Config for cloud-hosted model
GenerationConfig generationConfig = new GenerationConfig.Builder()
.setMaxOutputTokens(200)
.setStopSequences(List.of("red"))
.build();
// Config for on-device model
OnDeviceConfig onDeviceConfig = new OnDeviceConfig(
/* mode = */ InferenceMode.PREFER_ON_DEVICE,
/* maxOutputTokens = */ 200,
/* temperature = */ null,
/* topK = */ null,
/* seed = */ null,
/* candidateCount = */ 1,
/* modelOption = */ null
);
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
"CLOUD_MODEL_NAME",
generationConfig,
/* safetySettings = */ null,
/* tools = */ null,
/* toolConfig = */ null,
/* systemInstruction = */ null,
/* requestOptions = */ new RequestOptions(),
onDeviceConfig
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
就您使用 Firebase AI Logic 的体验提供反馈