Class: Legion::Extensions::Llm::Connection
- Inherits:
-
Object
- Object
- Legion::Extensions::Llm::Connection
- Extended by:
- Logging::Helper
- Includes:
- Logging::Helper
- 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.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/legion/extensions/llm/connection.rb', line 39 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.
10 11 12 |
# File 'lib/legion/extensions/llm/connection.rb', line 10 def config @config end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/legion/extensions/llm/connection.rb', line 10 def connection @connection end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
10 11 12 |
# File 'lib/legion/extensions/llm/connection.rb', line 10 def provider @provider end |
Class Method Details
.basic ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/legion/extensions/llm/connection.rb', line 15 def basic(&) logger = faraday_logger Faraday.new do |f| f.response :logger, 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
60 61 62 63 64 65 |
# File 'lib/legion/extensions/llm/connection.rb', line 60 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
67 68 69 |
# File 'lib/legion/extensions/llm/connection.rb', line 67 def instance_variables super - %i[@config @connection] end |
#post(url, payload) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/legion/extensions/llm/connection.rb', line 53 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 |