Skip to content

Commit 1f80109

Browse files
committed
Update quickstart to match the API of VertexAI 16.0.0
There have been several changes in the API of the SDK and this change makes the corresponding updates to the quickstart app.
1 parent efd7b70 commit 1f80109

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

vertexai/app/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ plugins {
1919
id("org.jetbrains.kotlin.android")
2020
id("org.jetbrains.kotlin.plugin.compose")
2121
id("com.google.gms.google-services")
22+
2223
}
2324

2425
android {
@@ -56,16 +57,16 @@ dependencies {
5657
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6")
5758
implementation("androidx.activity:activity-compose:1.9.2")
5859
implementation("androidx.navigation:navigation-compose:2.8.1")
59-
60+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
6061
implementation(platform("androidx.compose:compose-bom:2024.09.02"))
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+
6768
implementation("com.google.firebase:firebase-analytics:22.1.0")
68-
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta05")
69+
implementation("com.google.firebase:firebase-vertexai:16.0.0")
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: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,18 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
7474

7575
isAssignableFrom(FunctionsChatViewModel::class.java) -> {
7676
// Declare the functions you want to make available to the model
77+
val functionDeclaration = FunctionDeclaration(
78+
"upperCase", "Returns the upper case version of the input string", mapOf(
79+
"input" to Schema.string( "Text to transform")))
7780
val tools = listOf(
78-
Tool(
81+
Tool.functionDeclarations(
7982
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-
}
83+
functionDeclaration
8784
)
8885
)
8986
)
9087

88+
9189
// Initialize a GenerativeModel with the `gemini-pro` AI model for function calling chat
9290
val generativeModel = Firebase.vertexAI.generativeModel(
9391
modelName = "gemini-1.5-pro-preview-0514",

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("audio/aac", audioBytes)
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
@@ -19,6 +19,7 @@ package com.google.firebase.quickstart.vertexai.feature.functioncalling
1919
import androidx.lifecycle.ViewModel
2020
import androidx.lifecycle.viewModelScope
2121
import com.google.firebase.vertexai.GenerativeModel
22+
import com.google.firebase.vertexai.type.FunctionResponse
2223
import com.google.firebase.vertexai.type.FunctionResponsePart
2324
import com.google.firebase.vertexai.type.InvalidStateException
2425
import com.google.firebase.vertexai.type.asTextOrNull
@@ -27,6 +28,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
2728
import kotlinx.coroutines.flow.StateFlow
2829
import kotlinx.coroutines.flow.asStateFlow
2930
import kotlinx.coroutines.launch
31+
import kotlinx.serialization.json.JsonPrimitive
32+
import kotlinx.serialization.json.buildJsonObject
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.functionCall
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 InvalidStateException(
93+
"Model requested nonexistent function \"${firstFunctionCall.functionCall.name}\" "
94+
)
95+
}
8896

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

vertexai/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ plugins {
2020
id("org.jetbrains.kotlin.android") version "2.0.20" apply false
2121
id("org.jetbrains.kotlin.plugin.compose") version "2.0.20" apply false
2222
id("com.google.gms.google-services") version "4.4.2" apply false
23+
kotlin("jvm") version "2.0.20" // or kotlin("multiplatform") or any other kotlin plugin
24+
kotlin("plugin.serialization") version "2.0.20"
2325
}
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)