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


Firebase AI Logic 及其用戶端 SDK 舊稱為「Vertex AI in Firebase」。為了更準確反映我們擴充的服務和功能 (例如,我們現在支援 Gemini Developer API!),我們重新命名並重新封裝服務,改名為 Firebase AI Logic。

如要直接從行動或網頁應用程式安全存取 Google 的生成式 AI 模型,現在可以選擇「Gemini API」供應商,包括長期提供的 Agent Platform Gemini API (formerly Vertex AI) 或現在的 Gemini Developer API。也就是說,您現在可以選擇使用 Gemini Developer API,該服務提供免付費方案,並設有合理的頻率限制和配額。

遷移至 Firebase AI Logic SDK 的步驟總覽

  • 步驟 1:根據應用程式和用途,選擇最合適的「Gemini API」供應商。

  • 步驟 2:設定 Firebase 專案,以便使用 Gemini Developer API。
    僅適用於從 Agent Platform Gemini API (formerly Vertex AI) 改用 Gemini Developer API 的情況。

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

  • 步驟 4:更新應用程式中的初始化作業。

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

步驟 1:為應用程式選擇最合適的「Gemini API」供應商

遷移時,您可以選擇「Gemini API」供應商:

  • 舊版「Vertex AI in Firebase」SDK 只能使用 Agent Platform Gemini API (formerly Vertex AI)。

  • 新的 Firebase AI Logic SDK 可讓您選擇要直接從行動或網頁應用程式呼叫哪個「Gemini API」供應商,包括 Gemini Developer API 或 Agent Platform Gemini API (formerly Vertex AI)。

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

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

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

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

  1. 在 Firebase 控制台,依序前往「AI Services」(AI 服務) >「 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 套件更新至 12.5.0 以上版本:

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

    • 方法 2:個別更新 Firebase:在「Package Dependencies」部分,前往 Firebase 套件。在 Firebase 套件上按一下滑鼠右鍵,然後選取「Update Package」。

  2. 確認 Firebase 套件現在顯示 12.5.0 以上版本。如果沒有,請確認指定的套件需求條件允許更新至 12.5.0 以上版本。

  3. 在專案編輯器中選取應用程式的目標,然後前往「Frameworks, Libraries, and Embedded Content」部分。

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

  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:35.1.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:35.1.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。

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

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

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

更新所選 API 供應商的服務初始化方式,並建立 GenerativeModel 執行個體。

Swift


import FirebaseAILogic

// Initialize the Agent Platform Gemini API backend service (fomerly Vertex AI).
let ai = FirebaseAI.firebaseAI(backend: .agentPlatform())

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

Kotlin


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

Java


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

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

Unity

「Vertex AI in Firebase」不支援 Unity。

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

請注意,視您使用的功能而定,您不一定會建立 GenerativeModel 執行個體。如要使用 Gemini Live API 串流輸入和輸出內容,請建立 LiveModel 執行個體。

步驟 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 (而非 Agent Platform Gemini API (formerly Vertex AI)) 時,才需要進行變更:

  • 安全性設定

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

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

Dart

沒有其他變更。

Unity

「Vertex AI in Firebase」不支援 Unity。

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


提供「Firebase AI Logic」使用體驗的意見回饋