本页介绍了以下混合体验配置选项:
请确保您已完成打造混合体验入门指南。
设置推理模式
快速入门指南中的示例使用 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
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE)
)
Java
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
"MODEL_NAME",
new OnDeviceConfig(InferenceMode.PREFER_ON_DEVICE)
);
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
查找所有受支持的 Gemini 模型的模型名称。
使用模型配置来控制回答
|
点击您的 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);