Firebase AI Logic و SDK های کلاینت آن قبلاً " Vertex AI in Firebase " نامیده میشدند. برای انعکاس بهتر خدمات و ویژگیهای توسعهیافته ما (به عنوان مثال، اکنون از Gemini Developer API پشتیبانی میکنیم!)، نام خدمات خود را به Firebase AI Logic تغییر دادیم و بستهبندی مجدد کردیم .
برای دسترسی ایمن به مدلهای هوش مصنوعی مولد گوگل مستقیماً از طریق برنامههای تلفن همراه یا وب خود، اکنون میتوانید یک ارائهدهنده « Gemini API » را انتخاب کنید - یا API پلتفرم عامل Gemini که مدتهاست در دسترس است (قبلاً Vertex AI) یا اکنون API توسعهدهنده Gemini . این بدان معناست که اکنون میتوانید از API توسعهدهنده Gemini استفاده کنید که یک سطح بدون هزینه با محدودیتها و سهمیههای معقول ارائه میدهد.
مروری بر مراحل مهاجرت به SDK های Firebase AI Logic
مرحله ۱ : بهترین ارائهدهنده «Gemini API» را برای برنامه و موارد استفاده خود انتخاب کنید.
مرحله ۲ : فعال کردن API های مورد نیاز
مرحله ۳ : کتابخانه مورد استفاده در برنامه خود را بهروزرسانی کنید.
مرحله ۴ : مقداردهی اولیه را در برنامه خود بهروزرسانی کنید.
مرحله ۵ : کد خود را بسته به ویژگیهایی که استفاده میکنید، بهروزرسانی کنید.
مرحله ۱ : بهترین ارائهدهنده «Gemini API» را برای برنامه خود انتخاب کنید
با این مهاجرت، شما میتوانید ارائهدهندهی « Gemini API » را انتخاب کنید:
SDK های قدیمی « Vertex AI in Firebase » فقط میتوانستند از API مربوط به Agent Platform Gemini (که قبلاً Vertex AI نام داشت) استفاده کنند.
کیتهای توسعه نرمافزار (SDK) جدید Firebase AI Logic به شما امکان میدهند انتخاب کنید که میخواهید کدام ارائهدهنده « Gemini API » را مستقیماً از برنامه تلفن همراه یا وب خود فراخوانی کنید - یا Gemini Developer API یا Agent Platform Gemini API (که قبلاً Vertex AI نام داشت) .
تفاوتهای بین استفاده از دو ارائهدهندهی API مربوط به Gemini را بررسی کنید، به خصوص از نظر ویژگیهای پشتیبانیشده، قیمتگذاری و محدودیتهای نرخ. فقط برای یک مثال، API توسعهدهندگان Gemini از ارائه فایلها با استفاده از URLهای Cloud Storage پشتیبانی نمیکند، اما اگر میخواهید از ردیف رایگان و سهمیهی معقول آن بهرهمند شوید، میتواند انتخاب خوبی باشد.
مرحله ۲ : فعال کردن API های مورد نیاز
مطمئن شوید که تمام APIهای مورد نیاز در پروژه Firebase شما فعال هستند تا بتوانید از ارائهدهنده « Gemini API » انتخابی خود استفاده کنید.
توجه داشته باشید که میتوانید هر دو ارائهدهنده API را همزمان در پروژه خود فعال کنید.
وارد کنسول Firebase شوید و سپس پروژه Firebase خود را انتخاب کنید.
در کنسول Firebase ، به بخش سرویسهای هوش مصنوعی > منطق هوش مصنوعی بروید.
برای شروع یک گردش کار هدایتشده که به شما در تنظیم APIها و منابع مورد نیاز پروژهتان کمک میکند، روی «شروع به کار» کلیک کنید.
ارائهدهندهی «Gemini API» را که میخواهید با Firebase AI Logic SDKs استفاده کنید، انتخاب کنید. در صورت تمایل، همیشه میتوانید ارائهدهندهی API دیگری را بعداً تنظیم و استفاده کنید.
رابط برنامهنویسی کاربردی Gemini - پرداخت هزینه اختیاری (در طرح قیمتگذاری بدون هزینه Spark موجود است)
گردش کار کنسول، APIهای مورد نیاز را فعال کرده و یک کلید API جمینی در پروژه شما ایجاد میکند.
این کلید API مربوط به Gemini را به کدبیس برنامه خود اضافه نکنید . اطلاعات بیشتر.رابط برنامهنویسی کاربردی پلتفرم عامل Gemini (که قبلاً Vertex AI نام داشت) - نیاز به پرداخت صورتحساب دارد (نیازمند طرح قیمتگذاری Blaze با قابلیت پرداخت در محل است)
گردش کار کنسول، APIهای مورد نیاز را در پروژه شما فعال میکند.
برای بهروزرسانی کتابخانه و مقداردهی اولیه در برنامه خود، در این راهنمای مهاجرت ادامه دهید.
مرحله ۳ : بهروزرسانی کتابخانه مورد استفاده در برنامه
کدبیس برنامه خود را برای استفاده از کتابخانه Firebase AI Logic بهروزرسانی کنید.
سویفت
در Xcode، در حالی که پروژه برنامه شما باز است، بسته Firebase خود را با استفاده از یکی از گزینههای زیر به نسخه ۱۱.۱۳.۰ یا بالاتر بهروزرسانی کنید:
گزینه ۱ : بهروزرسانی همه بستهها: به مسیر File > Packages > Update to Latest Package Versions بروید.
گزینه ۲ : بهروزرسانی جداگانه فایربیس: در بخشی به نام Package Dependencies به پکیج فایربیس بروید. روی پکیج فایربیس کلیک راست کنید و سپس Update Package را انتخاب کنید.
مطمئن شوید که بسته Firebase اکنون نسخه 11.13.0 یا بالاتر را نشان میدهد. اگر اینطور نیست، بررسی کنید که الزامات بستهبندی مشخصشده شما امکان بهروزرسانی به نسخه 11.13.0 یا بالاتر را فراهم میکند.
هدف برنامه خود را در ویرایشگر پروژه انتخاب کنید و سپس به بخش چارچوبها، کتابخانهها و محتوای جاسازیشده بروید.
کتابخانه جدید را اضافه کنید: دکمه + را انتخاب کنید و سپس FirebaseAI را از بسته Firebase اضافه کنید.
پس از اتمام انتقال برنامه خود (به بخشهای باقیمانده در این راهنما مراجعه کنید)، حتماً کتابخانه قدیمی را حذف کنید:
FirebaseVertexAI-Preview را انتخاب کنید و سپس دکمهی — را فشار دهید.
Kotlin
در فایل Gradle ماژول (سطح برنامه) خود (معمولاً
<project>/<app-module>/build.gradle.ktsیا<project>/<app-module>/build.gradle)، وابستگیهای قدیمی را (در صورت وجود) با موارد زیر جایگزین کنید.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف وابستگی قدیمی، آسانتر باشد.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }پروژه اندروید خود را با فایلهای Gradle همگامسازی کنید.
توجه داشته باشید که اگر تصمیم دارید از Firebase Android BoM استفاده نکنید، کافیست وابستگی مربوط به کتابخانه firebase-ai را اضافه کنید و آخرین نسخه پیشنهادی اندروید استودیو را بپذیرید.
Java
در فایل Gradle ماژول (سطح برنامه) خود (معمولاً
<project>/<app-module>/build.gradle.ktsیا<project>/<app-module>/build.gradle)، وابستگیهای قدیمی را (در صورت وجود) با موارد زیر جایگزین کنید.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف وابستگی قدیمی، آسانتر باشد.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }پروژه اندروید خود را با فایلهای Gradle همگامسازی کنید.
توجه داشته باشید که اگر تصمیم دارید از Firebase Android BoM استفاده نکنید، کافیست وابستگی مربوط به کتابخانه firebase-ai را اضافه کنید و آخرین نسخه پیشنهادی اندروید استودیو را بپذیرید.
Web
آخرین نسخه Firebase JS SDK برای وب را با استفاده از npm دریافت کنید:
npm i firebase@latest
یا
yarn add firebase@latest
هر جا که کتابخانه را وارد کردهاید، دستورات import خود را بهروزرسانی کنید تا به جای آن
firebase/aiاستفاده شود.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف ایمپورتهای قدیمی، آسانتر باشد.
// BEFORE import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";// AFTER import { initializeApp } from "firebase/app"; import { getAI, getGenerativeModel } from "firebase/ai";
Dart
با اجرای دستور زیر از دایرکتوری پروژه Flutter خود، به استفاده از بسته
firebase_aiدر فایلpubspec.yamlخود بهروزرسانی کنید:flutter pub add firebase_ai
پروژه فلاتر خود را بازسازی کنید:
flutter run
پس از اتمام انتقال برنامه خود (به بخشهای باقیمانده در این راهنما مراجعه کنید)، حتماً بسته قدیمی را حذف کنید:
flutter pub remove firebase_vertexai
وحدت
پشتیبانی از Unity از « Vertex AI در Firebase » در دسترس نبود.
یاد بگیرید که چگونه با Firebase AI Logic SDK برای Unity شروع به کار کنید .
مرحله ۴ : مقداردهی اولیه را در برنامه خود بهروزرسانی کنید
برای مشاهده محتوا و کد مخصوص ارائهدهنده در این صفحه، روی ارائهدهنده API Gemini خود کلیک کنید. |
نحوهی مقداردهی اولیهی سرویس برای ارائهدهندهی API انتخابی خود را بهروزرسانی کنید و یک نمونهی GenerativeModel ایجاد کنید.
سویفت
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.7-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.7-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.7-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.7-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.7-flash');
وحدت
پشتیبانی از Unity از « Vertex AI در Firebase » در دسترس نبود.
یاد بگیرید که چگونه با Firebase AI Logic SDK برای Unity شروع به کار کنید .
توجه داشته باشید که بسته به قابلیتی که استفاده میکنید، ممکن است همیشه یک نمونه GenerativeModel ایجاد نکنید . برای پخش ورودی و خروجی با استفاده از Gemini Live API ، یک نمونه LiveModel ایجاد کنید.
مرحله ۵ : کد خود را بسته به ویژگیهایی که استفاده میکنید، بهروزرسانی کنید
این مرحله تغییراتی را شرح میدهد که ممکن است بسته به ویژگیهایی که استفاده میکنید، مورد نیاز باشند.
اگر از آدرسهای اینترنتی Cloud Storage URLs) استفاده میکنید و در این انتقال، API توسعهدهندگان Gemini را جایگزین کردهاید، باید درخواستهای چندوجهی خود را بهروزرسانی کنید تا فایلها به عنوان دادههای درونخطی (inline data) لحاظ شوند (یا از آدرسهای اینترنتی یوتیوب برای ویدیوها استفاده کنید).
چندین تغییر برای نسخههای GA از SDK های " Vertex AI in Firebase " معرفی شده است. همین تغییرات برای استفاده از SDK های Firebase AI Logic نیز مورد نیاز است. لیستهای زیر را برای هرگونه تغییری که ممکن است برای تطبیق با SDK Firebase AI Logic در کد خود ایجاد کنید، مرور کنید.
مورد نیاز برای همه زبانها و پلتفرمها
فراخوانی تابع
اگر این ویژگی را قبل از GA پیادهسازی کردهاید، باید نحوه تعریف طرحواره خود را بهروزرسانی کنید. توصیه میکنیم برای یادگیری نحوه نوشتن اعلانهای تابع، راهنمای فراخوانی تابع بهروزرسانیشده را مرور کنید.تولید خروجی ساختاریافته (مانند JSON) با استفاده از
responseSchema
اگر این ویژگی را قبل از GA پیادهسازی کردهاید، باید نحوه تعریف طرحواره خود را بهروزرسانی کنید. توصیه میکنیم برای یادگیری نحوه نوشتن طرحوارههای JSON، راهنمای جدید خروجی ساختاریافته را مرور کنید.تایم اوت
- زمان پیشفرض برای درخواستها به ۱۸۰ ثانیه تغییر یافت.
مورد نیاز بر اساس پلتفرم یا زبان
سویفت
شمارشها
اکثر انواع
enumباstructها با متغیرهای استاتیک جایگزین شدند. این تغییر، انعطافپذیری بیشتری را برای تکامل API به روشی سازگار با نسخههای قبلی فراهم میکند. هنگام استفاده از دستوراتswitch، اکنون باید یک موردdefault:را برای پوشش مقادیر ناشناخته یا مدیریت نشده، از جمله مقادیر جدیدی که در آینده به SDK اضافه میشوند، لحاظ کنید.نام شمارش
BlockThresholdبهHarmBlockThresholdتغییر یافت؛ این نوع اکنون یکstructاست.موارد
unknownوunspecifiedاز شمارشهای زیر (که اکنونstructهستند) حذف شدند:HarmCategory،HarmBlockThreshold،HarmProbability،BlockReasonوFinishReason.شمارش
ModelContent.Partبا پروتکلی به نامPartجایگزین شد تا امکان اضافه کردن انواع جدید به روشی سازگار با نسخههای قبلی فراهم شود. این تغییر با جزئیات بیشتر در بخش قطعات محتوا توضیح داده شده است.
بخشهای محتوا
پروتکل
ThrowingPartsRepresentableحذف شد و مقداردهی اولیهModelContentسادهسازی شد تا از خطاهای گاه به گاه کامپایلر جلوگیری شود. تصاویری که به درستی کدگذاری نمیشوند، هنگام استفاده درgenerateContentهمچنان خطا میدهند.موارد
ModelContent.Partبا انواعstructزیر که مطابق با پروتکلPartهستند، جایگزین شدند:-
.textبهTextPart -
.dataبهInlineDataPart -
.fileDataبهFileDataPart -
.functionCallبهFunctionCallPart -
.functionResponseبهFunctionResponsePart
-
دسته بندی آسیب
- تغییر داده شد تا
HarmCategoryدیگر در نوعSafetySettingبه صورت تودرتو نباشد. اگر به آنSafetySetting.HarmCategoryاشاره میکنید، میتوان آن را باHarmCategoryجایگزین کرد.
- تغییر داده شد تا
بازخورد ایمنی
- نوع
SafetyFeedbackحذف شد، زیرا در هیچ یک از پاسخها استفاده نشده بود.
- نوع
فراداده استناد
- نام ویژگی
citationSourcesدرCitationMetadataبهcitationsتغییر یافت.
- نام ویژگی
کل کاراکترهای قابل پرداخت
- ویژگی
totalBillableCharactersدرCountTokensResponseبه صورت اختیاری تغییر یافت تا موقعیتهایی را که هیچ کاراکتری ارسال نمیشود، منعکس کند.
- ویژگی
پاسخ نامزد
- برای مطابقت با سایر پلتفرمها، نام
CandidateResponseبهCandidateتغییر یافت.
- برای مطابقت با سایر پلتفرمها، نام
پیکربندی تولید
- ویژگیهای عمومی
GenerationConfigبهinternalتغییر داده شد. همه آنها در مقداردهنده اولیه قابل تنظیم باقی میمانند.
- ویژگیهای عمومی
Kotlin
شمارشها
کلاسهای
enumو کلاسهایsealedبا کلاسهای معمولی جایگزین شدند. این تغییر، انعطافپذیری بیشتری را برای تکامل API به روشی سازگار با نسخههای قبلی فراهم میکند.نام شمارش
BlockThresholdرا بهHarmBlockThresholdتغییر داد.مقادیر از شمارشهای زیر حذف شدهاند:
HarmBlockThreshold،HarmProbability،HarmSeverity،BlockReasonوFinishReason.
روشهای لکهای
- تمام متدهایی که
Blobبه عنوان بخشی از نام خود داشتند، بهInlineDataتغییر نام دادند.
- تمام متدهایی که
تنظیمات ایمنی
-
methodفیلد را به nullable تغییر دادم.
-
کلاس مدت زمان
- تمام کاربردهای کلاس
Durationکاتلین حذف شده و باlongجایگزین شده است. این تغییر قابلیت همکاری بهتری با جاوا فراهم میکند.
- تمام کاربردهای کلاس
فراداده استناد
- تمام فیلدهایی که قبلاً در
CitationMetadataتعریف شده بودند را در یک کلاس جدید به نامCitationقرار داد. Citationها را میتوان در لیستی به نامcitationsدرCitationMetadataیافت. این تغییر امکان ترازبندی بهتر انواع دادهها را در پلتفرمهای مختلف فراهم میکند.
- تمام فیلدهایی که قبلاً در
تعداد توکنها
- فیلد
totalBillableCharactersبه nullable تغییر داده شد.
- فیلد
کل کاراکترهای قابل پرداخت
- ویژگی
totalBillableCharactersدرCountTokensResponseبه صورت اختیاری تغییر یافت تا موقعیتهایی را که هیچ کاراکتری ارسال نمیشود، منعکس کند.
- ویژگی
نمونهسازی یک مدل
- پارامتر
requestOptionsبه انتهای لیست پارامترها منتقل شد تا با سایر پلتفرمها هماهنگ شود.
- پارامتر
Live API
مقدار
UNSPECIFIEDبرای کلاس شمارشیResponseModalityحذف شد. به جای آنnullاستفاده کنید.تغییر نام
LiveGenerationConfig.setResponseModalitiesبهLiveGenerationConfig.setResponseModality.کلاس
LiveContentResponse.Statusحذف شده و در عوض فیلدهای وضعیت به عنوان ویژگیهایLiveContentResponseتو در تو شدهاند.کلاس
LiveContentResponseحذف شده و در عوض زیرکلاسهایی ازLiveServerMessageارائه شده است که با پاسخهای مدل مطابقت دارند.LiveModelFutures.connectتغییر کرد تاListenableFuture<LiveSessionFutures>را به جایListenableFuture<LiveSession>برگرداند.
Java
شمارشها
کلاسهای
enumو کلاسهایsealedبا کلاسهای معمولی جایگزین شدند. این تغییر، انعطافپذیری بیشتری را برای تکامل API به روشی سازگار با نسخههای قبلی فراهم میکند.نام شمارش
BlockThresholdرا بهHarmBlockThresholdتغییر داد.مقادیر از شمارشهای زیر حذف شدهاند:
HarmBlockThreshold،HarmProbability،HarmSeverity،BlockReasonوFinishReason.
روشهای لکهای
- تمام متدهایی که
Blobبه عنوان بخشی از نام خود داشتند، بهInlineDataتغییر نام دادند.
- تمام متدهایی که
تنظیمات ایمنی
-
methodفیلد را به nullable تغییر دادم.
-
کلاس مدت زمان
- تمام کاربردهای کلاس
Durationکاتلین حذف شده و باlongجایگزین شده است. این تغییر قابلیت همکاری بهتری با جاوا فراهم میکند.
- تمام کاربردهای کلاس
فراداده استناد
- تمام فیلدهایی که قبلاً در
CitationMetadataتعریف شده بودند را در یک کلاس جدید به نامCitationقرار داد. Citationها را میتوان در لیستی به نامcitationsدرCitationMetadataیافت. این تغییر امکان ترازبندی بهتر انواع دادهها را در پلتفرمهای مختلف فراهم میکند.
- تمام فیلدهایی که قبلاً در
تعداد توکنها
- فیلد
totalBillableCharactersبه nullable تغییر داده شد.
- فیلد
کل کاراکترهای قابل پرداخت
- ویژگی
totalBillableCharactersدرCountTokensResponseبه صورت اختیاری تغییر یافت تا موقعیتهایی را که هیچ کاراکتری ارسال نمیشود، منعکس کند.
- ویژگی
نمونهسازی یک مدل
- پارامتر
requestOptionsبه انتهای لیست پارامترها منتقل شد تا با سایر پلتفرمها هماهنگ شود.
- پارامتر
Live API
مقدار
UNSPECIFIEDبرای کلاس شمارشیResponseModalityحذف شد. به جای آنnullاستفاده کنید.تغییر نام
LiveGenerationConfig.setResponseModalitiesبهLiveGenerationConfig.setResponseModality.کلاس
LiveContentResponse.Statusحذف شده و در عوض فیلدهای وضعیت به عنوان ویژگیهایLiveContentResponseتو در تو شدهاند.کلاس
LiveContentResponseحذف شده و در عوض زیرکلاسهایی ازLiveServerMessageارائه شده است که با پاسخهای مدل مطابقت دارند.LiveModelFutures.connectتغییر کرد تاListenableFuture<LiveSessionFutures>را به جایListenableFuture<LiveSession>برگرداند.
متدهای مختلف سازنده جاوا تغییر یافتهاند تا اکنون به جای
void، نمونه کلاس خود را به درستی برگردانند.
Web
شمارشها
- مقادیر از شمارشهای زیر حذف شدهاند:
HarmCategory،BlockThreshold،HarmProbability،HarmSeverity،BlockReasonوFinishReason.
- مقادیر از شمارشهای زیر حذف شدهاند:
دلیل مسدود شدن
-
blockReasonدرPromptFeedbackبه صورت اختیاری تغییر یافت.
-
تغییرات فقط در صورتی لازم است که شروع به استفاده از رابط برنامهنویسی نرمافزار Gemini Developer API کنید (به جای رابط برنامهنویسی نرمافزار پلتفرم Gemini (که قبلاً Vertex AI نام داشت) ):
تنظیمات ایمنی
- کاربردهای پشتیبانی نشدهی
SafetySetting.methodحذف شد.
- کاربردهای پشتیبانی نشدهی
دادههای درونخطی
- کاربردهای پشتیبانی نشدهی
InlineDataPart.videoMetadataحذف شد.
- کاربردهای پشتیبانی نشدهی
Dart
شمارشها
- مقادیر از شمارشهای زیر حذف شدند:
HarmCategory،HarmProbability،BlockReasonوFinishReason.
- مقادیر از شمارشهای زیر حذف شدند:
بخش داده
- نام
DataPartبهInlineDataPartو تابعdatastaticبهinlineDataتغییر یافت تا با سایر پلتفرمها هماهنگ شود.
- نام
گزینههای درخواست
-
RequestOptionsبه دلیل عدم کارکردtimeoutحذف شد. در آینده نزدیک دوباره اضافه خواهد شد، اما برای مطابقت با سایر پلتفرمها به نوعGenerativeModelمنتقل میشود.
-
توالیهای توقف
- پارامتر
stopSequencesدرGenerationConfigرا به اختیاری تغییر داد و مقدار پیشفرض آن را به جای یک آرایه خالی،nullقرار داد.
- پارامتر
نقل قولها
- نام ویژگی
citationSourcesدرCitationMetadataبهcitationsتغییر یافت. نوعCitationSourceبرای مطابقت با سایر پلتفرمها بهCitationتغییر نام یافت.
- نام ویژگی
انواع، متدها و ویژگیهای عمومی غیرضروری
- انواع، متدها و ویژگیهای زیر که به طور ناخواسته در معرض نمایش قرار گرفته بودند، حذف شدند:
defaultTimeout،CountTokensResponseFields،parseCountTokensResponse،parseEmbedContentResponse،parseGenerateContentResponse،parseContent،BatchEmbedContentsResponse،ContentEmbedding،EmbedContentRequestوEmbedContentResponse.
- انواع، متدها و ویژگیهای زیر که به طور ناخواسته در معرض نمایش قرار گرفته بودند، حذف شدند:
تعداد توکنها
- فیلدهای اضافی که دیگر لازم نیستند از تابع
countTokensحذف شدهاند. فقطcontentsمورد نیاز است.
- فیلدهای اضافی که دیگر لازم نیستند از تابع
نمونهسازی یک مدل
- پارامتر
systemInstructionبه انتهای لیست پارامترها منتقل شد تا با سایر پلتفرمها هماهنگ شود.
- پارامتر
قابلیت جاسازی
- قابلیت جاسازی پشتیبانی نشده (
embedContentوbatchEmbedContents) از مدل حذف شد.
- قابلیت جاسازی پشتیبانی نشده (
وحدت
پشتیبانی از Unity از « Vertex AI در Firebase » در دسترس نبود.
یاد بگیرید که چگونه با Firebase AI Logic SDK برای Unity شروع به کار کنید .
خطاهای احتمالی مربوط به مهاجرت
همانطور که در حال مهاجرت به نسخه GA از Firebase AI Logic هستید، اگر تمام تغییرات مورد نیاز را طبق آنچه در این راهنمای مهاجرت توضیح داده شده است، تکمیل نکرده باشید، ممکن است با خطا مواجه شوید.
خطای ۴۰۳: Requests to this API firebasevertexai.googleapis.com ... are blocked.
اگر خطای ۴۰۳ با مضمون « Requests to this API firebasevertexai.googleapis.com ... are blocked. دریافت کردید، معمولاً به این معنی است که کلید API فایربیس در فایل یا شیء پیکربندی فایربیس شما، API مورد نیاز را در فهرست مجاز خود برای محصولی که میخواهید استفاده کنید، ندارد.
مطمئن شوید که کلید API فایربیس مورد استفاده توسط برنامه شما، تمام APIهای مورد نیاز را در لیست مجاز "محدودیتهای API" کلید داشته باشد. برای Firebase AI Logic ، کلید API فایربیس شما باید حداقل API فایربیس AI Logic را در لیست مجاز خود داشته باشد. این API باید هنگام فعال کردن APIهای مورد نیاز در کنسول Firebase ، به طور خودکار به لیست مجاز کلید API شما اضافه شده باشد.
شما میتوانید تمام کلیدهای API خود را در پنل APIs & Services > Credentials در کنسول Google Cloud مشاهده کنید.
درباره تجربه خود با Firebase AI Logic بازخورد دهید
Firebase AI Logic و SDK های کلاینت آن قبلاً " Vertex AI in Firebase " نامیده میشدند. برای انعکاس بهتر خدمات و ویژگیهای توسعهیافته ما (به عنوان مثال، اکنون از Gemini Developer API پشتیبانی میکنیم!)، نام خدمات خود را به Firebase AI Logic تغییر دادیم و بستهبندی مجدد کردیم .
برای دسترسی ایمن به مدلهای هوش مصنوعی مولد گوگل مستقیماً از طریق برنامههای تلفن همراه یا وب خود، اکنون میتوانید یک ارائهدهنده « Gemini API » را انتخاب کنید - یا API پلتفرم عامل Gemini که مدتهاست در دسترس است (قبلاً Vertex AI) یا اکنون API توسعهدهنده Gemini . این بدان معناست که اکنون میتوانید از API توسعهدهنده Gemini استفاده کنید که یک سطح بدون هزینه با محدودیتها و سهمیههای معقول ارائه میدهد.
مروری بر مراحل مهاجرت به SDK های Firebase AI Logic
مرحله ۱ : بهترین ارائهدهنده «Gemini API» را برای برنامه و موارد استفاده خود انتخاب کنید.
مرحله ۲ : فعال کردن API های مورد نیاز
مرحله ۳ : کتابخانه مورد استفاده در برنامه خود را بهروزرسانی کنید.
مرحله ۴ : مقداردهی اولیه را در برنامه خود بهروزرسانی کنید.
مرحله ۵ : کد خود را بسته به ویژگیهایی که استفاده میکنید، بهروزرسانی کنید.
مرحله ۱ : بهترین ارائهدهنده «Gemini API» را برای برنامه خود انتخاب کنید
با این مهاجرت، شما میتوانید ارائهدهندهی « Gemini API » را انتخاب کنید:
SDK های قدیمی « Vertex AI in Firebase » فقط میتوانستند از API مربوط به Agent Platform Gemini (که قبلاً Vertex AI نام داشت) استفاده کنند.
کیتهای توسعه نرمافزار (SDK) جدید Firebase AI Logic به شما امکان میدهند انتخاب کنید که میخواهید کدام ارائهدهنده « Gemini API » را مستقیماً از برنامه تلفن همراه یا وب خود فراخوانی کنید - یا Gemini Developer API یا Agent Platform Gemini API (که قبلاً Vertex AI نام داشت) .
تفاوتهای بین استفاده از دو ارائهدهندهی API مربوط به Gemini را بررسی کنید، به خصوص از نظر ویژگیهای پشتیبانیشده، قیمتگذاری و محدودیتهای نرخ. فقط برای یک مثال، API توسعهدهندگان Gemini از ارائه فایلها با استفاده از URLهای Cloud Storage پشتیبانی نمیکند، اما اگر میخواهید از ردیف رایگان و سهمیهی معقول آن بهرهمند شوید، میتواند انتخاب خوبی باشد.
مرحله ۲ : فعال کردن API های مورد نیاز
مطمئن شوید که تمام APIهای مورد نیاز در پروژه Firebase شما فعال هستند تا بتوانید از ارائهدهنده « Gemini API » انتخابی خود استفاده کنید.
توجه داشته باشید که میتوانید هر دو ارائهدهنده API را همزمان در پروژه خود فعال کنید.
وارد کنسول Firebase شوید و سپس پروژه Firebase خود را انتخاب کنید.
در کنسول Firebase ، به بخش سرویسهای هوش مصنوعی > منطق هوش مصنوعی بروید.
برای شروع یک گردش کار هدایتشده که به شما در تنظیم APIها و منابع مورد نیاز پروژهتان کمک میکند، روی «شروع به کار» کلیک کنید.
ارائهدهندهی «Gemini API» را که میخواهید با Firebase AI Logic SDKs استفاده کنید، انتخاب کنید. در صورت تمایل، همیشه میتوانید ارائهدهندهی API دیگری را بعداً تنظیم و استفاده کنید.
رابط برنامهنویسی کاربردی Gemini - پرداخت هزینه اختیاری (در طرح قیمتگذاری بدون هزینه Spark موجود است)
گردش کار کنسول، APIهای مورد نیاز را فعال کرده و یک کلید API جمینی در پروژه شما ایجاد میکند.
این کلید API مربوط به Gemini را به کدبیس برنامه خود اضافه نکنید . اطلاعات بیشتر.رابط برنامهنویسی کاربردی پلتفرم عامل Gemini (که قبلاً Vertex AI نام داشت) - نیاز به پرداخت صورتحساب دارد (نیازمند طرح قیمتگذاری Blaze با قابلیت پرداخت در محل است)
گردش کار کنسول، APIهای مورد نیاز را در پروژه شما فعال میکند.
برای بهروزرسانی کتابخانه و مقداردهی اولیه در برنامه خود، در این راهنمای مهاجرت ادامه دهید.
مرحله ۳ : بهروزرسانی کتابخانه مورد استفاده در برنامه
کدبیس برنامه خود را برای استفاده از کتابخانه Firebase AI Logic بهروزرسانی کنید.
سویفت
در Xcode، در حالی که پروژه برنامه شما باز است، بسته Firebase خود را با استفاده از یکی از گزینههای زیر به نسخه ۱۱.۱۳.۰ یا بالاتر بهروزرسانی کنید:
گزینه ۱ : بهروزرسانی همه بستهها: به مسیر File > Packages > Update to Latest Package Versions بروید.
گزینه ۲ : بهروزرسانی جداگانه فایربیس: در بخشی به نام Package Dependencies به پکیج فایربیس بروید. روی پکیج فایربیس کلیک راست کنید و سپس Update Package را انتخاب کنید.
مطمئن شوید که بسته Firebase اکنون نسخه 11.13.0 یا بالاتر را نشان میدهد. اگر اینطور نیست، بررسی کنید که الزامات بستهبندی مشخصشده شما امکان بهروزرسانی به نسخه 11.13.0 یا بالاتر را فراهم میکند.
هدف برنامه خود را در ویرایشگر پروژه انتخاب کنید و سپس به بخش چارچوبها، کتابخانهها و محتوای جاسازیشده بروید.
کتابخانه جدید را اضافه کنید: دکمه + را انتخاب کنید و سپس FirebaseAI را از بسته Firebase اضافه کنید.
پس از اتمام انتقال برنامه خود (به بخشهای باقیمانده در این راهنما مراجعه کنید)، حتماً کتابخانه قدیمی را حذف کنید:
FirebaseVertexAI-Preview را انتخاب کنید و سپس دکمهی — را فشار دهید.
Kotlin
در فایل Gradle ماژول (سطح برنامه) خود (معمولاً
<project>/<app-module>/build.gradle.ktsیا<project>/<app-module>/build.gradle)، وابستگیهای قدیمی را (در صورت وجود) با موارد زیر جایگزین کنید.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف وابستگی قدیمی، آسانتر باشد.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }پروژه اندروید خود را با فایلهای Gradle همگامسازی کنید.
توجه داشته باشید که اگر تصمیم دارید از Firebase Android BoM استفاده نکنید، کافیست وابستگی مربوط به کتابخانه firebase-ai را اضافه کنید و آخرین نسخه پیشنهادی اندروید استودیو را بپذیرید.
Java
در فایل Gradle ماژول (سطح برنامه) خود (معمولاً
<project>/<app-module>/build.gradle.ktsیا<project>/<app-module>/build.gradle)، وابستگیهای قدیمی را (در صورت وجود) با موارد زیر جایگزین کنید.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف وابستگی قدیمی، آسانتر باشد.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }پروژه اندروید خود را با فایلهای Gradle همگامسازی کنید.
توجه داشته باشید که اگر تصمیم دارید از Firebase Android BoM استفاده نکنید، کافیست وابستگی مربوط به کتابخانه firebase-ai را اضافه کنید و آخرین نسخه پیشنهادی اندروید استودیو را بپذیرید.
Web
آخرین نسخه Firebase JS SDK برای وب را با استفاده از npm دریافت کنید:
npm i firebase@latest
یا
yarn add firebase@latest
هر جا که کتابخانه را وارد کردهاید، دستورات import خود را بهروزرسانی کنید تا به جای آن
firebase/aiاستفاده شود.توجه داشته باشید که ممکن است مهاجرت کدبیس برنامه (به بخشهای باقیمانده این راهنما مراجعه کنید) قبل از حذف ایمپورتهای قدیمی، آسانتر باشد.
// BEFORE import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";// AFTER import { initializeApp } from "firebase/app"; import { getAI, getGenerativeModel } from "firebase/ai";
Dart
با اجرای دستور زیر از دایرکتوری پروژه Flutter خود، به استفاده از بسته
firebase_aiدر فایلpubspec.yamlخود بهروزرسانی کنید:flutter pub add firebase_ai
پروژه فلاتر خود را بازسازی کنید:
flutter run
پس از اتمام انتقال برنامه خود (به بخشهای باقیمانده در این راهنما مراجعه کنید)، حتماً بسته قدیمی را حذف کنید:
flutter pub remove firebase_vertexai
وحدت
پشتیبانی از Unity از « Vertex AI در Firebase » در دسترس نبود.
یاد بگیرید که چگونه با Firebase AI Logic SDK برای Unity شروع به کار کنید .
مرحله ۴ : مقداردهی اولیه را در برنامه خود بهروزرسانی کنید
برای مشاهده محتوا و کد مخصوص ارائهدهنده در این صفحه، روی ارائهدهنده API Gemini خود کلیک کنید. |
نحوهی مقداردهی اولیهی سرویس برای ارائهدهندهی API انتخابی خود را بهروزرسانی کنید و یک نمونهی GenerativeModel ایجاد کنید.
سویفت
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.7-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.7-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.7-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.7-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.7-flash');
وحدت
پشتیبانی از Unity از « Vertex AI در Firebase » در دسترس نبود.
یاد بگیرید که چگونه با Firebase AI Logic SDK برای Unity شروع به کار کنید .
توجه داشته باشید که بسته به قابلیتی که استفاده میکنید، ممکن است همیشه یک نمونه GenerativeModel ایجاد نکنید . برای پخش ورودی و خروجی با استفاده از Gemini Live API ، یک نمونه LiveModel ایجاد کنید.
مرحله ۵ : کد خود را بسته به ویژگیهایی که استفاده میکنید، بهروزرسانی کنید
این مرحله تغییراتی را شرح میدهد که ممکن است بسته به ویژگیهایی که استفاده میکنید، مورد نیاز باشند.
اگر از آدرسهای اینترنتی Cloud Storage URLs) استفاده میکنید و در این انتقال، API توسعهدهندگان Gemini را جایگزین کردهاید، باید درخواستهای چندوجهی خود را بهروزرسانی کنید تا فایلها به عنوان دادههای درونخطی (inline data) لحاظ شوند (یا از آدرسهای اینترنتی یوتیوب برای ویدیوها استفاده کنید).
چندین تغییر برای نسخههای GA از SDK های " Vertex AI in Firebase " معرفی شده است. همین تغییرات برای استفاده از SDK های Firebase AI Logic نیز مورد نیاز است. لیستهای زیر را برای هرگونه تغییری که ممکن است برای تطبیق با SDK Firebase AI Logic در کد خود ایجاد کنید، مرور کنید.
مورد نیاز برای همه زبانها و پلتفرمها
فراخوانی تابع
اگر این ویژگی را قبل از GA پیادهسازی کردهاید، باید نحوه تعریف طرحواره خود را بهروزرسانی کنید. توصیه میکنیم برای یادگیری نحوه نوشتن اعلانهای تابع، راهنمای فراخوانی تابع بهروزرسانیشده را مرور کنید.تولید خروجی ساختاریافته (مانند JSON) با استفاده از
responseSchema
اگر این ویژگی را قبل از GA پیادهسازی کردهاید، باید نحوه تعریف طرحواره خود را بهروزرسانی کنید. توصیه میکنیم برای یادگیری نحوه نوشتن طرحوارههای JSON، راهنمای جدید خروجی ساختاریافته را مرور کنید.تایم اوت
- زمان پیشفرض برای درخواستها به ۱۸۰ ثانیه تغییر یافت.
مورد نیاز بر اساس پلتفرم یا زبان
سویفت
شمارشها
اکثر انواع
enumباstructها با متغیرهای استاتیک جایگزین شدند. این تغییر، انعطافپذیری بیشتری را برای تکامل API به روشی سازگار با نسخههای قبلی فراهم میکند. هنگام استفاده از دستوراتswitch، اکنون باید یک موردdefault:را برای پوشش مقادیر ناشناخته یا مدیریت نشده، از جمله مقادیر جدیدی که در آینده به SDK اضافه میشوند، لحاظ کنید.نام شمارش
BlockThresholdبهHarmBlockThresholdتغییر یافت؛ این نوع اکنون یکstructاست.موارد
unknownوunspecifiedاز شمارشهای زیر (که اکنونstructهستند) حذف شدند:HarmCategory،HarmBlockThreshold،HarmProbability،BlockReasonوFinishReason.شمارش
ModelContent.Partبا پروتکلی به نامPartجایگزین شد تا امکان اضافه کردن انواع جدید به روشی سازگار با نسخههای قبلی فراهم شود. این تغییر با جزئیات بیشتر در بخش قطعات محتوا توضیح داده شده است.
بخشهای محتوا
پروتکل
ThrowingPartsRepresentableحذف شد و مقداردهی اولیهModelContentسادهسازی شد تا از خطاهای گاه به گاه کامپایلر جلوگیری شود. تصاویری که به درستی کدگذاری نمیشوند، هنگام استفاده درgenerateContentهمچنان خطا میدهند.موارد
ModelContent.Partبا انواعstructزیر که مطابق با پروتکلPartهستند، جایگزین شدند:-
.textبهTextPart -
.dataبهInlineDataPart -
.fileDataبهFileDataPart -
.functionCallبهFunctionCallPart -
.functionResponseبهFunctionResponsePart
-
دسته بندی آسیب
- تغییر داده شد تا
HarmCategoryدیگر در نوعSafetySettingبه صورت تودرتو نباشد. اگر به آنSafetySetting.HarmCategoryاشاره میکنید، میتوان آن را باHarmCategoryجایگزین کرد.
- تغییر داده شد تا
بازخورد ایمنی
- نوع
SafetyFeedbackحذف شد، زیرا در هیچ یک از پاسخها استفاده نشده بود.
- نوع
فراداده استناد
- نام ویژگی
citationSourcesرا درCitationMetadataبهcitationsتغییر داد.
- نام ویژگی
کل کاراکترهای قابل پرداخت
- ویژگی
totalBillableCharactersدرCountTokensResponseبه صورت اختیاری تغییر یافت تا موقعیتهایی را که هیچ کاراکتری ارسال نمیشود، منعکس کند.
- ویژگی
پاسخ نامزد
- برای مطابقت با سایر پلتفرمها، نام
CandidateResponseبهCandidateتغییر یافت.
- برای مطابقت با سایر پلتفرمها، نام
پیکربندی تولید
- ویژگیهای عمومی
GenerationConfigبهinternalتغییر داده شد. همه آنها در مقداردهنده اولیه قابل تنظیم باقی میمانند.
- ویژگیهای عمومی
Kotlin
شمارشها
کلاسهای
enumو کلاسهایsealedبا کلاسهای معمولی جایگزین شدند. این تغییر، انعطافپذیری بیشتری را برای تکامل API به روشی سازگار با نسخههای قبلی فراهم میکند.نام شمارش
BlockThresholdرا بهHarmBlockThresholdتغییر داد.مقادیر از شمارشهای زیر حذف شدهاند:
HarmBlockThreshold،HarmProbability،HarmSeverity،BlockReasonوFinishReason.
روشهای لکهای
- تمام متدهایی که
Blobبه عنوان بخشی از نام خود داشتند، بهInlineDataتغییر نام دادند.
- تمام متدهایی که
تنظیمات ایمنی
-
methodفیلد را به nullable تغییر دادم.
-
کلاس مدت زمان
- تمام کاربردهای کلاس
Durationکاتلین حذف شده و باlongجایگزین شده است. این تغییر قابلیت همکاری بهتری با جاوا فراهم میکند.
- تمام کاربردهای کلاس
فراداده استناد
- تمام فیلدهایی که قبلاً در
CitationMetadataتعریف شده بودند را در یک کلاس جدید به نامCitationقرار داد. Citationها را میتوان در لیستی به نامcitationsدرCitationMetadataیافت. این تغییر امکان ترازبندی بهتر انواع دادهها را در پلتفرمهای مختلف فراهم میکند.
- تمام فیلدهایی که قبلاً در
تعداد توکنها
- فیلد
totalBillableCharactersبه nullable تغییر داده شد.
- فیلد
کل کاراکترهای قابل پرداخت
- ویژگی
totalBillableCharactersدرCountTokensResponseبه صورت اختیاری تغییر یافت تا موقعیتهایی را که هیچ کاراکتری ارسال نمیشود، منعکس کند.
- ویژگی
نمونهسازی یک مدل
- پارامتر
requestOptionsبه انتهای لیست پارامترها منتقل شد تا با سایر پلتفرمها هماهنگ شود.
- پارامتر
Live API
مقدار
UNSPECIFIEDبرای کلاس شمارشیResponseModalityحذف شد. به جای آنnullاستفاده کنید.تغییر نام
LiveGenerationConfig.setResponseModalitiesبهLiveGenerationConfig.setResponseModality.کلاس
LiveContentResponse.Statusحذف شده و در عوض فیلدهای وضعیت به عنوان ویژگیهایLiveContentResponseتو در تو شدهاند.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Java
شمارشها
Replaced
enumclasses andsealedclasses with regular classes. This change allows more flexibility for evolving the API in a backward compatible way.Renamed the
BlockThresholdenumeration toHarmBlockThreshold.Removed values from the following enumerations:
HarmBlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
Blob methods
- Renamed all methods that included
Blobas part of their name to useInlineDatainstead.
- Renamed all methods that included
تنظیمات ایمنی
- Changed the field
methodto be nullable.
- Changed the field
Duration class
- Removed all usages of Kotlin's
Durationclass, and replaced it withlong. This change provides better interoperability with Java.
- Removed all usages of Kotlin's
Citation metadata
- Wrapped all the fields previously declared in
CitationMetadatainto a new class calledCitation. Citations can be found in the list calledcitationsinCitationMetadata. This change allows better alignment of types across platforms.
- Wrapped all the fields previously declared in
Count tokens
- Changed the field
totalBillableCharactersto be nullable.
- Changed the field
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Instantiating a model
- Moved the
requestOptionsparameter to the end of the parameter list to align with other platforms.
- Moved the
Live API
Removed
UNSPECIFIEDvalue for enum classResponseModality. Instead usenull.Renamed
LiveGenerationConfig.setResponseModalitiestoLiveGenerationConfig.setResponseModality.Removed the
LiveContentResponse.Statusclass, and instead have nested the status fields as properties ofLiveContentResponse.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Changed various Java builder methods to now correctly return the instance of their class, instead of
void.
Web
شمارشها
- Removed values from the following enumerations:
HarmCategory,BlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
- Removed values from the following enumerations:
دلیل مسدود شدن
- Changed
blockReasoninPromptFeedbackto be optional.
- Changed
Changes required only if you're starting to use the Gemini Developer API (instead of the Agent Platform Gemini API (formerly Vertex AI) ):
تنظیمات ایمنی
- Removed usages of the unsupported
SafetySetting.method.
- Removed usages of the unsupported
دادههای درونخطی
- Removed usages of the unsupported
InlineDataPart.videoMetadata.
- Removed usages of the unsupported
Dart
شمارشها
- Removed values from the following enumerations:
HarmCategory,HarmProbability,BlockReason, andFinishReason.
- Removed values from the following enumerations:
Data part
- Renamed
DataParttoInlineDataPart, and thestaticdatafunction toinlineDatato align with other platforms.
- Renamed
Request options
- Removed
RequestOptionssincetimeoutwasn't functional. It will be re-added in the near future, but it will be moved to theGenerativeModeltype to match other platforms.
- Removed
Stop sequences
- Changed the
stopSequencesparameter inGenerationConfigto be optional and to default tonullinstead of an empty array.
- Changed the
نقل قولها
- Renamed the
citationSourcesproperty tocitationsinCitationMetadata. TheCitationSourcetype was renamed toCitationto match other platforms.
- Renamed the
Unnecessary public types, methods, and properties
- Removed the following types, methods, and properties which were unintentionally exposed:
defaultTimeout,CountTokensResponseFields,parseCountTokensResponse,parseEmbedContentResponse,parseGenerateContentResponse,parseContent,BatchEmbedContentsResponse,ContentEmbedding,EmbedContentRequest, andEmbedContentResponse.
- Removed the following types, methods, and properties which were unintentionally exposed:
Count tokens
- Removed extra fields from the
countTokensfunction that are no longer necessary. Onlycontentsis needed.
- Removed extra fields from the
Instantiating a model
- Moved the
systemInstructionparameter to the end of the parameter list to align with other platforms.
- Moved the
Embedding functionality
- Removed unsupported embedding functionality (
embedContentandbatchEmbedContents) from the model.
- Removed unsupported embedding functionality (
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Possible errors related to migrating
As you're migrating to use the GA version of Firebase AI Logic , you might encounter errors if you haven't completed all of the required changes as described in this migration guide.
403 Error: Requests to this API firebasevertexai.googleapis.com ... are blocked.
If you receive a 403 error that says Requests to this API firebasevertexai.googleapis.com ... are blocked. , it usually means that the Firebase API key in your Firebase configuration file or object doesn't have a required API in its allowlist for the product that you're trying to use.
Make sure that the Firebase API key used by your app has all the required APIs included in the key's "API restrictions" allowlist . For Firebase AI Logic , your Firebase API key needs to have at minimum the Firebase AI Logic API in its allowlist. This API should have been automatically added to your API key's allowlist when you enabled the required APIs in the Firebase console .
You can view all your API keys in the APIs & Services > Credentials panel in the Google Cloud console.
Give feedback about your experience with Firebase AI Logic
Firebase AI Logic and its client SDKs were formerly called " Vertex AI in Firebase ". To better reflect our expanded services and features (for example, we now support the Gemini Developer API !), we renamed and repackaged our services into Firebase AI Logic .
To securely access Google's generative AI models directly from your mobile or web apps, you can now choose a " Gemini API " provider — either the long-available Agent Platform Gemini API (formerly Vertex AI) or now the Gemini Developer API . This means that you now have the option to use the Gemini Developer API , which provides a no-cost tier with reasonable rate limits and quotas.
Overview of steps to migrate to the Firebase AI Logic SDKs
Step 1 : Choose the best "Gemini API" provider for your app and use cases.
Step 2 : Enable the required APIs.
Step 3 : Update the library used in your app.
Step 4 : Update the initialization in your app.
Step 5 : Update your code depending on the features that you use.
Step 1 : Choose the best "Gemini API" provider for your app
With this migration, you have a choice in " Gemini API " provider:
The old " Vertex AI in Firebase " SDKs could only use the Agent Platform Gemini API (formerly Vertex AI) .
The new Firebase AI Logic SDKs let you choose which " Gemini API " provider you want to call directly from your mobile or web app – either the Gemini Developer API or the Agent Platform Gemini API (formerly Vertex AI) .
Review the differences between using the two Gemini API providers , especially in terms of supported features, pricing, and rate limits. For just one example, the Gemini Developer API doesn't support providing files using Cloud Storage URLs, but it might be a good choice if you want to take advantage of its no-cost tier and reasonable quota.
Step 2 : Enable the required APIs
Ensure that all required APIs are enabled in your Firebase project to use your chosen " Gemini API " provider.
Note that you can have both of API providers enabled in your project at the same time.
Sign into the Firebase console , and then select your Firebase project.
In the Firebase console, go to AI Services > AI Logic .
Click Get started to launch a guided workflow that helps you set up the required APIs and resources for your project.
Select the "Gemini API" provider that you'd like to use with the Firebase AI Logic SDKs. You can always set up and use the other API provider later, if you'd like.
Gemini Developer API — billing optional (available on the no-cost Spark pricing plan)
The console's workflow will enable the required APIs and create a Gemini API key in your project.
Do not add this Gemini API key into your app's codebase. Learn more.Agent Platform Gemini API (formerly Vertex AI) — billing required (requires the pay-as-you-go Blaze pricing plan)
The console's workflow will enable the required APIs in your project.
Continue in this migration guide to update the library and initialization in your app.
Step 3 : Update the library used in your app
Update your app's codebase to use the Firebase AI Logic library.
سویفت
In Xcode, with your app project open, update your Firebase package to v11.13.0 or later using one of the following options:
Option 1 : Update all packages: Navigate to File > Packages > Update to Latest Package Versions .
Option 2 : Update Firebase individually: Navigate to the Firebase package in the section called Package Dependencies . Right-click on the Firebase package, and then select Update Package .
Make sure that the Firebase package now shows v11.13.0 or later. If it doesn't, verify that your specified Package Requirements allow updating to v11.13.0 or later.
Select your app's target in the Project Editor, and then navigate to the Frameworks, Libraries, and Embedded Content section.
Add the new library: Select the + button, and then add FirebaseAI from the Firebase package.
After you've finished migrating your app (see the remaining sections in this guide), make sure to remove the old library:
Select FirebaseVertexAI-Preview , and then press the — button.
Kotlin
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle), replace old dependencies (as applicable) with the following.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old dependency.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }Sync your Android project with Gradle files.
Note that if you choose to not use the Firebase Android BoM , then just add the dependency for the firebase-ai library and accept the latest version that's suggested by Android Studio.
Java
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle), replace old dependencies (as applicable) with the following.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old dependency.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }Sync your Android project with Gradle files.
Note that if you choose to not use the Firebase Android BoM , then just add the dependency for the firebase-ai library and accept the latest version that's suggested by Android Studio.
Web
Get the latest version of the Firebase JS SDK for Web using npm:
npm i firebase@latest
یا
yarn add firebase@latest
Wherever you've imported the library, update your import statements to use
firebase/aiinstead.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old imports.
// BEFORE import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";// AFTER import { initializeApp } from "firebase/app"; import { getAI, getGenerativeModel } from "firebase/ai";
Dart
Update to the use the
firebase_aipackage in yourpubspec.yamlfile by running the following command from your Flutter project directory:flutter pub add firebase_ai
Rebuild your Flutter project:
flutter run
After you've finished migrating your app (see the remaining sections in this guide), make sure to delete the old package:
flutter pub remove firebase_vertexai
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Step 4 : Update the initialization in your app
Click your Gemini API provider to view provider-specific content and code on this page. |
Update how you initialize the service for your chosen API provider and create a GenerativeModel instance.
سویفت
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.7-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.7-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.7-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.7-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.7-flash');
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Note that depending on the capability you're using, you might not always create a GenerativeModel instance . To stream input and output using the Gemini Live API , create a LiveModel instance.
Step 5 : Update your code depending on features that you use
This step describes changes that may be required depending on which features you use.
If you use Cloud Storage URLs and you swapped to use the Gemini Developer API in this migration, then you must update your multimodal requests to include files as inline data (or use YouTube URLs for videos).
Several changes were introduced for the GA versions of the " Vertex AI in Firebase " SDKs. These same changes are required to use the Firebase AI Logic SDKs. Review the following lists for any changes that you might need to make in your code to accommodate taking up the Firebase AI Logic SDK.
Required for all languages and platforms
فراخوانی تابع
If you implemented this feature before GA, then you'll need to make updates to how you define your schema. We recommend reviewing the updated function calling guide to learn how to write your function declarations.Generating structured output (like JSON) using
responseSchema
If you implemented this feature before GA, then you'll need to make updates to how you define your schema. We recommend reviewing the new structured output guide to learn how to write JSON schemas.تایم اوت
- Changed the default timeout for requests to be 180 seconds.
Required based on platform or language
سویفت
شمارشها
Replaced most
enumtypes withstructs with static variables. This change allows more flexibility for evolving the API in a backward-compatible way. When usingswitchstatements, you must now include adefault:case to cover unknown or unhandled values, including new values that are added to the SDK in the future.Renamed the
BlockThresholdenumeration toHarmBlockThreshold; this type is now astruct.Removed
unknownandunspecifiedcases from the following enumerations (nowstructs):HarmCategory,HarmBlockThreshold,HarmProbability,BlockReason, andFinishReason.Replaced the enumeration
ModelContent.Partwith a protocol namedPartto allow new types to be added in a backward-compatible way. This change is described in greater detail in the Content parts section.
Content parts
Removed the
ThrowingPartsRepresentableprotocol, and simplified the initializers forModelContentto avoid occasional compiler errors. Images that don't encode properly will still throw errors when being used ingenerateContent.Replaced the
ModelContent.Partcases with the followingstructtypes conforming to thePartprotocol:-
.texttoTextPart -
.datatoInlineDataPart -
.fileDatatoFileDataPart -
.functionCalltoFunctionCallPart -
.functionResponsetoFunctionResponsePart
-
Harm category
- Changed the
HarmCategoryto no longer be nested in theSafetySettingtype. If you're referring to it asSafetySetting.HarmCategory, that can be replaced withHarmCategory.
- Changed the
Safety feedback
- Removed the
SafetyFeedbacktype, since it wasn't used in any of the responses.
- Removed the
Citation metadata
- Renamed the
citationSourcesproperty tocitationsinCitationMetadata.
- Renamed the
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Candidate response
- Renamed
CandidateResponsetoCandidateto match other platforms.
- Renamed
Generation configuration
- Changed the public properties of
GenerationConfigtointernal. They all remain configurable in the initializer.
- Changed the public properties of
Kotlin
شمارشها
Replaced
enumclasses andsealedclasses with regular classes. This change allows more flexibility for evolving the API in a backward compatible way.Renamed the
BlockThresholdenumeration toHarmBlockThreshold.Removed values from the following enumerations:
HarmBlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
Blob methods
- Renamed all methods that included
Blobas part of their name to useInlineDatainstead.
- Renamed all methods that included
تنظیمات ایمنی
- Changed the field
methodto be nullable.
- Changed the field
Duration class
- Removed all usages of Kotlin's
Durationclass, and replaced it withlong. This change provides better interoperability with Java.
- Removed all usages of Kotlin's
Citation metadata
- Wrapped all the fields previously declared in
CitationMetadatainto a new class calledCitation. Citations can be found in the list calledcitationsinCitationMetadata. This change allows better alignment of types across platforms.
- Wrapped all the fields previously declared in
Count tokens
- Changed the field
totalBillableCharactersto be nullable.
- Changed the field
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Instantiating a model
- Moved the
requestOptionsparameter to the end of the parameter list to align with other platforms.
- Moved the
Live API
Removed
UNSPECIFIEDvalue for enum classResponseModality. Instead usenull.Renamed
LiveGenerationConfig.setResponseModalitiestoLiveGenerationConfig.setResponseModality.Removed the
LiveContentResponse.Statusclass, and instead have nested the status fields as properties ofLiveContentResponse.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Java
شمارشها
Replaced
enumclasses andsealedclasses with regular classes. This change allows more flexibility for evolving the API in a backward compatible way.Renamed the
BlockThresholdenumeration toHarmBlockThreshold.Removed values from the following enumerations:
HarmBlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
Blob methods
- Renamed all methods that included
Blobas part of their name to useInlineDatainstead.
- Renamed all methods that included
تنظیمات ایمنی
- Changed the field
methodto be nullable.
- Changed the field
Duration class
- Removed all usages of Kotlin's
Durationclass, and replaced it withlong. This change provides better interoperability with Java.
- Removed all usages of Kotlin's
Citation metadata
- Wrapped all the fields previously declared in
CitationMetadatainto a new class calledCitation. Citations can be found in the list calledcitationsinCitationMetadata. This change allows better alignment of types across platforms.
- Wrapped all the fields previously declared in
Count tokens
- Changed the field
totalBillableCharactersto be nullable.
- Changed the field
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Instantiating a model
- Moved the
requestOptionsparameter to the end of the parameter list to align with other platforms.
- Moved the
Live API
Removed
UNSPECIFIEDvalue for enum classResponseModality. Instead usenull.Renamed
LiveGenerationConfig.setResponseModalitiestoLiveGenerationConfig.setResponseModality.Removed the
LiveContentResponse.Statusclass, and instead have nested the status fields as properties ofLiveContentResponse.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Changed various Java builder methods to now correctly return the instance of their class, instead of
void.
Web
شمارشها
- Removed values from the following enumerations:
HarmCategory,BlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
- Removed values from the following enumerations:
دلیل مسدود شدن
- Changed
blockReasoninPromptFeedbackto be optional.
- Changed
Changes required only if you're starting to use the Gemini Developer API (instead of the Agent Platform Gemini API (formerly Vertex AI) ):
تنظیمات ایمنی
- Removed usages of the unsupported
SafetySetting.method.
- Removed usages of the unsupported
دادههای درونخطی
- Removed usages of the unsupported
InlineDataPart.videoMetadata.
- Removed usages of the unsupported
Dart
شمارشها
- Removed values from the following enumerations:
HarmCategory,HarmProbability,BlockReason, andFinishReason.
- Removed values from the following enumerations:
Data part
- Renamed
DataParttoInlineDataPart, and thestaticdatafunction toinlineDatato align with other platforms.
- Renamed
Request options
- Removed
RequestOptionssincetimeoutwasn't functional. It will be re-added in the near future, but it will be moved to theGenerativeModeltype to match other platforms.
- Removed
Stop sequences
- Changed the
stopSequencesparameter inGenerationConfigto be optional and to default tonullinstead of an empty array.
- Changed the
نقل قولها
- Renamed the
citationSourcesproperty tocitationsinCitationMetadata. TheCitationSourcetype was renamed toCitationto match other platforms.
- Renamed the
Unnecessary public types, methods, and properties
- Removed the following types, methods, and properties which were unintentionally exposed:
defaultTimeout,CountTokensResponseFields,parseCountTokensResponse,parseEmbedContentResponse,parseGenerateContentResponse,parseContent,BatchEmbedContentsResponse,ContentEmbedding,EmbedContentRequest, andEmbedContentResponse.
- Removed the following types, methods, and properties which were unintentionally exposed:
Count tokens
- Removed extra fields from the
countTokensfunction that are no longer necessary. Onlycontentsis needed.
- Removed extra fields from the
Instantiating a model
- Moved the
systemInstructionparameter to the end of the parameter list to align with other platforms.
- Moved the
Embedding functionality
- Removed unsupported embedding functionality (
embedContentandbatchEmbedContents) from the model.
- Removed unsupported embedding functionality (
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Possible errors related to migrating
As you're migrating to use the GA version of Firebase AI Logic , you might encounter errors if you haven't completed all of the required changes as described in this migration guide.
403 Error: Requests to this API firebasevertexai.googleapis.com ... are blocked.
If you receive a 403 error that says Requests to this API firebasevertexai.googleapis.com ... are blocked. , it usually means that the Firebase API key in your Firebase configuration file or object doesn't have a required API in its allowlist for the product that you're trying to use.
Make sure that the Firebase API key used by your app has all the required APIs included in the key's "API restrictions" allowlist . For Firebase AI Logic , your Firebase API key needs to have at minimum the Firebase AI Logic API in its allowlist. This API should have been automatically added to your API key's allowlist when you enabled the required APIs in the Firebase console .
You can view all your API keys in the APIs & Services > Credentials panel in the Google Cloud console.
Give feedback about your experience with Firebase AI Logic
Firebase AI Logic and its client SDKs were formerly called " Vertex AI in Firebase ". To better reflect our expanded services and features (for example, we now support the Gemini Developer API !), we renamed and repackaged our services into Firebase AI Logic .
To securely access Google's generative AI models directly from your mobile or web apps, you can now choose a " Gemini API " provider — either the long-available Agent Platform Gemini API (formerly Vertex AI) or now the Gemini Developer API . This means that you now have the option to use the Gemini Developer API , which provides a no-cost tier with reasonable rate limits and quotas.
Overview of steps to migrate to the Firebase AI Logic SDKs
Step 1 : Choose the best "Gemini API" provider for your app and use cases.
Step 2 : Enable the required APIs.
Step 3 : Update the library used in your app.
Step 4 : Update the initialization in your app.
Step 5 : Update your code depending on the features that you use.
Step 1 : Choose the best "Gemini API" provider for your app
With this migration, you have a choice in " Gemini API " provider:
The old " Vertex AI in Firebase " SDKs could only use the Agent Platform Gemini API (formerly Vertex AI) .
The new Firebase AI Logic SDKs let you choose which " Gemini API " provider you want to call directly from your mobile or web app – either the Gemini Developer API or the Agent Platform Gemini API (formerly Vertex AI) .
Review the differences between using the two Gemini API providers , especially in terms of supported features, pricing, and rate limits. For just one example, the Gemini Developer API doesn't support providing files using Cloud Storage URLs, but it might be a good choice if you want to take advantage of its no-cost tier and reasonable quota.
Step 2 : Enable the required APIs
Ensure that all required APIs are enabled in your Firebase project to use your chosen " Gemini API " provider.
Note that you can have both of API providers enabled in your project at the same time.
Sign into the Firebase console , and then select your Firebase project.
In the Firebase console, go to AI Services > AI Logic .
Click Get started to launch a guided workflow that helps you set up the required APIs and resources for your project.
Select the "Gemini API" provider that you'd like to use with the Firebase AI Logic SDKs. You can always set up and use the other API provider later, if you'd like.
Gemini Developer API — billing optional (available on the no-cost Spark pricing plan)
The console's workflow will enable the required APIs and create a Gemini API key in your project.
Do not add this Gemini API key into your app's codebase. Learn more.Agent Platform Gemini API (formerly Vertex AI) — billing required (requires the pay-as-you-go Blaze pricing plan)
The console's workflow will enable the required APIs in your project.
Continue in this migration guide to update the library and initialization in your app.
Step 3 : Update the library used in your app
Update your app's codebase to use the Firebase AI Logic library.
سویفت
In Xcode, with your app project open, update your Firebase package to v11.13.0 or later using one of the following options:
Option 1 : Update all packages: Navigate to File > Packages > Update to Latest Package Versions .
Option 2 : Update Firebase individually: Navigate to the Firebase package in the section called Package Dependencies . Right-click on the Firebase package, and then select Update Package .
Make sure that the Firebase package now shows v11.13.0 or later. If it doesn't, verify that your specified Package Requirements allow updating to v11.13.0 or later.
Select your app's target in the Project Editor, and then navigate to the Frameworks, Libraries, and Embedded Content section.
Add the new library: Select the + button, and then add FirebaseAI from the Firebase package.
After you've finished migrating your app (see the remaining sections in this guide), make sure to remove the old library:
Select FirebaseVertexAI-Preview , and then press the — button.
Kotlin
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle), replace old dependencies (as applicable) with the following.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old dependency.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }Sync your Android project with Gradle files.
Note that if you choose to not use the Firebase Android BoM , then just add the dependency for the firebase-ai library and accept the latest version that's suggested by Android Studio.
Java
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.ktsor<project>/<app-module>/build.gradle), replace old dependencies (as applicable) with the following.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old dependency.
// BEFORE dependencies {
implementation("com.google.firebase:firebase-vertexai:16.0.0-betaXX")} // AFTER dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.18.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") }Sync your Android project with Gradle files.
Note that if you choose to not use the Firebase Android BoM , then just add the dependency for the firebase-ai library and accept the latest version that's suggested by Android Studio.
Web
Get the latest version of the Firebase JS SDK for Web using npm:
npm i firebase@latest
یا
yarn add firebase@latest
Wherever you've imported the library, update your import statements to use
firebase/aiinstead.Note that it might be easier to migrate your app's codebase (see the remaining sections in this guide) before deleting the old imports.
// BEFORE import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai-preview";// AFTER import { initializeApp } from "firebase/app"; import { getAI, getGenerativeModel } from "firebase/ai";
Dart
Update to the use the
firebase_aipackage in yourpubspec.yamlfile by running the following command from your Flutter project directory:flutter pub add firebase_ai
Rebuild your Flutter project:
flutter run
After you've finished migrating your app (see the remaining sections in this guide), make sure to delete the old package:
flutter pub remove firebase_vertexai
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Step 4 : Update the initialization in your app
Click your Gemini API provider to view provider-specific content and code on this page. |
Update how you initialize the service for your chosen API provider and create a GenerativeModel instance.
سویفت
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.7-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.7-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.7-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.7-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.7-flash');
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Note that depending on the capability you're using, you might not always create a GenerativeModel instance . To stream input and output using the Gemini Live API , create a LiveModel instance.
Step 5 : Update your code depending on features that you use
This step describes changes that may be required depending on which features you use.
If you use Cloud Storage URLs and you swapped to use the Gemini Developer API in this migration, then you must update your multimodal requests to include files as inline data (or use YouTube URLs for videos).
Several changes were introduced for the GA versions of the " Vertex AI in Firebase " SDKs. These same changes are required to use the Firebase AI Logic SDKs. Review the following lists for any changes that you might need to make in your code to accommodate taking up the Firebase AI Logic SDK.
Required for all languages and platforms
فراخوانی تابع
If you implemented this feature before GA, then you'll need to make updates to how you define your schema. We recommend reviewing the updated function calling guide to learn how to write your function declarations.Generating structured output (like JSON) using
responseSchema
If you implemented this feature before GA, then you'll need to make updates to how you define your schema. We recommend reviewing the new structured output guide to learn how to write JSON schemas.تایم اوت
- Changed the default timeout for requests to be 180 seconds.
Required based on platform or language
سویفت
شمارشها
Replaced most
enumtypes withstructs with static variables. This change allows more flexibility for evolving the API in a backward-compatible way. When usingswitchstatements, you must now include adefault:case to cover unknown or unhandled values, including new values that are added to the SDK in the future.Renamed the
BlockThresholdenumeration toHarmBlockThreshold; this type is now astruct.Removed
unknownandunspecifiedcases from the following enumerations (nowstructs):HarmCategory,HarmBlockThreshold,HarmProbability,BlockReason, andFinishReason.Replaced the enumeration
ModelContent.Partwith a protocol namedPartto allow new types to be added in a backward-compatible way. This change is described in greater detail in the Content parts section.
Content parts
Removed the
ThrowingPartsRepresentableprotocol, and simplified the initializers forModelContentto avoid occasional compiler errors. Images that don't encode properly will still throw errors when being used ingenerateContent.Replaced the
ModelContent.Partcases with the followingstructtypes conforming to thePartprotocol:-
.texttoTextPart -
.datatoInlineDataPart -
.fileDatatoFileDataPart -
.functionCalltoFunctionCallPart -
.functionResponsetoFunctionResponsePart
-
Harm category
- Changed the
HarmCategoryto no longer be nested in theSafetySettingtype. If you're referring to it asSafetySetting.HarmCategory, that can be replaced withHarmCategory.
- Changed the
Safety feedback
- Removed the
SafetyFeedbacktype, since it wasn't used in any of the responses.
- Removed the
Citation metadata
- Renamed the
citationSourcesproperty tocitationsinCitationMetadata.
- Renamed the
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Candidate response
- Renamed
CandidateResponsetoCandidateto match other platforms.
- Renamed
Generation configuration
- Changed the public properties of
GenerationConfigtointernal. They all remain configurable in the initializer.
- Changed the public properties of
Kotlin
شمارشها
Replaced
enumclasses andsealedclasses with regular classes. This change allows more flexibility for evolving the API in a backward compatible way.Renamed the
BlockThresholdenumeration toHarmBlockThreshold.Removed values from the following enumerations:
HarmBlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
Blob methods
- Renamed all methods that included
Blobas part of their name to useInlineDatainstead.
- Renamed all methods that included
تنظیمات ایمنی
- Changed the field
methodto be nullable.
- Changed the field
Duration class
- Removed all usages of Kotlin's
Durationclass, and replaced it withlong. This change provides better interoperability with Java.
- Removed all usages of Kotlin's
Citation metadata
- Wrapped all the fields previously declared in
CitationMetadatainto a new class calledCitation. Citations can be found in the list calledcitationsinCitationMetadata. This change allows better alignment of types across platforms.
- Wrapped all the fields previously declared in
Count tokens
- Changed the field
totalBillableCharactersto be nullable.
- Changed the field
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Instantiating a model
- Moved the
requestOptionsparameter to the end of the parameter list to align with other platforms.
- Moved the
Live API
Removed
UNSPECIFIEDvalue for enum classResponseModality. Instead usenull.Renamed
LiveGenerationConfig.setResponseModalitiestoLiveGenerationConfig.setResponseModality.Removed the
LiveContentResponse.Statusclass, and instead have nested the status fields as properties ofLiveContentResponse.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Java
شمارشها
Replaced
enumclasses andsealedclasses with regular classes. This change allows more flexibility for evolving the API in a backward compatible way.Renamed the
BlockThresholdenumeration toHarmBlockThreshold.Removed values from the following enumerations:
HarmBlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
Blob methods
- Renamed all methods that included
Blobas part of their name to useInlineDatainstead.
- Renamed all methods that included
تنظیمات ایمنی
- Changed the field
methodto be nullable.
- Changed the field
Duration class
- Removed all usages of Kotlin's
Durationclass, and replaced it withlong. This change provides better interoperability with Java.
- Removed all usages of Kotlin's
Citation metadata
- Wrapped all the fields previously declared in
CitationMetadatainto a new class calledCitation. Citations can be found in the list calledcitationsinCitationMetadata. This change allows better alignment of types across platforms.
- Wrapped all the fields previously declared in
Count tokens
- Changed the field
totalBillableCharactersto be nullable.
- Changed the field
Total billable characters
- Changed the
totalBillableCharactersproperty inCountTokensResponseto be optional to reflect situations where no characters are sent.
- Changed the
Instantiating a model
- Moved the
requestOptionsparameter to the end of the parameter list to align with other platforms.
- Moved the
Live API
Removed
UNSPECIFIEDvalue for enum classResponseModality. Instead usenull.Renamed
LiveGenerationConfig.setResponseModalitiestoLiveGenerationConfig.setResponseModality.Removed the
LiveContentResponse.Statusclass, and instead have nested the status fields as properties ofLiveContentResponse.Removed the
LiveContentResponseclass, and instead have provided subclasses ofLiveServerMessagethat match the responses from the model.Changed
LiveModelFutures.connectto returnListenableFuture<LiveSessionFutures>instead ofListenableFuture<LiveSession>.
Changed various Java builder methods to now correctly return the instance of their class, instead of
void.
Web
شمارشها
- Removed values from the following enumerations:
HarmCategory,BlockThreshold,HarmProbability,HarmSeverity,BlockReason, andFinishReason.
- Removed values from the following enumerations:
دلیل مسدود شدن
- Changed
blockReasoninPromptFeedbackto be optional.
- Changed
Changes required only if you're starting to use the Gemini Developer API (instead of the Agent Platform Gemini API (formerly Vertex AI) ):
تنظیمات ایمنی
- Removed usages of the unsupported
SafetySetting.method.
- Removed usages of the unsupported
دادههای درونخطی
- Removed usages of the unsupported
InlineDataPart.videoMetadata.
- Removed usages of the unsupported
Dart
شمارشها
- Removed values from the following enumerations:
HarmCategory,HarmProbability,BlockReason, andFinishReason.
- Removed values from the following enumerations:
Data part
- Renamed
DataParttoInlineDataPart, and thestaticdatafunction toinlineDatato align with other platforms.
- Renamed
Request options
- Removed
RequestOptionssincetimeoutwasn't functional. It will be re-added in the near future, but it will be moved to theGenerativeModeltype to match other platforms.
- Removed
Stop sequences
- Changed the
stopSequencesparameter inGenerationConfigto be optional and to default tonullinstead of an empty array.
- Changed the
نقل قولها
- Renamed the
citationSourcesproperty tocitationsinCitationMetadata. TheCitationSourcetype was renamed toCitationto match other platforms.
- Renamed the
Unnecessary public types, methods, and properties
- Removed the following types, methods, and properties which were unintentionally exposed:
defaultTimeout,CountTokensResponseFields,parseCountTokensResponse,parseEmbedContentResponse,parseGenerateContentResponse,parseContent,BatchEmbedContentsResponse,ContentEmbedding,EmbedContentRequest, andEmbedContentResponse.
- Removed the following types, methods, and properties which were unintentionally exposed:
Count tokens
- Removed extra fields from the
countTokensfunction that are no longer necessary. Onlycontentsis needed.
- Removed extra fields from the
Instantiating a model
- Moved the
systemInstructionparameter to the end of the parameter list to align with other platforms.
- Moved the
Embedding functionality
- Removed unsupported embedding functionality (
embedContentandbatchEmbedContents) from the model.
- Removed unsupported embedding functionality (
وحدت
Support for Unity wasn't available from " Vertex AI in Firebase ".
Learn how to get started with the Firebase AI Logic SDK for Unity .
Possible errors related to migrating
As you're migrating to use the GA version of Firebase AI Logic , you might encounter errors if you haven't completed all of the required changes as described in this migration guide.
403 Error: Requests to this API firebasevertexai.googleapis.com ... are blocked.
If you receive a 403 error that says Requests to this API firebasevertexai.googleapis.com ... are blocked. , it usually means that the Firebase API key in your Firebase configuration file or object doesn't have a required API in its allowlist for the product that you're trying to use.
Make sure that the Firebase API key used by your app has all the required APIs included in the key's "API restrictions" allowlist . For Firebase AI Logic , your Firebase API key needs to have at minimum the Firebase AI Logic API in its allowlist. This API should have been automatically added to your API key's allowlist when you enabled the required APIs in the Firebase console .
You can view all your API keys in the APIs & Services > Credentials panel in the Google Cloud console.
Give feedback about your experience with Firebase AI Logic