firebase-ai-logic-basics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFirebase 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将生成式AI添加到移动和Web应用中。你无需管理专用后端,即可直接从应用中调用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@latestIf you're in a firebase directory (with a firebase.json) the currently selected project will be marked with "current" using this command:
npx -y firebase-tools@latest projects:listEnsure there's at least one app associated with the current project
npx -y firebase-tools@latest apps:listInitialize AI logic SDK with the init command
npx -y firebase-tools@latest init # Choose AI logicThis will automatically enable the Gemini Developer API in the Firebase console.
More info in Firebase AI Logic Getting Started
该库是标准Firebase Web SDK的一部分。
npm install -g firebase@latest如果你处于firebase目录(包含firebase.json文件),可通过以下命令查看当前选中的项目(标记为“current”):
npx -y firebase-tools@latest projects:list确保当前项目关联至少一个应用:
npx -y firebase-tools@latest apps:list使用init命令初始化AI Logic SDK:
npx -y firebase-tools@latest 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错误),请将其存储在Cloud Storage for Firebase中,并将其URL传递给Gemini Developer API。
Chat Session (Multi-turn)
对话会话(多轮交互)
Maintain history automatically using .
startChat使用自动维护对话历史。
startChatStreaming Responses
流式响应
To improve the user experience by showing partial results as they arrive (like a typing effect), use instead of for faster display of results.
generateContentStreamgenerateContent为提升用户体验,可在结果生成过程中显示部分内容(类似打字效果),使用替代以更快地展示结果。
generateContentStreamgenerateContentGenerate 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. (Example: 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
[!WARNING] Critical Safety Requirement: In order to use AI Logic safely, you MUST set up App Check on your app. This prevents unauthorized clients from using your API quota and accessing your backend resources.
See App Check with reCAPTCHA Enterprise for setup instructions.
[!WARNING] 关键安全要求: 为安全使用AI Logic,你必须在应用中设置App Check。这可防止未授权客户端占用你的API配额并访问后端资源。
设置说明请查看使用reCAPTCHA Enterprise的App Check。
Remote Config
Remote Config
Consider that you do not need to hardcode model names (e.g., ). Use Firebase Remote Config to update model versions dynamically without deploying new client code. See Changing model names remotely
gemini-flash-lite-latest建议不要硬编码模型名称(例如)。使用Firebase Remote Config动态更新模型版本,无需部署新的客户端代码。详情请查看远程更改模型名称
gemini-flash-lite-latestInitialization Code References
初始化代码参考
| Language, Framework, Platform | Gemini API provider | Context URL |
|---|---|---|
| Web Modular API | Gemini Developer API (Developer API) | firebase://docs/ai-logic/get-started |
Always use the most recent version of Gemini (gemini-flash-latest) unless another model is requested by the docs or the user. DO NOT USE gemini-1.5-flash
| 语言、框架、平台 | Gemini API提供商 | 上下文URL |
|---|---|---|
| Web模块化API | Gemini Developer API(开发者API) | firebase://docs/ai-logic/get-started |
除非文档或用户要求使用其他模型,否则请始终使用最新版本的Gemini(gemini-flash-latest)。请勿使用gemini-1.5-flash
References
参考资料
Web SDK code examples and usage patterns
Web SDK代码示例与使用模式