本指南說明如何開始使用所選平台的 Vertex AI in Firebase SDK,直接從應用程式呼叫 Vertex AI Gemini API。
其他與 Gemini API 相關的選項
您可以選擇嘗試 Gemini API
的「Google AI」替代版本,使用 Google AI Studio 和 Google AI 用戶端 SDK 取得免付費存取權 (在限制範圍內,且適用於特定地區)。這些 SDK 應僅用於行動應用程式和網頁應用程式中的原型設計。熟悉 Gemini API 的運作方式後,請遷移至我們的 Vertex AI in Firebase SDK (本說明文件),其中提供許多對行動和網頁應用程式重要的額外功能,例如使用 Firebase App Check 保護 API 免遭濫用,以及支援要求中的大型媒體檔案。
視情況呼叫 Vertex AI Gemini API 伺服器端 (例如使用 Python、Node.js 或 Go)
使用伺服器端 Vertex AI SDK、Genkit 或 Firebase Extensions 來處理 Gemini API。
請注意,您也可以使用這份指南,開始使用 Vertex AI in Firebase SDK 存取 Imagen 模型。
事前準備
本指南假設您熟悉使用 Xcode 為 Apple 平台 (例如 iOS) 開發應用程式。
請確認您的開發環境和 Apple 平台應用程式符合下列規定:
- Xcode 16.2 以上版本
- 應用程式指定 iOS 15 以上版本或 macOS 12 以上版本
(選用) 查看範例應用程式。
您可以快速試用 SDK、查看各種用途的完整實作方式,或使用範例應用程式 (如果您沒有自己的 Apple 平台應用程式)。如要使用範例應用程式,您必須將其連結至 Firebase 專案。
本指南假設您熟悉如何使用 Android Studio 開發 Android 應用程式。
請確認開發環境和 Android 應用程式符合下列規定:
- Android Studio (最新版本)
- 應用程式指定的目標 API 級別為 21 以上
(選用) 查看範例應用程式。
您可以快速試用 SDK、查看各種用途的完整實作方式,或在沒有自有 Android 應用程式時使用範例應用程式。如要使用範例應用程式,您必須將其連結至 Firebase 專案。
本指南假設您熟悉如何使用 Android Studio 開發 Android 應用程式。
請確認開發環境和 Android 應用程式符合下列規定:
- Android Studio (最新版本)
- 應用程式指定的目標 API 級別為 21 以上
(選用) 查看範例應用程式。
您可以快速試用 SDK、查看各種用途的完整實作方式,或在沒有自有 Android 應用程式時使用範例應用程式。如要使用範例應用程式,您必須將其連結至 Firebase 專案。
本指南假設您已熟悉使用 JavaScript 開發網路應用程式。本指南不受架構限制。
請確認開發環境和網頁應用程式符合下列規定:
- (選用) Node.js
- 新式網路瀏覽器
(選用) 查看範例應用程式。
您可以快速試用 SDK、查看各種用途的完整導入方式,或在沒有自有網頁應用程式時使用範例應用程式。如要使用範例應用程式,您必須將其連結至 Firebase 專案。
本指南假設您已熟悉如何使用 Flutter 開發應用程式。
請確認您的開發環境和 Flutter 應用程式符合下列規定:
- Dart 3.2.0 以上
(選用) 查看範例應用程式。
您可以快速試用 SDK、查看各種用途的完整實作方式,或在沒有自有 Flutter 應用程式時使用範例應用程式。如要使用範例應用程式,您必須將其連結至 Firebase 專案。
步驟 1:設定 Firebase 專案,並將應用程式連結至 Firebase
如果您已擁有 Firebase 專案和已連結至 Firebase 的應用程式
在 Firebase 控制台中,前往「Vertex AI」頁面。
按一下 Vertex AI in Firebase 資訊卡,即可啟動工作流程,協助您完成下列工作:
升級專案,採用即付即用 Blaze 定價方案。
在專案中啟用必要的 API (Vertex AI API 和 Vertex AI in Firebase API)。
請繼續參閱本指南的後續步驟,將 SDK 新增至應用程式。
如果您沒有 Firebase 專案和已連結至 Firebase 的應用程式
設定 Firebase 專案
登入 Firebase 控制台。
按一下「建立專案」,然後使用下列任一選項:
選項 1:在「建立專案」工作流程的第一個步驟中輸入新專案名稱,即可建立全新的 Firebase 專案 (以及其底層 Google Cloud 專案)。
選項 2:在現有的 Google Cloud 專案中「新增 Firebase」,方法是在「建立專案」工作流程的第一個步驟中,從下拉式選單中選取 Google Cloud 專案名稱。
請注意,在系統提示時,您不需要設定 Google Analytics 來使用 Vertex AI in Firebase SDK。
按一下 Vertex AI in Firebase 資訊卡,即可啟動工作流程,協助您完成下列工作:
升級專案,採用即付即用 Blaze 定價方案。
在專案中啟用必要的 API (Vertex AI API 和 Vertex AI in Firebase API)。
將應用程式連結至 Firebase
繼續在控制台的 Vertex AI in Firebase 設定工作流程中,將應用程式連結至 Firebase,其中包括以下工作:
透過 Firebase 專案註冊應用程式。
將 Firebase 設定檔 (
) 新增至應用程式。GoogleService-Info.plist
在本指南的後續步驟中,您將在應用程式中新增 Vertex AI in Firebase SDK,並完成使用 SDK 和 Gemini API 所需的初始化作業。
繼續在控制台的 Vertex AI in Firebase 設定工作流程中,將應用程式連結至 Firebase,其中包括以下工作:
透過 Firebase 專案註冊應用程式。
將 Firebase 設定檔 (
) 和google-services.json
Gradle 外掛程式新增至應用程式。google-services
在本指南的後續步驟中,您將在應用程式中新增 Vertex AI in Firebase SDK,並完成使用 SDK 和 Gemini API 所需的初始化作業。
繼續在控制台的 Vertex AI in Firebase 設定工作流程中,將應用程式連結至 Firebase,其中包括以下工作:
透過 Firebase 專案註冊應用程式。
將 Firebase 設定檔 (
) 和google-services.json
Gradle 外掛程式新增至應用程式。google-services
在本指南的後續步驟中,您將在應用程式中新增 Vertex AI in Firebase SDK,並完成使用 SDK 和 Gemini API 所需的初始化作業。
繼續在控制台的 Vertex AI in Firebase 設定工作流程中,將應用程式連結至 Firebase,其中包括以下工作:
透過 Firebase 專案註冊應用程式。
將 Firebase 設定物件新增至應用程式。
在本指南的後續步驟中,您將在應用程式中新增 Vertex AI in Firebase SDK,並完成使用 SDK 和 Gemini API 所需的初始化作業。
安裝必要的指令列工具:
如果您尚未安裝,請安裝 Firebase CLI。
執行下列指令,使用 Google 帳戶登入 Firebase:
firebase login
請從任何目錄執行下列指令,安裝 FlutterFire CLI:
dart pub global activate flutterfire_cli
將應用程式設為使用 Firebase:
使用 FlutterFire CLI 設定 Flutter 應用程式,以便連線至 Firebase。
在 Flutter 專案目錄中執行下列指令,即可啟動應用程式設定工作流程:
flutterfire configure
這個
flutterfire configure
工作流程有什麼功能?flutterfire configure
工作流程會執行以下操作:要求您選取 Flutter 應用程式支援的平台 (iOS、Android、網頁)。對於每個所選平台,FlutterFire CLI 都會在 Firebase 專案中建立新的 Firebase 應用程式。
您可以選擇使用現有的 Firebase 專案,或建立新的 Firebase 專案。如果您已在現有的 Firebase 專案中註冊應用程式,FlutterFire CLI 會根據目前的 Flutter 專案設定嘗試比對這些應用程式。
建立 Firebase 設定檔 (
firebase_options.dart
),並將其新增至 Flutter 應用程式的lib/
目錄。
在本指南的後續步驟中,您將在應用程式中新增 Vertex AI in Firebase SDK,並完成使用 SDK 和 Gemini API 所需的初始化作業。
步驟 2:新增 SDK
Firebase 專案設定完成,應用程式已連結至 Firebase (請參閱上一個步驟),現在您可以將 Vertex AI in Firebase SDK 新增至應用程式。
使用 Swift Package Manager 安裝及管理 Firebase 依附元件。
Vertex AI in Firebase 程式庫提供 API 存取權,可用於與 Gemini 和 Imagen 模型互動。這個程式庫是 Apple 平台專用的 Firebase SDK 的一部分 (firebase-ios-sdk
)。
在 Xcode 中保持開啟應用程式專案,然後依序點選「File」(檔案) 和「Add Packages」(新增 Package)。
系統提示時,請新增 Firebase Apple 平台 SDK 存放區:
https://github.com/firebase/firebase-ios-sdk
選取最新的 SDK 版本。
選取
FirebaseVertexAI
程式庫。
完成後,Xcode 就會自動開始在背景中解析並下載依附元件。
Android 版 Vertex AI in Firebase SDK (firebase-vertexai
) 提供 API 存取權,可用於與 Gemini 和 Imagen 模型互動。
在模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts
) 中,新增 Android 適用的 Vertex AI in Firebase 程式庫依附元件。建議您使用 Firebase Android BoM 來控制程式庫版本。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.13.0")) // Add the dependency for the Vertex AI in Firebase library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai") }
只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。
(替代做法) 不使用 BoM 新增 Firebase 程式庫依附元件
如果您選擇不使用 Firebase BoM,則必須在依附元件行中指定每個 Firebase 程式庫版本。
請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您使用 BoM 來管理程式庫版本,確保所有版本皆相容。
dependencies { // Add the dependency for the Vertex AI in Firebase library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai:16.4.0") }
Android 版 Vertex AI in Firebase SDK (firebase-vertexai
) 提供 API 存取權,可用於與 Gemini 和 Imagen 模型互動。
在模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts
) 中,新增 Android 適用的 Vertex AI in Firebase 程式庫依附元件。建議您使用 Firebase Android BoM 來控制程式庫版本。
針對 Java,您需要額外新增兩個程式庫。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.13.0")) // Add the dependency for the Vertex AI in Firebase library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai") // 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") }
只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。
(替代做法) 不使用 BoM 新增 Firebase 程式庫依附元件
如果您選擇不使用 Firebase BoM,則必須在依附元件行中指定每個 Firebase 程式庫版本。
請注意,如果您在應用程式中使用多個 Firebase 程式庫,強烈建議您使用 BoM 來管理程式庫版本,確保所有版本皆相容。
dependencies { // Add the dependency for the Vertex AI in Firebase library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-vertexai:16.4.0") }
Vertex AI in Firebase 程式庫提供 API 存取權,可用於與 Gemini 和 Imagen 模型互動。這個程式庫是 Firebase JavaScript SDK for Web 的一部分。
使用 npm 安裝適用於網頁的 Firebase JS SDK:
npm install firebase
在應用程式中初始化 Firebase:
import { initializeApp } from "firebase/app"; // TODO(developer) Replace the following with your app's Firebase configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize FirebaseApp const firebaseApp = initializeApp(firebaseConfig);
Flutter 的 Vertex AI in Firebase 外掛程式 (firebase_vertexai
) 提供 API 存取權,可用於與 Gemini 和 Imagen 模型互動。
在 Flutter 專案目錄中執行下列指令,安裝核心外掛程式和 Vertex AI in Firebase 外掛程式:
flutter pub add firebase_core && flutter pub add firebase_vertexai
在
lib/main.dart
檔案中,匯入 Firebase 核心外掛程式、Vertex AI in Firebase 外掛程式,以及您先前產生的設定檔:import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_vertexai/firebase_vertexai.dart'; import 'firebase_options.dart';
在
lib/main.dart
檔案中,使用設定檔匯出的DefaultFirebaseOptions
物件初始化 Firebase:await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );
重新建構 Flutter 應用程式:
flutter run
步驟 3:初始化 Vertex AI 服務並建立 GenerativeModel
例項
您必須先初始化 Vertex AI 服務,並建立 GenerativeModel
執行個體,才能發出任何 API 呼叫,並向 Gemini 模型傳送提示。
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-flash")
Publisher
類型。
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');
完成這份入門指南後,請瞭解如何選擇適合用途和應用程式的模型和 (選用) 位置。
步驟 4:向模型傳送提示要求
您已將應用程式連結至 Firebase、新增 SDK,並初始化 Vertex AI 服務和生成模型,因此可以向 Gemini 模型傳送提示要求。
您可以使用 generateContent()
,根據僅限文字提示要求產生文字:
import FirebaseVertexAI
// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()
// Create a `GenerativeModel` instance with a model that supports your use case
let model = vertex.generativeModel(modelName: "gemini-2.0-flash")
// Provide a prompt that contains text
let prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
val generativeModel = Firebase.vertexAI.generativeModel("gemini-2.0-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)
ListenableFuture
。// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
GenerativeModel gm = FirebaseVertexAI.getInstance()
.generativeModel("gemini-2.0-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);
import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(vertexAI, { model: "gemini-2.0-flash" });
// Wrap in an async function so you can use await
async function run() {
// Provide a prompt that contains text
const prompt = "Write a story about a magic backpack."
// To generate text output, call generateContent with the text input
const result = await model.generateContent(prompt);
const response = result.response;
const text = response.text();
console.log(text);
}
run();
import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Vertex AI service and create a `GenerativeModel` instance
// Specify a model that supports your use case
final model =
FirebaseVertexAI.instance.generativeModel(model: 'gemini-2.0-flash');
// Provide a prompt that contains text
final prompt = [Content.text('Write a story about a magic backpack.')];
// To generate text output, call generateContent with the text input
final response = await model.generateContent(prompt);
print(response.text);
你還可以做些什麼?
進一步瞭解支援的型號
瞭解可用於各種用途的模型,以及相關配額和價格。
試用其他功能
- 進一步瞭解如何透過文字提示產生文字,包括如何串流回應。
- 透過提示各種檔案類型 (例如圖片、PDF 檔案、影片和音訊) 產生文字。
- 建構多輪對話 (聊天)。
- 從文字和多模態提示產生結構化輸出內容 (例如 JSON)。
- 使用文字提示生成圖片。
- 使用 Gemini Live API 串流輸入和輸出 (包括音訊)。
- 使用函式呼叫,將生成模型連結至外部系統和資訊。
瞭解如何控管內容產生作業
- 瞭解提示設計,包括最佳做法、策略和提示範例。
- 設定模型參數,例如溫度參數和輸出符記數量上限 (適用於 Gemini),或顯示比例和人物生成 (適用於 Imagen)。
- 使用安全性設定,調整可能會收到有害回應的機率。
針對使用 Vertex AI in Firebase 的體驗提供意見回饋