In this video, we're going to be writing the
endpoint that will be called by our front-end
to request the weather information.
This endpoint will use function calling with
the Gemini API to return weather information
from our custom function.
To begin, just after the request model we
defined for our first endpoint, let's define
another one for our weather request.
Scroll down or scroll up and that will be
right here.
I'm just going to say class, weatherRequest,
make it inherit from baseModel.
And here we simply just need our destination
as a string.
That's good.
Now let us create our endpoint.
We'll be doing that just below our last endpoint.
Let's scroll down to where that is.
I'll be right here and now let us create our
endpoint and declare the handler.
So here I'm just going to say art app dot
post and that will be for slash API for slash
weather and the corresponding handler function
is going to be an asynchronous function just
like others and we'll call it getWeatherInfo.
For the request, we'll pass in our weather
request, which is the model we just defined.
Now that we have our function defined, let
us start our Gemini API request by first writing
the try except block that we need to wrap
our request in.
So just going to say try, accept, exception
as e, just as we've been doing before.
And we'll also raise an HTTP exception, set
the status code to 500.
And for the description, we're going to return,
set our details parameter, and we say, error,
fetching weather, and give it a stringified
version of that error.
Good.
Now to use our weather function in a tool
call with the Gemini API, we need to add it
to our request generation configuration.
So let us write our generation configuration
and add the weather tool to it.
Come up here and say generation, config, and
that will be types.generateContentConfig
and
we simply set its tools parameter and set
it equal to a list because we can pass more
our own tool but here we only have the getWeather
function to pass.
That's good.
Now we can feed this to our Geminary API request.
Next we need a prompt that properly instructs
our model to use our tool and return a JSON
response.
And just as I've been doing with the other
prompts, I'm going to be bringing it in from
the completed version of the exercise files.
So copy, paste.
Now let us describe.
This says, get the current weather for the
destination, that is request.destination,
as we have in our request model, and return
the information in the following JSON structure.
Use the getWeatherWrapper function to fake
the data then format your response as valid
JSON.
I think we should make this getWeather not
getWeatherWrapper.
Yeah, getWeather function to fetch the data
then format your response as valid JSON.
Your response must be only in JSON with this
exact structure.
So inside the structure, we have a key for
our weather data which is returned the same
way we are returning data from our function
and also we have a description that is just
a summary that the model helps us to construct
from all this weather information.
So with that in place, we can now make our
request.
And for that, we'll simply just come down.
And we can say response equals genai underscore
client dot models dot generate content.
And here we can feed this our model, set to
our Gemini model constant.
For our content, we have the prompt.
And one more thing, we need our content generation
configuration.
We have defined that as generation config,
so I'm just going to copy that and paste it
down here good now just as we've been doing
with the other responses would
also need to clean up this response so I'm
going to just grab all that code
from here from the previous endpoint but just
with some little changes so just
grab it from here to here.
That's good enough.
Let's collapse this once again,
expand this, come down here and continue writing
our handler.
So we clean up the
response text, we strip it and finally what
we need to do is to get our weather
response and that will be json.loads
and then we pass it that cleaned up response
text.
It's passed and because of the structure that
this has been instructed
to come with, which is a dictionary with another
dictionary inside specifying
weather data and also a key for the description,
we can simply just return it as
as is, and our frontend will be able to process
that.
Beautiful.
We now have our weather endpoint that will
help us retrieve weather information for destinations
using Gemini and our custom tool.
In the next video, we'll be integrating this
endpoint with our frontend.