從 Firebase SDK 中的 Vertex AI GA 版本,遷移至 Firebase AI Logic SDK


Firebase AI Logic 及其用戶端 SDK 先前稱為「Vertex AI in Firebase」。為了更準確地反映我們擴充的服務和功能 (例如,我們現在支援 Gemini Developer API!),我們將服務重新命名並重新包裝為 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 APIVertex AI Gemini API

請查看兩個 Gemini API 供應商之間的差異,特別是支援的功能、價格和費率限制。舉例來說,Gemini Developer API 不支援使用 Cloud Storage 網址提供檔案,但如果您想利用免費層級和合理配額,這可能會是個不錯的選擇。

步驟 2:設定 Firebase 專案,以便使用 Gemini Developer API

只有在您想改用 Gemini Developer API 搭配 Firebase AI Logic 用戶端 SDK 時,才需要執行這個步驟。不過,如果您想繼續使用 Vertex AI Gemini API,請跳到下一個步驟。

請注意,您可以在專案中同時啟用兩個「Gemini API」供應商。

  1. 前往 Firebase 控制台的「Firebase AI Logic頁面。

  2. 前往「設定」分頁,然後選取 Gemini Developer API

  3. 啟用 Gemini Developer API

    控制台會確保已啟用必要的 API,並在 Firebase 專案中產生 Gemini API 金鑰。
    將此 Gemini API 金鑰新增至應用程式程式碼庫。 瞭解詳情

  4. 請繼續參閱本遷移指南,更新應用程式中的程式庫和初始化作業。

步驟 3:更新應用程式中使用的程式庫

更新應用程式的程式碼庫,以便使用 Firebase AI Logic 程式庫。

Swift

  1. 在 Xcode 中保持開啟應用程式專案,然後使用下列任一選項將 Firebase 套件更新至 11.13.0 以上版本:

    • 選項 1:更新所有套件:依序前往「File」>「Packages」>「Update to Latest Package Versions」

    • 選項 2:個別更新 Firebase:前往「Package Dependencies」專區,找到 Firebase 套件。在 Firebase 套件上按一下滑鼠右鍵,然後選取「Update Package」

  2. 確認 Firebase 套件現在顯示 v11.13.0 以上版本。如果不是,請確認您指定的套件需求是否允許更新至 11.13.0 以上版本。

  3. 在專案編輯器中選取應用程式目標,然後前往「Framework、Library 和嵌入內容」部分。

  4. 新增程式庫:選取「+」按鈕,然後從 Firebase 套件中新增 FirebaseAI

  5. 完成應用程式遷移作業後 (請參閱本指南的其餘部分),請務必移除舊的程式庫:
    選取 FirebaseVertexAI,然後按下 按鈕。

Kotlin

  1. 模組 (應用程式層級) 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 BoM
      implementation("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.14.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")
    }
  2. 將 Android 專案與 Gradle 檔案同步處理。

請注意,如果您選擇不使用 Firebase Android BoM,請新增 firebase-ai 程式庫的依附元件,並接受 Android Studio 建議的最新版本。

Java

  1. 模組 (應用程式層級) 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 BoM
      implementation("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.14.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")
    }
  2. 將 Android 專案與 Gradle 檔案同步處理。

請注意,如果您選擇不使用 Firebase Android BoM,請新增 firebase-ai 程式庫的依附元件,並接受 Android Studio 建議的最新版本。

Web

  1. 使用 npm 取得最新版的 Firebase JS SDK for Web:

    npm i firebase@latest

    yarn add firebase@latest
  2. 在匯入程式庫的所有位置,請更新匯入陳述式,改為使用 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

  1. 請在 Flutter 專案目錄中執行下列指令,更新為使用 pubspec.yaml 檔案中的 firebase_ai 套件:

    flutter pub add firebase_ai
  2. 重建 Flutter 專案:

    flutter run
  3. 完成應用程式遷移作業後 (請參閱本指南的其餘部分),請務必刪除舊套件:

    flutter pub remove firebase_vertexai

Unity

無法透過「Vertex AI in Firebase」支援 Unity。

瞭解如何開始使用 Unity 專用 Firebase AI Logic SDK

步驟 4:更新應用程式中的初始化

按一下您的 Gemini API 供應商,即可在本頁查看供應商專屬內容和程式碼。

更新您為所選 API 供應商初始化服務的方式,並建立 GenerativeModel 例項。

Swift


import FirebaseAI

// Initialize the Vertex AI Gemini API backend service
let ai = FirebaseAI.firebaseAI(backend: .vertexAI())

// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(modelName: "gemini-2.0-flash")

Kotlin


// Initialize the Vertex AI Gemini API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.vertexAI())
                        .generativeModel("gemini-2.0-flash")

Java


// Initialize the Vertex AI Gemini API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI())
        .generativeModel("gemini-2.0-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, VertexAIBackend } 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 Vertex AI Gemini API backend service
const ai = getAI(firebaseApp, { backend: new VertexAIBackend() });

// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(ai, { model: "gemini-2.0-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 Vertex AI Gemini API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
final model =
      FirebaseAI.vertexAI().generativeModel(model: 'gemini-2.0-flash');

Unity

無法透過「Vertex AI in Firebase」支援 Unity。

瞭解如何開始使用 Firebase AI Logic SDK for Unity

請注意,視您使用的功能而定,您可能不會一律建立 GenerativeModel 例項

步驟 5:根據您使用的功能更新程式碼

這個步驟會說明您可能需要根據所使用的功能進行哪些變更。

  • 如果您使用 Cloud Storage 網址 ,並在本次遷移中改用 Gemini Developer API,則需要更新多模態要求,將檔案納入內嵌資料 (或使用影片的 YouTube 網址)。

  • 請查看下列清單,瞭解您可能需要在程式碼中進行哪些變更,以便採用 Firebase AI Logic SDK。

Swift

沒有其他變更。

Kotlin

  • Live API

    • 已移除列舉類別 ResponseModalityUNSPECIFIED 值。改用 null

Java

  • Live API

    • 已移除列舉類別 ResponseModalityUNSPECIFIED 值。改用 null
  • 變更各種 Java 建構函式方法,現在會正確傳回其類別的例項,而非 void。

Web

只有在您開始使用 Gemini Developer API (而非 Vertex AI Gemini API) 時,才需要進行變更:

  • 安全設定

    • 移除不支援的 SafetySetting.method 用法。
  • 內嵌資料

    • 移除不支援的 InlineDataPart.videoMetadata 用法。

Dart

沒有其他變更。

Unity

Vertex AI in Firebase」不支援 Unity。

瞭解如何開始使用 Unity 專用 Firebase AI Logic SDK


針對使用 Firebase AI Logic 的體驗提供意見回饋