Function calling makes it easier for you to get structured data outputs from generative models. You can then use these outputs to call other APIs and return the relevant response data to the model. In other words, function calling helps you connect generative models to external systems so that the generated content includes the most up-to-date and accurate information.
You can provide Gemini models with descriptions of functions. These are functions that you write in the language of your app (that is, they're not Google Cloud Functions). The model may ask you to call a function and send back the result to help the model handle your query.
If you haven't already, check out the Introduction to function calling to learn more.
Example API for lighting control
Imagine you have a basic lighting control system with an application programming interface (API) and you want to allow users to control the lights through simple text requests. You can use the Function Calling feature to interpret lighting change requests from users and translate them into API calls to set the lighting values. This hypothetical lighting control system lets you control the brightness of the light and it's color temperature, defined as two separate parameters:
Parameter | Type | Required | Description |
---|---|---|---|
brightness |
number | yes | Light level from 0 to 100. Zero is off and 100 is full brightness. |
colorTemperature |
string | yes | Color temperature of the light fixture which can be daylight , cool or warm . |
For simplicity, this imaginary lighting system only has one light, so the user does not have to specify a room or location. Here is an example JSON request you could send to the lighting control API to change the light level to 50% using the daylight color temperature:
{
"brightness": "50",
"colorTemperature": "daylight"
}
This tutorial shows you how to set up a Function Call for the Gemini API to interpret users lighting requests and map them to API settings to control a light's brightness and color temperature values.
Before you begin: Set up your project and API key
Before calling the Gemini API, you need to set up your project and configure your API key.
Get and secure your API key
You need an API key to call the Gemini API. If you don't already have one, create a key in Google AI Studio.
It's strongly recommended that you do not check an API key into your version control system.
This tutorial assumes that you're accessing your API key as an environment variable.
Configure function calling
The following example shows how to declare functions and configure them as tools for the model. Note that this REST example implements a simplified version of the hypothetical lighting control API discussed in the previous section.
cat > tools.json << EOF
{
"function_declarations": [
{
"name": "enable_lights",
"description": "Turn on the lighting system.",
"parameters": { "type": "object" }
},
{
"name": "set_light_color",
"description": "Set the light color. Lights must be enabled for this to work.",
"parameters": {
"type": "object",
"properties": {
"rgb_hex": {
"type": "string",
"description": "The light color as a 6-digit hex string, e.g. ff0000 for red."
}
},
"required": [
"rgb_hex"
]
}
},
{
"name": "stop_lights",
"description": "Turn off the lighting system.",
"parameters": { "type": "object" }
}
]
}
EOF
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY" \
-H 'Content-Type: application/json' \
-d @<(echo '
{
"system_instruction": {
"parts": {
"text": "You are a helpful lighting system bot. You can turn lights on and off, and you can set the color. Do not perform any other tasks."
}
},
"tools": ['$(source "$tools")'],
"tool_config": {
"function_calling_config": {"mode": "none"}
},
"contents": {
"role": "user",
"parts": {
"text": "What can you do?"
}
}
}
') 2>/dev/null |sed -n '/"content"/,/"finishReason"/p'