Skip to content
Open
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
21 changes: 21 additions & 0 deletions components/model/gemini/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,27 @@ func (cm *ChatModel) genInputAndConf(input []*schema.Message, opts ...model.Opti
return "", nil, nil, nil, err
}

// When using built-in tools (e.g. GoogleSearch, URLContext) alongside function calling,
// the Gemini API requires IncludeServerSideToolInvocations to be enabled.
hasBuiltInTools := cm.enableGoogleSearch != nil || cm.enableGoogleSearchRetrieval != nil ||
cm.enableCodeExecution || cm.enableComputerUse != nil || cm.enableURLContext != nil ||
cm.enableFileSearch != nil || cm.enableGoogleMaps != nil
if hasBuiltInTools && len(tools) > 0 {
if cm.cli.ClientConfig().Backend == genai.BackendVertexAI {
return "", nil, nil, nil, fmt.Errorf("combining built-in tools with function calling requires IncludeServerSideToolInvocations, which is not supported by the Vertex AI backend")
}
if m.ToolConfig == nil {
m.ToolConfig = &genai.ToolConfig{}
}
m.ToolConfig.IncludeServerSideToolInvocations = genai.Ptr(true)
// AUTO mode is not supported when IncludeServerSideToolInvocations is enabled; default to VALIDATED.
if m.ToolConfig.FunctionCallingConfig == nil {
m.ToolConfig.FunctionCallingConfig = &genai.FunctionCallingConfig{Mode: genai.FunctionCallingConfigModeValidated}
} else if mode := m.ToolConfig.FunctionCallingConfig.Mode; mode == genai.FunctionCallingConfigModeAuto || mode == genai.FunctionCallingConfigModeUnspecified || mode == "" {
m.ToolConfig.FunctionCallingConfig.Mode = genai.FunctionCallingConfigModeValidated
}
}

if geminiOptions.ResponseJSONSchema != nil {
m.ResponseMIMEType = "application/json"
m.ResponseJsonSchema = geminiOptions.ResponseJSONSchema
Expand Down
86 changes: 86 additions & 0 deletions components/model/gemini/gemini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,92 @@ func TestPopulateToolChoice(t *testing.T) {
}
}

func TestIncludeServerSideToolInvocations(t *testing.T) {
ctx := context.Background()
funcTools := []*genai.FunctionDeclaration{{Name: "tool1"}}
user := []*schema.Message{{Role: schema.User, Content: "hi"}}

geminiClient := &genai.Client{Models: &genai.Models{}}
vertexClient, err := genai.NewClient(ctx, &genai.ClientConfig{
Backend: genai.BackendVertexAI,
APIKey: "fake-key",
})
assert.NoError(t, err)
assert.Equal(t, genai.BackendVertexAI, vertexClient.ClientConfig().Backend)

t.Run("built-in tool with function calling defaults to VALIDATED", func(t *testing.T) {
cm := &ChatModel{
cli: geminiClient,
model: "gemini-x",
tools: funcTools,
enableGoogleSearch: &genai.GoogleSearch{},
}
_, _, m, _, err := cm.genInputAndConf(user)
assert.NoError(t, err)
assert.NotNil(t, m.ToolConfig)
assert.NotNil(t, m.ToolConfig.IncludeServerSideToolInvocations)
assert.True(t, *m.ToolConfig.IncludeServerSideToolInvocations)
assert.NotNil(t, m.ToolConfig.FunctionCallingConfig)
assert.Equal(t, genai.FunctionCallingConfigModeValidated, m.ToolConfig.FunctionCallingConfig.Mode)
})

t.Run("AUTO mode is switched to VALIDATED", func(t *testing.T) {
toolChoiceAllowed := schema.ToolChoiceAllowed
cm := &ChatModel{
cli: geminiClient,
model: "gemini-x",
tools: funcTools,
toolChoice: &toolChoiceAllowed,
enableGoogleSearch: &genai.GoogleSearch{},
}
_, _, m, _, err := cm.genInputAndConf(user)
assert.NoError(t, err)
assert.NotNil(t, m.ToolConfig.IncludeServerSideToolInvocations)
assert.True(t, *m.ToolConfig.IncludeServerSideToolInvocations)
assert.Equal(t, genai.FunctionCallingConfigModeValidated, m.ToolConfig.FunctionCallingConfig.Mode)
})

t.Run("forced (ANY) mode is preserved", func(t *testing.T) {
toolChoiceForced := schema.ToolChoiceForced
cm := &ChatModel{
cli: geminiClient,
model: "gemini-x",
tools: funcTools,
toolChoice: &toolChoiceForced,
enableGoogleSearch: &genai.GoogleSearch{},
}
_, _, m, _, err := cm.genInputAndConf(user)
assert.NoError(t, err)
assert.True(t, *m.ToolConfig.IncludeServerSideToolInvocations)
assert.Equal(t, genai.FunctionCallingConfigModeAny, m.ToolConfig.FunctionCallingConfig.Mode)
})

t.Run("no built-in tools leaves flag unset", func(t *testing.T) {
cm := &ChatModel{
cli: geminiClient,
model: "gemini-x",
tools: funcTools,
}
_, _, m, _, err := cm.genInputAndConf(user)
assert.NoError(t, err)
if m.ToolConfig != nil {
assert.Nil(t, m.ToolConfig.IncludeServerSideToolInvocations)
}
})

t.Run("vertex backend returns unsupported error", func(t *testing.T) {
cm := &ChatModel{
cli: vertexClient,
model: "gemini-x",
tools: funcTools,
enableGoogleSearch: &genai.GoogleSearch{},
}
_, _, _, _, err := cm.genInputAndConf(user)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Vertex AI")
})
}

// isValidUUID checks if a string is a valid UUID format
func isValidUUID(u string) bool {
_, err := uuid.Parse(u)
Expand Down
4 changes: 2 additions & 2 deletions components/model/gemini/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require (
github.com/bytedance/sonic v1.14.1
github.com/cloudwego/eino v0.7.13
github.com/eino-contrib/jsonschema v1.0.3
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.10.0
github.com/wk8/go-ordered-map/v2 v2.1.8
google.golang.org/genai v1.36.0
google.golang.org/genai v1.52.1
)

require (
Expand All @@ -26,7 +27,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/goph/emperror v0.17.2 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions components/model/gemini/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genai v1.36.0 h1:sJCIjqTAmwrtAIaemtTiKkg2TO1RxnYEusTmEQ3nGxM=
google.golang.org/genai v1.36.0/go.mod h1:A3kkl0nyBjyFlNjgxIwKq70julKbIxpSxqKO5gw/gmk=
google.golang.org/genai v1.52.1 h1:dYoljKtLDXMiBdVaClSJ/ZPwZ7j1N0lGjMhwOKOQUlk=
google.golang.org/genai v1.52.1/go.mod h1:A3kkl0nyBjyFlNjgxIwKq70julKbIxpSxqKO5gw/gmk=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
Expand Down
Loading