Xây dựng các ứng dụng và tính năng dựa trên AI cho thiết bị di động và web bằng các mô hình Gemini và Imagen thông qua Firebase AI Logic
Firebase AI Logic mang đến cho bạn quyền truy cập vào các mô hình AI tạo sinh mới nhất của Google: mô hình Gemini và mô hình Imagen.
Nếu cần gọi trực tiếp Gemini API hoặc Imagen API từ ứng dụng di động hoặc ứng dụng web của bạn (thay vì phía máy chủ), bạn có thể sử dụng SDK Firebase AI Logic. Các SDK máy khách này được tạo riêng để sử dụng với ứng dụng di động và ứng dụng web, cung cấp các lựa chọn bảo mật chống lại các máy khách trái phép cũng như tích hợp với các dịch vụ khác của Firebase.
Các SDK ứng dụng này có trong Swift cho các nền tảng của Apple, Kotlin và Java cho Android, JavaScript cho web, Dart cho Flutter và Unity.
Với các SDK máy khách này, bạn có thể thêm tính năng cá nhân hoá bằng AI vào các ứng dụng, tạo trải nghiệm trò chuyện bằng AI, tạo các tính năng tối ưu hoá và tự động hoá dựa trên AI, cũng như nhiều tính năng khác!
Bạn cần có tính linh hoạt cao hơn hoặc tích hợp phía máy chủ? Genkit là khung nguồn mở của Firebase để phát triển AI phía máy chủ tinh vi, có quyền truy cập rộng rãi vào các mô hình của Google, OpenAI, Anthropic và nhiều mô hình khác. Công cụ này có các tính năng AI nâng cao hơn và công cụ chuyên dụng tại địa phương.
Các chức năng chính
Thông tin đầu vào đa phương thức và ngôn ngữ tự nhiên
Các mô hình Gemini là mô hình đa phương thức, vì vậy, câu lệnh gửi đến Gemini API có thể bao gồm văn bản, hình ảnh, tệp PDF, video và âm thanh. Một số mô hình Gemini cũng có thể tạo ra đầu ra đa phương thức.
Bạn có thể đưa ra lời nhắc cho cả mô hình Gemini và Imagen bằng đầu vào ngôn ngữ tự nhiên.
Bảo mật và ngăn chặn hành vi sai trái đối với các ứng dụng phát hành công khai
Sử dụng Firebase App Check để giúp bảo vệ các API truy cập vào mô hình Gemini và Imagen khỏi hành vi xâm phạm của các ứng dụng trái phép.
Firebase AI Logic cũng có giới hạn về tốc độ cho mỗi người dùngtheo mặc định và bạn có thể định cấu hình hoàn toàn các giới hạn về tốc độ cho mỗi người dùng này.
Cơ sở hạ tầng mạnh mẽ
Tận dụng cơ sở hạ tầng có khả năng mở rộng được xây dựng để sử dụng với các ứng dụng di động và web, chẳng hạn như quản lý tệp bằng Cloud Storage for Firebase, quản lý dữ liệu có cấu trúc bằng các dịch vụ cơ sở dữ liệu của Firebase (chẳng hạn như Cloud Firestore) và thiết lập cấu hình thời gian chạy một cách linh động bằng Firebase Remote Config.
Tính năng này hoạt động như thế nào?
Firebase AI Logic cung cấp các SDK ứng dụng, dịch vụ proxy và các tính năng khác cho phép bạn truy cập vào các mô hình AI tạo sinh của Google để tạo các tính năng AI trong ứng dụng di động và ứng dụng web của mình.
Hỗ trợ các mô hình của Google và nhà cung cấp "Gemini API"
Chúng tôi hỗ trợ tất cả các mô hình Gemini và mô hình Imagen 3 mới nhất, đồng thời bạn có thể chọn nhà cung cấp "Gemini API" mà bạn muốn để truy cập vào các mô hình này.
Chúng tôi hỗ trợ cả Gemini Developer API và Vertex AI Gemini API. Tìm hiểu về sự khác biệt giữa việc sử dụng hai nhà cung cấp API.
Và nếu chọn sử dụng Gemini Developer API, bạn có thể tận dụng "bậc miễn phí" của họ để nhanh chóng bắt đầu và chạy.
SDK ứng dụng di động và SDK ứng dụng web
Bạn gửi yêu cầu đến các mô hình ngay từ ứng dụng di động hoặc ứng dụng web của mình bằng cách sử dụng Firebase AI Logic SDK máy khách của chúng tôi. SDK này có sẵn bằng Swift cho các nền tảng của Apple, Kotlin và Java cho Android, JavaScript cho Web, Dart cho Flutter và Unity.
Nếu đã thiết lập cả hai nhà cung cấp Gemini API trong dự án Firebase, thì bạn có thể chuyển đổi giữa các nhà cung cấp API chỉ bằng cách bật API khác và thay đổi một vài dòng mã khởi chạy.
Ngoài ra, SDK ứng dụng của chúng tôi cho Web cung cấp quyền truy cập thử nghiệm vào suy luận kết hợp và trên thiết bị cho các ứng dụng web chạy trên Chrome trên máy tính. Cấu hình này cho phép ứng dụng của bạn sử dụng mô hình trên thiết bị khi có sẵn, nhưng chuyển đổi liền mạch sang mô hình được lưu trữ trên đám mây khi cần.
Dịch vụ proxy
Dịch vụ proxy của chúng tôi đóng vai trò là cổng kết nối giữa máy khách và nhà cung cấp Gemini API mà bạn chọn (cũng như các mô hình của Google). Nó cung cấp các dịch vụ và hoạt động tích hợp quan trọng cho ứng dụng di động và ứng dụng web. Ví dụ: bạn có thể thiết lập Firebase App Check để giúp bảo vệ nhà cung cấp API mà bạn chọn và các tài nguyên phụ trợ của bạn khỏi hành vi sử dụng sai cách của các ứng dụng trái phép.
Điều này đặc biệt quan trọng nếu bạn chọn sử dụng Gemini Developer API vì dịch vụ proxy của chúng tôi và chế độ tích hợp App Check này đảm bảo rằng khoá API Gemini của bạn vẫn nằm trên máy chủ và không được nhúng vào cơ sở mã của ứng dụng.
Đường dẫn triển khai
Thiết lập dự án Firebase và kết nối ứng dụng của bạn với Firebase
Sử dụng quy trình có hướng dẫn trong trang Firebase AI Logic của bảng điều khiển Firebase để thiết lập dự án (bao gồm cả việc bật các API bắt buộc cho nhà cung cấp Gemini API mà bạn chọn), đăng ký ứng dụng với dự án Firebase, sau đó thêm cấu hình Firebase vào ứng dụng.
Cài đặt và khởi chạy SDK
Cài đặt SDK Firebase AI Logic dành riêng cho nền tảng của ứng dụng, sau đó khởi chạy dịch vụ và tạo một thực thể mô hình trong ứng dụng.
Gửi yêu cầu cho câu lệnh đến các mô hình Gemini và Imagen
Triển khai các hoạt động tích hợp quan trọng cho ứng dụng di động và ứng dụng web, chẳng hạn như bảo vệ API khỏi hành vi sai trái bằng Firebase App Check và sử dụng Firebase Remote Config để cập nhật các tham số trong mã từ xa (chẳng hạn như tên mô hình).
Các bước tiếp theo
Bắt đầu truy cập vào một mô hình từ ứng dụng di động hoặc ứng dụng web của bạn
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-28 UTC."],[],[],null,["Gemini API using Firebase AI Logic \nplat_ios plat_android plat_web plat_flutter plat_unity \nBuild AI-powered mobile and web apps and features with the Gemini and Imagen models using Firebase AI Logic \n\nFirebase AI Logic gives you access to the latest generative AI models from\nGoogle: the Gemini models and Imagen models.\n\nIf you need to call the Gemini API or Imagen API directly\nfrom your mobile or web app --- rather than server-side --- you can use the\nFirebase AI Logic client SDKs. These client SDKs are built\nspecifically for use with mobile and web apps, offering security options against\nunauthorized clients as well as integrations with other Firebase services.\n\n**These client SDKs are available in\nSwift for Apple platforms, Kotlin \\& Java for Android, JavaScript for web,\nDart for Flutter, and Unity.**\n\n\n| **Firebase AI Logic and its client SDKs were\n| formerly called \"Vertex AI in Firebase\".** In May 2025, we [renamed and\n| repackaged our services into Firebase AI Logic](/docs/ai-logic/faq-and-troubleshooting#renamed-product) to better reflect our expanded services and features --- for example, we now support the Gemini Developer API!\n\n\u003cbr /\u003e\n\nWith these client SDKs, you can add AI personalization to apps, build an AI chat\nexperience, create AI-powered optimizations and automation, and much more!\n\n[Get started](/docs/ai-logic/get-started)\n\n\u003cbr /\u003e\n\n**Need more flexibility or server-side integration?** \n\n[Genkit](https://genkit.dev/) is Firebase's open-source\nframework for sophisticated server-side AI development with broad access to\nmodels from Google, OpenAI, Anthropic, and more. It includes more advanced AI\nfeatures and dedicated local tooling.\n\nKey capabilities\n\n|---------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Multimodal and natural language input | The [Gemini models](/docs/ai-logic/models) are multimodal, so prompts sent to the Gemini API can include text, images, PDFs, video, and audio. Some Gemini models can also generate multimodal *output* . Both the Gemini and Imagen models can be prompted with natural language input. |\n| Growing suite of capabilities | With the SDKs, you can call the Gemini API or Imagen API directly from your mobile or web app to [build AI chat experiences](/docs/ai-logic/chat), [generate images,](/docs/ai-logic/generate-images-imagen) use tools (like [function calling](/docs/ai-logic/function-calling) and [grounding with Google Search](/docs/ai-logic/grounding-google-search)), [stream multimodal input and output (including audio)](/docs/ai-logic/live-api), and more. |\n| Security and abuse prevention for production apps | Use [Firebase App Check](/docs/ai-logic/app-check) to help protect the APIs that access the Gemini and Imagen models from abuse by unauthorized clients. Firebase AI Logic also has [rate limits per user](/docs/ai-logic/faq-and-troubleshooting#rate-limits-per-user) *by default*, and these per-user rate limits are fully configurable. |\n| Robust infrastructure | Take advantage of scalable infrastructure that's built for use with mobile and web apps, like [managing files with Cloud Storage for Firebase](/docs/ai-logic/solutions/cloud-storage), managing structured data with Firebase database offerings (like [Cloud Firestore](/docs/firestore)), and dynamically setting run-time configurations with [Firebase Remote Config](/docs/ai-logic/solutions/remote-config). |\n\nHow does it work?\n\nFirebase AI Logic provides client SDKs, a proxy service, and other features\nwhich allow you to access Google's generative AI models to build AI features in\nyour mobile and web apps.\n\nSupport for Google models and \"Gemini API\" providers\n\nWe support all the latest Gemini models and Imagen 3 models,\nand you choose your preferred \"Gemini API\" provider to access these models.\nWe support both the Gemini Developer API and\nVertex AI Gemini API. Learn about the\n[differences between using the two API providers](/docs/ai-logic/faq-and-troubleshooting#differences-between-gemini-api-providers).\n\nAnd if you choose to use the Gemini Developer API, you can take\nadvantage of their \"free tier\" to get you up and running fast.\n\nMobile \\& web client SDKs\n\nYou send requests to the models directly from your mobile or web app using our\nFirebase AI Logic client SDKs, available in\nSwift for Apple platforms, Kotlin \\& Java for Android, JavaScript for Web,\nDart for Flutter, and Unity.\n\nIf you have both of the Gemini API providers set up in your Firebase\nproject, then you can switch between API providers just by enabling the other\nAPI and changing a few lines of initialization code.\n\nAdditionally, our client SDK for Web offers experimental access to\n[hybrid and on-device inference for web apps](/docs/ai-logic/hybrid-on-device-inference)\nrunning on Chrome on desktop. This configuration allows your app to use the\non-device model when it's available, but fall back seamlessly to the\ncloud-hosted model when needed.\n\nProxy service\n\nOur proxy service acts as a gateway between the client and your chosen\nGemini API provider (and Google's models). It provides services and\nintegrations that are important for mobile and web apps. For example, you can\n[set up Firebase App Check](/docs/ai-logic/app-check) to help protect your\nchosen API provider and your backend resources from abuse by unauthorized\nclients.\n\nThis is particularly critical if you chose to use the\nGemini Developer API because our proxy service and this App Check\nintegration make sure that your Gemini API key stays on the server and\nis *not* embedded in your apps' codebase.\n\nImplementation path\n\n|---|---------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Set up your Firebase project and connect your app to Firebase | Use the guided workflow in the [**Firebase AI Logic** page](https://console.firebase.google.com/project/_/ailogic) of the Firebase console to set up your project (including enabling the required APIs for your chosen Gemini API provider), register your app with your Firebase project, and then add your Firebase configuration to your app. |\n| | Install the SDK and initialize | Install the Firebase AI Logic SDK that's specific to your app's platform, and then initialize the service and create a model instance in your app. |\n| | Send prompt requests to the Gemini and Imagen models | Use the SDKs to send text-only or multimodal prompts to a Gemini model to generate [text and code](/docs/ai-logic/generate-text), [structured output (like JSON)](/docs/ai-logic/generate-structured-output) and [images](/docs/ai-logic/generate-images-gemini). Alternatively, you can also prompt an Imagen model to [generate images](/docs/ai-logic/generate-images-imagen). Build richer experiences with [multi-turn chat](/docs/ai-logic/chat), [bidirectional streaming of text and audio](/docs/ai-logic/live-api), and [function calling](/docs/ai-logic/function-calling). |\n| | Prepare for production | Implement important integrations for mobile and web apps, like protecting the API from abuse with [Firebase App Check](/docs/ai-logic/app-check) and using [Firebase Remote Config](/docs/ai-logic/solutions/remote-config) to update parameters in your code remotely (like model name). |\n\nNext steps\n\nGet started with accessing a model from your mobile or web app\n\n[Go to Getting Started guide](/docs/ai-logic/get-started)\n\n\nLearn more about the supported models Learn about the [models available for various use cases](/docs/ai-logic/models) and their [quotas](/docs/ai-logic/quotas) and [pricing](/docs/ai-logic/pricing).\n\n\u003cbr /\u003e"]]