本指南說明如何使用所選平台的 Firebase AI Logic 用戶端 SDK,直接從應用程式開始呼叫 Gemini API。
事前準備
Swift
本指南假設您熟悉如何使用 Xcode 開發 Apple 平台 (例如 iOS) 的應用程式。
請確認開發環境和 Apple 平台應用程式符合下列規定:
- Xcode 26.2 以上版本
- 應用程式指定 iOS 15 以上版本或 macOS 12 以上版本
Kotlin
本指南假設您熟悉如何使用 Android Studio 開發 Android 應用程式。
請確認開發環境和 Android 應用程式符合下列規定:
- Android Studio (最新版本)
- 應用程式的目標 API 級別為 21 以上
Java
本指南假設您熟悉如何使用 Android Studio 開發 Android 應用程式。
請確認開發環境和 Android 應用程式符合下列規定:
- Android Studio (最新版本)
- 應用程式的目標 API 級別為 21 以上
Web
本指南假設您已熟悉使用 JavaScript 開發網路應用程式。本指南與架構無關。
請確認開發環境和網頁應用程式符合下列規定:
- (選用) Node.js
- 新式網路瀏覽器
Dart
本指南假設您已熟悉如何使用 Flutter 開發應用程式。
請確認開發環境和 Flutter 應用程式符合下列規定:
- Dart 3.2.0 以上版本
Unity
本指南假設您熟悉使用 Unity 開發遊戲。
請確認開發環境和 Unity 遊戲符合下列規定:
- Unity 編輯器 2021 LTS 以上版本
查看實用資源
Swift
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果沒有自己的 Apple 平台應用程式,也可以使用快速入門導覽課程應用程式。如要使用快速入門導覽課程應用程式,請將其連結至 Firebase 專案。
觀看教學影片
這部影片示範如何開始使用 Firebase AI Logic,建構實際的 AI 輔助餐飲規劃應用程式,根據文字提示詞生成食譜。
您也可以下載並探索影片中應用程式的程式碼庫。
Kotlin
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果沒有自己的 Android 應用程式,也可以使用快速入門應用程式。如要使用快速入門應用程式,請將其連結至 Firebase 專案。
觀看教學影片
這部影片示範如何開始使用 Firebase AI Logic,建構實際的 AI 輔助餐飲規劃應用程式,根據文字提示詞生成食譜。
您也可以下載並探索影片中應用程式的程式碼庫。
Java
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果沒有自己的 Android 應用程式,也可以使用快速入門應用程式。如要使用快速入門應用程式,請將其連結至 Firebase 專案。
觀看教學影片
這部影片示範如何開始使用 Firebase AI Logic,方法是建構實際的 AI 輔助餐飲規劃應用程式,並根據文字提示生成食譜。*
您也可以下載並探索影片中應用程式的程式碼庫。
* 這部影片和應用程式採用 Kotlin,但仍可協助 Java 開發人員瞭解如何開始使用 Firebase AI Logic 的基本概念。
Web
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果沒有自己的網頁應用程式,也可以使用快速入門應用程式。如要使用快速入門應用程式,請將其連結至 Firebase 專案。
Dart
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果沒有自己的 Flutter 應用程式,也可以使用快速入門導覽課程應用程式。如要使用快速入門導覽課程應用程式,請將其連結至 Firebase 專案。
觀看教學影片
這部影片示範如何開始使用 Firebase AI Logic,建構實際的 AI 輔助餐飲規劃應用程式,根據文字提示詞生成食譜。
您也可以下載並探索影片中應用程式的程式碼庫。
Unity
試用快速入門應用程式
使用快速入門應用程式快速試用 SDK,並查看各種應用情境的完整實作方式。如果您沒有自己的 Unity 遊戲,可以使用快速入門應用程式。如要使用快速入門應用程式,請將其連結至 Firebase 專案。
步驟 1:設定 Firebase 專案並啟用 API
登入 Firebase 控制台,然後選取 Firebase 專案。
在 Firebase 控制台中,依序前往「AI Services」(AI 服務) >「AI Logic」(AI 邏輯)。
按一下「開始使用」,啟動導覽工作流程,協助您為專案設定必要 API 和資源。
如有提示,請按照畫面上的指示註冊應用程式,並將 Firebase 設定新增至應用程式。
系統要求選擇「Gemini API 供應商」時,建議選取 Gemini Developer API,即可快速開始使用,不必付費。
您隨時可以設定Vertex AI Gemini API(以及帳單相關規定)。
繼續工作流程,為 Firebase AI Logic 設定必要的 API 和相關聯的服務。
自 2026 年 7 月初起,工作流程的此階段會自動強制執行Firebase App CheckAI Logic,這是重要服務,可協助保護Gemini API,避免直接從應用程式存取。開始使用時 (請參閱本指南稍後的步驟),您需要為本機開發環境設定App Check 偵錯提供者,才能強制執行App Check。
請繼續閱讀本指南的下一個步驟,將必要 SDK 新增至應用程式。
步驟 2:新增必要的 SDK
設定 Firebase 專案並啟用必要 API (請參閱上一個步驟) 後,您現在可以將必要 SDK 新增至應用程式。
Swift
使用 Swift Package Manager 安裝及管理 Firebase 依附元件。 如有需要,請瞭解其他安裝選項。
Firebase AI Logic 程式庫提供 API,可與 Gemini 模型互動。這個程式庫是 Firebase Apple 平台 SDK (firebase-ios-sdk) 的一部分。
如果您已使用 Firebase,請確認 Firebase 套件為 12.5.0 以上版本。
在 Xcode 中保持開啟應用程式專案,然後依序點選「File」(檔案) 和「Add Package Dependencies」(新增套件依附元件)。
系統提示時,請新增 Firebase Apple 平台 SDK 存放區:
https://github.com/firebase/firebase-ios-sdk選取最新版 SDK。
選取
FirebaseAILogic程式庫和FirebaseAppCheck程式庫。
完成後,Xcode 會自動開始在背景中解析並下載依附元件。
Kotlin
Firebase AI Logic Android 版 SDK (firebase-ai) 提供 API 存取權,可與 Gemini 模型互動。
在模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts) 中,新增 Android 適用的 Firebase AI Logic 和 App Check 程式庫依附元件。建議使用 Firebase Android BoM 控制程式庫版本。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.15.0")) // Add the dependencies for the Firebase AI Logic and App Check libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ai") implementation("com.google.firebase:firebase-appcheck-debug") }
只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。
Java
Firebase AI Logic Android 版 SDK (firebase-ai) 提供 API 存取權,可與 Gemini 模型互動。
在模組 (應用程式層級) Gradle 檔案 (例如 <project>/<app-module>/build.gradle.kts) 中,新增 Android 適用的 Firebase AI Logic 和 App Check 程式庫依附元件。建議使用 Firebase Android BoM 控制程式庫版本。
如果是 Java,則需要新增兩個額外程式庫。
dependencies { // ... other androidx dependencies // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.15.0")) // Add the dependencies for the Firebase AI Logic and App Check libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-ai") implementation("com.google.firebase:firebase-appcheck-debug") // 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 程式庫版本。
Web
Firebase AI Logic 程式庫提供 API,可與 Gemini 模型互動。這個程式庫是 Firebase JavaScript SDK for Web 的一部分。
使用 npm 安裝適用於網頁的 Firebase JS SDK:
npm install firebase在應用程式中初始化 Firebase:
import { initializeApp } from "firebase/app"; import { initializeAppCheck, DebugProvider } from "firebase/app-check"; // 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);
Dart
Flutter 的 Firebase AI Logic 外掛程式 (firebase_ai) 可存取與 Gemini 模型互動的 API。
在 Flutter 專案目錄中執行下列指令,安裝 Firebase AI Logic 和 App Check 外掛程式:
flutter pub add firebase_ai firebase_app_check在
lib/main.dart檔案中,匯入 Firebase AI Logic 和 App Check 外掛程式:import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_ai/firebase_ai.dart'; import 'package:firebase_app_check/firebase_app_check.dart'; import 'firebase_options.dart';此外,請確認您已在
lib/main.dart檔案中,使用設定檔匯出的DefaultFirebaseOptions物件初始化 Firebase:await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, );重建 Flutter 應用程式:
flutter run
Unity
下載 Firebase Unity SDK,然後將 SDK 解壓縮到方便的位置。
Firebase Unity SDK 不會因平台而異。
在開啟的 Unity 專案中,依序前往「Assets」 >「Import Package」 >「Custom Package」。
在解壓縮的 SDK 中,選取
FirebaseAI和FirebaseAppCheck套裝組合。在「Import Unity Package」視窗中,按一下「Import」。
步驟 3:為本機開發設定 App Check 偵錯供應商
2026 年 7 月初起,在 Play 管理中心的引導式設定工作流程中,系統會自動強制執行 AI LogicFirebase App Check,以保護Gemini API。如要進行本機開發,您需要設定App Check 偵錯提供者,略過認證程序,同時維持App Check的強制執行狀態。
Swift
在模擬器中以互動方式執行應用程式時 (例如在本機開發期間),請按照下列步驟使用偵錯供應器:
在 Xcode 專案中,匯入
FirebaseAppCheck並在設定Firebase前,使用偵錯供應商工廠初始化 App Check。import SwiftUI import FirebaseCore import FirebaseAppCheck @main struct YourApp: App { init() { let providerFactory = AppCheckDebugProviderFactory() AppCheck.setAppCheckProviderFactory(providerFactory) FirebaseApp.configure() } var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }取得偵錯權杖:
在模擬器或測試裝置上啟動應用程式。
開啟 Xcode 控制台,然後尋找App Check偵錯權杖。 例如:
<Warning> [AppCheckCore][I-GAC004001] App Check debug token: '123a4567-b89c-12d3-e456-789012345678'.複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
向 App Check 註冊偵錯權杖:
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
Kotlin
以下說明如何在模擬器中以互動方式執行應用程式時使用偵錯供應器 (例如在本機開發期間):
在偵錯版本中,將 App Check 設定為使用偵錯供應器工廠:
Firebase.initialize(context = this) Firebase.appCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance(), )
取得偵錯權杖:
在模擬器或測試裝置上執行應用程式。
在記錄中尋找 App Check 偵錯權杖。例如:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
向 App Check 註冊偵錯權杖:
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
Java
以下說明如何在模擬器中以互動方式執行應用程式時使用偵錯供應器 (例如在本機開發期間):
在偵錯版本中,將 App Check 設定為使用偵錯供應器工廠:
FirebaseApp.initializeApp(/*context=*/ this); FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance(); firebaseAppCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance());
取得偵錯權杖:
在模擬器或測試裝置上執行應用程式。
在記錄中尋找 App Check 偵錯權杖。例如:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
向 App Check 註冊偵錯權杖:
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
Web
以下說明如何從 localhost 互動式執行應用程式時 (例如在本機開發期間),使用偵錯供應器:
在偵錯版本中,請先將
self.FIREBASE_APPCHECK_DEBUG_TOKEN設為true,再初始化 App Check,即可啟用偵錯模式。例如:self.FIREBASE_APPCHECK_DEBUG_TOKEN = true; initializeAppCheck(app, { /* App Check options */ });在本地端造訪網路應用程式,然後開啟瀏覽器的開發人員工具。在偵錯控制台中,您會看到偵錯權杖:
AppCheck debug token: "123a4567-b89c-12d3-e456-789012345678". You will need to safelist it in the Firebase console for it to work.向 App Check 註冊偵錯權杖:
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
Dart
iOS+
在模擬器中以互動方式執行應用程式時 (例如在本機開發期間),請按照下列步驟使用偵錯供應器:
初始化 Firebase 應用程式後,請立即使用偵錯供應商啟動 App Check:
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; // Import the firebase_app_check plugin import 'package:firebase_app_check/firebase_app_check.dart'; Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); await FirebaseAppCheck.instance.activate( // Set appleProvider to `AppleProvider.debug` appleProvider: AppleProvider.debug, ); runApp(App()); }在 Xcode 專案中啟用偵錯記錄功能:
- 開啟「Product」>「Scheme」>「Edit scheme」。
- 在左選單中選取「Run」,然後選取「Arguments」分頁標籤。
- 在「Arguments Passed on Launch」部分中,加入
-FIRDebugEnabled。
取得偵錯權杖:
使用 Xcode 開啟
ios/Runner.xcworkspace,然後在模擬器或測試裝置上執行應用程式。開啟 Xcode 控制台,然後尋找App Check偵錯權杖。 例如:
<Warning> [AppCheckCore][I-GAC004001] App Check debug token: '123a4567-b89c-12d3-e456-789012345678'.複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
向 App Check 註冊偵錯權杖:
Android
以下說明如何在模擬器中以互動方式執行應用程式時使用偵錯供應器 (例如在本機開發期間):
初始化 Firebase 應用程式後,請立即使用偵錯供應商啟動 App Check:
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; // Import the firebase_app_check plugin import 'package:firebase_app_check/firebase_app_check.dart'; Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); await FirebaseAppCheck.instance.activate( // Set androidProvider to `AndroidProvider.debug` androidProvider: AndroidProvider.debug, ); runApp(App()); }取得偵錯權杖:
在模擬器或測試裝置上執行應用程式。
在記錄中尋找 App Check 偵錯權杖。例如:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678複製權杖 (例如
123a4567-b89c-12d3-e456-789012345678)。
向 App Check 註冊偵錯權杖:
網頁版
以下說明如何從 localhost 互動式執行應用程式時 (例如在本機開發期間),使用偵錯供應器:
在
web/index.html檔案中,將self.FIREBASE_APPCHECK_DEBUG_TOKEN設為true,即可啟用偵錯模式:<body> <script> self.FIREBASE_APPCHECK_DEBUG_TOKEN = true; </script> ... </body>在本機執行網路應用程式,然後開啟瀏覽器的開發人員工具。在偵錯控制台中,您會看到偵錯權杖:
AppCheck debug token: "123a4567-b89c-12d3-e456-789012345678". You will need to safelist it in the Firebase console for it to work.這個權杖會儲存在本機瀏覽器中,只要您在同一部電腦上使用同一個瀏覽器,系統就會使用這個權杖。如要在其他瀏覽器或其他電腦上使用權杖,請將
self.FIREBASE_APPCHECK_DEBUG_TOKEN設為權杖字串,而非true。向 App Check 註冊偵錯權杖:
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
Unity
以下說明如何在模擬器中以互動方式執行應用程式時使用偵錯供應器 (例如在本機開發期間):
在 Firebase 控制台中建立偵錯權杖:
在應用程式的初始化程式碼中,加入下列內容:
using Firebase.AppCheck; void InitializeFirebase() { // Configure the Debug Provider factory with your debug token. DebugAppCheckProviderFactory.Instance.SetDebugToken("YOUR_DEBUG_TOKEN"); // Set App Check to use the debug provider factory FirebaseAppCheck.SetAppCheckProviderFactory( DebugAppCheckProviderFactory.Instance); // Proceed to initialize Firebase as normal }
如要進一步瞭解偵錯供應商 (包括如何取得新的偵錯權杖),請參閱官方 App Check 文件。
步驟 4:初始化服務並建立模型例項
|
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
將提示傳送至 Gemini 模型前,請先初始化所選 API 供應商的服務,並建立 GenerativeModel 例項。
Swift
import FirebaseAILogic
// 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-3.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-3.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-3.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-3.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-3.5-flash');
Unity
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case.
var model = ai.GetGenerativeModel(modelName: "gemini-3.5-flash");
請注意,視您使用的功能而定,您可能不一定會建立 GenerativeModel 執行個體。如要使用 Gemini Live API 串流輸入和輸出內容,請建立 LiveModel 例項。
此外,完成這份入門指南後,請瞭解如何為您的用途和應用程式選擇模型。
步驟 5:向模型傳送要求
您現在已完成設定,可以向 Gemini 模型傳送要求。
你可以使用 generateContent(),根據含有文字的提示生成文字:
Swift
import FirebaseAILogic
// 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-3.5-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.")
Kotlin
如果是 Kotlin,這個 SDK 中的方法是暫停函式,需要從 Coroutine 範圍呼叫。
// 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-3.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 = model.generateContent(prompt)
print(response.text)
Java
如果是 Java,這個 SDK 中的方法會傳回ListenableFuture。
// 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-3.5-flash");
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// 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);
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-3.5-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();
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-3.5-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);
Unity
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case.
var model = ai.GetGenerativeModel(modelName: "gemini-3.5-flash");
// Provide a prompt that contains text
var prompt = "Write a story about a magic backpack.";
// To generate text output, call GenerateContentAsync with the text input
var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
你還可以做些什麼?
進一步瞭解支援的機型
瞭解各種用途適用的模型,以及這些模型的配額和價格。
試試其他功能
- 進一步瞭解如何從純文字提示生成文字,包括如何串流回應。
- 透過各種檔案類型 (例如圖片、PDF、影片和音訊) 建立提示,生成文字。
- 建構多輪對話 (即時通訊)。
- 從文字和多模態提示生成結構化輸出內容 (例如 JSON)。
- 生成及編輯圖片: 使用文字和多模態提示生成及編輯圖片。
- 使用 Gemini Live API 串流輸入和輸出 (包括音訊)。
- 使用工具 (例如函式呼叫和透過 Google 搜尋建立基準),將 Gemini 模型連結至應用程式的其他部分,以及外部系統和資訊。
瞭解如何控管內容生成
您也可以使用 Google AI Studio 測試提示和模型設定,甚至取得生成的程式碼片段。提供有關 Firebase AI Logic 的使用體驗意見回饋