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 Cloud Functions). The model may ask you to call a function and send back the result to help the model handle your query.
You can learn more about function calling in the Google Cloud documentation.
Optionally experiment with an alternative "Google AI" version of the Gemini API
Get free-of-charge access (within limits and where available) using Google AI Studio and Google AI client SDKs. These SDKs should be used for prototyping only in mobile and web apps.After you're familiar with how a Gemini API works, migrate to the Vertex AI in Firebase SDKs, which have many additional features important for mobile and web apps, like protecting the API from abuse using Firebase App Check and support for large media files in requests.
Optionally call the Vertex AI Gemini API server-side (like with Python, Node.js, or Go)
Use the server-side Vertex AI SDKs, Firebase Genkit, or Firebase Extensions for the Gemini API.
Before you begin
If you haven't already, complete the getting started guide for the Vertex AI in Firebase SDKs. Make sure that you've done all of the following:
Set up a new or existing Firebase project, including using the Blaze pricing plan and enabling the required APIs.
Connect your app to Firebase, including registering your app and adding your Firebase config to your app.
Add the SDK and initialize the Vertex AI service and the generative model in your app.
After you've connected your app to Firebase, added the SDK, and initialized the Vertex AI service and the generative model, you're ready to call the Gemini API.
Set up a function call
For this tutorial, you'll have the model interact with a hypothetical currency exchange API that supports the following parameters:
Parameter | Type | Required | Description |
---|---|---|---|
currencyFrom |
string | yes | Currency to convert from |
currencyTo |
string | yes | Currency to convert to |
Example API request
{
"currencyFrom": "USD",
"currencyTo": "SEK"
}
Example API response
{
"base": "USD",
"rates": {"SEK": 10.99}
}
Step 1: Create the function that makes the API request
If you haven't already, start by creating the function that makes an API request.
For demonstration purposes in this tutorial, rather than sending an actual API request, you'll be returning hardcoded values in the same format that an actual API would return.
Step 2: Create a function declaration
Create the function declaration that you'll pass to the generative model (next step of this tutorial).
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.
Step 3: Specify the function declaration during model initialization
Specify the function declaration when initializing the generative model by
setting the model's tools
parameter:
Learn how to choose a Gemini model and optionally a location appropriate for your use case and app.
Step 4: Generate a function call
Now you can prompt the model with the defined function.
The recommended way to use function calling is through the chat interface, since function calls fit nicely into chat's multi-turn structure.
What else can you do?
Try out other capabilities of the Gemini API
- Build multi-turn conversations (chat).
- Generate text from text-only prompts.
- Generate text from multimodal prompts (including text, images, PDFs, video, and audio).
Learn how to control content generation
- Understand prompt design, including best practices, strategies, and example prompts.
- Configure model parameters like temperature and maximum output tokens.
- Use safety settings to adjust the likelihood of getting responses that may be considered harmful.
Learn more about the Gemini models
Learn about the models available for various use cases and their quotas and pricing.Give feedback about your experience with Vertex AI in Firebase