From e3258a94dd56b12751c84a03fa8564706673cb4a Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 20 Aug 2025 19:27:10 +0100 Subject: [PATCH 1/5] feat: add Chat snippets --- .../example/ailogic/java/CommonSnippets.java | 134 ++++++++++++++++++ .../ailogic/java/GeneralViewModel.java | 4 - .../ailogic/java/GoogleAISnippets.java | 7 + .../ailogic/java/VertexAISnippets.java | 7 + .../example/ailogic/kotlin/CommonSnippets.kt | 51 +++++++ .../ailogic/kotlin/GeneralViewModel.kt | 4 - .../ailogic/kotlin/GoogleAISnippets.kt | 6 + .../ailogic/kotlin/VertexAISnippets.kt | 6 + 8 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java delete mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GeneralViewModel.java create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt delete mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GeneralViewModel.kt create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt create mode 100644 firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java new file mode 100644 index 00000000..057e2641 --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java @@ -0,0 +1,134 @@ +package com.google.firebase.example.ailogic.java; + +import androidx.lifecycle.ViewModel; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.GenerativeModel; +import com.google.firebase.ai.java.ChatFutures; +import com.google.firebase.ai.java.GenerativeModelFutures; +import com.google.firebase.ai.type.Content; +import com.google.firebase.ai.type.GenerateContentResponse; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Executor; +import org.reactivestreams.Publisher; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; + +public class CommonSnippets extends ViewModel { + private Executor executor; + private GenerativeModel ai = FirebaseAI.getInstance() + .generativeModel("gemini-2.5-flash"); + private GenerativeModelFutures model = GenerativeModelFutures.from(ai); + + public void chat() { + chatNonStreaming(); + chatStreaming(); + } + + void chatNonStreaming() { + // [START chat_non_streaming] + // (optional) Create previous chat history for context + Content.Builder userContentBuilder = new Content.Builder(); + userContentBuilder.setRole("user"); + userContentBuilder.addText("Hello, I have 2 dogs in my house."); + Content userContent = userContentBuilder.build(); + + Content.Builder modelContentBuilder = new Content.Builder(); + modelContentBuilder.setRole("model"); + modelContentBuilder.addText("Great to meet you. What would you like to know?"); + Content modelContent = userContentBuilder.build(); + + List history = Arrays.asList(userContent, modelContent); + + // Initialize the chat + ChatFutures chat = model.startChat(history); + + // Create a new user message + Content.Builder messageBuilder = new Content.Builder(); + messageBuilder.setRole("user"); + messageBuilder.addText("How many paws are in my house?"); + + Content message = messageBuilder.build(); + + // Send the message + ListenableFuture response = chat.sendMessage(message); + 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); + // [END chat_non_streaming] + } + + void chatStreaming() { + // [START chat_streaming] + // (optional) Create previous chat history for context + Content.Builder userContentBuilder = new Content.Builder(); + userContentBuilder.setRole("user"); + userContentBuilder.addText("Hello, I have 2 dogs in my house."); + Content userContent = userContentBuilder.build(); + + Content.Builder modelContentBuilder = new Content.Builder(); + modelContentBuilder.setRole("model"); + modelContentBuilder.addText("Great to meet you. What would you like to know?"); + Content modelContent = userContentBuilder.build(); + + List history = Arrays.asList(userContent, modelContent); + + // Initialize the chat + ChatFutures chat = model.startChat(history); + + // Create a new user message + Content.Builder messageBuilder = new Content.Builder(); + messageBuilder.setRole("user"); + messageBuilder.addText("How many paws are in my house?"); + + Content message = messageBuilder.build(); + + // Send the message + Publisher streamingResponse = + chat.sendMessageStream(message); + + final String[] fullResponse = {""}; + + streamingResponse.subscribe(new Subscriber() { + + @Override + public void onNext(GenerateContentResponse generateContentResponse) { + String chunk = generateContentResponse.getText(); + fullResponse[0] += chunk; + } + + + @Override + public void onComplete() { + System.out.println(fullResponse[0]); + } + + // ... other methods omitted for brevity + + // [START_EXCLUDE] + @Override + public void onSubscribe(Subscription s) { + + } + + @Override + public void onError(Throwable t) { + + } + // [END_EXCLUDE] + }); + // [END chat_streaming] + } +} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GeneralViewModel.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GeneralViewModel.java deleted file mode 100644 index b5f47b85..00000000 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GeneralViewModel.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.google.firebase.example.ailogic.java; - -public class GeneralViewModel { -} diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java new file mode 100644 index 00000000..1ea96827 --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java @@ -0,0 +1,7 @@ +package com.google.firebase.example.ailogic.java; + +import androidx.lifecycle.ViewModel; + +public class GoogleAISnippets extends ViewModel { + +} diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java new file mode 100644 index 00000000..a6dd9213 --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java @@ -0,0 +1,7 @@ +package com.google.firebase.example.ailogic.java; + +import androidx.lifecycle.ViewModel; + +public class VertexAISnippets extends ViewModel { + +} diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt new file mode 100644 index 00000000..97ec4afa --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt @@ -0,0 +1,51 @@ +package com.google.firebase.example.ailogic.kotlin + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.google.firebase.ai.GenerativeModel +import com.google.firebase.ai.type.content +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch + +class CommonSnippets( + private val generativeModel: GenerativeModel +) : ViewModel() { + + fun chat() { + viewModelScope.launch { + chatNonStreaming() + chatStreaming() + } + } + + suspend fun chatNonStreaming() { + // [START chat_non_streaming] + // Initialize the chat + val chat = generativeModel.startChat( + history = listOf( + content(role = "user") { text("Hello, I have 2 dogs in my house.") }, + content(role = "model") { text("Great to meet you. What would you like to know?") } + ) + ) + + val response = chat.sendMessage("How many paws are in my house?") + print(response.text) + // [END chat_non_streaming] + } + + suspend fun chatStreaming() { + // [START chat_streaming] + // Initialize the chat + val chat = generativeModel.startChat( + history = listOf( + content(role = "user") { text("Hello, I have 2 dogs in my house.") }, + content(role = "model") { text("Great to meet you. What would you like to know?") } + ) + ) + + chat.sendMessageStream("How many paws are in my house?").collect { chunk -> + print(chunk.text) + } + // [END chat_streaming] + } +} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GeneralViewModel.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GeneralViewModel.kt deleted file mode 100644 index fb26b802..00000000 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GeneralViewModel.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.google.firebase.example.ailogic.kotlin - -class GeneralViewModel { -} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt new file mode 100644 index 00000000..1bce91e3 --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt @@ -0,0 +1,6 @@ +package com.google.firebase.example.ailogic.kotlin + +import androidx.lifecycle.ViewModel + +class GoogleAISnippets : ViewModel() { +} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt new file mode 100644 index 00000000..8954e1b4 --- /dev/null +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt @@ -0,0 +1,6 @@ +package com.google.firebase.example.ailogic.kotlin + +import androidx.lifecycle.ViewModel + +class VertexAISnippets : ViewModel() { +} \ No newline at end of file From 5018f42b2953808c739ebaf87e3a9b563b3374ed Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 20 Aug 2025 20:33:25 +0100 Subject: [PATCH 2/5] feat: add Function Calling snippets --- .../example/ailogic/java/CommonSnippets.java | 209 ++++++++++++++---- .../ailogic/java/GoogleAISnippets.java | 24 +- .../ailogic/java/VertexAISnippets.java | 27 ++- .../example/ailogic/kotlin/CommonSnippets.kt | 129 +++++++++-- .../ailogic/kotlin/GoogleAISnippets.kt | 20 +- .../ailogic/kotlin/VertexAISnippets.kt | 20 +- 6 files changed, 371 insertions(+), 58 deletions(-) diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java index 057e2641..d05954c6 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/CommonSnippets.java @@ -1,6 +1,7 @@ package com.google.firebase.example.ailogic.java; import androidx.lifecycle.ViewModel; + import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -9,18 +10,33 @@ import com.google.firebase.ai.java.ChatFutures; import com.google.firebase.ai.java.GenerativeModelFutures; import com.google.firebase.ai.type.Content; +import com.google.firebase.ai.type.FunctionCallPart; +import com.google.firebase.ai.type.FunctionResponsePart; import com.google.firebase.ai.type.GenerateContentResponse; +import com.google.firebase.ai.type.Schema; +import com.google.firebase.ai.type.TextPart; +import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.type.FunctionDeclaration; + import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import kotlinx.serialization.json.JsonElement; +import kotlinx.serialization.json.JsonElementKt; +import kotlinx.serialization.json.JsonObject; +import kotlinx.serialization.json.JsonPrimitive; + import org.reactivestreams.Publisher; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; public class CommonSnippets extends ViewModel { private Executor executor; - private GenerativeModel ai = FirebaseAI.getInstance() - .generativeModel("gemini-2.5-flash"); + private GenerativeModel ai = FirebaseAI.getInstance().generativeModel("gemini-2.5-flash"); private GenerativeModelFutures model = GenerativeModelFutures.from(ai); public void chat() { @@ -55,18 +71,21 @@ void chatNonStreaming() { // Send the message ListenableFuture response = chat.sendMessage(message); - 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); + 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); // [END chat_non_streaming] } @@ -96,39 +115,155 @@ void chatStreaming() { Content message = messageBuilder.build(); // Send the message - Publisher streamingResponse = - chat.sendMessageStream(message); + Publisher streamingResponse = chat.sendMessageStream(message); final String[] fullResponse = {""}; - streamingResponse.subscribe(new Subscriber() { + streamingResponse.subscribe( + new Subscriber() { - @Override - public void onNext(GenerateContentResponse generateContentResponse) { - String chunk = generateContentResponse.getText(); - fullResponse[0] += chunk; - } + @Override + public void onNext(GenerateContentResponse generateContentResponse) { + String chunk = generateContentResponse.getText(); + fullResponse[0] += chunk; + } + @Override + public void onComplete() { + System.out.println(fullResponse[0]); + } - @Override - public void onComplete() { - System.out.println(fullResponse[0]); - } + // ... other methods omitted for brevity - // ... other methods omitted for brevity + // [START_EXCLUDE] + @Override + public void onSubscribe(Subscription s) { + } - // [START_EXCLUDE] - @Override - public void onSubscribe(Subscription s) { + @Override + public void onError(Throwable t) { + } - } + // [END_EXCLUDE] + }); + // [END chat_streaming] + } - @Override - public void onError(Throwable t) { + public void functionCalling() { + // [START function_calling_create_function_declaration] + FunctionDeclaration fetchWeatherTool = + new FunctionDeclaration( + "fetchWeather", + "Get the weather conditions for a specific city on a specific date.", + Map.of( + "location", + Schema.obj( + Map.of( + "city", Schema.str("The city of the location."), + "state", Schema.str("The US state of the location."))), + "date", + Schema.str( + "The date for which to get the weather. " + + "Date must be in the format: YYYY-MM-DD.")), + Collections.emptyList()); + // [END function_calling_create_function_declaration] - } - // [END_EXCLUDE] - }); - // [END chat_streaming] + // [START function_calling_generate_function_call] + String prompt = "What was the weather in Boston on October 17, 2024?"; + ChatFutures chatFutures = model.startChat(); + // Send the user's question (the prompt) to the model using multi-turn chat. + ListenableFuture response = + chatFutures.sendMessage(new Content("user", List.of(new TextPart(prompt)))); + + ListenableFuture handleFunctionCallFuture = + Futures.transform( + response, + result -> { + for (FunctionCallPart functionCall : result.getFunctionCalls()) { + if (functionCall.getName().equals("fetchWeather")) { + Map args = functionCall.getArgs(); + JsonObject locationJsonObject = JsonElementKt.getJsonObject(args.get("location")); + String city = + JsonElementKt.getContentOrNull( + JsonElementKt.getJsonPrimitive(locationJsonObject.get("city"))); + String state = + JsonElementKt.getContentOrNull( + JsonElementKt.getJsonPrimitive(locationJsonObject.get("state"))); + Location location = new Location(city, state); + + String date = + JsonElementKt.getContentOrNull( + JsonElementKt.getJsonPrimitive(args.get("date"))); + return fetchWeather(location, date); + } + } + return null; + }, + Executors.newSingleThreadExecutor()); + // [END function_calling_generate_function_call] + + // [START function_calling_pass_back_function_response] + ListenableFuture modelResponseFuture = + Futures.transformAsync( + handleFunctionCallFuture, + // Send the response(s) from the function back to the model + // so that the model can use it to generate its final response. + functionCallResult -> + chatFutures.sendMessage( + new Content( + "function", + List.of(new FunctionResponsePart("fetchWeather", functionCallResult)))), + Executors.newSingleThreadExecutor()); + + Futures.addCallback( + modelResponseFuture, + new FutureCallback() { + @Override + public void onSuccess(GenerateContentResponse result) { + if (result.getText() != null) { + // Log the text response. + System.out.println(result.getText()); + } + } + + @Override + public void onFailure(Throwable t) { + // handle error + } + }, + Executors.newSingleThreadExecutor()); + // [END function_calling_pass_back_function_response] + } + + // [START function_calling_write_function] + // This function calls a hypothetical external API that returns + // a collection of weather information for a given location on a given date. + // `location` is an object of the form { city: string, state: string } + public JsonObject fetchWeather(Location location, String date) { + + // TODO(developer): Write a standard function that would call to an external weather API. + + // For demo purposes, this hypothetical response is hardcoded here in the expected format. + return new JsonObject( + Map.of( + "temperature", + JsonElementKt.JsonPrimitive(38), + "chancePrecipitation", + JsonElementKt.JsonPrimitive("56%"), + "cloudConditions", + JsonElementKt.JsonPrimitive("partlyCloudy") + ) + ); + } + // [END function_calling_write_function] +} + +class Location { + public String city; + public String state; + + public Location(String city, String state) { + this.city = city; + this.state = state; } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java index 1ea96827..610a7f09 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java @@ -1,7 +1,27 @@ package com.google.firebase.example.ailogic.java; import androidx.lifecycle.ViewModel; +import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.java.GenerativeModelFutures; +import com.google.firebase.ai.type.GenerativeBackend; +import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.type.FunctionDeclaration; +import java.util.List; public class GoogleAISnippets extends ViewModel { - -} + void functionCalling(FunctionDeclaration fetchWeatherTool) { + // [START function_calling_specify_declaration_during_init] + // Initialize the Gemini Developer API backend service + // Create a `GenerativeModel` instance with a model that supports your use case + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + "gemini-2.5-flash", + null, + null, + // Provide the function declaration to the model. + List.of(Tool.functionDeclarations(List.of(fetchWeatherTool))))); + // [END function_calling_specify_declaration_during_init] + } +} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java index a6dd9213..ce3f348b 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java @@ -2,6 +2,29 @@ import androidx.lifecycle.ViewModel; -public class VertexAISnippets extends ViewModel { +import com.google.firebase.ai.type.GenerativeBackend; +import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.java.GenerativeModelFutures; +import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.type.FunctionDeclaration; + +import java.util.List; -} +public class VertexAISnippets extends ViewModel { + void functionCalling(FunctionDeclaration fetchWeatherTool) { + // [START function_calling_specify_declaration_during_init] + // Initialize the Vertex AI Gemini API backend service + // Optionally specify the location to access the model (`global` is recommended) + // Create a `GenerativeModel` instance with a model that supports your use case + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")) + .generativeModel( + "gemini-2.5-flash", + null, + null, + // Provide the function declaration to the model. + List.of(Tool.functionDeclarations(List.of(fetchWeatherTool))))); + // [END function_calling_specify_declaration_during_init] + } +} \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt index 97ec4afa..baa41c9d 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/CommonSnippets.kt @@ -3,9 +3,15 @@ package com.google.firebase.example.ailogic.kotlin import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.firebase.ai.GenerativeModel +import com.google.firebase.ai.type.FunctionResponsePart +import com.google.firebase.ai.type.Schema import com.google.firebase.ai.type.content -import kotlinx.coroutines.flow.collect +import com.google.firebase.ai.type.FunctionDeclaration import kotlinx.coroutines.launch +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive class CommonSnippets( private val generativeModel: GenerativeModel @@ -21,12 +27,16 @@ class CommonSnippets( suspend fun chatNonStreaming() { // [START chat_non_streaming] // Initialize the chat - val chat = generativeModel.startChat( - history = listOf( - content(role = "user") { text("Hello, I have 2 dogs in my house.") }, - content(role = "model") { text("Great to meet you. What would you like to know?") } + val chat = + generativeModel.startChat( + history = + listOf( + content(role = "user") { text("Hello, I have 2 dogs in my house.") }, + content(role = "model") { + text("Great to meet you. What would you like to know?") + } + ) ) - ) val response = chat.sendMessage("How many paws are in my house?") print(response.text) @@ -36,16 +46,105 @@ class CommonSnippets( suspend fun chatStreaming() { // [START chat_streaming] // Initialize the chat - val chat = generativeModel.startChat( - history = listOf( - content(role = "user") { text("Hello, I have 2 dogs in my house.") }, - content(role = "model") { text("Great to meet you. What would you like to know?") } + val chat = + generativeModel.startChat( + history = + listOf( + content(role = "user") { text("Hello, I have 2 dogs in my house.") }, + content(role = "model") { + text("Great to meet you. What would you like to know?") + } + ) ) - ) - chat.sendMessageStream("How many paws are in my house?").collect { chunk -> - print(chunk.text) - } + chat.sendMessageStream("How many paws are in my house?").collect { chunk -> print(chunk.text) } // [END chat_streaming] } -} \ No newline at end of file + + suspend fun functionCalling() { + // [START function_calling_create_function_declaration] + val fetchWeatherTool = + FunctionDeclaration( + "fetchWeather", + "Get the weather conditions for a specific city on a specific date.", + mapOf( + "location" to + Schema.obj( + mapOf( + "city" to Schema.string("The city of the location."), + "state" to Schema.string("The US state of the location."), + ), + description = + "The name of the city and its state for which " + + "to get the weather. Only cities in the " + + "USA are supported." + ), + "date" to + Schema.string( + "The date for which to get the weather." + + " Date must be in the format: YYYY-MM-DD." + ), + ), + ) + // [END function_calling_create_function_declaration] + + // [START function_calling_generate_function_call] + val prompt = "What was the weather in Boston on October 17, 2024?" + val chat = generativeModel.startChat() + // Send the user's question (the prompt) to the model using multi-turn chat. + val result = chat.sendMessage(prompt) + + val functionCalls = result.functionCalls + // When the model responds with one or more function calls, invoke the function(s). + val fetchWeatherCall = functionCalls.find { it.name == "fetchWeather" } + + // Forward the structured input data prepared by the model + // to the hypothetical external API. + val functionResponse = + fetchWeatherCall?.let { + // Alternatively, if your `Location` class is marked as @Serializable, you can use + // val location = Json.decodeFromJsonElement(it.args["location"]!!) + val location = + Location( + it.args["location"]!!.jsonObject["city"]!!.jsonPrimitive.content, + it.args["location"]!!.jsonObject["state"]!!.jsonPrimitive.content + ) + val date = it.args["date"]!!.jsonPrimitive.content + fetchWeather(location, date) + } + // [END function_calling_generate_function_call] + + // [START function_calling_pass_back_function_response] + // Send the response(s) from the function back to the model + // so that the model can use it to generate its final response. + val finalResponse = + chat.sendMessage( + content("function") { part(FunctionResponsePart("fetchWeather", functionResponse!!)) } + ) + + // Log the text response. + println(finalResponse.text ?: "No text in response") + // [END function_calling_pass_back_function_response] + } + + // [START function_calling_write_function] + // This function calls a hypothetical external API that returns + // a collection of weather information for a given location on a given date. + // `location` is an object of the form { city: string, state: string } + data class Location(val city: String, val state: String) + + suspend fun fetchWeather(location: Location, date: String): JsonObject { + + // TODO(developer): Write a standard function that would call to an external weather API. + + // For demo purposes, this hypothetical response is hardcoded here in the expected format. + return JsonObject( + mapOf( + "temperature" to JsonPrimitive(38), + "chancePrecipitation" to JsonPrimitive("56%"), + "cloudConditions" to JsonPrimitive("partlyCloudy") + ) + ) + } + // [END function_calling_write_function] +} diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt index 1bce91e3..c626be89 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt @@ -1,6 +1,24 @@ -package com.google.firebase.example.ailogic.kotlin +package com.google.firebase.example.ailogic.kotlin; import androidx.lifecycle.ViewModel +import com.google.firebase.Firebase +import com.google.firebase.ai.ai +import com.google.firebase.ai.type.FunctionDeclaration +import com.google.firebase.ai.type.GenerativeBackend +import com.google.firebase.ai.type.Tool class GoogleAISnippets : ViewModel() { + fun functionCalling(fetchWeatherTool: FunctionDeclaration) { + // [START function_calling_specify_declaration_during_init] + // 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-2.5-flash", + // Provide the function declaration to the model. + tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))) + ) + // [END function_calling_specify_declaration_during_init] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt index 8954e1b4..5f5386b6 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt @@ -1,6 +1,24 @@ -package com.google.firebase.example.ailogic.kotlin +package com.google.firebase.example.ailogic.kotlin; import androidx.lifecycle.ViewModel +import com.google.firebase.ai.FirebaseAI +import com.google.firebase.ai.type.FunctionDeclaration +import com.google.firebase.ai.type.GenerativeBackend +import com.google.firebase.ai.type.Tool class VertexAISnippets : ViewModel() { + fun functionCalling(fetchWeatherTool: FunctionDeclaration) { + // [START function_calling_specify_declaration_during_init] + // Initialize the Vertex AI Gemini API backend service + // Optionally specify the location to access the model (`global` is recommended) + // Create a `GenerativeModel` instance with a model that supports your use case + val model = + FirebaseAI.getInstance(backend = GenerativeBackend.vertexAI(location = "global")) + .generativeModel( + modelName = "gemini-2.5-flash", + // Provide the function declaration to the model. + tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))) + ) + // [END function_calling_specify_declaration_during_init] + } } \ No newline at end of file From c3328bb1ba85977257ed16e70251d03d09557d19 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 20 Aug 2025 20:57:44 +0100 Subject: [PATCH 3/5] feat: safety settings and model parameter snippets --- .../ailogic/java/GoogleAISnippets.java | 160 ++++++++++++++++- .../ailogic/java/VertexAISnippets.java | 161 +++++++++++++++++- .../ailogic/kotlin/GoogleAISnippets.kt | 143 +++++++++++++++- .../ailogic/kotlin/VertexAISnippets.kt | 143 +++++++++++++++- 4 files changed, 598 insertions(+), 9 deletions(-) diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java index 610a7f09..73b3762e 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java @@ -1,12 +1,29 @@ package com.google.firebase.example.ailogic.java; +import androidx.annotation.OptIn; import androidx.lifecycle.ViewModel; import com.google.firebase.ai.FirebaseAI; import com.google.firebase.ai.java.GenerativeModelFutures; import com.google.firebase.ai.type.GenerativeBackend; +import com.google.firebase.ai.type.PublicPreviewAPI; import com.google.firebase.ai.type.Tool; import com.google.firebase.ai.type.FunctionDeclaration; import java.util.List; +import com.google.firebase.ai.type.GenerationConfig; +import com.google.firebase.ai.type.ImagenGenerationConfig; +import com.google.firebase.ai.type.ImagenAspectRatio; +import com.google.firebase.ai.type.ImagenImageFormat; +import com.google.firebase.ai.type.LiveGenerationConfig; +import com.google.firebase.ai.type.ResponseModality; +import com.google.firebase.ai.type.SpeechConfig; +import com.google.firebase.ai.type.Voice; +import com.google.firebase.ai.type.Voices; +import com.google.firebase.ai.java.ImagenModelFutures; +import com.google.firebase.ai.java.LiveModelFutures; +import com.google.firebase.ai.type.SafetySetting; +import com.google.firebase.ai.type.HarmCategory; +import com.google.firebase.ai.type.HarmBlockThreshold; +import java.util.Collections; public class GoogleAISnippets extends ViewModel { void functionCalling(FunctionDeclaration fetchWeatherTool) { @@ -17,11 +34,152 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( - "gemini-2.5-flash", + "gemini-1.5-flash", null, null, // Provide the function declaration to the model. List.of(Tool.functionDeclarations(List.of(fetchWeatherTool))))); // [END function_calling_specify_declaration_during_init] } + + public void modelConfiguration_model_parameters_general() { + // [START model_parameters_general] + // ... + + // Set parameter values in a `GenerationConfig` (example values shown here) + GenerationConfig.Builder configBuilder = new GenerationConfig.Builder(); + configBuilder.maxOutputTokens = 200; + configBuilder.stopSequences = List.of("red"); + configBuilder.temperature = 0.9f; + configBuilder.topK = 16; + configBuilder.topP = 0.1f; + + GenerationConfig config = configBuilder.build(); + + // Specify the config as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + "gemini-1.5-flash", + config + ) + ); + + // ... + // [END model_parameters_general] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_model_parameters_imagen() { + // [START model_parameters_imagen] + // ... + + // Set parameter values in a `ImagenGenerationConfig` (example values shown here) + ImagenGenerationConfig config = new ImagenGenerationConfig.Builder() + .setNegativePrompt("frogs") + .setNumberOfImages(2) + .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) + .setImageFormat(ImagenImageFormat.jpeg(100)) + .setAddWatermark(false) + .build(); + + // Specify the config as part of creating the `ImagenModel` instance + ImagenModelFutures model = ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .imagenModel( + "imagen-3", + config + ) + ); + + // ... + // [END model_parameters_imagen] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_model_parameters_live() { + // [START model_parameters_live] + // ... + + // Set parameter values in a `LiveGenerationConfig` (example values shown here) + LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder(); + configBuilder.setMaxOutputTokens(200); + configBuilder.setResponseModality(ResponseModality.AUDIO); + + configBuilder.setSpeechConfig(new SpeechConfig(new Voice("FENRIR"))); + configBuilder.setTemperature(0.9f); + configBuilder.topK = 16; + configBuilder.topP = 0.1f; + + LiveGenerationConfig config = configBuilder.build(); + + // Initialize the Gemini Developer API backend service + // Specify the config as part of creating the `LiveModel` instance + LiveModelFutures model = LiveModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .liveModel( + "gemini-2.5-flash", + config + ) + ); + + // ... + // [END model_parameters_live] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_safety_settings_imagen() { + // [START safety_settings_imagen] + // Specify the safety settings as part of creating the `ImagenModel` instance + ImagenModelFutures model = ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .imagenModel( + /* modelName */ "imagen-3", + /* imageGenerationConfig */ null) + ); + + // ... + // [END safety_settings_imagen] + } + + public void modelConfiguration_safety_settings_multiple() { + // [START safety_settings_multiple] + SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, + HarmBlockThreshold.ONLY_HIGH, null); + + SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH, + HarmBlockThreshold.MEDIUM_AND_ABOVE, null); + + // Specify the safety settings as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + /* modelName */ "gemini-2.5-flash", + /* generationConfig is optional */ null, + List.of(harassmentSafety, hateSpeechSafety) + ) + ); + + // ... + // [END safety_settings_multiple] + } + + public void modelConfiguration_safety_settings_single() { + // [START safety_settings_single] + SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, + HarmBlockThreshold.ONLY_HIGH, null); + + // Specify the safety settings as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + Collections.singletonList(harassmentSafety) + ) + ); + + // ... + // [END safety_settings_single] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java index ce3f348b..94702b28 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java @@ -1,13 +1,29 @@ package com.google.firebase.example.ailogic.java; +import androidx.annotation.OptIn; import androidx.lifecycle.ViewModel; -import com.google.firebase.ai.type.GenerativeBackend; import com.google.firebase.ai.FirebaseAI; import com.google.firebase.ai.java.GenerativeModelFutures; -import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.java.ImagenModelFutures; +import com.google.firebase.ai.java.LiveModelFutures; import com.google.firebase.ai.type.FunctionDeclaration; +import com.google.firebase.ai.type.GenerationConfig; +import com.google.firebase.ai.type.GenerativeBackend; +import com.google.firebase.ai.type.HarmBlockThreshold; +import com.google.firebase.ai.type.HarmCategory; +import com.google.firebase.ai.type.ImagenAspectRatio; +import com.google.firebase.ai.type.ImagenGenerationConfig; +import com.google.firebase.ai.type.ImagenImageFormat; +import com.google.firebase.ai.type.LiveGenerationConfig; +import com.google.firebase.ai.type.PublicPreviewAPI; +import com.google.firebase.ai.type.ResponseModality; +import com.google.firebase.ai.type.SafetySetting; +import com.google.firebase.ai.type.SpeechConfig; +import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.type.Voice; +import java.util.Collections; import java.util.List; public class VertexAISnippets extends ViewModel { @@ -27,4 +43,145 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) { List.of(Tool.functionDeclarations(List.of(fetchWeatherTool))))); // [END function_calling_specify_declaration_during_init] } + + public void modelConfiguration_model_parameters_general() { + // [START model_parameters_general] + // ... + + // Set parameter values in a `GenerationConfig` (example values shown here) + GenerationConfig.Builder configBuilder = new GenerationConfig.Builder(); + configBuilder.maxOutputTokens = 200; + configBuilder.stopSequences = List.of("red"); + configBuilder.temperature = 0.9f; + configBuilder.topK = 16; + configBuilder.topP = 0.1f; + + GenerationConfig config = configBuilder.build(); + + // Specify the config as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + "gemini-1.5-flash", + config + ) + ); + + // ... + // [END model_parameters_general] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_model_parameters_imagen() { + // [START model_parameters_imagen] + // ... + + // Set parameter values in a `ImagenGenerationConfig` (example values shown here) + ImagenGenerationConfig config = new ImagenGenerationConfig.Builder() + .setNegativePrompt("frogs") + .setNumberOfImages(2) + .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) + .setImageFormat(ImagenImageFormat.jpeg(100)) + .setAddWatermark(false) + .build(); + + // Specify the config as part of creating the `ImagenModel` instance + ImagenModelFutures model = ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .imagenModel( + "imagen-3", + config + ) + ); + + // ... + // [END model_parameters_imagen] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_model_parameters_live() { + // [START model_parameters_live] + // ... + + // Set parameter values in a `LiveGenerationConfig` (example values shown here) + LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder(); + configBuilder.setMaxOutputTokens(200); + configBuilder.setResponseModality(ResponseModality.AUDIO); + + configBuilder.setSpeechConfig(new SpeechConfig(new Voice("FENRIR"))); + configBuilder.setTemperature(0.9f); + configBuilder.topK = 16; + configBuilder.topP = 0.1f; + + LiveGenerationConfig config = configBuilder.build(); + + // Initialize the Vertex AI Gemini API backend service + // Specify the config as part of creating the `LiveModel` instance + LiveModelFutures model = LiveModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .liveModel( + "gemini-1.5-flash", + config + ) + ); + + // ... + // [END model_parameters_live] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void modelConfiguration_safety_settings_imagen() { + // [START safety_settings_imagen] + // Specify the safety settings as part of creating the `ImagenModel` instance + ImagenModelFutures model = ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .imagenModel( + /* modelName */ "imagen-3", + /* imageGenerationConfig */ null) + ); + + // ... + // [END safety_settings_imagen] + } + + public void modelConfiguration_safety_settings_multiple() { + // [START safety_settings_multiple] + SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, + HarmBlockThreshold.ONLY_HIGH, null); + + SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH, + HarmBlockThreshold.MEDIUM_AND_ABOVE, null); + + // Specify the safety settings as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + List.of(harassmentSafety, hateSpeechSafety) + ) + ); + + // ... + // [END safety_settings_multiple] + } + + public void modelConfiguration_safety_settings_single() { + // [START safety_settings_single] + SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, + HarmBlockThreshold.ONLY_HIGH, null); + + // Specify the safety settings as part of creating the `GenerativeModel` instance + GenerativeModelFutures model = GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + Collections.singletonList(harassmentSafety) + ) + ); + + // ... + // [END safety_settings_single] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt index c626be89..4d04f94a 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt @@ -1,4 +1,4 @@ -package com.google.firebase.example.ailogic.kotlin; +package com.google.firebase.example.ailogic.kotlin import androidx.lifecycle.ViewModel import com.google.firebase.Firebase @@ -6,6 +6,23 @@ import com.google.firebase.ai.ai import com.google.firebase.ai.type.FunctionDeclaration import com.google.firebase.ai.type.GenerativeBackend import com.google.firebase.ai.type.Tool +import com.google.firebase.ai.type.ImagenGenerationConfig +import com.google.firebase.ai.type.ImagenAspectRatio +import com.google.firebase.ai.type.ImagenImageFormat +import com.google.firebase.ai.type.ResponseModality +import com.google.firebase.ai.type.SpeechConfig +import com.google.firebase.ai.type.Voices +import com.google.firebase.ai.type.ImagenSafetySettings +import com.google.firebase.ai.type.ImagenSafetyFilterLevel +import com.google.firebase.ai.type.ImagenPersonFilterLevel +import com.google.firebase.ai.type.SafetySetting +import com.google.firebase.ai.type.HarmCategory +import com.google.firebase.ai.type.HarmBlockThreshold +import com.google.firebase.ai.type.PublicPreviewAPI +import com.google.firebase.ai.type.Voice +import com.google.firebase.ai.type.generationConfig +import com.google.firebase.ai.type.imagenGenerationConfig +import com.google.firebase.ai.type.liveGenerationConfig class GoogleAISnippets : ViewModel() { fun functionCalling(fetchWeatherTool: FunctionDeclaration) { @@ -15,10 +32,130 @@ class GoogleAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel( - modelName = "gemini-2.5-flash", + modelName = "gemini-1.5-flash", // Provide the function declaration to the model. - tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))) + tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))), ) // [END function_calling_specify_declaration_during_init] } + + fun modelConfiguration_model_parameters_general() { + // [START model_parameters_general] + // ... + + // Set parameter values in a `GenerationConfig` (example values shown here) + val config = generationConfig { + maxOutputTokens = 200 + stopSequences = listOf("red") + temperature = 0.9f + topK = 16 + topP = 0.1f + } + + // Initialize the Gemini Developer API backend service + // Specify the config as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( + modelName = "gemini-1.5-flash", + generationConfig = config + ) + + // ... + // [END model_parameters_general] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_model_parameters_imagen() { + // [START model_parameters_imagen] + // ... + + // Set parameter values in a `ImagenGenerationConfig` (example values shown here) + val config = imagenGenerationConfig { + negativePrompt = "frogs" + numberOfImages = 2 + aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9 + imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100) + addWatermark = false + } + + // Initialize the Gemini Developer API backend service + // Specify the config as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( + modelName = "imagen-3", + generationConfig = config + ) + + // ... + // [END model_parameters_imagen] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_model_parameters_live() { + // [START model_parameters_live] + // ... + + // Set parameter values in a `LiveGenerationConfig` (example values shown here) + val config = liveGenerationConfig { + maxOutputTokens = 200 + responseModality = ResponseModality.AUDIO + speechConfig = SpeechConfig(Voice("FENRIR")) + temperature = 0.9f + topK = 16 + topP = 0.1f + } + + // Initialize the Gemini Developer API backend service + // Specify the config as part of creating the `LiveModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( + modelName = "gemini-1.5-flash", + generationConfig = config + ) + + // ... + // [END model_parameters_live] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_safety_settings_imagen() { + // [START safety_settings_imagen] + // Specify the safety settings as part of creating the `ImagenModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel( + modelName = "imagen-3", + safetySettings = ImagenSafetySettings( + safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, + personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL + ) + ) + + // ... + // [END safety_settings_imagen] + } + + fun modelConfiguration_safety_settings_multiple() { + // [START safety_settings_multiple] + val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) + val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) + + // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf(harassmentSafety, hateSpeechSafety) + ) + + // ... + // [END safety_settings_multiple] + } + + fun modelConfiguration_safety_settings_single() { + // [START safety_settings_single] + // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf( + SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) + ) + ) + + // ... + // [END safety_settings_single] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt index 5f5386b6..e8ad1dc0 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt @@ -1,10 +1,27 @@ -package com.google.firebase.example.ailogic.kotlin; +package com.google.firebase.example.ailogic.kotlin import androidx.lifecycle.ViewModel +import com.google.firebase.Firebase import com.google.firebase.ai.FirebaseAI +import com.google.firebase.ai.ai import com.google.firebase.ai.type.FunctionDeclaration import com.google.firebase.ai.type.GenerativeBackend +import com.google.firebase.ai.type.HarmBlockThreshold +import com.google.firebase.ai.type.HarmCategory +import com.google.firebase.ai.type.ImagenAspectRatio +import com.google.firebase.ai.type.ImagenImageFormat +import com.google.firebase.ai.type.ImagenPersonFilterLevel +import com.google.firebase.ai.type.ImagenSafetyFilterLevel +import com.google.firebase.ai.type.ImagenSafetySettings +import com.google.firebase.ai.type.PublicPreviewAPI +import com.google.firebase.ai.type.ResponseModality +import com.google.firebase.ai.type.SafetySetting +import com.google.firebase.ai.type.SpeechConfig import com.google.firebase.ai.type.Tool +import com.google.firebase.ai.type.Voice +import com.google.firebase.ai.type.generationConfig +import com.google.firebase.ai.type.imagenGenerationConfig +import com.google.firebase.ai.type.liveGenerationConfig class VertexAISnippets : ViewModel() { fun functionCalling(fetchWeatherTool: FunctionDeclaration) { @@ -15,10 +32,130 @@ class VertexAISnippets : ViewModel() { val model = FirebaseAI.getInstance(backend = GenerativeBackend.vertexAI(location = "global")) .generativeModel( - modelName = "gemini-2.5-flash", + modelName = "gemini-1.5-flash", // Provide the function declaration to the model. - tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))) + tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))), ) // [END function_calling_specify_declaration_during_init] } + + fun modelConfiguration_model_parameters_general() { + // [START model_parameters_general] + // ... + + // Set parameter values in a `GenerationConfig` (example values shown here) + val config = generationConfig { + maxOutputTokens = 200 + stopSequences = listOf("red") + temperature = 0.9f + topK = 16 + topP = 0.1f + } + + // Initialize the Vertex AI Gemini API backend service + // Specify the config as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( + modelName = "gemini-1.5-flash", + generationConfig = config + ) + + // ... + // [END model_parameters_general] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_model_parameters_imagen() { + // [START model_parameters_imagen] + // ... + + // Set parameter values in a `ImagenGenerationConfig` (example values shown here) + val config = imagenGenerationConfig { + negativePrompt = "frogs" + numberOfImages = 2 + aspectRatio = ImagenAspectRatio.LANDSCAPE_16x9 + imageFormat = ImagenImageFormat.jpeg(compressionQuality = 100) + addWatermark = false + } + + // Initialize the Vertex AI Gemini API backend service + // Specify the config as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( + modelName = "imagen-3", + generationConfig = config + ) + + // ... + // [END model_parameters_imagen] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_model_parameters_live() { + // [START model_parameters_live] + // ... + + // Set parameter values in a `LiveGenerationConfig` (example values shown here) + val config = liveGenerationConfig { + maxOutputTokens = 200 + responseModality = ResponseModality.AUDIO + speechConfig = SpeechConfig(Voice("FENRIR")) + temperature = 0.9f + topK = 16 + topP = 0.1f + } + + // Initialize the Vertex AI Gemini API backend service + // Specify the config as part of creating the `LiveModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel( + modelName = "gemini-1.5-flash", + generationConfig = config + ) + + // ... + // [END model_parameters_live] + } + + @OptIn(PublicPreviewAPI::class) + fun modelConfiguration_safety_settings_imagen() { + // [START safety_settings_imagen] + // Specify the safety settings as part of creating the `ImagenModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( + modelName = "imagen-3", + safetySettings = ImagenSafetySettings( + safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, + personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL + ) + ) + + // ... + // [END safety_settings_imagen] + } + + fun modelConfiguration_safety_settings_multiple() { + // [START safety_settings_multiple] + val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) + val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) + + // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf(harassmentSafety, hateSpeechSafety) + ) + + // ... + // [END safety_settings_multiple] + } + + fun modelConfiguration_safety_settings_single() { + // [START safety_settings_single] + // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf( + SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) + ) + ) + + // ... + // [END safety_settings_single] + } } \ No newline at end of file From b888231a88e8ab3e3d1528b1034909f732493888 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 20 Aug 2025 21:19:19 +0100 Subject: [PATCH 4/5] feat: add system instructions snippets --- .../ailogic/java/GoogleAISnippets.java | 158 ++++++++++-------- .../ailogic/java/VertexAISnippets.java | 137 ++++++++------- .../ailogic/kotlin/GoogleAISnippets.kt | 102 ++++++----- .../ailogic/kotlin/VertexAISnippets.kt | 89 ++++++---- 4 files changed, 291 insertions(+), 195 deletions(-) diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java index 73b3762e..e99d595b 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java @@ -2,28 +2,32 @@ import androidx.annotation.OptIn; import androidx.lifecycle.ViewModel; + import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.GenerativeModel; +import com.google.firebase.ai.LiveGenerativeModel; import com.google.firebase.ai.java.GenerativeModelFutures; -import com.google.firebase.ai.type.GenerativeBackend; -import com.google.firebase.ai.type.PublicPreviewAPI; -import com.google.firebase.ai.type.Tool; +import com.google.firebase.ai.java.ImagenModelFutures; +import com.google.firebase.ai.java.LiveModelFutures; +import com.google.firebase.ai.type.Content; import com.google.firebase.ai.type.FunctionDeclaration; -import java.util.List; import com.google.firebase.ai.type.GenerationConfig; -import com.google.firebase.ai.type.ImagenGenerationConfig; +import com.google.firebase.ai.type.GenerativeBackend; +import com.google.firebase.ai.type.HarmBlockThreshold; +import com.google.firebase.ai.type.HarmCategory; import com.google.firebase.ai.type.ImagenAspectRatio; +import com.google.firebase.ai.type.ImagenGenerationConfig; import com.google.firebase.ai.type.ImagenImageFormat; import com.google.firebase.ai.type.LiveGenerationConfig; +import com.google.firebase.ai.type.PublicPreviewAPI; import com.google.firebase.ai.type.ResponseModality; +import com.google.firebase.ai.type.SafetySetting; import com.google.firebase.ai.type.SpeechConfig; +import com.google.firebase.ai.type.Tool; import com.google.firebase.ai.type.Voice; -import com.google.firebase.ai.type.Voices; -import com.google.firebase.ai.java.ImagenModelFutures; -import com.google.firebase.ai.java.LiveModelFutures; -import com.google.firebase.ai.type.SafetySetting; -import com.google.firebase.ai.type.HarmCategory; -import com.google.firebase.ai.type.HarmBlockThreshold; + import java.util.Collections; +import java.util.List; public class GoogleAISnippets extends ViewModel { void functionCalling(FunctionDeclaration fetchWeatherTool) { @@ -57,13 +61,10 @@ public void modelConfiguration_model_parameters_general() { GenerationConfig config = configBuilder.build(); // Specify the config as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .generativeModel( - "gemini-1.5-flash", - config - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel("gemini-1.5-flash", config)); // ... // [END model_parameters_general] @@ -75,22 +76,19 @@ public void modelConfiguration_model_parameters_imagen() { // ... // Set parameter values in a `ImagenGenerationConfig` (example values shown here) - ImagenGenerationConfig config = new ImagenGenerationConfig.Builder() - .setNegativePrompt("frogs") - .setNumberOfImages(2) - .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) - .setImageFormat(ImagenImageFormat.jpeg(100)) - .setAddWatermark(false) - .build(); + ImagenGenerationConfig config = + new ImagenGenerationConfig.Builder() + .setNegativePrompt("frogs") + .setNumberOfImages(2) + .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) + .setImageFormat(ImagenImageFormat.jpeg(100)) + .setAddWatermark(false) + .build(); // Specify the config as part of creating the `ImagenModel` instance - ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .imagenModel( - "imagen-3", - config - ) - ); + ImagenModelFutures model = + ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel("imagen-3", config)); // ... // [END model_parameters_imagen] @@ -115,13 +113,10 @@ public void modelConfiguration_model_parameters_live() { // Initialize the Gemini Developer API backend service // Specify the config as part of creating the `LiveModel` instance - LiveModelFutures model = LiveModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .liveModel( - "gemini-2.5-flash", - config - ) - ); + LiveModelFutures model = + LiveModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .liveModel("gemini-1.5-flash", config)); // ... // [END model_parameters_live] @@ -131,12 +126,10 @@ public void modelConfiguration_model_parameters_live() { public void modelConfiguration_safety_settings_imagen() { // [START safety_settings_imagen] // Specify the safety settings as part of creating the `ImagenModel` instance - ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .imagenModel( - /* modelName */ "imagen-3", - /* imageGenerationConfig */ null) - ); + ImagenModelFutures model = + ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .imagenModel(/* modelName */ "imagen-3", /* imageGenerationConfig */ null)); // ... // [END safety_settings_imagen] @@ -144,21 +137,20 @@ public void modelConfiguration_safety_settings_imagen() { public void modelConfiguration_safety_settings_multiple() { // [START safety_settings_multiple] - SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, - HarmBlockThreshold.ONLY_HIGH, null); + SafetySetting harassmentSafety = + new SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH, null); - SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH, - HarmBlockThreshold.MEDIUM_AND_ABOVE, null); + SafetySetting hateSpeechSafety = + new SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE, null); // Specify the safety settings as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .generativeModel( - /* modelName */ "gemini-2.5-flash", - /* generationConfig is optional */ null, - List.of(harassmentSafety, hateSpeechSafety) - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + List.of(harassmentSafety, hateSpeechSafety))); // ... // [END safety_settings_multiple] @@ -166,20 +158,52 @@ public void modelConfiguration_safety_settings_multiple() { public void modelConfiguration_safety_settings_single() { // [START safety_settings_single] - SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, - HarmBlockThreshold.ONLY_HIGH, null); + SafetySetting harassmentSafety = + new SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH, null); // Specify the safety settings as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .generativeModel( - /* modelName */ "gemini-1.5-flash", - /* generationConfig is optional */ null, - Collections.singletonList(harassmentSafety) - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + Collections.singletonList(harassmentSafety))); // ... // [END safety_settings_single] } + + public void systemInstructions_general() { + // [START system_instructions_general] + // Specify the system instructions as part of creating the `GenerativeModel` instance + GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig (optional) */ null, + /* safetySettings (optional) */ null, + /* tools (optional) */ null, + /* toolConfig (optional) */ null, + /* systemInstructions (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() + ); + + GenerativeModelFutures model = GenerativeModelFutures.from(ai); + // [END system_instructions_general] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void systemInstructions_live() { + // [START system_instructions_live] + // Specify the system instructions as part of creating the `LiveModel` instance + LiveGenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .liveModel( + /* modelName */ "gemini-2.5-flash", + /* generationConfig (optional) */ null, + /* tools (optional) */ null, + /* systemInstructions (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() + ); + + LiveModelFutures model = LiveModelFutures.from(ai); + // [END system_instructions_live] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java index 94702b28..e28e060c 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java @@ -4,9 +4,12 @@ import androidx.lifecycle.ViewModel; import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.GenerativeModel; +import com.google.firebase.ai.LiveGenerativeModel; import com.google.firebase.ai.java.GenerativeModelFutures; import com.google.firebase.ai.java.ImagenModelFutures; import com.google.firebase.ai.java.LiveModelFutures; +import com.google.firebase.ai.type.Content; import com.google.firebase.ai.type.FunctionDeclaration; import com.google.firebase.ai.type.GenerationConfig; import com.google.firebase.ai.type.GenerativeBackend; @@ -36,7 +39,7 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")) .generativeModel( - "gemini-2.5-flash", + "gemini-1.5-flash", null, null, // Provide the function declaration to the model. @@ -59,13 +62,10 @@ public void modelConfiguration_model_parameters_general() { GenerationConfig config = configBuilder.build(); // Specify the config as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .generativeModel( - "gemini-1.5-flash", - config - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel("gemini-1.5-flash", config)); // ... // [END model_parameters_general] @@ -77,22 +77,19 @@ public void modelConfiguration_model_parameters_imagen() { // ... // Set parameter values in a `ImagenGenerationConfig` (example values shown here) - ImagenGenerationConfig config = new ImagenGenerationConfig.Builder() - .setNegativePrompt("frogs") - .setNumberOfImages(2) - .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) - .setImageFormat(ImagenImageFormat.jpeg(100)) - .setAddWatermark(false) - .build(); + ImagenGenerationConfig config = + new ImagenGenerationConfig.Builder() + .setNegativePrompt("frogs") + .setNumberOfImages(2) + .setAspectRatio(ImagenAspectRatio.LANDSCAPE_16x9) + .setImageFormat(ImagenImageFormat.jpeg(100)) + .setAddWatermark(false) + .build(); // Specify the config as part of creating the `ImagenModel` instance - ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .imagenModel( - "imagen-3", - config - ) - ); + ImagenModelFutures model = + ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()).imagenModel("imagen-3", config)); // ... // [END model_parameters_imagen] @@ -117,13 +114,10 @@ public void modelConfiguration_model_parameters_live() { // Initialize the Vertex AI Gemini API backend service // Specify the config as part of creating the `LiveModel` instance - LiveModelFutures model = LiveModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .liveModel( - "gemini-1.5-flash", - config - ) - ); + LiveModelFutures model = + LiveModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .liveModel("gemini-1.5-flash", config)); // ... // [END model_parameters_live] @@ -133,12 +127,10 @@ public void modelConfiguration_model_parameters_live() { public void modelConfiguration_safety_settings_imagen() { // [START safety_settings_imagen] // Specify the safety settings as part of creating the `ImagenModel` instance - ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .imagenModel( - /* modelName */ "imagen-3", - /* imageGenerationConfig */ null) - ); + ImagenModelFutures model = + ImagenModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .imagenModel(/* modelName */ "imagen-3", /* imageGenerationConfig */ null)); // ... // [END safety_settings_imagen] @@ -146,21 +138,20 @@ public void modelConfiguration_safety_settings_imagen() { public void modelConfiguration_safety_settings_multiple() { // [START safety_settings_multiple] - SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, - HarmBlockThreshold.ONLY_HIGH, null); + SafetySetting harassmentSafety = + new SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH, null); - SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH, - HarmBlockThreshold.MEDIUM_AND_ABOVE, null); + SafetySetting hateSpeechSafety = + new SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE, null); // Specify the safety settings as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .generativeModel( - /* modelName */ "gemini-1.5-flash", - /* generationConfig is optional */ null, - List.of(harassmentSafety, hateSpeechSafety) - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + List.of(harassmentSafety, hateSpeechSafety))); // ... // [END safety_settings_multiple] @@ -168,20 +159,52 @@ public void modelConfiguration_safety_settings_multiple() { public void modelConfiguration_safety_settings_single() { // [START safety_settings_single] - SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, - HarmBlockThreshold.ONLY_HIGH, null); + SafetySetting harassmentSafety = + new SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH, null); // Specify the safety settings as part of creating the `GenerativeModel` instance - GenerativeModelFutures model = GenerativeModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .generativeModel( - /* modelName */ "gemini-1.5-flash", - /* generationConfig is optional */ null, - Collections.singletonList(harassmentSafety) - ) - ); + GenerativeModelFutures model = + GenerativeModelFutures.from( + FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig is optional */ null, + Collections.singletonList(harassmentSafety))); // ... // [END safety_settings_single] } + + public void systemInstructions_general() { + // [START system_instructions_general] + // Specify the system instructions as part of creating the `GenerativeModel` instance + GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig (optional) */ null, + /* safetySettings (optional) */ null, + /* 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); + // [END system_instructions_general] + } + + @OptIn(markerClass = PublicPreviewAPI.class) + public void systemInstructions_live() { + // [START system_instructions_live] + // Specify the system instructions as part of creating the `LiveModel` instance + LiveGenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .liveModel( + /* modelName */ "gemini-1.5-flash", + /* generationConfig (optional) */ null, + /* tools (optional) */ null, + /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() + ); + + LiveModelFutures model = LiveModelFutures.from(ai); + // [END system_instructions_live] + } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt index 4d04f94a..82139fd5 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt @@ -5,21 +5,20 @@ import com.google.firebase.Firebase import com.google.firebase.ai.ai import com.google.firebase.ai.type.FunctionDeclaration import com.google.firebase.ai.type.GenerativeBackend -import com.google.firebase.ai.type.Tool -import com.google.firebase.ai.type.ImagenGenerationConfig +import com.google.firebase.ai.type.HarmBlockThreshold +import com.google.firebase.ai.type.HarmCategory import com.google.firebase.ai.type.ImagenAspectRatio import com.google.firebase.ai.type.ImagenImageFormat -import com.google.firebase.ai.type.ResponseModality -import com.google.firebase.ai.type.SpeechConfig -import com.google.firebase.ai.type.Voices -import com.google.firebase.ai.type.ImagenSafetySettings -import com.google.firebase.ai.type.ImagenSafetyFilterLevel import com.google.firebase.ai.type.ImagenPersonFilterLevel -import com.google.firebase.ai.type.SafetySetting -import com.google.firebase.ai.type.HarmCategory -import com.google.firebase.ai.type.HarmBlockThreshold +import com.google.firebase.ai.type.ImagenSafetyFilterLevel +import com.google.firebase.ai.type.ImagenSafetySettings import com.google.firebase.ai.type.PublicPreviewAPI +import com.google.firebase.ai.type.ResponseModality +import com.google.firebase.ai.type.SafetySetting +import com.google.firebase.ai.type.SpeechConfig +import com.google.firebase.ai.type.Tool import com.google.firebase.ai.type.Voice +import com.google.firebase.ai.type.content import com.google.firebase.ai.type.generationConfig import com.google.firebase.ai.type.imagenGenerationConfig import com.google.firebase.ai.type.liveGenerationConfig @@ -54,10 +53,9 @@ class GoogleAISnippets : ViewModel() { // Initialize the Gemini Developer API backend service // Specify the config as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( - modelName = "gemini-1.5-flash", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.googleAI()) + .generativeModel(modelName = "gemini-1.5-flash", generationConfig = config) // ... // [END model_parameters_general] @@ -79,10 +77,9 @@ class GoogleAISnippets : ViewModel() { // Initialize the Gemini Developer API backend service // Specify the config as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( - modelName = "imagen-3", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .imagenModel(modelName = "imagen-3", generationConfig = config) // ... // [END model_parameters_imagen] @@ -105,10 +102,9 @@ class GoogleAISnippets : ViewModel() { // Initialize the Gemini Developer API backend service // Specify the config as part of creating the `LiveModel` instance - val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( - modelName = "gemini-1.5-flash", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.googleAI()) + .liveModel(modelName = "gemini-1.5-flash", generationConfig = config) // ... // [END model_parameters_live] @@ -118,13 +114,16 @@ class GoogleAISnippets : ViewModel() { fun modelConfiguration_safety_settings_imagen() { // [START safety_settings_imagen] // Specify the safety settings as part of creating the `ImagenModel` instance - val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel( - modelName = "imagen-3", - safetySettings = ImagenSafetySettings( - safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, - personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL - ) - ) + val model = + Firebase.ai(backend = GenerativeBackend.googleAI()) + .imagenModel( + modelName = "imagen-3", + safetySettings = + ImagenSafetySettings( + safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, + personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL, + ), + ) // ... // [END safety_settings_imagen] @@ -133,13 +132,16 @@ class GoogleAISnippets : ViewModel() { fun modelConfiguration_safety_settings_multiple() { // [START safety_settings_multiple] val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) - val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) + val hateSpeechSafety = + SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) // Specify the safety settings as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( - modelName = "gemini-1.5-flash", - safetySettings = listOf(harassmentSafety, hateSpeechSafety) - ) + val model = + Firebase.ai(backend = GenerativeBackend.googleAI()) + .generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf(harassmentSafety, hateSpeechSafety), + ) // ... // [END safety_settings_multiple] @@ -148,14 +150,36 @@ class GoogleAISnippets : ViewModel() { fun modelConfiguration_safety_settings_single() { // [START safety_settings_single] // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = + Firebase.ai(backend = GenerativeBackend.googleAI()) + .generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = + listOf(SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)), + ) + + // ... + // [END safety_settings_single] + } + + fun systemInstructions_general() { + // [START system_instructions_general] + // Specify the system instructions as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( modelName = "gemini-1.5-flash", - safetySettings = listOf( - SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) - ) + systemInstruction = content { text("You are a cat. Your name is Neko.") } ) + // [END system_instructions_general] + } - // ... - // [END safety_settings_single] + @OptIn(PublicPreviewAPI::class) + fun systemInstructions_live() { + // [START system_instructions_live] + // Specify the system instructions as part of creating the `LiveModel` instance + val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( + modelName = "gemini-1.5-flash", + systemInstruction = content { text("You are a cat. Your name is Neko.") } + ) + // [END system_instructions_live] } } \ No newline at end of file diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt index e8ad1dc0..49dbacaa 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt @@ -2,7 +2,6 @@ package com.google.firebase.example.ailogic.kotlin import androidx.lifecycle.ViewModel import com.google.firebase.Firebase -import com.google.firebase.ai.FirebaseAI import com.google.firebase.ai.ai import com.google.firebase.ai.type.FunctionDeclaration import com.google.firebase.ai.type.GenerativeBackend @@ -19,6 +18,7 @@ import com.google.firebase.ai.type.SafetySetting import com.google.firebase.ai.type.SpeechConfig import com.google.firebase.ai.type.Tool import com.google.firebase.ai.type.Voice +import com.google.firebase.ai.type.content import com.google.firebase.ai.type.generationConfig import com.google.firebase.ai.type.imagenGenerationConfig import com.google.firebase.ai.type.liveGenerationConfig @@ -30,7 +30,7 @@ class VertexAISnippets : ViewModel() { // Optionally specify the location to access the model (`global` is recommended) // Create a `GenerativeModel` instance with a model that supports your use case val model = - FirebaseAI.getInstance(backend = GenerativeBackend.vertexAI(location = "global")) + Firebase.ai(backend = GenerativeBackend.vertexAI(location = "global")) .generativeModel( modelName = "gemini-1.5-flash", // Provide the function declaration to the model. @@ -54,10 +54,9 @@ class VertexAISnippets : ViewModel() { // Initialize the Vertex AI Gemini API backend service // Specify the config as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( - modelName = "gemini-1.5-flash", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .generativeModel(modelName = "gemini-1.5-flash", generationConfig = config) // ... // [END model_parameters_general] @@ -79,10 +78,9 @@ class VertexAISnippets : ViewModel() { // Initialize the Vertex AI Gemini API backend service // Specify the config as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( - modelName = "imagen-3", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .imagenModel(modelName = "imagen-3", generationConfig = config) // ... // [END model_parameters_imagen] @@ -105,10 +103,9 @@ class VertexAISnippets : ViewModel() { // Initialize the Vertex AI Gemini API backend service // Specify the config as part of creating the `LiveModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel( - modelName = "gemini-1.5-flash", - generationConfig = config - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .liveModel(modelName = "gemini-1.5-flash", generationConfig = config) // ... // [END model_parameters_live] @@ -118,13 +115,16 @@ class VertexAISnippets : ViewModel() { fun modelConfiguration_safety_settings_imagen() { // [START safety_settings_imagen] // Specify the safety settings as part of creating the `ImagenModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).imagenModel( - modelName = "imagen-3", - safetySettings = ImagenSafetySettings( - safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, - personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL - ) - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .imagenModel( + modelName = "imagen-3", + safetySettings = + ImagenSafetySettings( + safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, + personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL, + ), + ) // ... // [END safety_settings_imagen] @@ -133,13 +133,16 @@ class VertexAISnippets : ViewModel() { fun modelConfiguration_safety_settings_multiple() { // [START safety_settings_multiple] val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) - val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) + val hateSpeechSafety = + SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE) // Specify the safety settings as part of creating the `GenerativeModel` instance - val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( - modelName = "gemini-1.5-flash", - safetySettings = listOf(harassmentSafety, hateSpeechSafety) - ) + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = listOf(harassmentSafety, hateSpeechSafety), + ) // ... // [END safety_settings_multiple] @@ -148,14 +151,36 @@ class VertexAISnippets : ViewModel() { fun modelConfiguration_safety_settings_single() { // [START safety_settings_single] // Specify the safety settings as part of creating the `GenerativeModel` instance + val model = + Firebase.ai(backend = GenerativeBackend.vertexAI()) + .generativeModel( + modelName = "gemini-1.5-flash", + safetySettings = + listOf(SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)), + ) + + // ... + // [END safety_settings_single] + } + + fun systemInstructions_general() { + // [START system_instructions_general] + // Specify the system instructions as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( modelName = "gemini-1.5-flash", - safetySettings = listOf( - SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH) - ) + systemInstruction = content { text("You are a cat. Your name is Neko.") } ) + // [END system_instructions_general] + } - // ... - // [END safety_settings_single] + @OptIn(PublicPreviewAPI::class) + fun systemInstructions_live() { + // [START system_instructions_live] + // Specify the system instructions as part of creating the `LiveModel` instance + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel( + modelName = "gemini-1.5-flash", + systemInstruction = content { text("You are a cat. Your name is Neko.") } + ) + // [END system_instructions_live] } -} \ No newline at end of file +} From f68160cfb534d4dbbc87e331e53c64200dd89f86 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 20 Aug 2025 21:21:34 +0100 Subject: [PATCH 5/5] chore: use gemini-2.5-flash and imagen-4.0-generate-001 --- .../example/ailogic/java/GoogleAISnippets.java | 16 ++++++++-------- .../example/ailogic/java/VertexAISnippets.java | 18 +++++++++--------- .../example/ailogic/kotlin/GoogleAISnippets.kt | 18 +++++++++--------- .../example/ailogic/kotlin/VertexAISnippets.kt | 18 +++++++++--------- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java index e99d595b..eae4b311 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/GoogleAISnippets.java @@ -38,7 +38,7 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( - "gemini-1.5-flash", + "gemini-2.5-flash", null, null, // Provide the function declaration to the model. @@ -64,7 +64,7 @@ public void modelConfiguration_model_parameters_general() { GenerativeModelFutures model = GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .generativeModel("gemini-1.5-flash", config)); + .generativeModel("gemini-2.5-flash", config)); // ... // [END model_parameters_general] @@ -88,7 +88,7 @@ public void modelConfiguration_model_parameters_imagen() { // Specify the config as part of creating the `ImagenModel` instance ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel("imagen-3", config)); + FirebaseAI.getInstance(GenerativeBackend.googleAI()).imagenModel("imagen-4.0-generate-001", config)); // ... // [END model_parameters_imagen] @@ -116,7 +116,7 @@ public void modelConfiguration_model_parameters_live() { LiveModelFutures model = LiveModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .liveModel("gemini-1.5-flash", config)); + .liveModel("gemini-2.5-flash", config)); // ... // [END model_parameters_live] @@ -129,7 +129,7 @@ public void modelConfiguration_safety_settings_imagen() { ImagenModelFutures model = ImagenModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) - .imagenModel(/* modelName */ "imagen-3", /* imageGenerationConfig */ null)); + .imagenModel(/* modelName */ "imagen-4.0-generate-001", /* imageGenerationConfig */ null)); // ... // [END safety_settings_imagen] @@ -148,7 +148,7 @@ public void modelConfiguration_safety_settings_multiple() { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig is optional */ null, List.of(harassmentSafety, hateSpeechSafety))); @@ -166,7 +166,7 @@ public void modelConfiguration_safety_settings_single() { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig is optional */ null, Collections.singletonList(harassmentSafety))); @@ -179,7 +179,7 @@ public void systemInstructions_general() { // Specify the system instructions as part of creating the `GenerativeModel` instance GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* tools (optional) */ null, diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java index e28e060c..0f199d10 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/java/VertexAISnippets.java @@ -39,7 +39,7 @@ void functionCalling(FunctionDeclaration fetchWeatherTool) { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")) .generativeModel( - "gemini-1.5-flash", + "gemini-2.5-flash", null, null, // Provide the function declaration to the model. @@ -65,7 +65,7 @@ public void modelConfiguration_model_parameters_general() { GenerativeModelFutures model = GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .generativeModel("gemini-1.5-flash", config)); + .generativeModel("gemini-2.5-flash", config)); // ... // [END model_parameters_general] @@ -89,7 +89,7 @@ public void modelConfiguration_model_parameters_imagen() { // Specify the config as part of creating the `ImagenModel` instance ImagenModelFutures model = ImagenModelFutures.from( - FirebaseAI.getInstance(GenerativeBackend.vertexAI()).imagenModel("imagen-3", config)); + FirebaseAI.getInstance(GenerativeBackend.vertexAI()).imagenModel("imagen-4.0-generate-001", config)); // ... // [END model_parameters_imagen] @@ -117,7 +117,7 @@ public void modelConfiguration_model_parameters_live() { LiveModelFutures model = LiveModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .liveModel("gemini-1.5-flash", config)); + .liveModel("gemini-2.5-flash", config)); // ... // [END model_parameters_live] @@ -130,7 +130,7 @@ public void modelConfiguration_safety_settings_imagen() { ImagenModelFutures model = ImagenModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI()) - .imagenModel(/* modelName */ "imagen-3", /* imageGenerationConfig */ null)); + .imagenModel(/* modelName */ "imagen-4.0-generate-001", /* imageGenerationConfig */ null)); // ... // [END safety_settings_imagen] @@ -149,7 +149,7 @@ public void modelConfiguration_safety_settings_multiple() { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig is optional */ null, List.of(harassmentSafety, hateSpeechSafety))); @@ -167,7 +167,7 @@ public void modelConfiguration_safety_settings_single() { GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig is optional */ null, Collections.singletonList(harassmentSafety))); @@ -180,7 +180,7 @@ public void systemInstructions_general() { // Specify the system instructions as part of creating the `GenerativeModel` instance GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .generativeModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* tools (optional) */ null, @@ -198,7 +198,7 @@ public void systemInstructions_live() { // Specify the system instructions as part of creating the `LiveModel` instance LiveGenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .liveModel( - /* modelName */ "gemini-1.5-flash", + /* modelName */ "gemini-2.5-flash", /* generationConfig (optional) */ null, /* tools (optional) */ null, /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build() diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt index 82139fd5..bbff4aa6 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/GoogleAISnippets.kt @@ -31,7 +31,7 @@ class GoogleAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", // Provide the function declaration to the model. tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))), ) @@ -55,7 +55,7 @@ class GoogleAISnippets : ViewModel() { // Specify the config as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()) - .generativeModel(modelName = "gemini-1.5-flash", generationConfig = config) + .generativeModel(modelName = "gemini-2.5-flash", generationConfig = config) // ... // [END model_parameters_general] @@ -79,7 +79,7 @@ class GoogleAISnippets : ViewModel() { // Specify the config as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) - .imagenModel(modelName = "imagen-3", generationConfig = config) + .imagenModel(modelName = "imagen-4.0-generate-001", generationConfig = config) // ... // [END model_parameters_imagen] @@ -104,7 +104,7 @@ class GoogleAISnippets : ViewModel() { // Specify the config as part of creating the `LiveModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()) - .liveModel(modelName = "gemini-1.5-flash", generationConfig = config) + .liveModel(modelName = "gemini-2.5-flash", generationConfig = config) // ... // [END model_parameters_live] @@ -117,7 +117,7 @@ class GoogleAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .imagenModel( - modelName = "imagen-3", + modelName = "imagen-4.0-generate-001", safetySettings = ImagenSafetySettings( safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, @@ -139,7 +139,7 @@ class GoogleAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", safetySettings = listOf(harassmentSafety, hateSpeechSafety), ) @@ -153,7 +153,7 @@ class GoogleAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.googleAI()) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", safetySettings = listOf(SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)), ) @@ -166,7 +166,7 @@ class GoogleAISnippets : ViewModel() { // [START system_instructions_general] // Specify the system instructions as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", systemInstruction = content { text("You are a cat. Your name is Neko.") } ) // [END system_instructions_general] @@ -177,7 +177,7 @@ class GoogleAISnippets : ViewModel() { // [START system_instructions_live] // Specify the system instructions as part of creating the `LiveModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", systemInstruction = content { text("You are a cat. Your name is Neko.") } ) // [END system_instructions_live] diff --git a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt index 49dbacaa..069bed49 100644 --- a/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt +++ b/firebase-ai/app/src/main/java/com/google/firebase/example/ailogic/kotlin/VertexAISnippets.kt @@ -32,7 +32,7 @@ class VertexAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.vertexAI(location = "global")) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", // Provide the function declaration to the model. tools = listOf(Tool.functionDeclarations(listOf(fetchWeatherTool))), ) @@ -56,7 +56,7 @@ class VertexAISnippets : ViewModel() { // Specify the config as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) - .generativeModel(modelName = "gemini-1.5-flash", generationConfig = config) + .generativeModel(modelName = "gemini-2.5-flash", generationConfig = config) // ... // [END model_parameters_general] @@ -80,7 +80,7 @@ class VertexAISnippets : ViewModel() { // Specify the config as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) - .imagenModel(modelName = "imagen-3", generationConfig = config) + .imagenModel(modelName = "imagen-4.0-generate-001", generationConfig = config) // ... // [END model_parameters_imagen] @@ -105,7 +105,7 @@ class VertexAISnippets : ViewModel() { // Specify the config as part of creating the `LiveModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) - .liveModel(modelName = "gemini-1.5-flash", generationConfig = config) + .liveModel(modelName = "gemini-2.5-flash", generationConfig = config) // ... // [END model_parameters_live] @@ -118,7 +118,7 @@ class VertexAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .imagenModel( - modelName = "imagen-3", + modelName = "imagen-4.0-generate-001", safetySettings = ImagenSafetySettings( safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, @@ -140,7 +140,7 @@ class VertexAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", safetySettings = listOf(harassmentSafety, hateSpeechSafety), ) @@ -154,7 +154,7 @@ class VertexAISnippets : ViewModel() { val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", safetySettings = listOf(SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)), ) @@ -167,7 +167,7 @@ class VertexAISnippets : ViewModel() { // [START system_instructions_general] // Specify the system instructions as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).generativeModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", systemInstruction = content { text("You are a cat. Your name is Neko.") } ) // [END system_instructions_general] @@ -178,7 +178,7 @@ class VertexAISnippets : ViewModel() { // [START system_instructions_live] // Specify the system instructions as part of creating the `LiveModel` instance val model = Firebase.ai(backend = GenerativeBackend.vertexAI()).liveModel( - modelName = "gemini-1.5-flash", + modelName = "gemini-2.5-flash", systemInstruction = content { text("You are a cat. Your name is Neko.") } ) // [END system_instructions_live]