Module: RubyLLM::Protocols::ChatCompletions::Moderation

Defined in:
lib/ruby_llm/protocols/chat_completions/moderation.rb

Overview

Moderation methods of the OpenAI API integration

Class Method Summary collapse

Class Method Details

.format_moderation_attachment(attachment) ⇒ Object



44
45
46
47
48
# File 'lib/ruby_llm/protocols/chat_completions/moderation.rb', line 44

def format_moderation_attachment(attachment)
  raise UnsupportedAttachmentError, attachment.mime_type unless attachment.image?

  Media.format_image(attachment)
end

.moderation_input(input, attachments) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ruby_llm/protocols/chat_completions/moderation.rb', line 35

def moderation_input(input, attachments)
  return input if attachments.empty?

  parts = []
  parts << Media.format_text(input) if input
  parts.concat(attachments.map { |attachment| format_moderation_attachment(attachment) })
  parts
end

.moderation_urlObject



10
11
12
# File 'lib/ruby_llm/protocols/chat_completions/moderation.rb', line 10

def moderation_url
  'moderations'
end

.parse_moderation_response(response, model:) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_llm/protocols/chat_completions/moderation.rb', line 23

def parse_moderation_response(response, model:)
  data = response.body
  raise Error.new(data.dig('error', 'message'), response:) if data.dig('error', 'message')

  RubyLLM::Moderation.new(
    id: data['id'],
    model: model,
    results: Array(data['results']).map { |result| RubyLLM::Moderation::Result.from_h(result) },
    raw: data
  )
end

.render_moderation_payload(input, model:, with: [], provider_options: {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/ruby_llm/protocols/chat_completions/moderation.rb', line 14

def render_moderation_payload(input, model:, with: [], provider_options: {})
  attachments = Attachment.wrap(with)

  {
    model: model,
    input: moderation_input(input, attachments)
  }.merge(provider_options)
end