本頁說明混合式體驗的下列設定選項:
請確認您已完成建構混合式體驗的入門指南。
設定推論模式
入門指南中的範例使用 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 [Preview]」(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 的使用體驗意見