In this video, we'll be creating the function
that our model will use to retrieve the weather
information.
This function will be used in what is called
a function call by the model to present weather
information for a selected destination.
Let's get started.
Just above the root endpoint, let's define
a new function with the name getWeather.
Our function definition takes in a location
argument which represents the geographical
name of our destination.
It also takes in a unit argument that defines
the unit we want to get the temperature measurement
in.
That can be Celsius or Fahrenheit.
Now let's add a doc string to describe what
our function does.
It returns the weather information for a given
destination.
It is important to add a detailed docstring
to your function calling methods.
This helps the model to appropriately recommend
them to be called and also extract the right
arguments.
Now, in a real-world scenario, we'll be using
a live weather API like the Open Weather API
to retrieve real-time weather information
for display.
However, in this application, we'll return
a dummy weather information.
Most of the weather APIs require you to make
some sort of payment and we won't want a
a credit card to hinder you from following
along.
To begin construction of our dummy weather
data, first, let us add a list of random weather
conditions.
I'm just going to get those and paste it in
here so that you don't have to watch me boringly
type all that.
So we have conditions.
Like I said, you can get all your code from
the completed version of the exercise files.
Here we have a list of weather conditions,
clear sky, partly cloudy, cloudy, light rain.
In this way, we can randomly select any of
these to display different weather conditions
for different requests and not just a static
one for every single request.
Next, let us define some dummy but real-world
looking temperature and wind speed data.
Come down here and say if the unit equals
Celsius.
We want to set the temperature to a random
number between 15 and 30.
By the way, we need to import the random package.
So let us do that.
Just below IO, let's import random.
Good.
Let's go back down.
Now for the wind speed, if the temperature
unit is Celsius, we want to also return a
random number.
We say we want something random, not a random
integer, between 5 and 25 and that will be
kilometer per hour.
Now else, that is if the unit is Fahrenheit,
we want our temperature to be between 59 and
And for our wind speed, we want something
between 3 and 15 and this will be miles per
hour.
Good.
Now let us build up our weather data dictionary
that the function is going to return to the
model whenever it's called.
We say weatherData equals a dictionary, and
we're going to set all our weather properties.
So for location, we want to return the sentLocation,
for temperature, that's going to be the temperature
from here.
For unit, that would be the unit argument
sent to this function.
For condition,
we are simply going to pick a random condition
from our list of conditions.
Let's get that
here.
Paste it in here.
Next we'll have humidity.
And humidity will just simply be another random
integer between 40 and 80.
And we'll put the appropriate percentage unit.
Lastly, we're
We're going to add our wind speed and give
it the computed wind speed based on the temperature
unit.
This is good.
Now finally, let us return this data so that
our model can receive it.
Perfect.
Now the Gemini model can use this function
whenever we prompt it for weather information.
In the next video, we'll be writing our weather
endpoint.