1
00:00:00,140 --> 00:00:02,040
In this video, we're going to be writing the

2
00:00:02,040 --> 00:00:04,360
endpoint that will be called by our front-end

3
00:00:04,360 --> 00:00:06,440
to request the weather information.

4
00:00:06,440 --> 00:00:08,620
This endpoint will use function calling with

5
00:00:08,620 --> 00:00:11,100
the Gemini API to return weather information

6
00:00:11,100 --> 00:00:12,660
from our custom function.

7
00:00:12,660 --> 00:00:15,700
To begin, just after the request model we

8
00:00:15,700 --> 00:00:18,600
defined for our first endpoint, let's define

9
00:00:18,600 --> 00:00:21,880
another one for our weather request.

10
00:00:21,880 --> 00:00:26,700
Scroll down or scroll up and that will be

11
00:00:26,700 --> 00:00:27,200
right here.

12
00:00:27,200 --> 00:00:35,640
I'm just going to say class, weatherRequest,

13
00:00:35,640 --> 00:00:37,920
make it inherit from baseModel.

14
00:00:37,920 --> 00:00:40,400
And here we simply just need our destination

15
00:00:40,400 --> 00:00:43,720
as a string.

16
00:00:43,720 --> 00:00:45,260
That's good.

17
00:00:45,260 --> 00:00:47,300
Now let us create our endpoint.

18
00:00:47,300 --> 00:00:50,880
We'll be doing that just below our last endpoint.

19
00:00:50,880 --> 00:00:55,380
Let's scroll down to where that is.

20
00:00:55,380 --> 00:00:59,080
I'll be right here and now let us create our

21
00:00:59,080 --> 00:01:02,200
endpoint and declare the handler.

22
00:01:02,200 --> 00:01:05,040
So here I'm just going to say art app dot

23
00:01:05,040 --> 00:01:13,460
post and that will be for slash API for slash

24
00:01:13,460 --> 00:01:16,920
weather and the corresponding handler function

25
00:01:16,920 --> 00:01:18,980
is going to be an asynchronous function just

26
00:01:18,980 --> 00:01:25,200
like others and we'll call it getWeatherInfo.

27
00:01:25,200 --> 00:01:28,520
For the request, we'll pass in our weather

28
00:01:28,520 --> 00:01:34,560
request, which is the model we just defined.

29
00:01:34,560 --> 00:01:36,640
Now that we have our function defined, let

30
00:01:36,640 --> 00:01:41,140
us start our Gemini API request by first writing

31
00:01:41,140 --> 00:01:44,020
the try except block that we need to wrap

32
00:01:44,020 --> 00:01:46,620
our request in.

33
00:01:46,620 --> 00:01:53,060
So just going to say try, accept, exception

34
00:01:53,060 --> 00:01:57,360
as e, just as we've been doing before.

35
00:01:57,360 --> 00:02:04,440
And we'll also raise an HTTP exception, set

36
00:02:04,440 --> 00:02:13,460
the status code to 500.

37
00:02:13,460 --> 00:02:16,000
And for the description, we're going to return,

38
00:02:16,000 --> 00:02:22,200
set our details parameter, and we say, error,

39
00:02:22,200 --> 00:02:27,360
fetching weather, and give it a stringified

40
00:02:27,360 --> 00:02:30,260
version of that error.

41
00:02:30,260 --> 00:02:31,880
Good.

42
00:02:31,880 --> 00:02:33,620
Now to use our weather function in a tool

43
00:02:33,620 --> 00:02:36,100
call with the Gemini API, we need to add it

44
00:02:36,100 --> 00:02:39,240
to our request generation configuration.

45
00:02:39,240 --> 00:02:40,880
So let us write our generation configuration

46
00:02:40,880 --> 00:02:44,720
and add the weather tool to it.

47
00:02:44,720 --> 00:02:52,600
Come up here and say generation, config, and

48
00:02:52,600 --> 00:02:56,320
that will be types.generateContentConfig

49
00:02:56,320 --> 00:02:58,100
and

50
00:02:58,100 --> 00:03:03,160
we simply set its tools parameter and set

51
00:03:03,160 --> 00:03:05,440
it equal to a list because we can pass more

52
00:03:05,440 --> 00:03:07,800
our own tool but here we only have the getWeather

53
00:03:07,800 --> 00:03:10,320
function to pass.

54
00:03:10,320 --> 00:03:11,780
That's good.

55
00:03:11,780 --> 00:03:15,160
Now we can feed this to our Geminary API request.

56
00:03:15,160 --> 00:03:17,200
Next we need a prompt that properly instructs

57
00:03:17,200 --> 00:03:20,540
our model to use our tool and return a JSON

58
00:03:20,540 --> 00:03:22,300
response.

59
00:03:22,300 --> 00:03:23,760
And just as I've been doing with the other

60
00:03:23,760 --> 00:03:26,760
prompts, I'm going to be bringing it in from

61
00:03:26,760 --> 00:03:30,260
the completed version of the exercise files.

62
00:03:30,260 --> 00:03:34,580
So copy, paste.

63
00:03:34,580 --> 00:03:37,100
Now let us describe.

64
00:03:37,100 --> 00:03:40,120
This says, get the current weather for the

65
00:03:40,120 --> 00:03:42,660
destination, that is request.destination,

66
00:03:42,660 --> 00:03:45,300
as we have in our request model, and return

67
00:03:45,300 --> 00:03:48,440
the information in the following JSON structure.

68
00:03:48,440 --> 00:03:51,920
Use the getWeatherWrapper function to fake

69
00:03:51,920 --> 00:03:55,280
the data then format your response as valid

70
00:03:55,280 --> 00:03:56,320
JSON.

71
00:03:56,320 --> 00:03:58,720
I think we should make this getWeather not

72
00:03:58,720 --> 00:04:00,900
getWeatherWrapper.

73
00:04:00,900 --> 00:04:03,640
Yeah, getWeather function to fetch the data

74
00:04:03,640 --> 00:04:06,360
then format your response as valid JSON.

75
00:04:06,360 --> 00:04:09,500
Your response must be only in JSON with this

76
00:04:09,500 --> 00:04:10,580
exact structure.

77
00:04:10,580 --> 00:04:12,580
So inside the structure, we have a key for

78
00:04:12,580 --> 00:04:15,060
our weather data which is returned the same

79
00:04:15,060 --> 00:04:17,800
way we are returning data from our function

80
00:04:17,800 --> 00:04:21,320
and also we have a description that is just

81
00:04:21,320 --> 00:04:23,980
a summary that the model helps us to construct

82
00:04:23,980 --> 00:04:26,900
from all this weather information.

83
00:04:26,900 --> 00:04:28,960
So with that in place, we can now make our

84
00:04:28,960 --> 00:04:30,520
request.

85
00:04:30,520 --> 00:04:35,820
And for that, we'll simply just come down.

86
00:04:35,820 --> 00:04:41,200
And we can say response equals genai underscore

87
00:04:41,200 --> 00:04:48,900
client dot models dot generate content.

88
00:04:48,900 --> 00:04:53,940
And here we can feed this our model, set to

89
00:04:53,940 --> 00:04:59,300
our Gemini model constant.

90
00:04:59,300 --> 00:05:05,920
For our content, we have the prompt.

91
00:05:05,920 --> 00:05:10,020
And one more thing, we need our content generation

92
00:05:10,020 --> 00:05:11,700
configuration.

93
00:05:11,700 --> 00:05:14,920
We have defined that as generation config,

94
00:05:14,920 --> 00:05:17,700
so I'm just going to copy that and paste it

95
00:05:17,700 --> 00:05:22,900
down here good now just as we've been doing

96
00:05:22,900 --> 00:05:26,200
with the other responses would

97
00:05:26,200 --> 00:05:29,480
also need to clean up this response so I'm

98
00:05:29,480 --> 00:05:33,800
going to just grab all that code

99
00:05:33,800 --> 00:05:38,340
from here from the previous endpoint but just

100
00:05:38,340 --> 00:05:40,500
with some little changes so just

101
00:05:40,500 --> 00:05:44,520
grab it from here to here.

102
00:05:44,520 --> 00:05:46,760
That's good enough.

103
00:05:46,760 --> 00:05:51,360
Let's collapse this once again,

104
00:05:51,360 --> 00:05:56,300
expand this, come down here and continue writing

105
00:05:56,300 --> 00:05:57,560
our handler.

106
00:05:57,560 --> 00:05:58,580
So we clean up the

107
00:05:58,580 --> 00:06:02,940
response text, we strip it and finally what

108
00:06:02,940 --> 00:06:04,940
we need to do is to get our weather

109
00:06:04,940 --> 00:06:09,920
response and that will be json.loads

110
00:06:09,920 --> 00:06:13,780
and then we pass it that cleaned up response

111
00:06:13,780 --> 00:06:15,200
text.

112
00:06:15,200 --> 00:06:18,760
It's passed and because of the structure that

113
00:06:18,760 --> 00:06:20,820
this has been instructed

114
00:06:20,820 --> 00:06:23,780
to come with, which is a dictionary with another

115
00:06:23,780 --> 00:06:25,280
dictionary inside specifying

116
00:06:25,280 --> 00:06:28,360
weather data and also a key for the description,

117
00:06:28,360 --> 00:06:29,980
we can simply just return it as

118
00:06:29,980 --> 00:06:33,760
as is, and our frontend will be able to process

119
00:06:33,760 --> 00:06:34,900
that.

120
00:06:34,900 --> 00:06:35,640
Beautiful.

121
00:06:35,640 --> 00:06:37,660
We now have our weather endpoint that will

122
00:06:37,660 --> 00:06:40,260
help us retrieve weather information for destinations

123
00:06:40,260 --> 00:06:43,040
using Gemini and our custom tool.

124
00:06:43,040 --> 00:06:45,020
In the next video, we'll be integrating this

125
00:06:45,020 --> 00:06:47,000
endpoint with our frontend.

