Skip to content

Remove functionResponse accessor, and mark functionCallingConfig nullable #6373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions firebase-vertexai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Unreleased
* [changed] **Breaking Change**: Changed `functionCallingConfig` parameter type to be nullable in `ToolConfig`. (#6373)
* [changed] **Breaking Change**: Removed `functionResponse` accessor method from `GenerateContentResponse`. (#6373)
* [changed] **Breaking Change**: Migrated `FirebaseVertexAIException` from a sealed class to an abstract class, and marked constructors as internal. (#6368)
* [feature] Added support for `title` and `publicationDate` in citations. (#6309)
* [feature] Added support for `frequencyPenalty`, `presencePenalty`, and `HarmBlockMethod`. (#6309)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal data class Tool(

@Serializable
internal data class ToolConfig(
@SerialName("function_calling_config") val functionCallingConfig: FunctionCallingConfig
@SerialName("function_calling_config") val functionCallingConfig: FunctionCallingConfig?
)

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,19 @@ internal fun HarmBlockMethod.toInternal() =

internal fun ToolConfig.toInternal() =
com.google.firebase.vertexai.common.client.ToolConfig(
com.google.firebase.vertexai.common.client.FunctionCallingConfig(
when (functionCallingConfig.mode) {
FunctionCallingConfig.Mode.ANY ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.ANY
FunctionCallingConfig.Mode.AUTO ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.AUTO
FunctionCallingConfig.Mode.NONE ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.NONE
},
functionCallingConfig.allowedFunctionNames
)
functionCallingConfig?.let {
com.google.firebase.vertexai.common.client.FunctionCallingConfig(
when (it.mode) {
FunctionCallingConfig.Mode.ANY ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.ANY
FunctionCallingConfig.Mode.AUTO ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.AUTO
FunctionCallingConfig.Mode.NONE ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.NONE
},
it.allowedFunctionNames
)
}
)

internal fun HarmBlockThreshold.toInternal() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.firebase.vertexai.type

import android.util.Log

/**
* Represents a response from the model.
*
Expand All @@ -39,41 +37,4 @@ public class GenerateContentResponse(
public val functionCalls: List<FunctionCallPart> by lazy {
candidates.first().content.parts.filterIsInstance<FunctionCallPart>()
}

/**
* Convenience field representing the first function response part in the response, if it exists.
*/
public val functionResponse: FunctionResponsePart? by lazy { firstPartAs() }

private inline fun <reified T : Part> firstPartAs(): T? {
if (candidates.isEmpty()) {
warn("No candidates were found, but was asked to get a candidate.")
return null
}

val (parts, otherParts) = candidates.first().content.parts.partition { it is T }
val type = T::class.simpleName ?: "of the part type you asked for"

if (parts.isEmpty()) {
if (otherParts.isNotEmpty()) {
warn(
"We didn't find any $type, but we did find other part types. Did you ask for the right type?"
)
}

return null
}

if (parts.size > 1) {
warn("Multiple $type were found, returning the first one.")
} else if (otherParts.isNotEmpty()) {
warn("Returning the only $type found, but other part types were present as well.")
}

return parts.first() as T
}

private fun warn(message: String) {
Log.w("GenerateContentResponse", message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ package com.google.firebase.vertexai.type
*
* @param functionCallingConfig The config for function calling
*/
public class ToolConfig(internal val functionCallingConfig: FunctionCallingConfig)
public class ToolConfig(internal val functionCallingConfig: FunctionCallingConfig?)
Loading