Skip to content
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

feat(vertexai): Add function calling basic #4156

Merged
merged 3 commits into from
Jun 6, 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
12 changes: 6 additions & 6 deletions vertexai/function-calling/functioncalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
// with a value for the argument `productName`.
jsondata, err := json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call generated by the model:\n\t%s\n", string(jsondata))

Expand All @@ -121,7 +121,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
}
jsondata, err = json.MarshalIndent(funresp, "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call response sent to the model:\n\t%s\n\n", string(jsondata))

Expand All @@ -139,7 +139,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
// reformulated the response to the user.
jsondata, err = json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "Answer generated by the model:\n\t%s\n\n", string(jsondata))

Expand All @@ -160,7 +160,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
// with a value for the argument `store`.
jsondata, err = json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call generated by the model:\n\t%s\n", string(jsondata))

Expand All @@ -174,7 +174,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
}
jsondata, err = json.MarshalIndent(funresp, "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call response sent to the model:\n\t%s\n\n", string(jsondata))

Expand All @@ -192,7 +192,7 @@ func functionCallsChat(w io.Writer, projectID, location, modelName string) error
// reformulated the response to the user.
jsondata, err = json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "\t", " ")
if err != nil {
return fmt.Errorf("json.Marshal: %w", err)
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "Answer generated by the model:\n\t%s\n\n", string(jsondata))
return nil
Expand Down
125 changes: 125 additions & 0 deletions vertexai/function-calling/functioncalling_basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Function calling lets developers create a description of a function in their code, then pass
// that description to a language model in a request.
//
// This function call example involves 3 messages:
// - ask the model to generate a function call request
// - call the Open API service (simulated in this example)
// - ask the model to interpret the function call response
package functioncalling

// [START generativeaionvertexai_gemini_function_calling]
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"

"cloud.google.com/go/vertexai/genai"
)

// functionCallsBasic opens a chat session and sends 2 messages to the model:
// - first, to convert a text into a structured function call request
// - second, to convert a structured function call response into natural language
func functionCallsBasic(w io.Writer, prompt, projectID, location, modelName string) error {
// prompt := "What's the weather like in Boston?"
// location := "us-central1"
// modelName := "gemini-1.0-pro-002"
ctx := context.Background()
client, err := genai.NewClient(ctx, projectID, location)
if err != nil {
return fmt.Errorf("unable to create client: %w", err)
}
defer client.Close()

model := client.GenerativeModel(modelName)

// Build an OpenAPI schema, in memory
params := &genai.Schema{
Type: genai.TypeObject,
Properties: map[string]*genai.Schema{
"location": {
Type: genai.TypeString,
Description: "location",
},
},
}
fundecl := &genai.FunctionDeclaration{
Name: "getCurrentWeather",
Description: "Get the current weather in a given location",
Parameters: params,
}
model.Tools = []*genai.Tool{
{FunctionDeclarations: []*genai.FunctionDeclaration{fundecl}},
}

chat := model.StartChat()

fmt.Fprintf(w, "Question: %s\n", prompt)
resp, err := chat.SendMessage(ctx, genai.Text(prompt))
if err != nil {
return err
}
if len(resp.Candidates) == 0 ||
len(resp.Candidates[0].Content.Parts) == 0 {
return errors.New("empty response from model")
}

// The model has returned a function call to the declared function `getCurrentWeather`
// with a value for the argument `location`.
jsondata, err := json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "", " ")
if err != nil {
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call generated by the model:\n%s\n\n", string(jsondata))

// Create a function call response, to simulate the result of a call to a
// real service
funresp := &genai.FunctionResponse{
Name: "getCurrentWeather",
Response: map[string]any{
"currentWeather": "sunny",
},
}
jsondata, err = json.MarshalIndent(funresp, "", " ")
if err != nil {
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "function call response sent to the model:\n%s\n\n", string(jsondata))

// And provide the function call response to the model
resp, err = chat.SendMessage(ctx, funresp)
if err != nil {
return err
}
if len(resp.Candidates) == 0 ||
len(resp.Candidates[0].Content.Parts) == 0 {
return errors.New("empty response from model")
}

// The model has taken the function call response as input, and has
// reformulated the response to the user.
jsondata, err = json.MarshalIndent(resp.Candidates[0].Content.Parts[0], "", " ")
if err != nil {
return fmt.Errorf("json.MarshalIndent: %w", err)
}
fmt.Fprintf(w, "Answer generated by the model:\n%s\n", string(jsondata))

return nil
}

// [END generativeaionvertexai_gemini_function_calling]
14 changes: 14 additions & 0 deletions vertexai/function-calling/functioncalling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import (
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)

func Test_functionCallsBasic(t *testing.T) {
tc := testutil.SystemTest(t)

w := io.Discard
prompt := "What's the weather like in Boston?"
location := "us-central1"
modelName := "gemini-1.0-pro-002"

err := functionCallsBasic(w, prompt, tc.ProjectID, location, modelName)
if err != nil {
t.Errorf("Test_functionCalls: %v", err.Error())
}
}

func Test_functionCallsChat(t *testing.T) {
tc := testutil.SystemTest(t)

Expand Down
Loading