Class: RubyLLM::Transport::Connection

Inherits:
Object
  • Object
show all
Includes:
Support::Inspectable
Defined in:
lib/ruby_llm/transport/connection.rb

Overview

:nodoc:

Constant Summary collapse

IDEMPOTENT_KEY =
:ruby_llm_idempotent
STREAM_PROGRESS_KEY =
:ruby_llm_stream_progress

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

#initialize(provider, config, api_base: nil) ⇒ Connection

Returns a new instance of Connection.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_llm/transport/connection.rb', line 35

def initialize(provider, config, api_base: nil)
  @provider = provider
  @config = config

  @connection = Faraday.new(api_base || provider.api_base) do |faraday|
    setup_timeout(faraday)
    setup_logging(faraday)
    setup_retry(faraday)
    setup_middleware(faraday)
    setup_http_proxy(faraday)
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/ruby_llm/transport/connection.rb', line 18

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/ruby_llm/transport/connection.rb', line 18

def connection
  @connection
end

#providerObject (readonly)

Returns the value of attribute provider.



18
19
20
# File 'lib/ruby_llm/transport/connection.rb', line 18

def provider
  @provider
end

Class Method Details

.basic(config = RubyLLM.config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_llm/transport/connection.rb', line 20

def self.basic(config = RubyLLM.config, &)
  Faraday.new do |f|
    f.options.timeout = config.request_timeout
    f.proxy = config.http_proxy if config.http_proxy
    f.response :logger,
               RubyLLM.logger,
               bodies: false,
               errors: true,
               headers: false,
               log_level: :debug
    f.response :raise_error
    yield f if block_given?
  end
end

Instance Method Details

#delete(url) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ruby_llm/transport/connection.rb', line 77

def delete(url, &)
  instrument_request(:delete, url) do
    @connection.delete url do |req|
      req.headers.merge! @provider.headers
      yield req if block_given?
    end
  end
end

#get(url) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ruby_llm/transport/connection.rb', line 59

def get(url, &)
  instrument_request(:get, url) do
    @connection.get url do |req|
      req.headers.merge! @provider.headers
      yield req if block_given?
    end
  end
end

#patch(url, payload) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/ruby_llm/transport/connection.rb', line 68

def patch(url, payload, &)
  instrument_request(:patch, url) do
    @connection.patch url, payload do |req|
      req.headers.merge! @provider.headers
      yield req if block_given?
    end
  end
end

#post(url, payload, usage: nil, idempotent: true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_llm/transport/connection.rb', line 48

def post(url, payload, usage: nil, idempotent: true, &)
  instrument_request(:post, url) do
    @connection.post url, payload do |req|
      req.headers.merge! @provider.headers
      set_usage_tracker(req, usage) if usage
      mark_non_idempotent(req) unless idempotent
      yield req if block_given?
    end
  end
end