class RubyLLM::Moderation
A Moderation holds the result of screening text or images for potentially harmful content. Most code obtains one through RubyLLM.moderate.
result = RubyLLM.moderate("This is a safe message about Ruby programming") result.flagged? # => false RubyLLM.moderate(with: "profile.png").flagged?
Attributes
The provider-assigned identifier of the moderation request.
The id of the model that performed the moderation. A moderation run by a provider resource rather than a model names that resource, such as a Bedrock guardrail’s "id:version", as its usage does.
The original provider response, or an array of responses when each input requires a separate request.
The per-input verdicts, as an array of Result objects, one per moderated input.
Public Class Methods
# File lib/ruby_llm/moderation.rb, line 92 def self.moderate(input = nil, model: nil, with: nil, provider: nil, assume_model_exists: false, context: nil, provider_options: {}, metadata: nil, owner: nil) attachments = Attachment.wrap(with) raise ArgumentError, 'must provide input text, image attachment, or both' if input.nil? && attachments.empty? Accounting::Operation.new( 'moderation', model, operation: :moderate, default: :default_moderation_model, provider:, assume_model_exists:, context:, owner:, details: ->(event, result) { event[:flagged] = result.flagged? } ).run(input:, attachment_count: attachments.size, provider_options:, metadata:) do |provider_instance, model| provider_instance.moderate(input, model:, with: attachments, provider_options:) end end
Screens input and optional image attachments and returns a Moderation with the provider’s verdict. Uses the configured default moderation model when model is not given. Pass provider: and assume_model_exists: true to use a model that is not in the registry. An explicitly selected provider may instead use a configured resource without a model. owner: attributes the usage to a record, such as a user, and wins over RubyLLM.with_owner.
RubyLLM.moderate("User message") RubyLLM.moderate(["First comment", "Second comment"]).results RubyLLM.moderate("Caption", with: "screenshot.png")
Public Instance Methods
Source
# File lib/ruby_llm/moderation.rb, line 144 def category_scores results.map(&:category_scores).reduce({}) do |merged, scores| merged.merge(scores) { |_category, left, right| [left, right].max } end end
Returns the confidence scores across all results, as a hash of category name to a score between 0.0 and 1.0. Keeps the highest score per category when there are multiple results.
result.category_scores["violence"] # => 0.0001
Source
# File lib/ruby_llm/moderation.rb, line 126 def cost ruby_llm_usage_cost end
Returns the moderation cost across every provider attempt.
Source
# File lib/ruby_llm/moderation.rb, line 115 def flagged? results.any?(&:flagged?) end
Returns true if any input was flagged as potentially harmful, false otherwise.
Source
# File lib/ruby_llm/moderation.rb, line 134 def flagged_categories results.flat_map(&:categories).uniq end
Returns the unique names of the categories flagged across all results.
result.flagged_categories # => ["harassment", "violence"]
Source
# File lib/ruby_llm/moderation.rb, line 121 def tokens ruby_llm_usage_tokens end
Returns provider-reported usage across every attempt. Its fields are nil when the provider did not report any.