您可以使用 Firebase AI Logic 透過混合式推論建構 AI 技術輔助 Android 應用程式和功能。混合式推論功能可在裝置端模型可用時,使用該模型執行推論,否則會順暢地改用雲端主機模型 (反之亦然)。
本頁說明如何開始使用用戶端 SDK,並介紹其他設定選項和功能,例如溫度。
請注意,透過 Firebase AI Logic 進行裝置端推論適用於在特定裝置上執行的 Android 應用程式,並受機器學習套件條款和機器學習套件生成式 AI 相關條款規範。
建議用途和支援的功能
建議用途
使用裝置端模型進行推論的優點:
- 強化隱私權
- 查看當地特色
- 免付費推論
- 離線功能
使用混合功能提供:
- 配合裝置端模型可用性和網際網路連線,觸及更多目標對象
支援的裝置端推論功能
裝置端推論僅支援單輪文字生成 (不支援對話), 並提供串流或非串流輸出。支援的文字生成功能如下:
從文字和圖片輸入內容生成文字,具體來說,就是以單一點陣圖做為輸入內容
請務必查看本頁面底部的裝置端推論功能清單。
事前準備
請注意下列事項:
支援的 API:
雲端推論會使用您選擇的 Gemini API 提供者 (Gemini Developer API 或 Vertex AI Gemini API)。
裝置端推論作業會使用 ML Kit 的 Prompt API,這項 API 目前為測試版,僅適用於特定裝置。
本頁面說明如何開始使用。
完成這項標準設定後,請查看其他設定選項和功能 (例如設定溫度)。
支援的 Android 裝置和裝置端模型
如要瞭解裝置端推論 (使用 ML Kit 的 Prompt API),請參閱 ML Kit 說明文件中的支援裝置和裝置端模型清單。
開始使用
這些入門步驟說明傳送任何支援的提示要求時,所需的一般設定。
步驟 1:設定 Firebase 專案,並將應用程式連結至 Firebase
登入 Firebase 控制台,然後選取 Firebase 專案。
前往 Firebase 控制台的「Firebase AI Logic」頁面。
按一下「開始使用」,啟動導覽工作流程,協助您為專案設定必要 API 和資源。
設定專案以使用「Gemini API」供應商。
建議您先使用 Gemini Developer API。 您隨時可以 設定 Vertex AI Gemini API (以及帳單的相關規定)。
對於 Gemini Developer API,控制台會啟用必要的 API,並在專案中建立 Gemini API 金鑰。
請勿將這個 Gemini API 金鑰加入應用程式的程式碼庫。 瞭解詳情。如果控制台工作流程中出現提示,請按照畫面上的指示註冊應用程式並連結至 Firebase。
請繼續按照本指南的下一個步驟,將 SDK 新增至應用程式。
步驟 2:新增必要的 SDK
Firebase AI Logic Android 適用的 SDK (firebase-aifirebase-ai-ondevice
在模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts) 中,加入 Android 適用的 Firebase AI Logic 程式庫依附元件:
Kotlin
dependencies { // ... other androidx dependencies // Add the dependencies for the Firebase AI Logic libraries // Note that the on-device SDK is not yet included in the Firebase Android BoM implementation("com.google.firebase:firebase-ai:17.10.0") implementation("com.google.firebase:firebase-ai-ondevice:16.0.0-beta01") }
Java
如果是 Java,則需要新增兩個額外程式庫。
dependencies { // ... other androidx dependencies // Add the dependencies for the Firebase AI Logic libraries // Note that the on-device SDK is not yet included in the Firebase Android BoM implementation("com.google.firebase:firebase-ai:17.10.0") implementation("com.google.firebase:firebase-ai-ondevice:16.0.0-beta01") // Required for one-shot operations (to use `ListenableFuture` from Guava Android) implementation("com.google.guava:guava:31.0.1-android") // Required for streaming operations (to use `Publisher` from Reactive Streams) implementation("org.reactivestreams:reactive-streams:1.0.4") }
步驟 3:確認裝置端模型是否可用
使用
FirebaseAIOnDevice,檢查裝置端模型是否可用,如果不可用,請下載模型。
下載完成後,AICore 會自動更新模型。如要進一步瞭解 AICore 和管理裝置端模型下載作業,請參閱程式碼片段後的附註。
Kotlin
val status = FirebaseAIOnDevice.checkStatus()
when (status) {
OnDeviceModelStatus.UNAVAILABLE -> {
Log.w(TAG, "On-device model is unavailable")
}
OnDeviceModelStatus.DOWNLOADABLE -> {
FirebaseAIOnDevice.download().collect { status ->
when (status) {
is DownloadStatus.DownloadStarted ->
Log.w(TAG, "Starting download - ${status.bytesToDownload}")
is DownloadStatus.DownloadInProgress ->
Log.w(TAG, "Download in progress ${status.totalBytesDownloaded} bytes downloaded")
is DownloadStatus.DownloadCompleted ->
Log.w(TAG, "On-device model download complete")
is DownloadStatus.DownloadFailed ->
Log.e(TAG, "Download failed ${status}")
}
}
}
OnDeviceModelStatus.DOWNLOADING -> {
Log.w(TAG, "On-device model is being downloaded")
}
OnDeviceModelStatus.AVAILABLE -> {
Log.w(TAG, "On-device model is available")
}
}
Java
Checking for and downloading the model is not yet available for Java.
However, all other APIs and interactions in this guide are available for Java.
下載裝置端模型時,請注意下列事項:
下載裝置端模型所需的時間取決於多項因素,包括網路。
如果程式碼使用裝置端模型進行主要或備用推論,請確保在應用程式生命週期的早期下載模型,以便在使用者遇到應用程式中的程式碼之前,裝置端模型已可供使用。
如果系統在發出裝置端推論要求時無法使用裝置端模型,SDK 不會自動觸發下載裝置端模型。SDK 會改用雲端代管模型,或擲回例外狀況 (請參閱推論模式的行為詳細資料)。
AICore (Android 系統服務) 會為您管理下載的模型和版本,並保持模型更新等。請注意,裝置只會下載一個模型,因此如果裝置上的其他應用程式先前已成功下載裝置端模型,這項檢查就會傳回模型可用的結果。
延遲時間最佳化
如要針對第一次推論呼叫進行最佳化,應用程式可以呼叫 warmup()。這會將裝置端模型載入記憶體,並初始化執行階段元件。
步驟 4:初始化服務並建立模型例項
|
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
傳送提示要求給模型前,請先完成下列設定。
為所選 API 供應商初始化服務。
建立
GenerativeModel例項,並將mode設為下列其中一個值。這裡的說明非常概略,但您可以在「設定推論模式」中瞭解這些模式的行為詳細資料。PREFER_ON_DEVICE:嘗試使用裝置端模型;否則改用雲端代管模型。ONLY_ON_DEVICE:嘗試使用裝置端模型;否則,擲回例外狀況。PREFER_IN_CLOUD:嘗試使用雲端代管模型;否則改用裝置端模型。ONLY_IN_CLOUD:嘗試使用雲端代管模型;否則擲回例外狀況。
Kotlin
// Using this SDK to access on-device inference is an Experimental release and requires opt-in
@OptIn(PublicPreviewAPI::class)
// ...
// Initialize the Gemini Developer API backend service
// Create a GenerativeModel instance with a model that supports your use case
// Set the inference mode (like PREFER_ON_DEVICE to use the on-device model if available)
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel(
modelName = "MODEL_NAME",
onDeviceConfig = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE)
)
Java
// Initialize the Gemini Developer API backend service
// Create a GenerativeModel instance with a model that supports your use case
// Set the inference mode (like PREFER_ON_DEVICE to use the on-device model if available)
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
"MODEL_NAME",
new OnDeviceConfig(InferenceMode.PREFER_ON_DEVICE)
);
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
步驟 5:將提示要求傳送至模型
本節說明如何傳送各種輸入內容,生成不同類型的輸出內容,包括:
使用純文字輸入生成文字
| 嘗試這個範例之前,請先完成本指南的「開始使用」一節。 |
您可以使用 generateContent() 從含有文字的提示生成文字:
Kotlin
// Imports + initialization of Gemini API backend service + creation of model instance
// Provide a prompt that contains text
val prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
val response = model.generateContent(prompt)
print(response.text)
Java
// Imports + initialization of Gemini API backend service + creation of model instance
// Provide a prompt that contains text
Content prompt = new Content.Builder()
.addText("Write a story about a magic backpack.")
.build();
// To generate text output, call generateContent with the text input
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
請注意,Firebase AI Logic 也支援使用 generateContentStream (而非 generateContent) 串流傳輸文字回應。
根據文字和圖片 (多模態) 輸入內容生成文字
| 嘗試這個範例之前,請先完成本指南的「開始使用」一節。 |
您可以透過generateContent(),從包含文字和最多一個圖片檔 (僅限點陣圖) 的提示生成文字,並提供每個輸入檔案的 mimeType 和檔案本身。
Kotlin
// Imports + initialization of Gemini API backend service + creation of model instance
// Loads an image from the app/res/drawable/ directory
val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky)
// Provide a prompt that includes the image specified above and text
val prompt = content {
image(bitmap)
text("What developer tool is this mascot from?")
}
// To generate text output, call generateContent with the prompt
val response = model.generateContent(prompt)
print(response.text)
Java
// Imports + initialization of Gemini API backend service + creation of model instance
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky);
// Provide a prompt that includes the image specified above and text
Content content = new Content.Builder()
.addImage(bitmap)
.addText("What developer tool is this mascot from?")
.build();
// To generate text output, call generateContent with the prompt
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
請注意,Firebase AI Logic 也支援使用 generateContentStream (而非 generateContent) 串流傳輸文字回應。
你還能做些什麼?
您可以為混合式體驗使用各種額外的設定選項和功能:
目前尚未支援裝置端推論的功能
由於是實驗版,裝置端推論功能無法使用雲端模型的所有功能。
本節列出的功能尚未支援裝置端推論。如要使用上述任一項功能,建議採用 ONLY_IN_CLOUD 推論模式,獲得更一致的體驗。
產生結構化輸出內容 (例如 JSON 或列舉)
從點陣圖以外的圖片檔輸入類型生成文字 (載入記憶體的圖片)
從多個圖片檔生成文字
從音訊、影片和文件 (例如 PDF) 輸入內容生成文字
使用 Gemini 或 Imagen 模型生成圖片
在多模態要求中,使用網址提供檔案。您必須以內嵌資料的形式,將檔案提供給裝置端模型
傳送超過 4,000 個權杖 (或約 3,000 個英文字) 的要求。
多輪對話
為模型提供工具,協助生成回覆 (例如函式呼叫、程式碼執行、網址情境和以 Google 搜尋建立基準)
Firebase 控制台中的 AI 監控功能不會顯示任何裝置端推論資料 (包括裝置端記錄)。不過,使用雲端代管模型進行的任何推論,都可以透過 Firebase AI Logic 監控,就像其他推論一樣。
其他限制
除了上述限制外,裝置端推論還有以下限制 (詳情請參閱 ML Kit 說明文件):
應用程式的終端使用者必須使用支援的裝置,才能進行裝置端推論。
應用程式只能在前景執行時,在裝置上執行推論作業。
目前只有英文和韓文通過裝置端推論驗證。
整個裝置端推論要求的權杖數量上限為 4000 個。如果要求可能會超出這個限制,請務必設定可使用雲端代管模型的推論模式。
建議您避免使用需要長輸出內容 (超過 256 個符記) 的裝置端推論用途。
AICore (管理裝置端模型的 Android 系統服務) 會為每個應用程式強制執行推論配額。如果在短時間內發出過多 API 要求,系統會傳回
ErrorCode.BUSY回應。如果收到這項錯誤,請考慮使用指數輪詢重試要求。此外,如果應用程式超出長期配額 (例如每日配額),系統也會傳回ErrorCode.PER_APP_BATTERY_USE_QUOTA_EXCEEDED。
提供有關 Firebase AI Logic 的使用體驗意見回饋