本页介绍了以下混合体验配置选项:
请确保您已完成构建混合体验入门指南。
设置推理模式
入门指南中的示例使用 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_HOSTED_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_HOSTED_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 提供商,以查看此页面上特定于提供商的内容和代码。 |
在向模型发送的每个请求中,您都可以同时发送模型配置,以控制模型如何生成回答。云端托管模型和设备端模型提供不同的配置选项(云端与设备端参数)。
对于云托管的模型,请直接在模型的配置中设置其配置。不过,对于设备端模型,请在 onDeviceConfig 中设置其配置。
该配置在实例的整个生命周期内保持不变。如果您想使用其他配置,请使用该配置创建新的 GenerativeModel 实例。
以下示例展示了在设置 PREFER_ON_DEVICE 推理模式时可使用的云端托管模型和设备端模型的配置:
Kotlin
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("MODEL_NAME",
// Config for cloud-hosted model
generationConfig = generationConfig {
temperature = 0.8f,
topK = 10
},
// Config for on-device model
onDeviceConfig = onDeviceConfig {
mode = InferenceMode.PREFER_ON_DEVICE,
temperature = 0.8f,
topK = 5
})
Java
// Config for cloud-hosted model
GenerationConfig generationConfig = new GenerationConfig.Builder()
.setTemperature(0.8f)
.setTopK(10)
.build();
// Config for on-device model
OnDeviceConfig onDeviceConfig = new OnDeviceConfig.Builder()
.setMode(InferenceMode.PREFER_ON_DEVICE)
.setTemperature(0.8f)
.setTopK(5)
.build();
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
"MODEL_NAME",
generationConfig,
onDeviceConfig
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);