Skip to content

Commit 836e6d3

Browse files
authored
Update vertex quickstart to use correct API (#1673)
There have been several changes in the API of the SDK and this change makes the corresponding updates to the quickstart app.
1 parent a31a78e commit 836e6d3

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

vertexai/app/build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ dependencies {
5656
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6")
5757
implementation("androidx.activity:activity-compose:1.9.3")
5858
implementation("androidx.navigation:navigation-compose:2.8.3")
59-
59+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
6060
implementation(platform("androidx.compose:compose-bom:2024.10.00"))
61+
6162
implementation("androidx.compose.ui:ui")
6263
implementation("androidx.compose.ui:ui-graphics")
6364
implementation("androidx.compose.ui:ui-tooling-preview")
6465
implementation("androidx.compose.material3:material3")
6566
implementation("io.coil-kt:coil-compose:2.7.0")
66-
67-
implementation("com.google.firebase:firebase-analytics:22.1.2")
68-
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta06")
67+
68+
implementation(platform("com.google.firebase:firebase-bom:33.5.1"))
69+
implementation("com.google.firebase:firebase-vertexai")
6970

7071
testImplementation("junit:junit:4.13.2")
7172
androidTestImplementation("androidx.test.ext:junit:1.2.1")

vertexai/app/src/main/kotlin/com/google/firebase/quickstart/vertexai/GenerativeAiViewModelFactory.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import com.google.firebase.quickstart.vertexai.feature.multimodal.PhotoReasoning
2727
import com.google.firebase.quickstart.vertexai.feature.text.SummarizeViewModel
2828
import com.google.firebase.vertexai.type.Schema
2929
import com.google.firebase.vertexai.type.Tool
30-
import com.google.firebase.vertexai.type.defineFunction
30+
import com.google.firebase.vertexai.type.FunctionDeclaration
3131
import com.google.firebase.vertexai.type.generationConfig
3232
import com.google.firebase.vertexai.vertexAI
33-
import org.json.JSONObject
3433

3534
val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
3635
override fun <T : ViewModel> create(
@@ -47,7 +46,7 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
4746
// Initialize a GenerativeModel with the `gemini-flash` AI model
4847
// for text generation
4948
val generativeModel = Firebase.vertexAI.generativeModel(
50-
modelName = "gemini-1.5-flash-preview-0514",
49+
modelName = "gemini-1.5-flash",
5150
generationConfig = config
5251
)
5352
SummarizeViewModel(generativeModel)
@@ -57,7 +56,7 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
5756
// Initialize a GenerativeModel with the `gemini-flash` AI model
5857
// for multimodal text generation
5958
val generativeModel = Firebase.vertexAI.generativeModel(
60-
modelName = "gemini-1.5-flash-preview-0514",
59+
modelName = "gemini-1.5-flash",
6160
generationConfig = config
6261
)
6362
PhotoReasoningViewModel(generativeModel)
@@ -66,31 +65,32 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
6665
isAssignableFrom(ChatViewModel::class.java) -> {
6766
// Initialize a GenerativeModel with the `gemini-flash` AI model for chat
6867
val generativeModel = Firebase.vertexAI.generativeModel(
69-
modelName = "gemini-1.5-flash-preview-0514",
68+
modelName = "gemini-1.5-flash",
7069
generationConfig = config
7170
)
7271
ChatViewModel(generativeModel)
7372
}
7473

7574
isAssignableFrom(FunctionsChatViewModel::class.java) -> {
7675
// Declare the functions you want to make available to the model
76+
val functionDeclaration = FunctionDeclaration(
77+
name = "upperCase",
78+
description = "Returns the upper case version of the input string",
79+
parameters = mapOf(
80+
"input" to Schema.string( "Text to transform"))
81+
)
7782
val tools = listOf(
78-
Tool(
83+
Tool.functionDeclarations(
7984
listOf(
80-
defineFunction(
81-
"upperCase",
82-
"Returns the upper case version of the input string",
83-
Schema.str("input", "Text to transform")
84-
) { input ->
85-
JSONObject("{\"response\": \"${input.uppercase()}\"}")
86-
}
85+
functionDeclaration
8786
)
8887
)
8988
)
9089

90+
9191
// Initialize a GenerativeModel with the `gemini-pro` AI model for function calling chat
9292
val generativeModel = Firebase.vertexAI.generativeModel(
93-
modelName = "gemini-1.5-pro-preview-0514",
93+
modelName = "gemini-1.5-flash",
9494
generationConfig = config,
9595
tools = tools
9696
)

vertexai/app/src/main/kotlin/com/google/firebase/quickstart/vertexai/feature/audio/AudioViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class AudioViewModel(
4747
viewModelScope.launch(Dispatchers.IO) {
4848
try {
4949
val inputContent = content {
50-
blob("audio/aac", audioBytes)
50+
inlineData(audioBytes, "audio/aac")
5151
text(prompt)
5252
}
5353

vertexai/app/src/main/kotlin/com/google/firebase/quickstart/vertexai/feature/functioncalling/FunctionsChatViewModel.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
2727
import kotlinx.coroutines.flow.StateFlow
2828
import kotlinx.coroutines.flow.asStateFlow
2929
import kotlinx.coroutines.launch
30+
import kotlinx.serialization.json.JsonPrimitive
31+
import kotlinx.serialization.json.buildJsonObject
32+
import java.lang.IllegalArgumentException
3033

3134
class FunctionsChatViewModel(
3235
private val generativeModel: GenerativeModel
@@ -77,18 +80,23 @@ class FunctionsChatViewModel(
7780
val firstFunctionCall = response.functionCalls.firstOrNull()
7881

7982
if (firstFunctionCall != null) {
80-
val matchingFunction =
81-
generativeModel.tools?.flatMap { it.functionDeclarations }
82-
?.first { it.name == firstFunctionCall.name }
83-
?: throw InvalidStateException(
84-
"Model requested nonexistent function \"${firstFunctionCall.name}\" "
83+
val functionCall = firstFunctionCall
84+
val result = when (functionCall.name) {
85+
"upperCase" -> buildJsonObject {
86+
put(
87+
"result",
88+
JsonPrimitive(functionCall.args["text"].toString().uppercase() ?: "")
8589
)
90+
}
8691

87-
val funResult = matchingFunction.execute(firstFunctionCall)
92+
else -> throw IllegalArgumentException(
93+
"Model requested nonexistent function \"${firstFunctionCall.name}\" "
94+
)
95+
}
8896

8997
response = chat.sendMessage(
9098
content(role = "function") {
91-
part(FunctionResponsePart("output", funResult))
99+
part(FunctionResponsePart("upperCase", result))
92100
}
93101
)
94102
}

vertexai/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ plugins {
2020
id("org.jetbrains.kotlin.android") version "2.0.21" apply false
2121
id("org.jetbrains.kotlin.plugin.compose") version "2.0.21" apply false
2222
id("com.google.gms.google-services") version "4.4.2" apply false
23+
kotlin("plugin.serialization") version "2.0.20"
2324
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Mon Sep 30 15:29:58 EDT 2024
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
45
zipStoreBase=GRADLE_USER_HOME
56
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)