Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions vertexai/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
id("org.jetbrains.kotlin.android")
id("org.jetbrains.kotlin.plugin.compose")
id("com.google.gms.google-services")

}

android {
Expand Down Expand Up @@ -56,16 +57,16 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.6")
implementation("androidx.activity:activity-compose:1.9.2")
implementation("androidx.navigation:navigation-compose:2.8.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")

implementation(platform("androidx.compose:compose-bom:2024.09.03"))

implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("io.coil-kt:coil-compose:2.7.0")

implementation("com.google.firebase:firebase-analytics:22.1.2")
implementation("com.google.firebase:firebase-vertexai:16.0.0-beta06")
implementation("com.google.firebase:firebase-vertexai:16.0.0")

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import com.google.firebase.quickstart.vertexai.feature.multimodal.PhotoReasoning
import com.google.firebase.quickstart.vertexai.feature.text.SummarizeViewModel
import com.google.firebase.vertexai.type.Schema
import com.google.firebase.vertexai.type.Tool
import com.google.firebase.vertexai.type.defineFunction
import com.google.firebase.vertexai.type.FunctionDeclaration
import com.google.firebase.vertexai.type.generationConfig
import com.google.firebase.vertexai.vertexAI
import org.json.JSONObject

val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(
Expand All @@ -47,7 +46,7 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
// Initialize a GenerativeModel with the `gemini-flash` AI model
// for text generation
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-1.5-flash-preview-0514",
modelName = "gemini-1.5-flash",
generationConfig = config
)
SummarizeViewModel(generativeModel)
Expand All @@ -57,7 +56,7 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
// Initialize a GenerativeModel with the `gemini-flash` AI model
// for multimodal text generation
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-1.5-flash-preview-0514",
modelName = "gemini-1.5-flash",
generationConfig = config
)
PhotoReasoningViewModel(generativeModel)
Expand All @@ -66,31 +65,29 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
isAssignableFrom(ChatViewModel::class.java) -> {
// Initialize a GenerativeModel with the `gemini-flash` AI model for chat
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-1.5-flash-preview-0514",
modelName = "gemini-1.5-flash",
generationConfig = config
)
ChatViewModel(generativeModel)
}

isAssignableFrom(FunctionsChatViewModel::class.java) -> {
// Declare the functions you want to make available to the model
val functionDeclaration = FunctionDeclaration(
"upperCase", "Returns the upper case version of the input string", mapOf(
"input" to Schema.string( "Text to transform")))
val tools = listOf(
Tool(
Tool.functionDeclarations(
listOf(
defineFunction(
"upperCase",
"Returns the upper case version of the input string",
Schema.str("input", "Text to transform")
) { input ->
JSONObject("{\"response\": \"${input.uppercase()}\"}")
}
functionDeclaration
)
)
)


// Initialize a GenerativeModel with the `gemini-pro` AI model for function calling chat
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-1.5-pro-preview-0514",
modelName = "gemini-1.5-pro",
generationConfig = config,
tools = tools
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AudioViewModel(
viewModelScope.launch(Dispatchers.IO) {
try {
val inputContent = content {
blob("audio/aac", audioBytes)
inlineData("audio/aac", audioBytes)
text(prompt)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.firebase.quickstart.vertexai.feature.functioncalling
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.firebase.vertexai.GenerativeModel
import com.google.firebase.vertexai.type.FunctionResponse
import com.google.firebase.vertexai.type.FunctionResponsePart
import com.google.firebase.vertexai.type.InvalidStateException
import com.google.firebase.vertexai.type.asTextOrNull
Expand All @@ -27,6 +28,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject

class FunctionsChatViewModel(
private val generativeModel: GenerativeModel
Expand Down Expand Up @@ -77,18 +80,23 @@ class FunctionsChatViewModel(
val firstFunctionCall = response.functionCalls.firstOrNull()

if (firstFunctionCall != null) {
val matchingFunction =
generativeModel.tools?.flatMap { it.functionDeclarations }
?.first { it.name == firstFunctionCall.name }
?: throw InvalidStateException(
"Model requested nonexistent function \"${firstFunctionCall.name}\" "
val functionCall = firstFunctionCall.functionCall
val result = when (functionCall.name) {
"upperCase" -> buildJsonObject {
put(
"result",
JsonPrimitive(functionCall.args["text"].toString().uppercase() ?: "")
)
}

val funResult = matchingFunction.execute(firstFunctionCall)
else -> throw InvalidStateException(
"Model requested nonexistent function \"${firstFunctionCall.functionCall.name}\" "
)
}

response = chat.sendMessage(
content(role = "function") {
part(FunctionResponsePart("output", funResult))
part(FunctionResponsePart(FunctionResponse("upperCase", result)))
}
)
}
Expand Down
2 changes: 2 additions & 0 deletions vertexai/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ plugins {
id("org.jetbrains.kotlin.android") version "2.0.20" apply false
id("org.jetbrains.kotlin.plugin.compose") version "2.0.20" apply false
id("com.google.gms.google-services") version "4.4.2" apply false
kotlin("jvm") version "2.0.20" // or kotlin("multiplatform") or any other kotlin plugin
kotlin("plugin.serialization") version "2.0.20"
}
3 changes: 2 additions & 1 deletion vertexai/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Sep 30 15:29:58 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading