Class: SavvyOpenrouter::Resources::Chat

Inherits:
Base
  • Object
show all
Defined in:
lib/savvy_openrouter/resources/chat.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SavvyOpenrouter::Resources::Base

Instance Method Details

#completions(messages:, **params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/savvy_openrouter/resources/chat.rb', line 10

def completions(messages:, **params)
  policy = CompletionRetryPolicy.new(config.chat_retries)
  body = config.merge_chat_body({ messages: messages }.merge(params))
  attempt = 0
  last_response = nil
  loop do
    attempt += 1
    begin
      last_response = conn.post("/chat/completions", body: body)
    rescue SavvyOpenrouter::ApiError => e
      raise e if attempt >= policy.max_attempts || !policy.retry_http_error?(e)

      policy.wait_after_attempt(attempt)
      next
    end

    return last_response unless policy.retry_response?(last_response)
    return last_response if attempt >= policy.max_attempts

    policy.wait_after_attempt(attempt)
  end
end

#completions_stream(messages:, **params, &block) ⇒ Object

Yields each SSE ‘data:` payload string (JSON text from the model stream).



34
35
36
37
38
39
40
41
42
# File 'lib/savvy_openrouter/resources/chat.rb', line 34

def completions_stream(messages:, **params, &block)
  body = config.merge_chat_body({ messages: messages, stream: true }.merge(params))
  return enum_for(:completions_stream, messages: messages, **params) unless block

  chunk_enum = Enumerator.new do |y|
    conn.stream_post("/chat/completions", body) { |ch| y << ch }
  end
  Streaming.each_sse_data(chunk_enum, &block)
end