Class: Braintrust::Contrib::RubyOpenAI::ChatPatcher

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

Overview

Patcher for ruby-openai chat completions. Instruments OpenAI::Client#chat method.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 15

def applicable?
  defined?(::OpenAI::Client)
end

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 19

def patched?(**options)
  target_class = options[:target]&.singleton_class || ::OpenAI::Client
  Instrumentation::Chat.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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/braintrust/contrib/ruby_openai/patcher.rb', line 29

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::Chat)
  else
    # Class-level (for all clients)
    ::OpenAI::Client.include(Instrumentation::Chat)
  end
end