### Code Execution Tool Setup Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples?api=vertex&hl=zh-tw This example configures the model to use the code execution tool. This allows the model to generate and run Python code to perform calculations or other tasks. ```yaml --- model: 'gemini-3-flash-preview' tools: - codeExecution --- ``` ```prompt What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50. ``` -------------------------------- ### Gemini Model Configuration Example Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples This example shows how to configure Gemini model generation parameters such as temperature, top-K, top-P, and max output tokens within the YAML frontmatter. ```yaml --- model: 'gemini-3-flash-preview' config: candidateCount: 1 temperature: 0.9 topP: 0.1 topK: 16 maxOutputTokens: 200 stopSequences: ["red"] --- Write a story about a magic backpack. ``` -------------------------------- ### Basic Multimodal Input Example Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples?hl=es-419 Use this basic example for providing simple multimodal input. Ensure the media type and image data are correctly formatted. ```yaml --- model: "gemini-3-flash-preview" --- ``` ```prompt Describe this image {{media type="mimeType" data="imageData"}} ``` -------------------------------- ### Basic Gemini Request Example Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples?api=vertex This is a minimal example of a server prompt template for a Gemini model. It includes YAML frontmatter for model selection and a simple prompt. ```yaml --- model: 'gemini-3-flash-preview' --- All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. Create an example customer invoice for a customer named {{customerName}}. ``` -------------------------------- ### Stream audio with Kotlin Source: https://firebase.google.com/docs/ai-logic/live-api Initializes a LiveModel with AUDIO modality and starts an audio conversation session. ```kotlin // Initialize the Gemini Developer API backend service // Create a `liveModel` instance with a model that supports the Live API val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "gemini-2.5-flash-native-audio-preview-12-2025", // Configure the model to respond with audio generationConfig = liveGenerationConfig { responseModality = ResponseModality.AUDIO } ) val session = liveModel.connect() // This is the recommended approach. // However, you can create your own recorder and handle the stream. session.startAudioConversation() ``` -------------------------------- ### Initialize and Generate Content with Web SDK Source: https://firebase.google.com/docs/ai-logic/analyze-documents?hl=fr Setup Firebase AI and generate content from a file input using the Web SDK. ```javascript 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-2.5-flash" }); ``` ```javascript // Converts a File object to a Part object. async function fileToGenerativePart(file) { const base64EncodedDataPromise = new Promise((resolve) => { const reader = new FileReader(); reader.onloadend = () => resolve(reader.result.split(',')); reader.readAsDataURL(file); }); return { inlineData: { data: await base64EncodedDataPromise, mimeType: file.type }, }; } async function run() { // Provide a text prompt to include with the PDF file const prompt = "Summarize the important results in this report."; // Prepare PDF file for input const fileInputEl = document.querySelector("input[type=file]"); const pdfPart = await fileToGenerativePart(fileInputEl.files); // To generate text output, call `generateContent` with the text and PDF file const result = await model.generateContent([prompt, pdfPart]); // Log the generated text, handling the case where it might be undefined console.log(result.response.text() ?? "No text in response."); } run(); ``` -------------------------------- ### Example Response Output Source: https://firebase.google.com/docs/ai-logic/thinking?hl=it Sample output showing the final answer returned by the model. ```text # Example Response: # Okay, let's solve the quadratic equation x² + 4x + 4 = 0. # ... # **Answer:** # The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2). ``` -------------------------------- ### Gemini Model Request Example Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples This example demonstrates a basic request to a Gemini model, including YAML frontmatter for model specification and a user prompt with a Handlebars variable. ```yaml --- model: 'gemini-3-flash-preview' --- {{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer named {{customerName}}. ``` -------------------------------- ### Set System Instructions in Java Source: https://firebase.google.com/docs/ai-logic/system-instructions Initialize a `GenerativeModel` with system instructions in Java. This allows you to configure the AI's behavior from the start. ```java // Specify the system instructions as part of creating the `GenerativeModel` instance GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( /* modelName */ "GEMINI_MODEL_NAME", /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* requestOptions (optional) */ new RequestOptions(), /* tools (optional) */ null, /* toolsConfig (optional) */ null, /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() ); GenerativeModelFutures model = GenerativeModelFutures.from(ai); ``` -------------------------------- ### Advanced Prompt with System Instructions and Input Variable Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/get-started This example demonstrates a more complex prompt structure using system instructions for output formatting and a user prompt with a Handlebars input variable for customer name. ```text {{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer named {{customerName}}. ``` -------------------------------- ### Enable URL Context and Google Search Grounding Source: https://firebase.google.com/docs/ai-logic/url-context Examples demonstrating how to initialize the Gemini model with both URL context and Google Search tools enabled across different programming languages. ```Swift 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_MODEL_NAME", // Enable both the URL context tool and Google Search tool. tools: [ Tool.urlContex(), Tool.googleSearch() ] ) // Specify one or more URLs for the tool to access. let url = "YOUR_URL" // Provide the URLs in the prompt sent in the request. // If the model can't generate a response using its own knowledge or the content in the specified URL, // then the model will use the grounding with Google Search tool. let prompt = "Give me a three day event schedule based on \(url). Also what do I need to pack according to the weather?" // Get and handle the model's response. let response = try await model.generateContent(prompt) print(response.text ?? "No text in response.") // Make sure to comply with the "Grounding with Google Search" usage requirements, // which includes how you use and display the grounded result ``` ```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( modelName = "GEMINI_MODEL_NAME", // Enable both the URL context tool and Google Search tool. tools = listOf(Tool.urlContext(), Tool.googleSearch()) ) // Specify one or more URLs for the tool to access. val url = "YOUR_URL" // Provide the URLs in the prompt sent in the request. // If the model can't generate a response using its own knowledge or the content in the specified URL, // then the model will use the grounding with Google Search tool. val prompt = "Give me a three day event schedule based on $url. Also what do I need to pack according to the weather?" // Get and handle the model's response. val response = model.generateContent(prompt) print(response.text) // Make sure to comply with the "Grounding with Google Search" usage requirements, // which includes how you use and display the grounded result ``` ```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_MODEL_NAME", null, null, // Enable both the URL context tool and Google Search tool. List.of(Tool.urlContext(new UrlContext()), Tool.googleSearch(new GoogleSearch()))); // Use the GenerativeModelFutures Java compatibility layer which offers // support for ListenableFuture and Publisher APIs GenerativeModelFutures model = GenerativeModelFutures.from(ai); // Specify one or more URLs for the tool to access. String url = "YOUR_URL"; // Provide the URLs in the prompt sent in the request. // If the model can't generate a response using its own knowledge or the content in the specified URL, // then the model will use the grounding with Google Search tool. String prompt = "Give me a three day event schedule based on " + url + ". Also what do I need to pack according to the weather?"; ListenableFuture response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback() { ``` -------------------------------- ### Stream audio with Java Source: https://firebase.google.com/docs/ai-logic/live-api Uses LiveModelFutures to manage the session lifecycle and start an audio conversation. ```java ExecutorService executor = Executors.newFixedThreadPool(1); // Initialize the Gemini Developer API backend service // Create a `liveModel` instance with a model that supports the Live API LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel( "gemini-2.5-flash-native-audio-preview-12-2025", // Configure the model to respond with audio new LiveGenerationConfig.Builder() .setResponseModality(ResponseModality.AUDIO) .build() ); LiveModelFutures liveModel = LiveModelFutures.from(lm); ListenableFuture sessionFuture = liveModel.connect(); Futures.addCallback(sessionFuture, new FutureCallback() { @Override public void onSuccess(LiveSession ses) { LiveSessionFutures session = LiveSessionFutures.from(ses); session.startAudioConversation(); } @Override public void onFailure(Throwable t) { // Handle exceptions } }, executor); ``` -------------------------------- ### Initialize and Generate Content with C# SDK Source: https://firebase.google.com/docs/ai-logic/analyze-documents?hl=fr Setup Firebase AI and generate content from a streaming asset PDF using the C# SDK. ```csharp using Firebase; using Firebase.AI; // Initialize the Gemini Developer API backend service var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()); // Create a `GenerativeModel` instance with a model that supports your use case var model = ai.GetGenerativeModel(modelName: "gemini-2.5-flash"); ``` ```csharp // Provide a text prompt to include with the PDF file var prompt = ModelContent.Text("Summarize the important results in this report."); // Provide the PDF file as `data` with the appropriate PDF file MIME type var doc = ModelContent.InlineData("application/pdf", System.IO.File.ReadAllBytes(System.IO.Path.Combine( UnityEngine.Application.streamingAssetsPath, "document0.pdf"))); // To generate text output, call `GenerateContentAsync` with the text and PDF file var response = await model.GenerateContentAsync(new [] { prompt, doc }); // Print the generated text UnityEngine.Debug.Log(response.Text ?? "No text in response."); ``` -------------------------------- ### Stream audio input in Kotlin Source: https://firebase.google.com/docs/ai-logic/live-api/capabilities This Kotlin code snippet shows how to initialize the Live API and start an audio conversation. It configures the model to respond with audio. ```kotlin // Initialize the Gemini Developer API backend service // Create a `liveModel` instance with a model that supports the Live API val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "gemini-2.5-flash-native-audio-preview-12-2025", // Configure the model to respond with audio generationConfig = liveGenerationConfig { responseModality = ResponseModality.AUDIO } ) val session = liveModel.connect() // This is the recommended approach. // However, you can create your own recorder and handle the stream. session.startAudioConversation() ``` -------------------------------- ### Basic Server Prompt Template Format Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/get-started This example shows the basic structure of a server prompt template using Dotprompt syntax. It includes YAML frontmatter for model configuration and the prompt body with system instructions and a user input variable. ```dotprompt --- model: 'gemini-3-flash-preview' --- {{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer named {{customerName}}. ``` -------------------------------- ### Example Thought Summary Output Source: https://firebase.google.com/docs/ai-logic/thinking?hl=it Sample output showing the internal reasoning process captured in the thought summary. ```text # Example Thought Summary: # **My Thought Process for Solving the Quadratic Equation** # # Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first: # it's a quadratic; the x² term gives it away, and we know the general form is # ax² + bx + c = 0. # # So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the # most efficient path to the solution? My gut tells me to try factoring; it's # often the fastest route if it works. If that fails, I'll default to the quadratic # formula, which is foolproof. Completing the square? It's good for deriving the # formula or when factoring is difficult, but not usually my first choice for # direct solving, but it can't hurt to keep it as an option. # # Factoring, then. I need to find two numbers that multiply to 'c' (4) and add # up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo! # They multiply to 4 and add up to 4. This means I can rewrite the equation as # (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now # trivial: x + 2 = 0, thus x = -2. # # Okay, just to be absolutely certain, I'll run the quadratic formula just to # double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ± # √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x = # -2 again - a repeated root. Nice. # # Now, let's check via completing the square. Starting from the same equation, # (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and # add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)² # = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as # expected. # # Always, *always* confirm! Let's substitute x = -2 back into the original # equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out. # # Conclusion: the solution is x = -2. Confirmed. ``` -------------------------------- ### Define a server prompt template Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/get-started?api=vertex Example of a Dotprompt template structure including YAML frontmatter for model configuration and Handlebars syntax for dynamic user inputs. ```text --- model: 'gemini-3-flash-preview' --- {{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer named {{customerName}}. ``` -------------------------------- ### Implement Real-time Remote Config Listener Source: https://firebase.google.com/docs/ai-logic/change-model-name-remotely Examples of adding real-time listeners to Remote Config to handle parameter updates and trigger activation. ```Swift // Add real-time Remote Config remoteConfig.addOnConfigUpdateListener { configUpdate, error in guard let configUpdate = configUpdate, error == nil else { print("Error listening for config updates: \(error?.localizedDescription ?? "No error available")") return } print("Updated keys: \(configUpdate.updatedKeys)") remoteConfig.activate { changed, error in guard error == nil else { print("Error activating config: \(error?.localizedDescription ?? "No error available")") return } print("Activated config successfully") } } ``` ```Kotlin // Add a real-time Remote Config listener remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener { override fun onUpdate(configUpdate : ConfigUpdate) { Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.updatedKeys); remoteConfig.activate().addOnCompleteListener { // Optionally, add an action to perform on update here. } } override fun onError(error : FirebaseRemoteConfigException) { Log.w(ContentValues.TAG, "Config update error with code: " + error.code, error) } } ``` ```Java // Add a real-time Remote Config listener remoteConfig.addOnConfigUpdateListener(new ConfigUpdateListener() { @Override public void onUpdate(ConfigUpdate configUpdate) { Log.d(ContentValues.TAG, "Updated keys: " + configUpdate.getUpdatedKeys()); remoteConfig.activate().addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { // Optionally, add an action to perform on update here. } }); } @Override public void onError(FirebaseRemoteConfigException error) { Log.w(ContentValues.TAG, "Config update error with code: " + error.getCode(), error); } }); ``` ```Web // Add a real-time Remote Config listener onConfigUpdate(remoteConfig, { next: (configUpdate) => { console.log("Updated keys:", configUpdate.getUpdatedKeys()); if (configUpdate.getUpdatedKeys().has("welcome_message")) { activate(remoteConfig).then(() => { showWelcomeMessage(); }); } }, error: (error) => { console.log("Config update error:", error); }, complete: () => { console.log("Listening stopped."); } }); ``` ```Dart // Add a real-time Remote Config listener remoteConfig.onConfigUpdated.listen((event) async { await remoteConfig.activate(); }); ``` ```Unity // Add a real-time Remote Config listener to automatically update whenever // a new template is published. // Note: the parameters can be anonymous as they are unused. remoteConfig.OnConfigUpdateListener += (_, _) => { remoteConfig.ActivateAsync(); }; ``` -------------------------------- ### Reference Cache in Prompt Template Source: https://firebase.google.com/docs/ai-logic/context-caching Syntax and example for including a cache resource in a server prompt template. ```text {{cachedContent name="YOUR_CACHE_RESOURCE_NAME"}} {{role "user"}} {{userPrompt}} ``` ```text {{cachedContent name="projects/861083271981/locations/global/cachedContents/4545031458888089601"}} {{role "user"}} {{userPrompt}} ``` -------------------------------- ### Initialize and Chat with Gemini in JavaScript Source: https://firebase.google.com/docs/ai-logic/chat Setup the Firebase AI service and manage a multi-turn chat session with history. ```javascript 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-flash-preview" }); async function run() { const chat = model.startChat({ history: [ { role: "user", parts: [{ text: "Hello, I have 2 dogs in my house." }], }, { role: "model", parts: [{ text: "Great to meet you. What would you like to know?" }], }, ], generationConfig: { maxOutputTokens: 100, }, }); const msg = "How many paws are in my house?"; const result = await chat.sendMessage(msg); const text = result.response.text(); console.log(text); } run(); ``` -------------------------------- ### Initialize Firebase Vertex AI Source: https://firebase.google.com/docs/ai-logic/edit-images-imagen-remove-objects Setup the Firebase app and initialize the Vertex AI service with a specific location. ```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 Vertex AI Gemini API backend service // Optionally specify a location to access the model (for example, `us-central1`) final ai = FirebaseAI.vertexAI(location: 'us-central1'); // Create an `ImagenModel` instance with an Imagen "capability" model final model = ai.imagenModel(model: 'imagen-3.0-capability-001'); ``` -------------------------------- ### Include Cloud Storage files in AI requests Source: https://firebase.google.com/docs/ai-logic/solutions/cloud-storage Examples for uploading files and referencing them in AI model prompts across different platforms. ```Swift // Upload an image file using Cloud Storage for Firebase. let storageRef = Storage.storage().reference(withPath: "images/image.jpg") guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { fatalError("File 'image.jpg' not found in main bundle.") } let metadata = try await storageRef.putFileAsync(from: imageURL) // Get the MIME type and Cloud Storage for Firebase URL. guard let mimeType = metadata.contentType else { fatalError("The MIME type of the uploaded image is nil.") } // Construct a URL in the required format. let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" let prompt = "What's in this picture?" // Construct the imagePart with the MIME type and the URL. let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) // To generate text output, call generateContent with the prompt and the imagePart. let result = try await model.generateContent(prompt, imagePart) if let text = result.text { print(text) } ``` ```Kotlin // Upload an image file using Cloud Storage for Firebase. val storageRef = Firebase.storage.reference.child("images/image.jpg") val fileUri = Uri.fromFile(File("image.jpg")) try { val taskSnapshot = storageRef.putFile(fileUri).await() // Get the MIME type and Cloud Storage for Firebase file path. val mimeType = taskSnapshot.metadata?.contentType val bucket = taskSnapshot.metadata?.bucket val filePath = taskSnapshot.metadata?.path if (mimeType != null && bucket != null) { // Construct a URL in the required format. val storageUrl = "gs://$bucket/$filePath" // Construct a prompt that includes text, the MIME type, and the URL. val prompt = content { fileData(mimeType = mimeType, uri = storageUrl) text("What's in this picture?") } // To generate text output, call generateContent with the prompt. val response = model.generateContent(prompt) println(response.text) } } catch (e: StorageException) { // An error occurred while uploading the file. } catch (e: GoogleGenerativeAIException) { // An error occurred while generating text. } ``` ```Java // Upload an image file using Cloud Storage for Firebase. StorageReference storage = FirebaseStorage.getInstance().getReference("images/image.jpg"); Uri fileUri = Uri.fromFile(new File("images/image.jpg")); storage.putFile(fileUri).addOnSuccessListener(taskSnapshot -> { // Get the MIME type and Cloud Storage for Firebase file path. String mimeType = taskSnapshot.getMetadata().getContentType(); String bucket = taskSnapshot.getMetadata().getBucket(); String filePath = taskSnapshot.getMetadata().getPath(); if (mimeType != null && bucket != null) { // Construct a URL in the required format. String storageUrl = "gs://" + bucket + "/" + filePath; // Create a prompt that includes text, the MIME type, and the URL. Content prompt = new Content.Builder() .addFileData(storageUrl, mimeType) .addText("What's in this picture?") .build(); // To generate text output, call generateContent with the prompt. GenerativeModelFutures modelFutures = GenerativeModelFutures.from(model); ListenableFuture response = modelFutures.generateContent(prompt); Futures.addCallback(response, new FutureCallback<>() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); System.out.println(resultText); } @Override public void onFailure(@NonNull Throwable t) { t.printStackTrace(); } }, executor); } }).addOnFailureListener(e -> { // An error occurred while uploading the file. e.printStackTrace(); }); ``` ```JavaScript // Upload an image file using Cloud Storage for Firebase. const storageRef = ref(storage, "image.jpg"); const uploadResult = await uploadBytes(storageRef, file); // Get the MIME type and Cloud Storage for Firebase URL. // toString() is the simplest way to construct the Cloud Storage for Firebase URL // in the required format. const mimeType = uploadResult.metadata.contentType; const storageUrl = uploadResult.ref.toString(); ``` -------------------------------- ### Set System Instructions for Model Behavior Source: https://firebase.google.com/docs/ai-logic/server-prompt-templates/syntax-and-examples Steer model behavior by specifying system instructions using `{{role "system"}}` syntax within the prompt. This example ensures output is a structured invoice. ```prompt {{role "system"}} All output must be a clearly structured invoice document. Use a tabular or clearly delineated list format for line items. {{role "user"}} Create an example customer invoice for a customer. ``` -------------------------------- ### Initialize and Generate Content with Dart SDK Source: https://firebase.google.com/docs/ai-logic/analyze-documents?hl=fr Setup Firebase AI and generate content from a local PDF file using the Dart SDK. ```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-2.5-flash'); ``` ```dart // Provide a text prompt to include with the PDF file final prompt = TextPart("Summarize the important results in this report."); // Prepare the PDF file for input final doc = await File('document0.pdf').readAsBytes(); // Provide the PDF file as `Data` with the appropriate PDF file MIME type final docPart = InlineDataPart('application/pdf', doc); // To generate text output, call `generateContent` with the text and PDF file final response = await model.generateContent([ Content.multi([prompt,docPart]) ]); // Print the generated text print(response.text); ``` -------------------------------- ### Prepare Image and Start Chat in Dart Source: https://firebase.google.com/docs/ai-logic/generate-images-gemini?hl=vi Reads an image file, creates an `InlineDataPart`, and initializes a chat session with a text prompt. This sets up the initial interaction for image editing. ```dart // Prepare an image for the model to edit final image = await File('scones.jpg').readAsBytes(); final imagePart = InlineDataPart('image/jpeg', image); // Provide an initial text prompt instructing the model to edit the image final prompt = TextPart("Edit this image to make it look like a cartoon"); // Initialize the chat final chat = model.startChat(); ``` -------------------------------- ### Generate Content with Explicit Image Data (Swift) Source: https://firebase.google.com/docs/ai-logic/solutions/cloud-storage Use this Swift code to construct an image part with explicit MIME type and Cloud Storage URL for multimodal requests. Ensure you have completed the Firebase AI Logic SDKs getting started guide. ```swift let prompt = "What's in this picture?" // Construct an imagePart that explicitly includes the MIME type and // Cloud Storage for Firebase URL values. let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") // To generate text output, call generateContent with the prompt and imagePart. let result = try await model.generateContent(prompt, imagePart) if let text = result.text { print(text) } ``` -------------------------------- ### Set system instructions in Java Source: https://firebase.google.com/docs/ai-logic/system-instructions Initialize the LiveModel with system instructions using the Content.Builder. ```Java // ... // Specify the system instructions as part of creating the `LiveModel` instance LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel( /* modelName */ "GEMINI_LIVE_MODEL_NAME", /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() // ... ); LiveModelFutures liveModel = LiveModelFutures.from(lm); // ... ``` -------------------------------- ### Prepare Image and Send Initial Prompt (Dart) Source: https://firebase.google.com/docs/ai-logic/generate-images-gemini Prepares an image file for the Gemini model and sends an initial prompt to start an image editing chat. This includes reading the image file and creating an `InlineDataPart`. ```dart // Prepare an image for the model to edit final image = await File('scones.jpg').readAsBytes(); final imagePart = InlineDataPart('image/jpeg', image); // Provide an initial text prompt instructing the model to edit the image final prompt = TextPart("Edit this image to make it look like a cartoon"); // Initialize the chat final chat = model.startChat(); // To generate an initial response, send a user message with the image and text prompt ``` -------------------------------- ### Generate Content with Image Data (Dart) Source: https://firebase.google.com/docs/ai-logic/solutions/cloud-storage?hl=de This Dart example demonstrates uploading an image to Firebase Storage and then using its metadata to generate content. It requires Firebase Storage setup and the image data. ```dart // Upload an image file using Cloud Storage for Firebase. final storageRef = FirebaseStorage.instance.ref(); final imageRef = storageRef.child("images/image.jpg"); await imageRef.putData(data); // Get the MIME type and Cloud Storage for Firebase file path. final metadata = await imageRef.getMetadata(); final mimeType = metadata.contentType; final bucket = imageRef.bucket; final fullPath = imageRef.fullPath; final prompt = TextPart("What's in the picture?"); // Construct a URL in the required format. final storageUrl = 'gs://$bucket/$fullPath'; // Construct the filePart with the MIME type and the URL. final filePart = FileData(mimeType, storageUrl); // To generate text output, call generateContent with the text and the filePart. final response = await model.generateContent([ Content.multi([prompt, filePart]) ]); print(response.text); ``` -------------------------------- ### Initialize and stream audio with Live API in C# Source: https://firebase.google.com/docs/ai-logic/live-api/capabilities Demonstrates connecting to a LiveSession and buffering audio data for playback in a Unity environment. ```csharp using Firebase; using Firebase.AI; async Task SendTextReceiveAudio() { // Initialize the Gemini Developer API backend service // Create a `LiveModel` instance with a model that supports the Live API var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel( modelName: "gemini-2.5-flash-native-audio-preview-12-2025", // Configure the model to respond with audio liveGenerationConfig: new LiveGenerationConfig( responseModalities: new[] { ResponseModality.Audio }) ); LiveSession session = await liveModel.ConnectAsync(); // Provide a text prompt var prompt = ModelContent.Text("Convert this text to audio."); await session.SendAsync(content: prompt, turnComplete: true); // Start receiving the response await ReceiveAudio(session); } Queue audioBuffer = new(); async Task ReceiveAudio(LiveSession session) { int sampleRate = 24000; int channelCount = 1; // Create a looping AudioClip to fill with the received audio data int bufferSamples = (int)(sampleRate * channelCount); AudioClip clip = AudioClip.Create("StreamingPCM", bufferSamples, channelCount, sampleRate, true, OnAudioRead); // Attach the clip to an AudioSource and start playing it AudioSource audioSource = GetComponent(); audioSource.clip = clip; audioSource.loop = true; audioSource.Play(); // Start receiving the response await foreach (var message in session.ReceiveAsync()) { // Process the received message foreach (float[] pcmData in message.AudioAsFloat) { lock (audioBuffer) { foreach (float sample in pcmData) { audioBuffer.Enqueue(sample); } } } } } // This method is called by the AudioClip to load audio data. ``` -------------------------------- ### Edit Image with Gemini in C# Source: https://firebase.google.com/docs/ai-logic/generate-images-gemini?hl=ru This C# example shows how to edit an image using Firebase and Gemini in a Unity environment. It initializes the Gemini backend, prepares image data from a file, and uses `GenerateContentAsync` to get the response. The code also includes logic to extract and process generated image parts. ```csharp using Firebase; using Firebase.AI; var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel( modelName: "gemini-2.5-flash-image", generationConfig: new GenerationConfig( responseModalities: new[] { ResponseModality.Text, ResponseModality.Image }) ); var imageFile = System.IO.File.ReadAllBytes(System.IO.Path.Combine( UnityEngine.Application.streamingAssetsPath, "scones.jpg")); var image = ModelContent.InlineData("image/jpeg", imageFile); var prompt = ModelContent.Text("Edit this image to make it look like a cartoon."); var response = await model.GenerateContentAsync(new [] { prompt, image }); var text = response.Text; if (!string.IsNullOrWhiteSpace(text)) { // Do something with the text } var imageParts = response.Candidates.First().Content.Parts .OfType() .Where(part => part.MimeType == "image/png"); foreach (var imagePart in imageParts) { // Load the Image into a Unity Texture2D object Texture2D texture2D = new Texture2D(2, 2); if (texture2D.LoadImage(imagePart.Data.ToArray())) { // Do something with the image } } ``` -------------------------------- ### Edit Image with Imagen Model in Dart Source: https://firebase.google.com/docs/ai-logic/edit-images-imagen-controlled-customization Use this code to edit an image using the Imagen model via Firebase AI. Ensure Firebase is initialized and the Vertex AI backend is configured. The example uses a reference image and a prompt to guide the editing process. Handle potential errors and check the response for generated images. ```dart import 'dart:typed_data'; 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 Vertex AI Gemini API backend service // Optionally specify a location to access the model (for example, `us-central1`) final ai = FirebaseAI.vertexAI(location: 'us-central1'); // Create an `ImagenModel` instance with an Imagen "capability" model final model = ai.imagenModel(model: 'imagen-3.0-capability-001'); // This example assumes 'referenceImage' is a pre-loaded Uint8List. // In a real app, this might come from the user's device or a URL. final Uint8List referenceImage = Uint8List(0); // TODO: Load your reference image data here // Define the control reference using the reference image. final controlReference = ImagenControlReference( image: referenceImage, referenceId: 1, controlType: ImagenControlType.scribble, ); // Provide a prompt that describes the final image. // The "[1]" links the prompt to the subject reference with ID 1. final prompt = "A cat flying through outer space arranged like the space scribble[1]"; try { // Use the editImage API to perform the controlled customization. // Pass the list of references, the prompt, and an editing configuration. final response = await model.editImage( [controlReference], prompt, config: ImagenEditingConfig( editSteps: 50, // Number of editing steps, a higher value can improve quality ), ); // Process the result. if (response.images.isNotEmpty) { final editedImage = response.images.first.bytes; // Use the editedImage (a Uint8List) to display the image, save it, etc. print('Image successfully generated!'); } else { // Handle the case where no images were generated. print('Error: No images were generated.'); } } catch (e) { // Handle any potential errors during the API call. print('An error occurred: $e'); } ``` -------------------------------- ### Create and Download On-Device Model Source: https://firebase.google.com/docs/ai-logic/hybrid/web/get-started If the model is 'downloadable', execute this command to initiate the download. This ensures the on-device model is ready for inference. ```javascript await LanguageModel.create(); ``` -------------------------------- ### Initialize and Stream Audio with Unity Source: https://firebase.google.com/docs/ai-logic/live-api Sets up the Live API session and manages microphone input and audio playback using Unity coroutines and AudioClips. ```csharp using Firebase; using Firebase.AI; async Task SendTextReceiveAudio() { // Initialize the Gemini Developer API backend service // Create a `LiveModel` instance with a model that supports the Live API var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel( modelName: "gemini-2.5-flash-native-audio-preview-12-2025", // Configure the model to respond with audio liveGenerationConfig: new LiveGenerationConfig( responseModalities: new[] { ResponseModality.Audio }) ); LiveSession session = await liveModel.ConnectAsync(); // Start a coroutine to send audio from the Microphone var recordingCoroutine = StartCoroutine(SendAudio(session)); // Start receiving the response await ReceiveAudio(session); } IEnumerator SendAudio(LiveSession liveSession) { string microphoneDeviceName = null; int recordingFrequency = 16000; int recordingBufferSeconds = 2; var recordingClip = Microphone.Start(microphoneDeviceName, true, recordingBufferSeconds, recordingFrequency); int lastSamplePosition = 0; while (true) { if (!Microphone.IsRecording(microphoneDeviceName)) { yield break; } int currentSamplePosition = Microphone.GetPosition(microphoneDeviceName); if (currentSamplePosition != lastSamplePosition) { // The Microphone uses a circular buffer, so we need to check if the // current position wrapped around to the beginning, and handle it // accordingly. int sampleCount; if (currentSamplePosition > lastSamplePosition) { sampleCount = currentSamplePosition - lastSamplePosition; } else { sampleCount = recordingClip.samples - lastSamplePosition + currentSamplePosition; } if (sampleCount > 0) { // Get the audio chunk float[] samples = new float[sampleCount]; recordingClip.GetData(samples, lastSamplePosition); // Send the data, discarding the resulting Task to avoid the warning _ = liveSession.SendAudioAsync(samples); lastSamplePosition = currentSamplePosition; } } // Wait for a short delay before reading the next sample from the Microphone const float MicrophoneReadDelay = 0.5f; yield return new WaitForSeconds(MicrophoneReadDelay); } } Queue audioBuffer = new(); async Task ReceiveAudio(LiveSession liveSession) { int sampleRate = 24000; int channelCount = 1; // Create a looping AudioClip to fill with the received audio data int bufferSamples = (int)(sampleRate * channelCount); AudioClip clip = AudioClip.Create("StreamingPCM", bufferSamples, channelCount, sampleRate, true, OnAudioRead); // Attach the clip to an AudioSource and start playing it AudioSource audioSource = GetComponent(); audioSource.clip = clip; audioSource.loop = true; audioSource.Play(); // Start receiving the response await foreach (var message in liveSession.ReceiveAsync()) { // Process the received message foreach (float[] pcmData in message.AudioAsFloat) { lock (audioBuffer) { foreach (float sample in pcmData) { audioBuffer.Enqueue(sample); } } } } } // This method is called by the AudioClip to load audio data. private void OnAudioRead(float[] data) { int samplesToProvide = data.Length; int samplesProvided = 0; lock(audioBuffer) { while (samplesProvided < samplesToProvide && audioBuffer.Count > 0) { data[samplesProvided] = audioBuffer.Dequeue(); samplesProvided++; } } while (samplesProvided < samplesToProvide) { data[samplesProvided] = 0.0f; samplesProvided++; } } ``` -------------------------------- ### Install Firebase SDK for Web using npm Source: https://firebase.google.com/docs/ai-logic/get-started?hl=pt-br Install the Firebase SDK for JavaScript for Web using npm. This command installs the necessary package to integrate Firebase services into your web application. ```bash npm install firebase ``` -------------------------------- ### Set system instructions in Kotlin Source: https://firebase.google.com/docs/ai-logic/system-instructions Use the content builder to specify system instructions during LiveModel initialization. ```Kotlin // ... // Specify the system instructions as part of creating the `LiveModel` instance val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( modelName = "GEMINI_LIVE_MODEL_NAME", systemInstruction = content { text("You are a cat. Your name is Neko.") }, // ... ) // ... ``` -------------------------------- ### Configure Vertex AI Gemini API with Remote Config in Java Source: https://firebase.google.com/docs/ai-logic/solutions/remote-config?api=vertex Initialize the Vertex AI Gemini API backend service and create a GenerativeModel using values sourced from Remote Config. This example demonstrates fetching the backend location, model name, system instructions, and user prompt from Remote Config. It uses `ListenableFuture` for asynchronous operations. ```java // Initialize the Vertex AI Gemini API backend service // The location for where to access the model will be sourced from Remote Config FirebaseAI ai = FirebaseAI.getInstance( GenerativeBackend.vertexAI(remoteConfig.getString("vertex_location"))); // Create a `GenerativeModel` and add system instructions into its config // Both the model name and the system instructions will be sourced from Remote Config GenerativeModel gm = ai.generativeModel( /* modelName */ remoteConfig.getString("model_name"), /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* tools (optional) */ null, /* toolsConfig (optional) */ null, /* systemInstruction (optional) */ new Content.Builder() .addText(remoteConfig.getString("system_instructions")).build(), /* requestOptions (optional) */ new RequestOptions() ); GenerativeModelFutures model = GenerativeModelFutures.from(gm); // Provide a prompt that contains text // The text in the prompt will be sourced from Remote Config Content userPrompt = new Content.Builder().addText( remoteConfig.getString("prompt")).build(); // To generate text output, call `generateContent` with the text input ListenableFuture response = model.generateContent(userPrompt); Futures.addCallback(response, new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); System.out.println(resultText); } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); ``` -------------------------------- ### Install Firebase JS SDK for Web Source: https://firebase.google.com/docs/ai-logic/get-started Install the Firebase JS SDK for Web using npm. This is the first step to using Firebase services in your web application. ```bash npm install firebase ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.