- [Instructor] In this video, you'll be writing
your very first function calling implementation,
autodeletion and holding.
As you understand the concepts more and more
along your journey through this course,
you'll become more confident
in writing the entire implementation all by yourself.
In this function calling example,
we'll be writing a function
that generates a random password.
You know, the typical ones with the symbols
and numbers mixed in.
Users will be able to request the model
to generate strong passwords
of certain length and properties,
and the model will do that using our function.
Sounds cool, right?
Let's jump in.
First, let's create our script file.
Going to call it your_first_function_call.py
Now that we have our file,
the first thing we'll do is to import our dependencies.
So first, I'm going to say from Google import gen AI,
which is our generative AI SDK.
Then we'll also say from Google.genAI import types.
Types is where we get most of our configuration
and tool classes.
Then for our password,
let us import random and also import string.
Random is going to help us select our characters at random
and string will help us build our password characters.
Now to start our function calling implementation,
the first step is to define our function.
We need a function declaration to give to our model.
So let's do that first.
Now, function declaration is an extensive topic on its own
and we have dedicated an entire section
of this course to that.
So for this example,
I'll simply be bringing in the function declaration
for our strong password generator
and we'll walk through it to understand its structure.
So I'm just going to grab that and drop it here.
Don't let this intimidate you.
You'll know how to write all this very, very soon.
So step one is to define a simple function declaration.
Think of function declarations
like blueprints for your function.
So we have one here defined in JSON.
It has a name, generate password.
The description is that it generates a secure password
with specified length and character types.
Then we need to define the arguments
that this function requires.
So there's going to be a length argument,
which is an integer and this is the length of the password.
And also we can determine
whether we want our password to include symbols or not.
Then we have a required property here
that just tells the model
that it should always return this argument
whenever it is returning our function call object.
So this is the function blueprint.
The next thing after this is to define the function itself.
That's the function that is going to be performing
our password generation.
I'll also be bringing this in to save time
so that you don't have to watch me type
an entire password generating function.
But don't worry, you have access to all these codes
in your exercise files to follow along.
So let me just grab the actual function itself.
Then we'll come down here, below our function declaration
and paste in the actual function.
To get some room here, let me just collapse this.
So down here, step two, we define our actual function.
It's called generate password.
It takes a length argument, which is an int
and then includes symbols argument, which is a Boolean
and it returns a dictionary.
We have a little def string here,
generate secure password with specified criteria.
The default length is eight.
And we just get characters to mix in to our password
in case we want to include symbols.
And lastly, we generate our password
and return all the password information back
using this dictionary.
So we have our function declaration
and we have the actual function
that is going to perform the password generation.
The next task is to configure our Gemini client
and our function as a tool.
Then give our function to our Gemini model.
So first, I'm going to say client equals genAI.Client
to create a new instance of the client.
Then we set up our tools by saying tools equals types.Tool
and we're passing function declarations
and we set it equal to a list.
And this list is going to be
a list of our function declarations.
So here we have one function declaration,
say generate password declaration.
So we're going to put it in here.
And now to give this tool to our model,
we need to pass it in the model's generation configuration.
So going to be creating a new instance
of the model's generation configuration
by calling types.GenerateContentConfig.
And we're going to be passing our tool
to the tools argument.
Tools argument, which takes a list.
And also we're going to be passing this inside here.
I think I should just rename this tool.
Yeah, 'cause we have one tool.
Then we can pass this here into this list
as one of the tools that should be used
when we use this configuration for our model request.
With all this in place, we can now prompt our model.
Let's ask our to generate a secure password
with 15 characters, which would also include symbols.
Let's simply write that prompt down here.
Going to say user_question,
say I need a secure password
that's 15 characters long
and includes special symbols.
So we have our prompt.
Now let us send our request.
You can say response equals,
we take our client, which is our model,
and say generate content.
Now inside generate content
we're going to be passing in a few arguments.
First is our model.
And for this demo we're going to be using Gemini 2.5 Flash.
That's good.
We're going to send our prompt using the contents argument
and we'll set it to our string prompt,
which is user_question.
And finally, we set our generation configuration
to our config variable using the config argument.
So we have our request in place.
Now we need to extract a function call
from the response that comes from this request.
So let us write the code that does that.
To get our function call,
simply going to create a new variable,
call it function call.
Then we get our response, we check the candidates,
we pick the first one.
Then we'll get the content, we get parts of the content
and get the function call from that part.
So this is good.
Instead of our response,
our response contains a couple of candidates,
but the API currently just returns one.
So we just get one.
Then we get the content for that
which also contains different parts.
If it returns the function,
it'll definitely only contain the function call part.
So we pick just that one
and we call function_call,
which is the name for the function call object
that is returned.
Then we can simply print our function name.
So we can say function name or function suggested
and that will be function_call,
which is our function call variable,
and our function call object as a name property,
which is a name of the suggested function.
Need to get rid of this bracket, good.
And also the function call object contains arguments
that the model has extracted from our user query
in order for us to run the function.
So let just print that out also.
So print, F string, let's say arguments
and our arguments, let's cast them into a dictionary.
And I'm simply going to say function_call.args.
That is the name of our argument dictionary.
So that's good.
We have declared our function declaration.
We have written the actual function.
We also have our client and our two definitions.
And we have passed our tool to our client using config,
which we used in the request.
Our prompt says that we need a secure password
that's 15 characters long and includes special symbols.
And down here, we are extracting our function name
and our function arguments from the function call object
that is returned by the model.
Now let's run this file and see what we get back.
Let's save the file.
I'm going to pull up the command line
and now we can run Python.
Your_first_function_call.py.
Hit enter.
And as you can see, the model processes the prompt,
realizes that it has a tool for generating a password,
suggests the tool and also extracts
the arguments we'll need to run the function.
Here, function suggested is generate password
and the arguments are include symbols, which is set true,
and length, which is 15
according to what we stated in our prompt.
How amazing is that?
With this results, let's head back to our code
and actually run the suggested function.
So I'm going to close out of the command line
and down here, we're going to run the actual function.
So first, I'll do a check
and say if function_call.name
is equal to generate_password.
If there is a function returned,
then we want to run the function.
So I'll say results,
then call the generate password function itself
and we can pass in the arguments
that is returned by our model.
That's function_call.args.
So we'll run this and let us simply print out the result.
Going to say F string.
Here we can say answer
and give you the result.
So here, we're checking to make sure
that this is the function that is returned.
Then we can run the actual function
by passing in the argument returned by our model
and we print out the answer.
So let us save this and run it
and see if we are correctly running our function.
So I'm just going to run the file once again.
And now we have our result,
a strong password generated with our function
as suggested by the Gemini model.