Class: Brute::Middleware::OpenRouter::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/middleware/open_router.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config: {}, **options) ⇒ Completion

config: keyword arguments for OpenRouter::Client.new (access_token:, request_timeout:, uri_base:, extra_headers:). Defaults to OpenRouter.configuration's global settings. options: keyword arguments for OpenRouter::CompletionOptions.new (model:, temperature:, tools:, ...).



12
13
14
15
16
# File 'lib/brute/middleware/open_router.rb', line 12

def initialize(app, config: {}, **options)
  @app = app
  @config = config
  @options = ::OpenRouter::CompletionOptions.new(**options)
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/brute/middleware/open_router.rb', line 18

def call(env)
  messages = Brute::MessageTransport::OpenRouter.dump_all(env[:messages])

  ::OpenRouter::Client.new(**@config).then do |client|
    client.complete(messages, @options).then do |response|

      # OpenRouter in fact only returns a single message...
      # https://github.com/estiens/open_router_enhanced/blob/main/lib/open_router/response.rb
      Brute::MessageTransport::OpenRouter.wrap_each(response) do |message|
        env[:messages] << message
      end
    end
  end

  env
end