Class: RubyLLM::Moderation::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/moderation.rb

Overview

A Result is the verdict for a single moderated input. Providers return results in different shapes. RubyLLM normalizes all of them into Result objects on Moderation#results.

result = RubyLLM.moderate("Some user input").results.first
result.flagged?               # => false
result.categories             # => []
result.category_scores["violence"]  # => 0.0004

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flagged:, categories:, category_scores:) ⇒ Result

:nodoc:



35
36
37
38
39
# File 'lib/ruby_llm/moderation.rb', line 35

def initialize(flagged:, categories:, category_scores:) # :nodoc:
  @flagged = flagged
  @categories = categories
  @category_scores = category_scores
end

Instance Attribute Details

#categoriesObject (readonly)

The names of the categories flagged for this input, as an array of strings, empty when nothing was flagged.



29
30
31
# File 'lib/ruby_llm/moderation.rb', line 29

def categories
  @categories
end

#category_scoresObject (readonly)

The confidence scores for this input, as a hash of category name to a score between 0.0 and 1.0. Empty when the provider reports none.



33
34
35
# File 'lib/ruby_llm/moderation.rb', line 33

def category_scores
  @category_scores
end

Class Method Details

.from_h(data) ⇒ Object

:nodoc:



41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/moderation.rb', line 41

def self.from_h(data) # :nodoc:
  flagged_names = (data['categories'] || {}).select { |_category, flagged| flagged }.keys
  new(
    flagged: data.fetch('flagged', flagged_names.any?),
    categories: flagged_names,
    category_scores: data['category_scores'] || {}
  )
end

Instance Method Details

#flagged?Boolean

Returns true if this input was flagged as potentially harmful, false otherwise.

Returns:

  • (Boolean)


52
53
54
# File 'lib/ruby_llm/moderation.rb', line 52

def flagged?
  @flagged
end