1
00:00:00,200 --> 00:00:02,320
In this video, we'll be creating the function

2
00:00:02,320 --> 00:00:04,620
that our model will use to retrieve the weather

3
00:00:04,620 --> 00:00:06,060
information.

4
00:00:06,060 --> 00:00:07,940
This function will be used in what is called

5
00:00:07,940 --> 00:00:10,960
a function call by the model to present weather

6
00:00:10,960 --> 00:00:13,600
information for a selected destination.

7
00:00:13,600 --> 00:00:15,660
Let's get started.

8
00:00:15,660 --> 00:00:18,040
Just above the root endpoint, let's define

9
00:00:18,040 --> 00:00:26,480
a new function with the name getWeather.

10
00:00:26,480 --> 00:00:28,540
Our function definition takes in a location

11
00:00:28,540 --> 00:00:30,620
argument which represents the geographical

12
00:00:30,620 --> 00:00:32,740
name of our destination.

13
00:00:32,740 --> 00:00:35,500
It also takes in a unit argument that defines

14
00:00:35,500 --> 00:00:37,940
the unit we want to get the temperature measurement

15
00:00:37,940 --> 00:00:38,720
in.

16
00:00:38,720 --> 00:00:41,400
That can be Celsius or Fahrenheit.

17
00:00:41,400 --> 00:00:44,280
Now let's add a doc string to describe what

18
00:00:44,280 --> 00:00:46,060
our function does.

19
00:00:46,060 --> 00:00:51,360
It returns the weather information for a given

20
00:00:51,360 --> 00:00:54,920
destination.

21
00:00:54,920 --> 00:00:56,960
It is important to add a detailed docstring

22
00:00:56,960 --> 00:00:59,260
to your function calling methods.

23
00:00:59,260 --> 00:01:01,460
This helps the model to appropriately recommend

24
00:01:01,460 --> 00:01:04,720
them to be called and also extract the right

25
00:01:04,720 --> 00:01:06,040
arguments.

26
00:01:06,040 --> 00:01:07,920
Now, in a real-world scenario, we'll be using

27
00:01:07,920 --> 00:01:10,800
a live weather API like the Open Weather API

28
00:01:10,800 --> 00:01:13,600
to retrieve real-time weather information

29
00:01:13,600 --> 00:01:14,920
for display.

30
00:01:14,920 --> 00:01:17,600
However, in this application, we'll return

31
00:01:17,600 --> 00:01:19,640
a dummy weather information.

32
00:01:19,640 --> 00:01:21,820
Most of the weather APIs require you to make

33
00:01:21,820 --> 00:01:24,080
some sort of payment and we won't want a

34
00:01:24,080 --> 00:01:26,160
a credit card to hinder you from following

35
00:01:26,160 --> 00:01:27,460
along.

36
00:01:27,460 --> 00:01:28,980
To begin construction of our dummy weather

37
00:01:28,980 --> 00:01:32,340
data, first, let us add a list of random weather

38
00:01:32,340 --> 00:01:33,520
conditions.

39
00:01:33,520 --> 00:01:35,500
I'm just going to get those and paste it in

40
00:01:35,500 --> 00:01:37,820
here so that you don't have to watch me boringly

41
00:01:37,820 --> 00:01:40,200
type all that.

42
00:01:40,200 --> 00:01:41,980
So we have conditions.

43
00:01:41,980 --> 00:01:44,120
Like I said, you can get all your code from

44
00:01:44,120 --> 00:01:47,260
the completed version of the exercise files.

45
00:01:47,260 --> 00:01:49,000
Here we have a list of weather conditions,

46
00:01:49,000 --> 00:01:52,200
clear sky, partly cloudy, cloudy, light rain.

47
00:01:52,200 --> 00:01:54,620
In this way, we can randomly select any of

48
00:01:54,620 --> 00:01:56,960
these to display different weather conditions

49
00:01:56,960 --> 00:01:59,400
for different requests and not just a static

50
00:01:59,400 --> 00:02:01,940
one for every single request.

51
00:02:01,940 --> 00:02:04,840
Next, let us define some dummy but real-world

52
00:02:04,840 --> 00:02:07,860
looking temperature and wind speed data.

53
00:02:07,860 --> 00:02:12,360
Come down here and say if the unit equals

54
00:02:12,360 --> 00:02:16,040
Celsius.

55
00:02:16,040 --> 00:02:21,120
We want to set the temperature to a random

56
00:02:21,120 --> 00:02:28,020
number between 15 and 30.

57
00:02:28,020 --> 00:02:30,660
By the way, we need to import the random package.

58
00:02:30,660 --> 00:02:32,280
So let us do that.

59
00:02:32,280 --> 00:02:36,860
Just below IO, let's import random.

60
00:02:36,860 --> 00:02:39,400
Good.

61
00:02:39,400 --> 00:02:42,120
Let's go back down.

62
00:02:42,120 --> 00:02:45,080
Now for the wind speed, if the temperature

63
00:02:45,080 --> 00:02:48,340
unit is Celsius, we want to also return a

64
00:02:48,340 --> 00:02:50,780
random number.

65
00:02:50,780 --> 00:02:53,880
We say we want something random, not a random

66
00:02:53,880 --> 00:02:59,920
integer, between 5 and 25 and that will be

67
00:02:59,920 --> 00:03:04,820
kilometer per hour.

68
00:03:04,820 --> 00:03:08,480
Now else, that is if the unit is Fahrenheit,

69
00:03:08,480 --> 00:03:20,480
we want our temperature to be between 59 and

70
00:03:20,480 --> 00:03:28,740
And for our wind speed, we want something

71
00:03:28,740 --> 00:03:35,880
between 3 and 15 and this will be miles per

72
00:03:35,880 --> 00:03:38,520
hour.

73
00:03:38,520 --> 00:03:40,300
Good.

74
00:03:40,300 --> 00:03:42,760
Now let us build up our weather data dictionary

75
00:03:42,760 --> 00:03:45,640
that the function is going to return to the

76
00:03:45,640 --> 00:03:47,820
model whenever it's called.

77
00:03:47,820 --> 00:03:54,560
We say weatherData equals a dictionary, and

78
00:03:54,560 --> 00:03:57,380
we're going to set all our weather properties.

79
00:03:57,380 --> 00:04:02,620
So for location, we want to return the sentLocation,

80
00:04:02,620 --> 00:04:07,980
for temperature, that's going to be the temperature

81
00:04:07,980 --> 00:04:10,640
from here.

82
00:04:10,640 --> 00:04:17,380
For unit, that would be the unit argument

83
00:04:17,380 --> 00:04:19,519
sent to this function.

84
00:04:19,519 --> 00:04:22,960
For condition,

85
00:04:22,960 --> 00:04:27,140
we are simply going to pick a random condition

86
00:04:27,140 --> 00:04:29,880
from our list of conditions.

87
00:04:29,880 --> 00:04:30,420
Let's get that

88
00:04:30,420 --> 00:04:32,700
here.

89
00:04:32,700 --> 00:04:35,040
Paste it in here.

90
00:04:35,040 --> 00:04:39,360
Next we'll have humidity.

91
00:04:39,360 --> 00:04:44,240
And humidity will just simply be another random

92
00:04:44,240 --> 00:04:49,960
integer between 40 and 80.

93
00:04:49,960 --> 00:04:53,800
And we'll put the appropriate percentage unit.

94
00:04:53,800 --> 00:04:54,780
Lastly, we're

95
00:04:54,780 --> 00:04:58,460
We're going to add our wind speed and give

96
00:04:58,460 --> 00:05:02,980
it the computed wind speed based on the temperature

97
00:05:02,980 --> 00:05:05,100
unit.

98
00:05:05,100 --> 00:05:06,140
This is good.

99
00:05:06,140 --> 00:05:09,440
Now finally, let us return this data so that

100
00:05:09,440 --> 00:05:12,480
our model can receive it.

101
00:05:12,480 --> 00:05:13,700
Perfect.

102
00:05:13,700 --> 00:05:15,760
Now the Gemini model can use this function

103
00:05:15,760 --> 00:05:18,160
whenever we prompt it for weather information.

104
00:05:18,160 --> 00:05:20,520
In the next video, we'll be writing our weather

105
00:05:20,520 --> 00:05:22,000
endpoint.

