Skip to content

Add support for allowed_function_names #6273

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 1 commit into from
Sep 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ internal data class ToolConfig(
)

@Serializable
internal data class FunctionCallingConfig(val mode: Mode) {
internal data class FunctionCallingConfig(
val mode: Mode,
@SerialName("allowed_function_names") val allowedFunctionNames: List<String>? = null
) {
@Serializable
enum class Mode {
@SerialName("MODE_UNSPECIFIED") UNSPECIFIED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ internal fun ToolConfig.toInternal() =
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.AUTO
FunctionCallingConfig.Mode.NONE ->
com.google.firebase.vertexai.common.client.FunctionCallingConfig.Mode.NONE
}
},
functionCallingConfig.allowedFunctionNames
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ package com.google.firebase.vertexai.type
* calling predictions or disable them.
*
* @param mode The function calling mode of the model
* @param allowedFunctionNames Function names to call. Only set when the [Mode.ANY]. Function names
* should match [FunctionDeclaration.name]. With [Mode.ANY], model will predict a function call from
* the set of function names provided.
*/
class FunctionCallingConfig(val mode: Mode) {
class FunctionCallingConfig(val mode: Mode, val allowedFunctionNames: List<String>? = null) {

/** Configuration for dictating when the model should call the attached function. */
enum class Mode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class ToolConfig(val functionCallingConfig: FunctionCallingConfig) {
companion object {
/** Shorthand to construct a ToolConfig that restricts the model from calling any functions */
fun never(): ToolConfig = ToolConfig(FunctionCallingConfig(FunctionCallingConfig.Mode.NONE))
/** Shorthand to construct a ToolConfig that restricts the model to always call some function */
fun always(): ToolConfig = ToolConfig(FunctionCallingConfig(FunctionCallingConfig.Mode.ANY))
/**
* Shorthand to construct a ToolConfig that restricts the model to always call some function.
* You can optionally [allowedFunctionNames] to restrict the model to only call these functions.
* See [FunctionCallingConfig] for more information.
*/
fun always(allowedFunctionNames: List<String>? = null): ToolConfig =
ToolConfig(FunctionCallingConfig(FunctionCallingConfig.Mode.ANY, allowedFunctionNames))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,21 @@ internal class RequestFormatTests {
contents = listOf(Content(parts = listOf(TextPart("Arbitrary")))),
toolConfig =
ToolConfig(
functionCallingConfig =
FunctionCallingConfig(mode = FunctionCallingConfig.Mode.AUTO)
),
)
FunctionCallingConfig(
mode = FunctionCallingConfig.Mode.ANY,
allowedFunctionNames = listOf("allowedFunctionName")
)
)
),
)
.collect { channel.close() }
}

val requestBodyAsText = (mockEngine.requestHistory.first().body as TextContent).text

requestBodyAsText shouldContainJsonKey "tool_config.function_calling_config.mode"
requestBodyAsText shouldContainJsonKey
"tool_config.function_calling_config.allowed_function_names"
}

@Test
Expand Down
Loading