javascript/safety_settings.js (49 lines of code) (raw):

/** * @license * Copyright 2025 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { GoogleGenAI } from "@google/genai"; export async function safetySettings() { // [START safety_settings] // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const unsafePrompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them including expletives."; const response = await ai.models.generateContent({ model: "gemini-2.0-flash", contents: unsafePrompt, config: { safetySettings: [ { category: "HARM_CATEGORY_HARASSMENT", threshold: "BLOCK_ONLY_HIGH", }, ], }, }); console.log("Finish reason:", response.candidates[0].finishReason); console.log("Safety ratings:", response.candidates[0].safetyRatings); // [END safety_settings] return response; } export async function safetySettingsMulti() { // [START safety_settings_multi] // Make sure to include the following import: // import {GoogleGenAI} from '@google/genai'; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const unsafePrompt = "I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them including expletives."; const response = await ai.models.generateContent({ model: "gemini-2.0-flash", contents: unsafePrompt, config: { safetySettings: [ { category: "HARM_CATEGORY_HATE_SPEECH", threshold: "BLOCK_MEDIUM_AND_ABOVE", }, { category: "HARM_CATEGORY_HARASSMENT", threshold: "BLOCK_ONLY_HIGH", }, ], }, }); try { console.log("Generated text:", response.text); } catch (error) { console.log("No information generated by the model."); } console.log("Safety ratings:", response.candidates[0].safetyRatings); // [START safety_settings_multi] return response; }