Class: Roseflow::OpenRouter::Client
- Inherits:
-
Object
- Object
- Roseflow::OpenRouter::Client
- 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
- #completion(model:, prompt:, **options) ⇒ Object
-
#initialize(config = Config.new, provider = nil) ⇒ Client
constructor
A new instance of Client.
- #models ⇒ Object
- #post(operation, &block) ⇒ Object
- #streaming_completion(model:, prompt:, **options, &block) ⇒ Object
Constructor Details
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:, **) connection.post("/api/v1/chat/completions") do |request| request.body = .merge({ model: model.name, messages: prompt, }) end end |
#models ⇒ Object
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..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:, **, &block) streamed = [] raw_ = !!.delete(:raw) .delete(:streaming) connection.post("/api/v1/chat/completions") do |request| request.body = .merge({ model: model.name, messages: prompt, stream: true, }) request..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 |