firebase-ai-logic

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Firebase AI Logic Basics

Firebase AI Logic 基础

Overview

概述

Firebase AI Logic is a product of Firebase that allows developers to add gen AI to their mobile and web apps using client-side SDKs. You can call Gemini models directly from your app without managing a dedicated backend. Firebase AI Logic, which was previously known as "Vertex AI for Firebase", represents the evolution of Google's AI integration platform for mobile and web developers.
It supports the two Gemini API providers:
  • Gemini Developer API: It has a free tier ideal for prototyping, and pay-as-you-go for production
  • Vertex AI Gemini API: Ideal for scale with enterprise-grade production readiness, requires Blaze plan
Use the Gemini Developer API as a default, and only Vertex AI Gemini API if the application requires it.
Firebase AI Logic是Firebase旗下产品,允许开发者通过客户端SDK在移动和Web应用中添加生成式AI功能。你可以直接从应用中调用Gemini模型,无需管理专用后端。Firebase AI Logic前身为“Vertex AI for Firebase”,是谷歌面向移动和Web开发者的AI集成平台的演进版本。
它支持两种Gemini API提供商:
  • Gemini Developer API:提供免费层级,非常适合原型开发;生产环境采用按需付费模式
  • Vertex AI Gemini API:适合规模化场景,具备企业级生产就绪能力,需要Blaze付费计划
默认使用Gemini Developer API,仅当应用有特定需求时才选择Vertex AI Gemini API。

Setup & Initialization

设置与初始化

Prerequisites

前提条件

  • Before starting, ensure you have Node.js 16+ and npm installed. Install them if they aren’t already available.
  • Identify the platform the user is interested in building on prior to starting: Android, iOS, Flutter or Web.
  • If their platform is unsupported, Direct the user to Firebase Docs to learn how to set up AI Logic for their application (share this link with the user https://firebase.google.com/docs/ai-logic/get-started)
  • 开始操作前,请确保已安装**Node.js 16+**和npm。如果尚未安装,请先完成安装。
  • 在开始前确认用户感兴趣的开发平台:Android、iOS、Flutter或Web。
  • 如果用户的平台不受支持,请引导他们查看Firebase文档以了解如何为其应用设置AI Logic(可分享此链接:https://firebase.google.com/docs/ai-logic/get-started)

Installation

安装

The library is part of the standard Firebase Web SDK.
npm install -g firebase@latest
If you're in a firebase directory (with a firebase.json) the currently selected project will be marked with "current" using this command:
firebase projects:list
Ensure there's at least one app associated with the current project
firebase apps:list
Initialize AI logic SDK with the init command
firebase init # Choose AI logic
This will automatically enable the Gemini Developer API in the Firebase console.
该库是标准Firebase Web SDK的一部分。
npm install -g firebase@latest
如果你处于firebase目录下(包含firebase.json文件),可通过以下命令查看当前选中的项目(标记为“current”):
firebase projects:list
确保当前项目至少关联了一个应用
firebase apps:list
使用init命令初始化AI Logic SDK
firebase init # 选择AI Logic
此操作会自动在Firebase控制台中启用Gemini Developer API。
更多信息请查看Firebase AI Logic 快速入门

Core Capabilities

核心功能

Text-Only Generation

纯文本生成

Multimodal (Text + Images/Audio/Video/PDF input)

多模态(文本 + 图片/音频/视频/PDF输入)

Firebase AI Logic allows Gemini models to analyze image files directly from your app. This enables features like creating captions, answering questions about images, detecting objects, and categorizing images. Beyond images, Gemini can analyze other media types like audio, video, and PDFs by passing them as inline data with their MIME type. For files larger than 20 megabytes (which can cause HTTP 413 errors as inline data), store them in Cloud Storage for Firebase and pass their URLs to the Gemini Developer API.
Firebase AI Logic允许Gemini模型直接分析应用中的图片文件。这可实现生成图片说明、回答图片相关问题、检测物体以及图片分类等功能。除图片外,Gemini还可分析音频、视频和PDF等其他媒体类型,只需将它们作为带有MIME类型的内联数据传递即可。对于超过20MB的文件(作为内联数据传递可能会导致HTTP 413错误),请将其存储在Firebase Cloud Storage中,然后将其URL传递给Gemini Developer API。

Chat Session (Multi-turn)

对话会话(多轮交互)

Maintain history automatically using
startChat
.
使用
startChat
自动维持对话历史。

Streaming Responses

流式响应

To improve the user experience by showing partial results as they arrive (like a typing effect), use
generateContentStream
instead of
generateContent
for faster display of results.
为提升用户体验,可在结果生成过程中显示部分内容(类似打字效果),请使用
generateContentStream
替代
generateContent
以更快地展示结果。

Generate Images with Nano Banana

使用Nano Banana生成图片

  • Start with Gemini for most use cases, and choose Imagen for specialized tasks where image quality and specific styles are critical. (gemini-2.5-flash-image)
  • Requires an upgraded Blaze pay-as-you-go billing plan.
  • 大多数场景下优先使用Gemini,当对图片质量和特定风格有特殊要求时,再选择Imagen。(推荐模型:gemini-2.5-flash-image)
  • 需要升级至Blaze按需付费计费计划。

Search Grounding with the built in googleSearch tool

内置googleSearch工具的搜索 Grounding

Supported Platforms and Frameworks

支持的平台与框架

Supported Platforms and Frameworks include Kotlin and Java for Android, Swift for iOS, JavaScript for web apps, Dart for Flutter, and C Sharp for Unity.
支持的平台与框架包括:Android平台的Kotlin和Java、iOS平台的Swift、Web应用的JavaScript、Flutter平台的Dart,以及Unity平台的C#。

Advanced Features

高级功能

Structured Output (JSON)

结构化输出(JSON)

Enforce a specific JSON schema for the response.
强制要求响应符合特定的JSON schema。

On-Device AI (Hybrid)

设备端AI(混合模式)

Hybrid on-device inference for web apps, where the Firebase Javascript SDK automatically checks for Gemini Nano's availability (after installation) and switches between on-device or cloud-hosted prompt execution. This requires specific steps to enable model usage in the Chrome browser, more info in the hybrid-on-device-inference documentation.
Web应用的混合设备端推理:Firebase JavaScript SDK会自动检查Gemini Nano的可用性(安装完成后),并在设备端或云端执行提示词之间自动切换。这需要特定步骤来启用Chrome浏览器中的模型使用,更多信息请查看混合设备端推理文档

Security & Production

安全与生产环境

App Check

App Check

Recommended: The developer must enable Firebase App Check to prevent unauthorized clients from using their API quota. see App-check recaptcha enterprise.
推荐操作:开发者必须启用Firebase App Check,以防止未授权客户端占用API配额。详情请查看App-check recaptcha enterprise

Remote Config

Remote Config

Consider that you do not need to hardcode model names (e.g.,
gemini-2.5-flash-lite
). Use Firebase Remote Config to update model versions dynamically without deploying new client code. See Changing model names remotely
建议不要硬编码模型名称(例如
gemini-2.5-flash-lite
)。使用Firebase Remote Config可动态更新模型版本,无需部署新的客户端代码。 详情请查看远程更改模型名称

Initialization Code References

初始化代码参考

Language, Framework, PlatformGemini API providerContext URL
Web Modular APIGemini Developer API (Developer API)firebase://docs/ai-logic/get-started
Always use gemini-2.5-flash or gemini-3-flash-preview unless another model is requested by the docs or the user. DO NOT USE gemini 1.5 flash
语言、框架、平台Gemini API 提供商参考链接
Web Modular APIGemini Developer API (开发者API)firebase://docs/ai-logic/get-started
除非文档要求或用户指定,否则请始终使用gemini-2.5-flash或gemini-3-flash-preview,请勿使用gemini 1.5 flash

References

参考资料

Web SDK code examples and usage patterns
Web SDK代码示例与使用模式