Module: LlmCostTracker::Integrations::Openai::PatchBuilder

Defined in:
lib/llm_cost_tracker/integrations/openai/patches.rb

Class Method Summary collapse

Class Method Details

.build(record_method:, methods:) ⇒ Object



7
8
9
10
11
# File 'lib/llm_cost_tracker/integrations/openai/patches.rb', line 7

def self.build(record_method:, methods:)
  Module.new.tap do |mod|
    methods.each { |method_name| define_blocking_method(mod, method_name, record_method) }
  end
end

.build_stream(methods:) ⇒ Object



13
14
15
16
17
# File 'lib/llm_cost_tracker/integrations/openai/patches.rb', line 13

def self.build_stream(methods:)
  Module.new.tap do |mod|
    methods.each { |method_name| define_stream_method(mod, method_name) }
  end
end

.define_blocking_method(mod, method_name, record_method) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/llm_cost_tracker/integrations/openai/patches.rb', line 19

def self.define_blocking_method(mod, method_name, record_method)
  mod.define_method(method_name) do |*args, **kwargs, &block|
    host = LlmCostTracker::Integrations::Openai.client_host_for(self)
    LlmCostTracker::Integrations::Openai.wrap_blocking(
      args,
      kwargs,
      provider: LlmCostTracker::Integrations::Openai.provider_for_host(host),
      record: lambda do |response, request, latency_ms|
        LlmCostTracker::Integrations::Openai.public_send(
          record_method, response, request: request, latency_ms: latency_ms, host: host
        )
      end
    ) { super(*args, **kwargs, &block) }
  end
end

.define_stream_method(mod, method_name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/llm_cost_tracker/integrations/openai/patches.rb', line 35

def self.define_stream_method(mod, method_name)
  mod.define_method(method_name) do |*args, **kwargs|
    LlmCostTracker::Integrations::Openai.wrap_stream(
      args, kwargs, **LlmCostTracker::Integrations::Openai.stream_seam(self)
    ) { super(*args, **kwargs) }
  end
end