From the course: Gemini API: Tool and Function Calling

About declaring basic functions - Gemini Tutorial

From the course: Gemini API: Tool and Function Calling

About declaring basic functions

- [Instructor] In this section, we're going to be learning all about how to define the functions that will be used in our function calling implementations. We'll be learning how to declare functions, explore the structure of a function call response returned by a model, learn how to use multiple functions, and even look at the different function calling modes that the Gemini API provides. In this video, we'll be learning and demonstrating the art of function declarations. Remember the JSON blueprint that we used to declare a function and give it to our model in our first example? Yeah, this video will teach you how to create that. So, let's dive in. A function declaration is a structured representation of a function that you provide to a Gemini model. It is not the actual code of your function. Rather, it is a metadata-rich description. The model then uses this declaration to understand the nature and responsibility of the function. Here is our model uses a function declaration in more detail. First is to understand its capabilities. Function declarations help the model learn about the external actions or data sources it can access beyond its training data. Next is determination of intent. The model using the declarations automatically and intelligently decides when a user's natural language query requires one of your declared functions. There's also the extraction of parameters. After determining the function to return, the model then extracts necessary arguments from the user's prompt and formats them correctly for your function. Now that we understand how a model uses a function declaration, let's understand the different parts of the JSON object used in creating them. A function declaration is defined as an object with three properties: a name, a description, and a parameters object. The name property is a unique descriptive string that represents the name of your function. This must match the name of the actual function in your code. The model uses this name to identify the function you should call. Because this is the same name as your exact Python function, it can be any standard Python function name. For example, get_current_weather, get_local_time, or send_customer_email. Be careful to avoid using vague names, like do_stuff or print_x. The more descriptive your names are of their purpose, the easier it is for the model to select them. The next property is the description property. This is arguably the most important part for the function declaration, as the model relies on it to understand the function's purpose and decide if it is relevant to the user's query. The more detailed the description, the better the model will perform, so ensure to make your descriptions clear and accurate. The third property in our function declaration is the parameters property. This property is used to define the argument that the function expects. It consists of three child properties, which are type, properties, and an optional required property. Type is the data type of the argument list that should be returned. You should always set this property to the value object. The properties parameter, on the other hand, is an object where you define the actual argument, the function we use, to perform its operation. Here, each argument will be defined as a key of the properties object and set to an object itself. Each argument object will have a type property, which is set to the data type of the argument; and a description property, where you provide a clear description of what the argument represents. An example of the parameters definition is shown on the screen to show how the setup looks like. The final property of parameters, the required property, is a list that contains any of the arguments that is compulsory for the function to run. This is to tell the model to always extract a value for the arguments that appear in this list. So, that's how you design a function declaration. A full example is shown on the screen for you to see how everything we just described fits together. This is how you pass the blueprint of a function to a model to make the model aware of its enhanced capabilities through the function. Now that we understand how functions are declared for models to use, in the next video, let's head over to our code editor and write one from scratch.

Contents