和用戶端 SDK 先前稱為「Vertex AI in Firebase」。為更準確反映我們擴展的服務和功能 (例如,我們現在支援 Gemini Developer API!),我們將服務重新命名並重新封裝為 Firebase AI Logic。Firebase AI Logic
如要直接從行動或網頁應用程式安全地存取 Google 的生成式 AI 模型,現在可以選擇「Gemini API」供應商,包括長期可用的 Vertex AI Gemini API 或現在的 Gemini Developer API。也就是說,您現在可以選擇使用 Gemini Developer API,這項服務提供免付費層級,並設有合理的速率限制和配額。
遷移至 Firebase AI Logic SDK 的步驟總覽
步驟 1:為應用程式和用途選擇最合適的「Gemini API」供應商。
步驟 2:設定 Firebase 專案,以便使用 Gemini Developer API。
僅適用於改用 Gemini Developer API 而非 Vertex AI Gemini API 的情況。步驟 3:更新應用程式中使用的程式庫。
步驟 4:更新應用程式中的初始化設定。
步驟 5:根據您使用的功能更新程式碼。
步驟 1:為應用程式選擇最合適的「Gemini API」供應商
遷移時,您可以選擇「Gemini API」供應商:
舊版「Vertex AI in Firebase」SDK 只能使用 Vertex AI Gemini API。
透過新的 Firebase AI Logic SDK,您可以選擇要從行動或網頁應用程式直接呼叫的「Gemini API」供應商,也就是 Gemini Developer API 或 Vertex AI Gemini API。
請查看使用這兩個Gemini API供應商的差異,特別是支援的功能、價格和速率限制。舉例來說,Gemini Developer API 不支援使用 Cloud Storage 網址提供檔案,但如果您想善用免付費方案和合理配額,這或許是不錯的選擇。
如要繼續使用 Vertex AI Gemini API:
請跳過下一個步驟,直接前往更新應用程式中的程式庫, 然後按照本指南的其餘步驟操作。如要改用 Gemini Developer API:
請繼續下一個步驟,設定 Firebase 專案以使用該 API, 然後按照本指南的其餘內容操作。
步驟 2:設定 Firebase 專案,以便使用 Gemini Developer API
如果您想改用 Gemini Developer API 和 Firebase AI Logic 用戶端 SDK,才需要執行這個步驟。不過,如果想繼續使用 Vertex AI Gemini API,請跳到下一個步驟。
請注意,專案可以同時啟用這兩個「Gemini API」供應商。
前往 Firebase 控制台的「Firebase AI Logic」頁面。
前往「設定」分頁,然後選取 Gemini Developer API。
啟用 Gemini Developer API。
控制台會確保啟用必要的 API,並在 Firebase 專案中產生 Gemini API 金鑰。
請勿將這個 Gemini API 金鑰加入應用程式的程式碼集。 瞭解詳情。請繼續閱讀本遷移指南,瞭解如何更新應用程式中的程式庫和初始化作業。
步驟 3:更新應用程式中使用的程式庫
更新應用程式的程式碼集,使用 Firebase AI Logic 程式庫。
Swift
在 Xcode 中保持開啟應用程式專案,然後使用下列其中一種做法,將 Firebase 軟體包更新至 11.13.0 以上版本:
選項 1:更新所有套件:依序前往「File」>「Packages」>「Update to Latest Package Versions」。
方法 2:個別更新 Firebase:前往「Package Dependencies」(套件依附元件) 區段中的 Firebase 套件。在 Firebase 套件上按一下滑鼠右鍵,然後選取「Update Package」。
確認 Firebase 套件現在顯示 11.13.0 以上版本。如果沒有,請確認您指定的套件需求允許更新至 11.13.0 以上版本。
在專案編輯器中選取應用程式的目標,然後前往「Frameworks, Libraries, and Embedded Content」部分。
新增程式庫:選取「+」按鈕,然後從 Firebase 套件新增「FirebaseAI」。
完成應用程式遷移作業後 (請參閱本指南的其餘章節),請務必移除舊程式庫:
選取「FirebaseVertexAI」,然後按下「—」按鈕。
Kotlin
在模組 (應用程式層級) Gradle 檔案 (通常為
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
) 中,將舊版依附元件 (如適用) 替換為下列項目。請注意,刪除舊版依附元件前,遷移應用程式的程式碼集可能比較容易 (請參閱本指南的其餘章節)。
// BEFORE dependencies { implementation(platform("com.google.firebase:firebase-bom:33.x.y"))
implementation("com.google.firebase:firebase-vertexai")// OR if not using the BoMimplementation("com.google.firebase:firebase-vertexai:16.x.y")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.16.0")) // Add the dependency for the Firebase AI Logic library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ai") }將 Android 專案與 Gradle 檔案同步。
請注意,如果您選擇不使用 Firebase Android BoM,只要新增 firebase-ai
程式庫的依附元件,並接受 Android Studio 建議的最新版本即可。
Java
在模組 (應用程式層級) Gradle 檔案 (通常為
<project>/<app-module>/build.gradle.kts
或<project>/<app-module>/build.gradle
) 中,將舊版依附元件 (如適用) 替換為下列項目。請注意,刪除舊版依附元件前,遷移應用程式的程式碼集可能比較容易 (請參閱本指南的其餘章節)。
// BEFORE dependencies { implementation(platform("com.google.firebase:firebase-bom:33.x.y"))
implementation("com.google.firebase:firebase-vertexai")// OR if not using the BoMimplementation("com.google.firebase:firebase-vertexai:16.x.y")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.16.0")) // Add the dependency for the Firebase AI Logic library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ai") }將 Android 專案與 Gradle 檔案同步。
請注意,如果您選擇不使用 Firebase Android BoM,只要新增 firebase-ai
程式庫的依附元件,並接受 Android Studio 建議的最新版本即可。
Web
使用 npm 取得最新版的 Firebase JS SDK for Web:
npm i firebase@latest
或
yarn add firebase@latest
請更新匯入程式庫的匯入陳述式,改為使用
firebase/ai
。請注意,刪除舊匯入項目之前,可能先遷移應用程式的程式碼集會比較容易 (請參閱本指南的其餘章節)。
// BEFORE import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";// AFTER import { initializeApp } from "firebase/app"; import { getAI, getGenerativeModel } from "firebase/ai";
Dart
在
pubspec.yaml
檔案中更新為使用firebase_ai
套件,方法是在 Flutter 專案目錄中執行下列指令:flutter pub add firebase_ai
重建 Flutter 專案:
flutter run
完成應用程式遷移作業後 (請參閱本指南的其餘章節),請務必刪除舊套件:
flutter pub remove firebase_vertexai
Unity
「Vertex AI in Firebase」不支援 Unity。
步驟 4:更新應用程式中的初始化設定
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
更新所選 API 供應商的服務初始化方式,並建立 GenerativeModel
例項。
Swift
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.5-flash")
Kotlin
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash")
Java
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("gemini-2.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// 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 Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.5-flash" });
Dart
import 'package:firebase_ai/firebase_ai.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
Unity
「Vertex AI in Firebase」不支援 Unity。
請注意,視您使用的功能而定,您可能不一定會建立 GenerativeModel
執行個體。
- 如要存取 Imagen 模型,請建立
ImagenModel
執行個體。
步驟 5:根據您使用的功能更新程式碼
本步驟說明您可能需要進行的變更,視您使用的功能而定。
如果您使用 Cloud Storage 網址並在這次遷移中改用 Gemini Developer API,則需要更新多模態要求,將檔案納入為內嵌資料 (或使用 YouTube 網址)。
請查看下列清單,瞭解程式碼中可能需要進行哪些變更,才能採用 Firebase AI Logic SDK。
Swift
沒有其他變更。
Kotlin
Live API
- 移除了列舉類別
ResponseModality
的UNSPECIFIED
值。請改用null
。
- 移除了列舉類別
Java
Live API
- 移除了列舉類別
ResponseModality
的UNSPECIFIED
值。請改用null
。
- 移除了列舉類別
變更各種 Java 建構工具方法,現在會正確傳回其類別的例項,而非 void。
Web
只有在開始使用 Gemini Developer API (而非 Vertex AI Gemini API) 時,才需要進行變更:
安全設定
- 移除不支援的
SafetySetting.method
用法。
- 移除不支援的
內嵌資料
- 移除不支援的
InlineDataPart.videoMetadata
用法。
- 移除不支援的
Dart
沒有其他變更。
Unity
「Vertex AI in Firebase」不支援 Unity。
提供有關 Firebase AI Logic 的使用體驗意見回饋