Class: OpenrouterClient

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/openrouter_client.rb

Constant Summary collapse

OPENROUTER_URL =
'https://openrouter.ai'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpenrouterClient

Returns a new instance of OpenrouterClient.



8
9
10
# File 'app/services/openrouter_client.rb', line 8

def initialize
  @connection = self.class.shared_connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'app/services/openrouter_client.rb', line 6

def connection
  @connection
end

Class Method Details

.create_connectionObject



16
17
18
19
20
21
22
23
# File 'app/services/openrouter_client.rb', line 16

def self.create_connection
  DefaultFaradayClient.create_faraday_connection(OPENROUTER_URL) do |faraday|
    faraday.headers['Authorization'] = "Bearer #{Figaro.env.OPENROUTER_KEY!}"
    faraday.headers['Content-Type'] = 'application/json'
    faraday.options.timeout = 60
    faraday.options.open_timeout = 10
  end
end

.openrouter_modelObject



29
30
31
# File 'app/services/openrouter_client.rb', line 29

def self.openrouter_model
  @openrouter_model ||= AdminSetting.openrouter_model
end

.reset_connection!Object



25
26
27
# File 'app/services/openrouter_client.rb', line 25

def self.reset_connection!
  @shared_connection = nil
end

.reset_openrouter_model!Object



33
34
35
# File 'app/services/openrouter_client.rb', line 33

def self.reset_openrouter_model!
  @openrouter_model = nil
end

.shared_connectionObject



12
13
14
# File 'app/services/openrouter_client.rb', line 12

def self.shared_connection
  @shared_connection ||= create_connection
end

Instance Method Details

#generate_text(prompt) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/openrouter_client.rb', line 37

def generate_text(prompt)
  Retriable.retriable(
    tries: 3,
    base_interval: 1,
    multiplier: 1,
    on: [Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::ServerError],
  ) do
    connection.post('/api/v1/chat/completions') do |req|
      req.body = {
        model: self.class.openrouter_model,
        messages: [{ role: 'user', content: prompt }],
        response_format: { type: 'json_object' },
      }.to_json
    end
  end
end