@@ -109,3 +109,75 @@ print(response.content)
109109Hybrid on-device inference is not available via Firebase AI Logic on Flutter
110110(including Flutter web); the ` firebase_ai ` package always calls the cloud-hosted
111111Gemini API. See https://firebase.google.com/docs/ai-logic/hybrid.md.txt .
112+
113+ ## Web (JavaScript)
114+
115+ On Web, on-device inference uses Chrome's built-in
116+ [ Prompt API] ( https://developer.chrome.com/docs/extensions/ai/prompt-api ) on
117+ Chrome Desktop (v139+, with at least 22 GB free storage and >4 GB VRAM or 16 GB
118+ RAM). For local development, enable
119+ ` chrome://flags/#prompt-api-for-gemini-nano-multimodal-input ` and restart
120+ Chrome; for end users, serve the
121+ [ Prompt API Chrome Origin Trial] ( https://developer.chrome.com/origintrials/#/view_trial/2533837740349325313 )
122+ token.
123+
124+ Create the hybrid model with ` mode: InferenceMode.PREFER_ON_DEVICE ` , call
125+ ` await hybridModel.initializeDeviceModel() ` on a user gesture to ensure Gemini
126+ Nano is downloaded and initialized, then call ` generateContent ` and inspect
127+ ` result.response.inferenceSource ` :
128+
129+ ``` javascript
130+ import { getGenerativeModel , InferenceMode , InferenceSource } from " firebase/ai" ;
131+
132+ const hybridModel = getGenerativeModel (ai, {
133+ mode: InferenceMode .PREFER_ON_DEVICE ,
134+ });
135+
136+ // Call initializeDeviceModel() on or after an end-user interaction (e.g. button click)
137+ document .querySelector (" #generate" ).addEventListener (" click" , async () => {
138+ await hybridModel .initializeDeviceModel ((progress ) => {
139+ console .log (` On-device model download progress: ${ Math .round (progress * 100 )} %` );
140+ });
141+
142+ const prompt = " Write a story about a magic backpack." ;
143+ const result = await hybridModel .generateContent (prompt);
144+
145+ // Compare with InferenceSource.ON_DEVICE ("on_device") or InferenceSource.IN_CLOUD ("in_cloud")
146+ const usedOnDevice = result .response .inferenceSource === InferenceSource .ON_DEVICE ;
147+ console .log (` Source: ${ result .response .inferenceSource } (onDevice=${ usedOnDevice} )` );
148+ console .log (result .response .text ());
149+ });
150+ ```
151+
152+ To override the default cloud model (` gemini-3.5-flash-lite ` in
153+ ` firebase@13.0.0+ ` ; ` gemini-2.5-flash-lite ` in ` firebase@12.x ` ) or request
154+ structured output from both paths, configure ` inCloudParams ` and
155+ ` onDeviceParams ` together:
156+
157+ ``` javascript
158+ import { getGenerativeModel , InferenceMode , Schema } from " firebase/ai" ;
159+
160+ const jsonSchema = Schema .object ({
161+ properties: { name: Schema .string (), species: Schema .string () },
162+ });
163+
164+ const hybridModel = getGenerativeModel (ai, {
165+ mode: InferenceMode .PREFER_ON_DEVICE ,
166+ // Cloud-hosted model settings (overrides the SDK's default cloud model)
167+ inCloudParams: {
168+ model: " <latest_supported_model>" , // [AGENT] Replace with the latest model from https://firebase.google.com/docs/ai-logic/models.md.txt
169+ generationConfig: {
170+ responseMimeType: " application/json" ,
171+ responseSchema: jsonSchema,
172+ },
173+ },
174+ // On-device model settings (Chrome Prompt API options)
175+ onDeviceParams: {
176+ promptOptions: { responseConstraint: jsonSchema },
177+ },
178+ });
179+ ```
180+
181+ For enum output use ` Schema.enumString({ enum: [...] }) ` with
182+ ` responseMimeType: "text/x.enum" ` in ` inCloudParams ` and the same schema as the
183+ ` responseConstraint ` .
0 commit comments