Class: Roseflow::OpenRouter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/roseflow/open_router/client.rb

Constant Summary collapse

FARADAY_RETRY_OPTIONS =
{
  max: 3,
  interval: 0.05,
  interval_randomness: 0.5,
  backoff_factor: 2,
}

Instance Method Summary collapse

Constructor Details

#initialize(config = Config.new, provider = nil) ⇒ Client

Returns a new instance of Client.



19
20
21
22
# File 'lib/roseflow/open_router/client.rb', line 19

def initialize(config = Config.new, provider = nil)
  @config = config
  @provider = provider
end

Instance Method Details

#completion(model:, prompt:, **options) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/roseflow/open_router/client.rb', line 44

def completion(model:, prompt:, **options)
  connection.post("/api/v1/chat/completions") do |request|
    request.body = options.merge({
      model: model.name,
      messages: prompt,
    })
  end
end

#modelsObject



24
25
26
27
28
29
30
# File 'lib/roseflow/open_router/client.rb', line 24

def models
  response = connection.get("/api/v1/models")
  body = FastJsonparser.parse(response.body)
  body.fetch("data", []).map do |model|
    OpenRouter::Model.new(model, self)
  end
end

#post(operation, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/roseflow/open_router/client.rb', line 32

def post(operation, &block)
  connection.post(operation.path) do |request|
    request.body = operation.body

    if operation.stream
      request.options.on_data = Proc.new do |chunk|
        yield chunk
      end
    end
  end
end

#streaming_completion(model:, prompt:, **options, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roseflow/open_router/client.rb', line 53

def streaming_completion(model:, prompt:, **options, &block)
  streamed = []
  raw_ = !!options.delete(:raw)
  options.delete(:streaming)

  connection.post("/api/v1/chat/completions") do |request|
    request.body = options.merge({
      model: model.name,
      messages: prompt,
      stream: true,
    })

    request.options.on_data = Proc.new do |chunk|
      if block_given?
        yield streaming_chunk(chunk) unless raw_
        yield chunk if raw_
      end

      streamed << chunk unless block_given?
    end
  end

  streamed unless block_given?
end