Class: Braintrust::Contrib::OpenAI::ModerationsPatcher
- 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
- .applicable? ⇒ Boolean
- .patched?(**options) ⇒ Boolean
-
.perform_patch(**options) ⇒ void
Perform the actual patching.
Methods inherited from Patcher
Class Method Details
.applicable? ⇒ 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
138 139 140 141 142 143 |
# File 'lib/braintrust/contrib/openai/patcher.rb', line 138 def patched?(**) # Use the target's singleton class if provided, otherwise check the base class. target_class = get_singleton_class([:target]) || ::OpenAI::Resources::Moderations Instrumentation::Moderations.applied?(target_class) end |
.perform_patch(**options) ⇒ void
This method returns an undefined value.
Perform the actual patching.
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(**) return unless applicable? if [:target] # Instance-level (for only this client) raise ArgumentError, "target must be a kind of ::OpenAI::Client" unless [:target].is_a?(::OpenAI::Client) get_singleton_class([:target]).include(Instrumentation::Moderations) else # Class-level (for all clients) ::OpenAI::Resources::Moderations.include(Instrumentation::Moderations) end end |