Class: Braintrust::Contrib::RubyLLM::ChatPatcher

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

Overview

Patcher for RubyLLM chat completions. Instruments RubyLLM::Chat#complete and #execute_tool methods.

Class Method Summary collapse

Methods inherited from Patcher

inherited, patch!, reset!

Class Method Details

.applicable?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/braintrust/contrib/ruby_llm/patcher.rb', line 13

def applicable?
  defined?(::RubyLLM::Chat)
end

.patched?(**options) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/braintrust/contrib/ruby_llm/patcher.rb', line 17

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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/braintrust/contrib/ruby_llm/patcher.rb', line 27

def perform_patch(**options)
  return unless applicable?

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

    options[:target].singleton_class.include(Instrumentation::Chat)
  else
    # Class-level (for all chat instances)
    ::RubyLLM::Chat.include(Instrumentation::Chat)
  end
end