Class: RubyLLM::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/connection.rb

Overview

Connection class for managing API connections to various providers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, config) ⇒ Connection

Returns a new instance of Connection.



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

def initialize(provider, config)
  @provider = provider
  @config = config

  ensure_configured!
  @connection ||= Faraday.new(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.



6
7
8
# File 'lib/ruby_llm/connection.rb', line 6

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/ruby_llm/connection.rb', line 6

def connection
  @connection
end

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/ruby_llm/connection.rb', line 6

def provider
  @provider
end

Class Method Details

.basicObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_llm/connection.rb', line 8

def self.basic(&)
  Faraday.new do |f|
    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

#get(url) ⇒ Object



42
43
44
45
46
47
# File 'lib/ruby_llm/connection.rb', line 42

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

#instance_variablesObject



49
50
51
# File 'lib/ruby_llm/connection.rb', line 49

def instance_variables
  super - %i[@config @connection]
end

#post(url, payload) ⇒ Object



35
36
37
38
39
40
# File 'lib/ruby_llm/connection.rb', line 35

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