Record Class GeminiSafetyRating

java.lang.Object
java.lang.Record
dev.langchain4j.model.googleai.GeminiSafetyRating
Record Components:
category - the harm category being assessed, for example "HARM_CATEGORY_HARASSMENT" or "HARM_CATEGORY_DANGEROUS_CONTENT"
probability - how likely the content is to belong to this category: "NEGLIGIBLE", "LOW", "MEDIUM" or "HIGH"
blocked - whether this category caused the content to be blocked; null when the API does not report it

public record GeminiSafetyRating(String category, String probability, Boolean blocked) extends Record
A safety assessment that Gemini reports for a single harm category.

Gemini evaluates both the prompt you send and the content it generates against a set of harm categories, and returns one rating per category. Ratings are available from GoogleAiGeminiChatResponseMetadata.safetyRatings() (for the generated content) and GoogleAiGeminiChatResponseMetadata.promptSafetyRatings() (for the prompt):

ChatResponse response = model.chat(ChatRequest.builder().messages(userMessage).build());

var metadata = (GoogleAiGeminiChatResponseMetadata) response.metadata();
for (GeminiSafetyRating rating : metadata.safetyRatings()) {
    System.out.println(rating.category() + ": " + rating.probability());
}

category and probability are the raw values returned by the Gemini API rather than Java enums, so that categories introduced by Google in the future are surfaced as-is instead of breaking response parsing.

See Also:
  • Constructor Details

    • GeminiSafetyRating

      public GeminiSafetyRating(String category, String probability, Boolean blocked)
      Creates an instance of a GeminiSafetyRating record class.
      Parameters:
      category - the value for the category record component
      probability - the value for the probability record component
      blocked - the value for the blocked record component
  • Method Details

    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • category

      public String category()
      Returns the value of the category record component.
      Returns:
      the value of the category record component
    • probability

      public String probability()
      Returns the value of the probability record component.
      Returns:
      the value of the probability record component
    • blocked

      public Boolean blocked()
      Returns the value of the blocked record component.
      Returns:
      the value of the blocked record component