Class: Braintrust::Contrib::OpenAI::ModerationsPatcher

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

Overview

Patcher for OpenAI Moderations API - implements class-level patching. All new OpenAI::Client instances created after patch! will be automatically instrumented.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/braintrust/contrib/openai/patcher.rb', line 134

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

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
# File 'lib/braintrust/contrib/openai/patcher.rb', line 138

def patched?(**options)
  # Use the target's singleton class if provided, otherwise check the base class.
  target_class = get_singleton_class(options[:target]) || ::OpenAI::Resources::Moderations

  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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/braintrust/contrib/openai/patcher.rb', line 150

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)

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