本指南將說明如何使用 Vertex AI for Firebase SDK,直接在應用程式中呼叫 Vertex AI Gemini API。
事前準備
本指南假設您已熟悉如何使用 Android Studio 開發 Android 應用程式。
請確認您的開發環境和 Android 應用程式符合下列規定:
- Android Studio (最新版本)
- 您的 Android 應用程式必須指定 API 級別 21 以上版本。
(選用) 查看範例應用程式。
您可以快速試用 SDK,查看各種用途的完整實作方式,如果沒有自己的 Android 應用程式,也可以使用範例應用程式。如要使用範例應用程式,請將 SDK 連結至 Firebase 專案。
步驟 1:設定 Firebase 專案並將應用程式連結至 Firebase
如果您已經有 Firebase 專案,且有一個應用程式連結至 Firebase
在 Firebase 控制台中,前往「Build with Gemini」(使用 Gemini 建構) 頁面,然後按一下第二張資訊卡來啟動工作流程,協助您完成下列工作。如果您在控制台中看到 Vertex AI 的分頁,表示這些工作已完成。
升級專案來使用Blaze 即付即用定價方案。
為專案啟用下列兩個 API:
aiplatform.googleapis.com
和firebaseml.googleapis.com
。
請繼續進行本指南的下一步驟,將 SDK 加入應用程式。
如果您尚未有 Firebase 專案,且有一個應用程式連結至 Firebase
步驟 2:新增 SDK
設定好 Firebase 專案,並將應用程式連結至 Firebase 後 (請查看上一步),即可將 Vertex AI for Firebase SDK 新增至應用程式。
Android 版 Vertex AI for Firebase SDK (firebase-vertexai
) 提供 Vertex AI Gemini API 的存取權。
在模組 (應用程式層級) Gradle 設定檔 (例如 <project>/<app-module>/build.gradle.kts
) 中,新增 Vertex AI for Firebase SDK for Android 的依附元件:
Kotlin+KTX
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta02")
}
Java
針對 Java,您必須額外新增兩個程式庫。
dependencies {
// ... other androidx dependencies
// add the dependency for the Vertex AI for Firebase SDK for Android
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta02")
// 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:初始化 Vertex AI 服務和生成式模型
您必須先初始化 Vertex AI 服務和生成式模型,才能發出 API 呼叫。
Kotlin+KTX
以 Kotlin 來說,這個 SDK 中的方法為暫停函式,需要從協同程式範圍呼叫。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")
Java
針對 Java,這個 SDK 中的串流方法會從 Reactive Streams 程式庫傳回Publisher
類型。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
完成入門指南後,請瞭解如何選擇 Gemini 模型,以及 (選用) 符合您用途和應用程式的位置。
步驟 4:呼叫 Vertex AI Gemini API
現在,您已將應用程式連結至 Firebase、新增 SDK,並初始化 Vertex AI 服務和生成式模型了,接著可以呼叫 Vertex AI Gemini API。
您可以使用 generateContent()
,透過純文字提示要求生成文字:
Kotlin+KTX
以 Kotlin 來說,這個 SDK 中的方法為暫停函式,需要從協同程式範圍呼叫。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
val generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash")
// 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 = generativeModel.generateContent(prompt)
print(response.text)
Java
針對 Java,這個 SDK 中的方法會傳回ListenableFuture
。// Initialize the Vertex AI service and the generative model
// Specify a model that supports your use case
// Gemini 1.5 models are versatile and can be used with all API capabilities
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-1.5-flash");
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
// 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);
您還能做些什麼?
進一步瞭解 Gemini 模型
瞭解不同用途適用的模型及配額與定價。
試用 Gemini API 的其他功能
- 進一步瞭解如何透過純文字提示產生文字,包括如何串流回覆。
- 使用多模態提示 (包括文字、圖片、PDF、影片和音訊) 生成文字。
- 建構多輪對話 (即時通訊)。
- 使用函式呼叫將生成式模型連結至外部系統和資訊。
瞭解如何控管內容生成功能
您也可以使用 Vertex AI Studio 測試提示和模型設定。針對 Vertex AI for Firebase 使用體驗提供意見回饋