Get startedGet started for free

Safety in Google Cloud GenAI

1. Safety in Google Cloud GenAI

Now, let's take a look at what Google Cloud tools we can use to ensure AI safety. Google Cloud helps create safer AI models in many ways from data collection, preprocessing, scalable training, prediction environments, and so on. In this lesson, we will focus on direct harm prevention tools that you can use in generative AI applications. Natural language API is one such tool that has the capability to do input-output moderation and pre-trained models that have built-in safeguards capabilities like Gemini. The natural language API provides a powerful set of tools to understand and process text using machine learning, including text classification, sentiment analysis, entity extraction, and so on. In addition to these tools, it also has the text safety moderation capability, which we can use for input and output safeguards. Natural language API has text moderation capabilities that analyze a document against a list of safety attributes, which include harmful categories and topics that may be considered sensitive. Each safety attribute has an associated confidence score between 0.0 and 1.0 to reflect the likelihood of the input or response belonging to a given category. You can define the confidence threshold that is right for your business for each category. To moderate content from a document, make a post request to the document's moderate text, rest method, and provide the appropriate request body. As shown in the examples, we use the sentence shut up as the text to moderate. You can either provide the text as a string or provide the cloud storage bucket file path where the file is stored. Let's say that the text body contains the words shut up. What you would see in the API tool is a response body that contains data with a list of classification categories and their associated confidence score. In this case, the API tool has a high confidence score of 0.8 that the text shut up has a high likelihood of being toxic. You can use a variety of programming languages to call the Google Cloud Natural Language APIs documents moderate text in point, like Python, Java, and Go. Using curl commands is just one of them. Because it is easy to use, we can then easily incorporate safe guardrailing capabilities in any generative AI systems by employing natural language APIs. Some foundational models, including Gemini, have in-built safe guardrailing capability. Gemini is a family of powerful multimodal large language models, LLMs, developed by Google DeepMind. It is also available via Google Cloud. Gemini models can accept text, and image, and prompts, depending on what model variation you choose and output text responses. The Gemini API has adjustable safety settings to help you tailor the safety settings to your business requirements and use case. During the prototyping stage, you can adjust safety settings on four dimensions to quickly assess if your application requires more or less restrictive configuration. With Gemini safeguarding on Google Cloud, safety settings block content with medium or high probability of being unsafe across four categories: harassment, hate speech, sexually explicit, and dangerous. There are four thresholds: Block, always show content regardless of the probability of unsafe content; Block only high, block when there is high probability of unsafe content; Block medium and above, block when there is medium or high probability of unsafe content; And block low and above, block when there is low, medium, or high probability of unsafe content. The safety categories and threshold settings should always be established in pairs based on what you determine is appropriate for your use case. In Gemini, safety ratings include the category and the probability of the harm classification. Gemini outputs a probability of the block confidence levels ranging from negligible, low, medium, and high. So if the probability returns back as high, this means the content has a high probability of being unsafe to that category. For example, while prototyping a new first-person shooter game, a game developer might deem it acceptable to allow more content that's rated as dangerous due to the nature of the game. While Gemini API safety filters are adjustable, core harms such as content and dangers child safety are built-in protections and cannot be adjusted. In Gemini, we can set the category and threshold in pairs to what we need for our use case. For example, you could enter the harm category and harm block threshold into the safety settings parameters. Let's look at a scenario. Let's say we wanted to set the category of hate speech to a threshold of block low and above. This setting is sent to the API along with a prompt you want to provide. In our example, you can see that the output is blocked due to safety reasons. You can also see the safety feedback response for each category and learn which category it was blocked from. With Gemini by default, safety settings block content, including prompts, with medium or higher probability of being unsafe across any dimension or category. This baseline safety is designed to work for most use cases, so you should only adjust your safety settings if it's consistently required for your application. In our example, we cannot set the threshold for the harassment category. Therefore, the block medium and above threshold was applied. Thus, the content was blocked due to the harassment category having a medium probability. It also returns blocked true, along with the category and probability. Meanwhile, the blocked content is not returned. In this feedback, the content has a medium probability in the harassment category, negligible probability in the hate speech category, negligible in the sexually explicit category, and negligible in the dangerous category. The Gemini API gives you access to the latest generative models from Google. Once you're familiar with the general features available to you through the API, you can use your language of choice to develop for your use case.

2. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.