Understand and use safety settings


You can use safety settings to adjust the likelihood of getting responses that may be considered harmful. By default, safety settings block content with medium and/or high probability of being unsafe content across all dimensions.

Jump to Gemini safety settings Jump to Imagen safety settings

Safety settings for Gemini models

Learn more about safety settings in the Google Cloud documentation.

You configure SafetySettings during initialization of the model. Here are some basic examples.

Here's how to set one safety setting:

Kotlin

import com.google.firebase.vertexai.type.HarmBlockThreshold
import com.google.firebase.vertexai.type.HarmCategory
import com.google.firebase.vertexai.type.SafetySetting

val generativeModel = Firebase.vertexAI.generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    safetySettings = listOf(
        SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)
    )
)

// ...

Java

SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
    HarmBlockThreshold.ONLY_HIGH);

GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
    "GEMINI_MODEL_NAME",
    /* generationConfig is optional */ null,
    Collections.singletonList(harassmentSafety)
);

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// ...

You can also set more than one safety setting:

Kotlin

import com.google.firebase.vertexai.type.HarmBlockThreshold
import com.google.firebase.vertexai.type.HarmCategory
import com.google.firebase.vertexai.type.SafetySetting

val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)

val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE)

val generativeModel = Firebase.vertexAI.generativeModel(
    modelName = "GEMINI_MODEL_NAME",
    safetySettings = listOf(harassmentSafety, hateSpeechSafety)
)

// ...

Java

SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
    HarmBlockThreshold.ONLY_HIGH);

SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH,
    HarmBlockThreshold.MEDIUM_AND_ABOVE);

GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
    "GEMINI_MODEL_NAME",
    /* generationConfig is optional */ null,
    List.of(harassmentSafety, hateSpeechSafety)
);

GenerativeModelFutures model = GenerativeModelFutures.from(gm);

// ...

Safety settings for Imagen models

Learn about all the supported safety settings and their available values for Imagen models.

Kotlin

val imagenModel = Firebase.vertexAI.imagenModel(
  modelName = "IMAGEN_MODEL_NAME",
  // Configure image generation safety settings for the model
  safetySettings = ImagenSafetySettings(
    safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
    personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
  )
)

// ...

Java

ImagenModel imagenModel =
    FirebaseVertexAI.getInstance().imagenModel(
            /* modelName */ "IMAGEN_MODEL_NAME",
            /* imageGenerationConfig */ null);
ImagenModelFutures model = ImagenModelFutures.from(imagenModel);

// ...

Other options to control content generation

  • Learn more about prompt design so that you can influence the model to generate output specific to your needs.
  • Configure model parameters to control how the model generates a response. For Gemini models, these parameters include max output tokens, temperature, topK, and topP. For Imagen models, these include aspect ratio, person generation, watermarking, etc.
  • Set system instructions to steer the behavior of the model. This feature is like a "preamble" that you add before the model gets exposed to any further instructions from the end user.
  • Pass a response schema along with the prompt to specify a specific output schema. This feature is most commonly used when generating JSON output, but it can also be used for classification tasks (like when you want the model to use specific labels or tags).