▶️Fucntion calling

Define an API function

Create a function that makes an API request. This function should be defined within the code of your application, but could call services or APIs outside of your application. The Gemini API does not call this function directly, so you can control how and when this function is executed through your application code. For demonstration purposes, this tutorial defines a mock API function that just returns the requested lighting values:

// Set the brightness and color temperature of a room light. (mock API).
// brightness: Light level from 0 to 100. Zero is off and 100 is full brightness
// colorTemp: Color temperature of the light fixture, which can be `daylight`, `cool` or `warm`.
public Dictionary<string, object> SetLightValues(int brightness, string colorTemp)
{
    // Implement the real API call here

    // Return the set brightness and color temperature.
    return new Dictionary<string, object>
    {
        { "brightness", brightness },
        { "colorTemperature", colorTemp }
    };
}

When you create a function to be used in a function call by the model, you should include as much detail as possible in the function and parameter descriptions. The generative model uses this information to determine which function to select and how to provide values for the parameters in the function call.

For programing languages other than Python, you must create create a separate function declaration for your API. See the other language programming tutorials more details.

Declare functions during model initialization

When you want to use function calling with a model, you must declare your functions when you initialize the model object. You declare functions by setting the model's tools parameter:

Generate a function call

Once you have initialized model with your function declarations, you can prompt the model with the defined function. You should use use function calling using chat prompting (sendMessage()), since function calling generally benefits from having the context of previous prompts and responses.

The AI Development Kit's ChatSession object simplifies managing chat sessions by handling the conversation history for you. You can use the enable_automatic_function_calling to have the SDK automatically

Warning: Do not use this feature in production applications as there are no data input verification checks for automatic function calls.

Parallel function calling

In addition to basic function calling described above, you can also call multiple functions in a single turn. This section shows an example for how you can use parallel function calling.

Step 1. Define the arguments for tools.

Step 2. Define the delegates for tools.

Step 3. Now call the model with an instruction that could use all of the specified tools.

Each of the printed results reflects a single function call that the model has requested. To send the results back, include the responses in the same order as they were requested.

Function call data type mapping

When using the AI Development Kit to extract schemas from C# methods, certain cases, such as nested dictionary-objects, may not be handled correctly. However, the API does support these types. The supported types include:

  • int

  • float

  • bool

  • string

  • List<AllowedType>

  • Dictionary<string, AllowedType>

Important: The SDK converts method parameter type annotations to a format the API understands. The API only supports a limited selection of parameter types, and the C# SDK's automatic conversion only supports a subset of the allowed types above.

First peek inside the model's JsonSchema attribute, you can see how it describes the function(s) you passed it to the model:

Execute the function yourself:

Send the result to the model, to continue the conversation:

Google official document

Last updated