Class: Braintrust::Contrib::RubyOpenAI::ModerationsPatcher

Inherits:
Patcher
  • Object
show all
Defined in:
lib/braintrust/contrib/ruby_openai/patcher.rb

Overview

Patcher for ruby-openai moderations API. Instruments OpenAI::Client#moderations method.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 89

def applicable?
  defined?(::OpenAI::Client) && ::OpenAI::Client.method_defined?(:moderations)
end

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 93

def patched?(**options)
  target_class = options[:target]&.singleton_class || ::OpenAI::Client
  Instrumentation::Moderations.applied?(target_class)
end

.perform_patch(**options) ⇒ void

This method returns an undefined value.

Perform the actual patching.

Parameters:

  • options (Hash)

    Configuration options passed from integration

Options Hash (**options):

  • :target (Object)

    Optional target instance to patch

  • :tracer_provider (OpenTelemetry::SDK::Trace::TracerProvider)

    Optional tracer provider



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 103

def perform_patch(**options)
  return unless applicable?

  if options[:target]
    # Instance-level (for only this client)
    raise ArgumentError, "target must be a kind of ::OpenAI::Client" unless options[:target].is_a?(::OpenAI::Client)

    options[:target].singleton_class.include(Instrumentation::Moderations)
  else
    # Class-level (for all clients)
    ::OpenAI::Client.include(Instrumentation::Moderations)
  end
end