From 7499ad27f301a56e06f2eb835bd7c28eaa267185 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 29 May 2024 14:29:40 +0100 Subject: [PATCH 1/6] update region tags to match Swift snippets --- .../example/vertexai/java/ChatViewModel.java | 12 ++--- .../vertexai/java/ConfigurationViewModel.java | 8 ++-- .../java/GenerateContentViewModel.java | 44 +++++++++---------- .../example/vertexai/kotlin/ChatViewModel.kt | 12 ++--- .../vertexai/kotlin/ConfigurationViewModel.kt | 8 ++-- .../kotlin/GenerateContentViewModel.kt | 44 +++++++++---------- 6 files changed, 64 insertions(+), 64 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java index b5e15c798..bc63c7d66 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java @@ -29,7 +29,7 @@ public class ChatViewModel extends ViewModel { private GenerativeModelFutures model; void startChatSendMessageStream() { - // [START vertexai_send_message_stream] + // [START chat_streaming] // (optional) Create previous chat history for context Content.Builder userContentBuilder = new Content.Builder(); userContentBuilder.setRole("user"); @@ -84,11 +84,11 @@ public void onError(Throwable t) { } // [END_EXCLUDE] }); - // [END vertexai_send_message_stream] + // [END chat_streaming] } void startChatSendMessage(Executor executor) { - // [START vertexai_send_message] + // [START chat] // (optional) Create previous chat history for context Content.Builder userContentBuilder = new Content.Builder(); userContentBuilder.setRole("user"); @@ -126,12 +126,12 @@ public void onFailure(@NonNull Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_send_message] + // [END chat] } void countTokensChat(Executor executor) { ChatFutures chat = model.startChat(); - // [START vertexai_count_tokens_chat] + // [START count_tokens_chat] List history = chat.getChat().getHistory(); Content messageContent = new Content.Builder() @@ -156,7 +156,7 @@ public void onFailure(@NonNull Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_count_tokens_chat] + // [END count_tokens_chat] } void systemInstructionsText() { diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java index 8d393b2fe..f9ace55c0 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java @@ -18,7 +18,7 @@ public class ConfigurationViewModel extends ViewModel { void configModelParams() { - // [START vertexai_model_params] + // [START configure_model] GenerationConfig.Builder configBuilder = new GenerationConfig.Builder(); configBuilder.temperature = 0.9f; configBuilder.topK = 16; @@ -34,7 +34,7 @@ void configModelParams() { ); GenerativeModelFutures model = GenerativeModelFutures.from(gm); - // [END vertexai_model_params] + // [END configure_model] } void configSafetySettings() { @@ -49,7 +49,7 @@ void configSafetySettings() { GenerativeModelFutures model1 = GenerativeModelFutures.from(gm1); - // [START vertexai_safety_settings] + // [START multi_safety_settings] SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH); @@ -63,6 +63,6 @@ void configSafetySettings() { ); GenerativeModelFutures model = GenerativeModelFutures.from(gm); - // [END vertexai_safety_settings] + // [END multi_safety_settings] } } diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java index e5f0f15fa..fd85a7bb2 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java @@ -37,16 +37,16 @@ public class GenerateContentViewModel extends ViewModel { // Only meant to separate the scope of the initialization snippet // so that it doesn't cause a naming clash with the top level declaration static class InitializationSnippet { - // [START vertexai_init] + // [START initialize_model] GenerativeModel gm = FirebaseVertexAI.getInstance() .generativeModel("gemini-1.5-pro-preview-0409"); GenerativeModelFutures model = GenerativeModelFutures.from(gm); - // [END vertexai_init] + // [END initialize_model] } void generateContentStream() { - // [START vertexai_textonly_stream] + // [START text_gen_text_only_prompt_streaming] Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); @@ -77,11 +77,11 @@ public void onError(Throwable t) { public void onSubscribe(Subscription s) { } }); - // [END vertexai_textonly_stream] + // [END text_gen_text_only_prompt_streaming] } void generateContent(Executor executor) { - // [START vertexai_textonly] + // [START text_gen_text_only_prompt] // Provide a prompt that contains text Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") @@ -101,7 +101,7 @@ public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_textonly] + // [END text_gen_text_only_prompt] } // Fake implementation to exemplify Activity.getResources() @@ -115,7 +115,7 @@ Context getApplicationContext() { } void generateContentWithImageStream() { - // [START vertexai_text_and_image_stream] + // [START text_gen_multimodal_one_image_prompt_streaming] Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky); Content prompt = new Content.Builder() @@ -149,11 +149,11 @@ public void onError(Throwable t) { public void onSubscribe(Subscription s) { } }); - // [END vertexai_text_and_image_stream] + // [END text_gen_multimodal_one_image_prompt_streaming] } void generateContentWithImage(Executor executor) { - // [START vertexai_text_and_image] + // [START text_gen_multimodal_one_image_prompt] Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sparky); Content content = new Content.Builder() @@ -174,11 +174,11 @@ public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_text_and_image] + // [END text_gen_multimodal_one_image_prompt] } void generateContentWithMultipleImagesStream() { - // [START vertexai_text_and_images_stream] + // [START text_gen_multimodal_multi_image_prompt_streaming] Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky_eats_pizza); @@ -215,11 +215,11 @@ public void onError(Throwable t) { public void onSubscribe(Subscription s) { } }); - // [END vertexai_text_and_images_stream] + // [END text_gen_multimodal_multi_image_prompt_streaming] } void generateContentWithMultipleImages(Executor executor) { - // [START vertexai_text_and_images] + // [START text_gen_multimodal_multi_image_prompt] Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky); Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.sparky_eats_pizza); @@ -243,11 +243,11 @@ public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_text_and_images] + // [END text_gen_multimodal_multi_image_prompt] } void generateContentWithVideo(Executor executor, Uri videoUri) { - // [START vertexai_text_and_video] + // [START text_gen_multimodal_video_prompt] ContentResolver resolver = getApplicationContext().getContentResolver(); try (InputStream stream = resolver.openInputStream(videoUri)) { File videoFile = new File(new URI(videoUri.toString())); @@ -281,13 +281,13 @@ public void onFailure(Throwable t) { } catch (URISyntaxException e) { e.printStackTrace(); } - // [END vertexai_text_and_video] + // [END text_gen_multimodal_video_prompt] } void generateContentWithVideoStream( Uri videoUri ) { - // [START vertexai_text_and_video_stream] + // [START text_gen_multimodal_video_prompt_streaming] ContentResolver resolver = getApplicationContext().getContentResolver(); try (InputStream stream = resolver.openInputStream(videoUri)) { File videoFile = new File(new URI(videoUri.toString())); @@ -334,11 +334,11 @@ public void onSubscribe(Subscription s) { } catch (URISyntaxException e) { e.printStackTrace(); } - // [END vertexai_text_and_video_stream] + // [END text_gen_multimodal_video_prompt_streaming] } void countTokensText(Executor executor) { - // [START vertexai_count_tokens_text] + // [START count_tokens_text] Content text = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); @@ -359,11 +359,11 @@ public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_count_tokens_text] + // [END count_tokens_text] } void countTokensMultimodal(Executor executor, Bitmap bitmap) { - // [START vertexai_count_tokens_multimodal] + // [START count_tokens_text_image] Content text = new Content.Builder() .addImage(bitmap) .addText("Where can I buy this") @@ -386,6 +386,6 @@ public void onFailure(Throwable t) { t.printStackTrace(); } }, executor); - // [END vertexai_count_tokens_multimodal] + // [END count_tokens_text_image] } } diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt index abe65c958..06175aa5d 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt @@ -21,7 +21,7 @@ class ChatViewModel : ViewModel() { fun startChatSendMessageStream() { viewModelScope.launch { - // [START vertexai_send_message_stream] + // [START chat_streaming] val chat = generativeModel.startChat( history = listOf( content(role = "user") { text("Hello, I have 2 dogs in my house.") }, @@ -32,13 +32,13 @@ class ChatViewModel : ViewModel() { chat.sendMessageStream("How many paws are in my house?").collect { chunk -> Log.d(TAG, chunk.text ?: "") } - // [END vertexai_send_message_stream] + // [END chat_streaming] } } fun startChatSendMessage() { viewModelScope.launch { - // [START vertexai_send_message] + // [START chat] val chat = generativeModel.startChat( history = listOf( content(role = "user") { text("Hello, I have 2 dogs in my house.") }, @@ -48,19 +48,19 @@ class ChatViewModel : ViewModel() { val response = chat.sendMessage("How many paws are in my house?") Log.d(TAG, response.text ?: "") - // [END vertexai_send_message] + // [END chat] } } fun countTokensChat() { viewModelScope.launch { val chat = generativeModel.startChat() - // [START vertexai_count_tokens_chat] + // [START count_tokens_chat] // Count tokens for a chat prompt val history = chat.history val messageContent = content { text("This is the message I intend to send") } val (tokens, billableChars) = generativeModel.countTokens(*history.toTypedArray(), messageContent) - // [END vertexai_count_tokens_chat] + // [END count_tokens_chat] } } diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt index 7476c9d79..2ef117bb3 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt @@ -11,7 +11,7 @@ import com.google.firebase.vertexai.vertexAI class ConfigurationViewModel : ViewModel() { fun configModelParams() { - // [START vertexai_model_params] + // [START configure_model] val config = generationConfig { temperature = 0.9f topK = 16 @@ -23,7 +23,7 @@ class ConfigurationViewModel : ViewModel() { modelName = "gemini-1.5-pro-preview-0409", generationConfig = config ) - // [END vertexai_model_params] + // [END configure_model] } fun configSafetySettings() { @@ -34,7 +34,7 @@ class ConfigurationViewModel : ViewModel() { ) ) - // [START vertexai_safety_settings] + // [START multi_safety_settings] val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH) val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE) @@ -42,6 +42,6 @@ class ConfigurationViewModel : ViewModel() { modelName = "MODEL_NAME", safetySettings = listOf(harassmentSafety, hateSpeechSafety) ) - // [END vertexai_safety_settings] + // [END multi_safety_settings] } } \ No newline at end of file diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt index adb613463..b0499c74e 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt @@ -23,13 +23,13 @@ class GenerateContentViewModel : ViewModel() { // Only meant to separate the scope of the initialization snippet // so that it doesn't cause a naming clash with the top level generativeModel fun initialize() { - // [START vertexai_init] + // [START initialize_model] val generativeModel = Firebase.vertexAI.generativeModel( // Specify a model that supports your use case // Gemini 1.5 Pro is versatile and can accept both text-only and multimodal prompt inputs modelName = "gemini-1.5-pro-preview-0409" ) - // [END vertexai_init] + // [END initialize_model] } init { @@ -38,7 +38,7 @@ class GenerateContentViewModel : ViewModel() { fun generateContentStream() { viewModelScope.launch { - // [START vertexai_textonly_stream] + // [START text_gen_text_only_prompt_streaming] // Provide a prompt that includes only text val prompt = "Write a story about a magic backpack." // To stream generated text output, call generateContentStream and pass in the prompt @@ -47,26 +47,26 @@ class GenerateContentViewModel : ViewModel() { Log.d(TAG, chunk.text ?: "") fullResponse += chunk.text } - // [END vertexai_textonly_stream] + // [END text_gen_text_only_prompt_streaming] } } fun generateContent() { viewModelScope.launch { - // [START vertexai_textonly] + // [START text_gen_text_only_prompt] // Provide a prompt that includes only text val prompt = "Write a story about a magic backpack." // To generate text output, call generateContent and pass in the prompt val response = generativeModel.generateContent(prompt) Log.d(TAG, response.text ?: "") - // [END vertexai_textonly] + // [END text_gen_text_only_prompt] } } fun generateContentWithImageStream(resources: Resources) { viewModelScope.launch { - // [START vertexai_text_and_image_stream] + // [START text_gen_multimodal_one_image_prompt_streaming] // Loads an image from the app/res/drawable/ directory val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky) @@ -80,13 +80,13 @@ class GenerateContentViewModel : ViewModel() { Log.d(TAG, chunk.text ?: "") fullResponse += chunk.text } - // [END vertexai_text_and_image_stream] + // [END text_gen_multimodal_one_image_prompt_streaming] } } fun generateContentWithImage(resources: Resources) { viewModelScope.launch { - // [START vertexai_text_and_image] + // [START text_gen_multimodal_one_image_prompt] // Loads an image from the app/res/drawable/ directory val bitmap: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky) @@ -97,13 +97,13 @@ class GenerateContentViewModel : ViewModel() { val response = generativeModel.generateContent(prompt) Log.d(TAG, response.text ?: "") - // [END vertexai_text_and_image] + // [END text_gen_multimodal_one_image_prompt] } } fun generateContentWithMultipleImagesStream(resources: Resources) { viewModelScope.launch { - // [START vertexai_text_and_images_stream] + // [START text_gen_multimodal_multi_image_prompt_streaming] // Loads an image from the app/res/drawable/ directory val bitmap1: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky) val bitmap2: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky_eats_pizza) @@ -119,13 +119,13 @@ class GenerateContentViewModel : ViewModel() { Log.d(TAG, chunk.text ?: "") fullResponse += chunk.text } - // [END vertexai_text_and_images_stream] + // [END text_gen_multimodal_multi_image_prompt_streaming] } } fun generateContentWithMultipleImages(resources: Resources) { viewModelScope.launch { - // [START vertexai_text_and_images] + // [START text_gen_multimodal_multi_image_prompt] // Loads an image from the app/res/drawable/ directory val bitmap1: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky) val bitmap2: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.sparky_eats_pizza) @@ -138,7 +138,7 @@ class GenerateContentViewModel : ViewModel() { val response = generativeModel.generateContent(prompt) Log.d(TAG, response.text ?: "") - // [END vertexai_text_and_images] + // [END text_gen_multimodal_multi_image_prompt] } } @@ -147,7 +147,7 @@ class GenerateContentViewModel : ViewModel() { videoUri: Uri ) { viewModelScope.launch { - // [START vertexai_text_and_video_stream] + // [START text_gen_multimodal_video_prompt_streaming] val contentResolver = applicationContext.contentResolver contentResolver.openInputStream(videoUri).use { stream -> stream?.let { @@ -165,7 +165,7 @@ class GenerateContentViewModel : ViewModel() { } } } - // [END vertexai_text_and_video_stream] + // [END text_gen_multimodal_video_prompt_streaming] } } @@ -174,7 +174,7 @@ class GenerateContentViewModel : ViewModel() { videoUri: Uri ) { viewModelScope.launch { - // [START vertexai_text_and_video] + // [START text_gen_multimodal_video_prompt] val contentResolver = applicationContext.contentResolver contentResolver.openInputStream(videoUri).use { stream -> stream?.let { @@ -189,27 +189,27 @@ class GenerateContentViewModel : ViewModel() { Log.d(TAG, response.text ?: "") } } - // [END vertexai_text_and_video] + // [END text_gen_multimodal_video_prompt] } } fun countTokensText() { viewModelScope.launch { - // [START vertexai_count_tokens_text] + // [START count_tokens_text] val (tokens, billableChars) = generativeModel.countTokens("Write a story about a magic backpack.") - // [END vertexai_count_tokens_text] + // [END count_tokens_text] } } fun countTokensMultimodal(bitmap: Bitmap) { viewModelScope.launch { - // [START vertexai_count_tokens_multimodal] + // [START count_tokens_text_image] val prompt = content { image(bitmap) text("Where can I buy this?") } val (tokens, billableChars) = generativeModel.countTokens(prompt) - // [END vertexai_count_tokens_multimodal] + // [END count_tokens_text_image] } } } \ No newline at end of file From f7682d59d493c4e79571aa38c7f923fcc95d9996 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 29 May 2024 14:32:11 +0100 Subject: [PATCH 2/6] replace model name with devsite variable --- .../google/firebase/example/vertexai/java/ChatViewModel.java | 2 +- .../example/vertexai/java/GenerateContentViewModel.java | 2 +- .../google/firebase/example/vertexai/kotlin/ChatViewModel.kt | 4 ++-- .../example/vertexai/kotlin/ConfigurationViewModel.kt | 2 +- .../example/vertexai/kotlin/GenerateContentViewModel.kt | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java index bc63c7d66..ff68cc149 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java @@ -166,7 +166,7 @@ void systemInstructionsText() { .build(); GenerativeModel model = FirebaseVertexAI.getInstance() .generativeModel( - /* modelName */ "gemini-1.5-pro-preview-0409", + /* modelName */ "{{generic_model_name_initialization}}", /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* requestOptions (optional) */ new RequestOptions(), diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java index fd85a7bb2..dbd81e47d 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java @@ -39,7 +39,7 @@ public class GenerateContentViewModel extends ViewModel { static class InitializationSnippet { // [START initialize_model] GenerativeModel gm = FirebaseVertexAI.getInstance() - .generativeModel("gemini-1.5-pro-preview-0409"); + .generativeModel("{{generic_model_name_initialization}}"); GenerativeModelFutures model = GenerativeModelFutures.from(gm); // [END initialize_model] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt index 06175aa5d..e6131d0a9 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt @@ -16,7 +16,7 @@ class ChatViewModel : ViewModel() { private var generativeModel: GenerativeModel init { - generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-pro-preview-0409") + generativeModel = Firebase.vertexAI.generativeModel("{{generic_model_name_initialization}}") } fun startChatSendMessageStream() { @@ -67,7 +67,7 @@ class ChatViewModel : ViewModel() { fun systemInstructionsText() { // [START vertexai_si_text] val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "gemini-1.5-pro-preview-0409", + modelName = "{{generic_model_name_initialization}}", systemInstruction = content { text("You are a cat. Your name is Neko.") }, ) // [END vertexai_si_text] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt index 2ef117bb3..b0e996afc 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt @@ -20,7 +20,7 @@ class ConfigurationViewModel : ViewModel() { stopSequences = listOf("red") } val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "gemini-1.5-pro-preview-0409", + modelName = "{{generic_model_name_initialization}}", generationConfig = config ) // [END configure_model] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt index b0499c74e..ebdb370f5 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt @@ -27,13 +27,13 @@ class GenerateContentViewModel : ViewModel() { val generativeModel = Firebase.vertexAI.generativeModel( // Specify a model that supports your use case // Gemini 1.5 Pro is versatile and can accept both text-only and multimodal prompt inputs - modelName = "gemini-1.5-pro-preview-0409" + modelName = "{{generic_model_name_initialization}}" ) // [END initialize_model] } init { - generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-pro-preview-0409") + generativeModel = Firebase.vertexAI.generativeModel("{{generic_model_name_initialization}}") } fun generateContentStream() { From c9678d80e30e330ecacb1a93bb647d011dcb2fa8 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 29 May 2024 14:39:50 +0100 Subject: [PATCH 3/6] system instructions snippet fix --- .../firebase/example/vertexai/java/ChatViewModel.java | 6 ++++-- .../firebase/example/vertexai/kotlin/ChatViewModel.kt | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java index ff68cc149..d4ba4039d 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java @@ -160,7 +160,9 @@ public void onFailure(@NonNull Throwable t) { } void systemInstructionsText() { - // [START vertexai_si_text] + // [START system_instructions_text] + // Initialize the Vertex AI service and the generative model + // Specify a model that supports system instructions, like a Gemini 1.5 model Content systemInstruction = new Content.Builder() .addText("You are a cat. Your name is Neko.") .build(); @@ -174,6 +176,6 @@ void systemInstructionsText() { /* toolsConfig (optional) */ null, /* systemInstruction (optional) */ systemInstruction ); - // [END vertexai_si_text] + // [END system_instructions_text] } } diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt index e6131d0a9..a9ce8e4b7 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt @@ -65,11 +65,13 @@ class ChatViewModel : ViewModel() { } fun systemInstructionsText() { - // [START vertexai_si_text] + // [START system_instructions_text] + // Initialize the Vertex AI service and the generative model + // Specify a model that supports system instructions, like a Gemini 1.5 model val generativeModel = Firebase.vertexAI.generativeModel( modelName = "{{generic_model_name_initialization}}", systemInstruction = content { text("You are a cat. Your name is Neko.") }, ) - // [END vertexai_si_text] + // [END system_instructions_text] } } \ No newline at end of file From bdfe272f8e8329c12129fb0a56059f155f080d65 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 29 May 2024 21:02:24 +0100 Subject: [PATCH 4/6] add contextual code to count tokens --- .../java/GenerateContentViewModel.java | 17 ++++++++++++----- .../kotlin/GenerateContentViewModel.kt | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java index dbd81e47d..ee99d6734 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java @@ -339,11 +339,12 @@ public void onSubscribe(Subscription s) { void countTokensText(Executor executor) { // [START count_tokens_text] - Content text = new Content.Builder() + Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); - ListenableFuture countTokensResponse = model.countTokens(text); + // Count tokens and billable characters before calling generateContent + ListenableFuture countTokensResponse = model.countTokens(prompt); Futures.addCallback(countTokensResponse, new FutureCallback() { @Override @@ -352,6 +353,9 @@ public void onSuccess(CountTokensResponse result) { int totalBillableTokens = result.getTotalBillableCharacters(); System.out.println("totalTokens = " + totalTokens + "totalBillableTokens = " + totalBillableTokens); + + // To generate text output, call generateContent with the text input + ListenableFuture response = model.generateContent(prompt); } @Override @@ -364,13 +368,13 @@ public void onFailure(Throwable t) { void countTokensMultimodal(Executor executor, Bitmap bitmap) { // [START count_tokens_text_image] - Content text = new Content.Builder() + Content prompt = new Content.Builder() .addImage(bitmap) .addText("Where can I buy this") .build(); - // For text-only input - ListenableFuture countTokensResponse = model.countTokens(text); + // Count tokens and billable characters before calling generateContent + ListenableFuture countTokensResponse = model.countTokens(prompt); Futures.addCallback(countTokensResponse, new FutureCallback() { @Override @@ -379,6 +383,9 @@ public void onSuccess(CountTokensResponse result) { int totalBillableTokens = result.getTotalBillableCharacters(); System.out.println("totalTokens = " + totalTokens + "totalBillableTokens = " + totalBillableTokens); + + // To generate text output, call generateContent with the prompt + ListenableFuture response = model.generateContent(prompt); } @Override diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt index ebdb370f5..d710dea15 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt @@ -196,7 +196,16 @@ class GenerateContentViewModel : ViewModel() { fun countTokensText() { viewModelScope.launch { // [START count_tokens_text] - val (tokens, billableChars) = generativeModel.countTokens("Write a story about a magic backpack.") + val prompt = "Write a story about a magic backpack." + + // Count tokens and billable characters before calling generateContent + val (tokens, billableChars) = generativeModel.countTokens(prompt) + Log.d(TAG, "Total Tokens: $tokens") + Log.d(TAG, "Total Billable Characters: $billableChars") + + // To generate text output, call generateContent with the text input + val response = generativeModel.generateContent(prompt) + Log.d(TAG, response.text ?: "") // [END count_tokens_text] } } @@ -208,7 +217,15 @@ class GenerateContentViewModel : ViewModel() { image(bitmap) text("Where can I buy this?") } + + // Count tokens and billable characters before calling generateContent val (tokens, billableChars) = generativeModel.countTokens(prompt) + Log.d(TAG, "Total Tokens: $tokens") + Log.d(TAG, "Total Billable Characters: $billableChars") + + // To generate text output, call generateContent with the text input + val response = generativeModel.generateContent(prompt) + Log.d(TAG, response.text ?: "") // [END count_tokens_text_image] } } From f64428035181dbc01df0816746e8d654654f55f0 Mon Sep 17 00:00:00 2001 From: rosariopf Date: Wed, 29 May 2024 23:08:56 +0100 Subject: [PATCH 5/6] fix safety settings snippets --- .../vertexai/java/ConfigurationViewModel.java | 18 +++++++++++------- .../vertexai/kotlin/ConfigurationViewModel.kt | 15 +++++++++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java index f9ace55c0..aa2c37a6f 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java @@ -29,7 +29,7 @@ void configModelParams() { GenerationConfig generationConfig = configBuilder.build(); GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( - "MODEL_NAME", + "{{generic_model_name_initialization}}", generationConfig ); @@ -38,17 +38,21 @@ void configModelParams() { } void configSafetySettings() { - SafetySetting harassmentSafety1 = new SafetySetting(HarmCategory.HARASSMENT, + // [START safety_settings] + SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH); - GenerativeModel gm1 = FirebaseVertexAI.Companion.getInstance().generativeModel( - "MODEL_NAME", + GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( + "{{generic_model_name_initialization}}", /* generationConfig is optional */ null, - Collections.singletonList(harassmentSafety1) + Collections.singletonList(harassmentSafety) ); - GenerativeModelFutures model1 = GenerativeModelFutures.from(gm1); + GenerativeModelFutures model = GenerativeModelFutures.from(gm); + // [END safety_settings] + } + void configMultiSafetySettings() { // [START multi_safety_settings] SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH); @@ -57,7 +61,7 @@ void configSafetySettings() { BlockThreshold.MEDIUM_AND_ABOVE); GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( - "MODEL_NAME", + "{{generic_model_name_initialization}}", /* generationConfig is optional */ null, List.of(harassmentSafety, hateSpeechSafety) ); diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt index b0e996afc..99b2c02eb 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt @@ -26,20 +26,27 @@ class ConfigurationViewModel : ViewModel() { // [END configure_model] } - fun configSafetySettings() { - val generativeModel1 = Firebase.vertexAI.generativeModel( - modelName = "MODEL_NAME", + fun configSafetySetting() { + // [START safety_settings] + val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH) + val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE) + + val generativeModel = Firebase.vertexAI.generativeModel( + modelName = "{{generic_model_name_initialization}}", safetySettings = listOf( SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH) ) ) + // [END safety_settings] + } + fun configMultiSafetySettings() { // [START multi_safety_settings] val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH) val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE) val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "MODEL_NAME", + modelName = "{{generic_model_name_initialization}}", safetySettings = listOf(harassmentSafety, hateSpeechSafety) ) // [END multi_safety_settings] From dd5e0b7480e236b93c7040c5d72bff04459fe8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ros=C3=A1rio=20P=2E=20Fernandes?= Date: Wed, 5 Jun 2024 16:21:09 +0100 Subject: [PATCH 6/6] use gemini 1.5 flash --- .../firebase/example/vertexai/java/ChatViewModel.java | 2 +- .../example/vertexai/java/ConfigurationViewModel.java | 6 +++--- .../example/vertexai/java/GenerateContentViewModel.java | 2 +- .../firebase/example/vertexai/kotlin/ChatViewModel.kt | 4 ++-- .../example/vertexai/kotlin/ConfigurationViewModel.kt | 6 +++--- .../example/vertexai/kotlin/GenerateContentViewModel.kt | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java index d4ba4039d..addc9dd07 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ChatViewModel.java @@ -168,7 +168,7 @@ void systemInstructionsText() { .build(); GenerativeModel model = FirebaseVertexAI.getInstance() .generativeModel( - /* modelName */ "{{generic_model_name_initialization}}", + /* modelName */ "gemini-1.5-flash", /* generationConfig (optional) */ null, /* safetySettings (optional) */ null, /* requestOptions (optional) */ new RequestOptions(), diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java index aa2c37a6f..9f335a351 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/ConfigurationViewModel.java @@ -29,7 +29,7 @@ void configModelParams() { GenerationConfig generationConfig = configBuilder.build(); GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( - "{{generic_model_name_initialization}}", + "gemini-1.5-flash", generationConfig ); @@ -43,7 +43,7 @@ void configSafetySettings() { BlockThreshold.ONLY_HIGH); GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( - "{{generic_model_name_initialization}}", + "gemini-1.5-flash", /* generationConfig is optional */ null, Collections.singletonList(harassmentSafety) ); @@ -61,7 +61,7 @@ void configMultiSafetySettings() { BlockThreshold.MEDIUM_AND_ABOVE); GenerativeModel gm = FirebaseVertexAI.Companion.getInstance().generativeModel( - "{{generic_model_name_initialization}}", + "gemini-1.5-flash", /* generationConfig is optional */ null, List.of(harassmentSafety, hateSpeechSafety) ); diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java index ee99d6734..605dab4f4 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/java/GenerateContentViewModel.java @@ -39,7 +39,7 @@ public class GenerateContentViewModel extends ViewModel { static class InitializationSnippet { // [START initialize_model] GenerativeModel gm = FirebaseVertexAI.getInstance() - .generativeModel("{{generic_model_name_initialization}}"); + .generativeModel("gemini-1.5-flash"); GenerativeModelFutures model = GenerativeModelFutures.from(gm); // [END initialize_model] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt index a9ce8e4b7..47433dcee 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ChatViewModel.kt @@ -16,7 +16,7 @@ class ChatViewModel : ViewModel() { private var generativeModel: GenerativeModel init { - generativeModel = Firebase.vertexAI.generativeModel("{{generic_model_name_initialization}}") + generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash") } fun startChatSendMessageStream() { @@ -69,7 +69,7 @@ class ChatViewModel : ViewModel() { // Initialize the Vertex AI service and the generative model // Specify a model that supports system instructions, like a Gemini 1.5 model val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "{{generic_model_name_initialization}}", + modelName = "gemini-1.5-flash", systemInstruction = content { text("You are a cat. Your name is Neko.") }, ) // [END system_instructions_text] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt index 99b2c02eb..32a9eeeb2 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/ConfigurationViewModel.kt @@ -20,7 +20,7 @@ class ConfigurationViewModel : ViewModel() { stopSequences = listOf("red") } val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "{{generic_model_name_initialization}}", + modelName = "gemini-1.5-flash", generationConfig = config ) // [END configure_model] @@ -32,7 +32,7 @@ class ConfigurationViewModel : ViewModel() { val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE) val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "{{generic_model_name_initialization}}", + modelName = "gemini-1.5-flash", safetySettings = listOf( SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.ONLY_HIGH) ) @@ -46,7 +46,7 @@ class ConfigurationViewModel : ViewModel() { val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE) val generativeModel = Firebase.vertexAI.generativeModel( - modelName = "{{generic_model_name_initialization}}", + modelName = "gemini-1.5-flash", safetySettings = listOf(harassmentSafety, hateSpeechSafety) ) // [END multi_safety_settings] diff --git a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt index d710dea15..3867c32ab 100644 --- a/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt +++ b/vertexai/app/src/main/java/com/google/firebase/example/vertexai/kotlin/GenerateContentViewModel.kt @@ -27,13 +27,13 @@ class GenerateContentViewModel : ViewModel() { val generativeModel = Firebase.vertexAI.generativeModel( // Specify a model that supports your use case // Gemini 1.5 Pro is versatile and can accept both text-only and multimodal prompt inputs - modelName = "{{generic_model_name_initialization}}" + modelName = "gemini-1.5-flash" ) // [END initialize_model] } init { - generativeModel = Firebase.vertexAI.generativeModel("{{generic_model_name_initialization}}") + generativeModel = Firebase.vertexAI.generativeModel("gemini-1.5-flash") } fun generateContentStream() {