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

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

Class Method Summary collapse

Class Method Details

.build(record_method:, methods:) ⇒ Object



361
362
363
364
365
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 361

def 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



367
368
369
370
371
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 367

def 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



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 373

def define_blocking_method(mod, method_name, record_method)
  mod.define_method(method_name) do |*args, **kwargs, &block|
    integration = LlmCostTracker::Integrations::Openai
    request = integration.request_params(args, kwargs)
    integration.enforce_budget!(request: request)
    started_at = LlmCostTracker::Timing.now_monotonic
    response = super(*integration.normalize_sdk_args(args, kwargs), &block)
    integration.public_send(
      record_method, response,
      request: request,
      latency_ms: LlmCostTracker::Timing.elapsed_ms(started_at),
      host: integration.client_host_for(self)
    )
    response
  end
end

.define_stream_method(mod, method_name) ⇒ Object



390
391
392
393
394
395
396
# File 'lib/llm_cost_tracker/integrations/openai.rb', line 390

def define_stream_method(mod, method_name)
  mod.define_method(method_name) do |*args, **kwargs|
    LlmCostTracker::Integrations::Openai.wrap_stream_call(args, kwargs, self) do |normalized, _|
      super(*normalized)
    end
  end
end