AI applications can be quite autonomous with
their responses.
And while we can define an output structure
and pass several instructions in our prompts,
some guardrails are best implemented at the
root level.
In this video, we'll be implementing safety
settings that will ensure that our application
does not generate any harmful response.
We don't want our application generating content
that becomes offensive to its users,
especially being a family-friendly application.
Luckily for us, Gemini provides the tools
to do that.
To implement this, we're going to be defining
some general safety settings that we'll be
applying to all our requests.
We'll be using Gemini's standard AMP categories
and setting a threshold for each of them.
This will ensure that Gemini blocks any malicious
query trying to trigger AMPful responses and
also prevents the model from recklessly generating
such.
So let us begin by writing our safety settings.
We are going to come down under the model
definitions and here we'll begin writing
our safety settings.
We need to have them in a list so I'm going
to create a variable called safetySettingsList.
Set that to a list.
Now the first category we're going to be dealing
with is hate speech.
So we're going to say types.safetySetting,
this will take a category and threshold parameters.
So we say category first, say types.harmCategory.
And like I said, we want to deal with hate
speech.
So harm category, hate speech.
Good.
Now the threshold we'll be setting for that
is that we want to block all its pitch.
So the threshold will be types.andBlocksThreshold
and we want to block low and above.
Good.
The next one we're going to be doing is for
harassment.
So I'm just going to copy this.
here and here we're going to select the category
for harassment.
See?
Harassment.
And we're also going to be blocking everything.
Lastly, we're going to deal
with sexually explicit content.
So we'll say arm category sexually explicit
and
we're also going to be blocking everything.
So we're keeping the entire
application PG.
Now that we have our safety settings, let
us add them to our
requests.
This can be done by adding a safety settings
parameter to our
generation config.
So let us do that.
Let's scroll down to our first endpoint
And down here, we can set the safety settings
parameter, and that can be set to our safety
– let me just copy that, let's go back up
– safety settings list.
Good.
Finally, we need to check our response to
see if there was a block so that we can undo
it.
In any case where any of this type of language
or generation is noticed, Gemini is going
to automatically block it.
So we need to know when our response is blocked
so that we can undo it appropriately on the
back end.
In this case, we will simply just add an if-else
statement to our return value and print out
something to the console.
So up here, I'm just going to say if response.candidates,
that is if there's a response, and if response.candidates,
the very first one, which for now, it only
just returns one, if the finish reason is
equal to types.finishRaising,
I need a double equal there because it's comparison.
So if it's double equal to types.finishRaising
and if that finishRaising is for safety, then
I know that a handful content has just been
blocked and for that we're just going to print
and say response blocked due to safety filter.
So our safety filters have helped us block
a particular response.
Otherwise, we just have business as usual,
which would be these guys.
I'm just going to indent them in,
and we'll just return our normal results.
Now, with this in place, our application is
now safeguarded
from generating harmful responses.
Now, this might be a bit difficult to test
without writing something harmful in the request,
but be rest assured that your application
is now protected
from returning offensive responses,
according to the Gemini API's definition
of what is categorized as offensive responses.
Now, as your own work,
try implementing this for our image search
endpoint.
Our getWeather endpoint might not need this
because it uses a function.
And the function basically defines what is
returned.
So there's very almost impossible low probability
of anything offensive coming from there.
But yeah, just take a shot at the suggest
by image endpoint
and also ensure that the app is safeguarded
on that front.