The code sample demonstrates how to use the Vertex AI Generative Models to do function calling.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Function calling reference](https://docs.cloud.google.com/gemini-enterprise-agent-platform/reference/models/function-calling)

## Code sample

### Go


Before trying this sample, follow the Go setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    import (
    	"context"
    	"encoding/json"
    	"errors"
    	"fmt"
    	"io"

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

    // functionCallsChat opens a chat session and sends 4 messages to the model:
    // - convert a first text question into a structured function call request
    // - convert the first structured function call response into natural language
    // - convert a second text question into a structured function call request
    // - convert the second structured function call response into natural language
    func functionCallsChat(w io.Writer, projectID, location, modelName string) error {
    	// location := "us-central1"
    	// modelName := "gemini-2.0-flash-001"
    	ctx := context.Background()
    	client, err := genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_NewClient(ctx, projectID, location)
    	if err != nil {
    		return fmt.Errorf("unable to create client: %w", err)
    	}
    	defer client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_Close()

    	model := client.GenerativeModel(modelName)

    	// Build an OpenAPI schema, in memory
    	paramsProduct := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    		Type: genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    		Properties: map[string]*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    			"productName": {
    				Type:        genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    				Description: "Product name",
    			},
    		},
    	}
    	fundeclProductInfo := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{
    		Name:        "getProductSku",
    		Description: "Get the SKU for a product",
    		Parameters:  paramsProduct,
    	}
    	paramsStore := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    		Type: genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    		Properties: map[string]*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{
    			"location": {
    				Type:        genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,
    				Description: "Location",
    			},
    		},
    	}
    	fundeclStoreLocation := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{
    		Name:        "getStoreLocation",
    		Description: "Get the location of the closest store",
    		Parameters:  paramsStore,
    	}
    	model.Tools = []*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Tool{
    		{FunctionDeclarations: []*genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionDeclaration{
    			fundeclProductInfo,
    			fundeclStoreLocation,
    		}},
    	}
    	model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerationConfig_SetTemperature(0.0)

    	chat := model.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_StartChat()

    	// Send a prompt for the first conversation turn that should invoke the getProductSku function
    	prompt := "Do you have the Pixel 8 Pro in stock?"
    	fmt.Fprintf(w, "Question: %s\n", prompt)
    	resp, err := chat.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_ChatSession_SendMessage(ctx, genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Text(prompt))
    	if err != nil {
    		return err
    	}
    	if len(resp.Candidates) == 0 ||
    		len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New("empty response from model")
    	}

    	// The model has returned a function call to the declared function `getProductSku`
    	// with a value for the argument `productName`.
    	jsondata, err := json.MarshalIndent(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0], "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "function call generated by the model:\n\t%s\n", string(jsondata))

    	// Create a function call response, to simulate the result of a call to a
    	// real service
    	funresp := &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{
    		Name: "getProductSku",
    		Response: map[string]any{
    			"sku":      "GA04834-US",
    			"in_stock": "yes",
    		},
    	}
    	jsondata, err = json.MarshalIndent(funresp, "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "function call response sent to the model:\n\t%s\n\n", string(jsondata))

    	// And provide the function call response to the model
    	resp, err = chat.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_ChatSession_SendMessage(ctx, funresp)
    	if err != nil {
    		return err
    	}
    	if len(resp.Candidates) == 0 ||
    		len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_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].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0], "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "Answer generated by the model:\n\t%s\n\n", string(jsondata))

    	// Send a prompt for the second conversation turn that should invoke the getStoreLocation function
    	prompt2 := "Is there a store in Mountain View, CA that I can visit to try it out?"
    	fmt.Fprintf(w, "Question: %s\n", prompt)

    	resp, err = chat.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_ChatSession_SendMessage(ctx, genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Text(prompt2))
    	if err != nil {
    		return err
    	}
    	if len(resp.Candidates) == 0 ||
    		len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New("empty response from model")
    	}

    	// The model has returned a function call to the declared function `getStoreLocation`
    	// with a value for the argument `store`.
    	jsondata, err = json.MarshalIndent(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0], "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "function call generated by the model:\n\t%s\n", string(jsondata))

    	// Create a function call response, to simulate the result of a call to a
    	// real service
    	funresp = &genai.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_FunctionResponse{
    		Name: "getStoreLocation",
    		Response: map[string]any{
    			"store": "2000 N Shoreline Blvd, Mountain View, CA 94043, US",
    		},
    	}
    	jsondata, err = json.MarshalIndent(funresp, "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "function call response sent to the model:\n\t%s\n\n", string(jsondata))

    	// And provide the function call response to the model
    	resp, err = chat.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_ChatSession_SendMessage(ctx, funresp)
    	if err != nil {
    		return err
    	}
    	if len(resp.Candidates) == 0 ||
    		len(resp.Candidates[0].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {
    		return errors.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_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].https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/vertexai/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0], "\t", "  ")
    	if err != nil {
    		return fmt.Errorf("json.MarshalIndent: %w", err)
    	}
    	fmt.Fprintf(w, "Answer generated by the model:\n\t%s\n\n", string(jsondata))
    	return nil
    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).