Class: Legion::Extensions::Llm::Connection
- Inherits:
-
Object
- Object
- Legion::Extensions::Llm::Connection
- Defined in:
- lib/legion/extensions/llm/connection.rb
Overview
Connection class for managing API connections to various providers.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Class Method Summary collapse
Instance Method Summary collapse
- #get(url) ⇒ Object
-
#initialize(provider, config) ⇒ Connection
constructor
A new instance of Connection.
- #instance_variables ⇒ Object
- #post(url, payload) ⇒ Object
Constructor Details
#initialize(provider, config) ⇒ Connection
Returns a new instance of Connection.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/legion/extensions/llm/connection.rb', line 23 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/legion/extensions/llm/connection.rb', line 8 def config @config end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/legion/extensions/llm/connection.rb', line 8 def connection @connection end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
8 9 10 |
# File 'lib/legion/extensions/llm/connection.rb', line 8 def provider @provider end |
Class Method Details
.basic ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/legion/extensions/llm/connection.rb', line 10 def self.basic(&) Faraday.new do |f| f.response :logger, Legion::Extensions::Llm.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
44 45 46 47 48 49 |
# File 'lib/legion/extensions/llm/connection.rb', line 44 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_variables ⇒ Object
51 52 53 |
# File 'lib/legion/extensions/llm/connection.rb', line 51 def instance_variables super - %i[@config @connection] end |
#post(url, payload) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/legion/extensions/llm/connection.rb', line 37 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 |