本页面介绍了混合体验的以下配置选项:
请确保您已完成 构建混合体验的入门指南。
设置推理模式
入门指南中的示例使用 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 提供商,以查看此页面上特定于提供商的内容和代码。 |
在向模型发出的每个请求中,您都可以发送模型配置,以控制模型如何生成回答。云托管的模型和设备端模型 提供不同的配置选项 (云端 参数与 设备端 参数)。
对于云托管的模型,请直接在模型的配置中设置其配置。不过,对于设备端模型,请在
an
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);