本頁說明混合式體驗的下列設定選項:
請務必完成建構混合式體驗的入門指南。
設定推論模式
入門指南中的範例使用 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 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
在傳送給模型的每個要求中,您可以一併傳送模型設定,藉此控制模型生成回覆的方式。雲端代管模型和裝置端模型提供不同的設定選項 (cloud 與 on-device 參數)。
如果是雲端託管模型,請直接在模型的設定中設定。不過,如果是裝置端模型,請在 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);