roseflow-openrouter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 87a20a3c7e3eabe923827fb046a4d418901e2719d845244b3cd6488301044a89
4
+ data.tar.gz: b0b3cc0894bba52d00d95b75ec093a2c86cf1621d2bc9f7849de1a6c61b9b754
5
+ SHA512:
6
+ metadata.gz: e393acceab954ba51cec59a2dd518928c9e9e3fcca4ec2f77475e1c4dc9b9caab8cd318b23f7d9026fbe8343e8cdf104d5f9f5d80a78e9505fe686444b6f6604
7
+ data.tar.gz: d026c4386e835b3841e47e8c14ad913d25413bf82d3b1c6506c5c596017001936bc13926e44fa905cb0b36c476053f5ce6c5070d47ef7adf8009cc6ac305d78b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-18
4
+
5
+ - Initial release
@@ -0,0 +1,7 @@
1
+ # Contributor Code of Conduct
2
+
3
+ The Roseflow team is committed to fostering a welcoming community.
4
+
5
+ **Our Code of Conduct can be found here**:
6
+
7
+ https://roseflow.ai/conduct
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in roseflow-openrouter.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "standard", "~> 1.3"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Lauri Jutila
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Roseflow OpenRouter integration
2
+
3
+ This gem adds [OpenRouter](https://openrouter.ai) support and integration for [Roseflow](https://github.com/roseflow-ai/roseflow), a framework for interacting with AI in Ruby.
4
+
5
+ ## Prerequisites
6
+
7
+ To use this gem effectively, you need the [core Roseflow gem](https://github.com/roseflow-ai/roseflow).
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add roseflow-openrouter
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install roseflow-openrouter
18
+
19
+ ## Usage
20
+
21
+ See full documentation how to configure and use Roseflow with OpenRouter at [docs.roseflow.ai](https://docs.roseflow.ai/openrouter).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/roseflow-openrouter.
26
+
27
+ ## Community
28
+
29
+ ### Discord
30
+
31
+ Join us in our [Discord](https://discord.gg/roseflow).
32
+
33
+ ### Twitter
34
+
35
+ Connect with the core team on Twitter.
36
+
37
+ <a href="https://twitter.com/ljuti" target="_blank">
38
+ <img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/ljuti?logo=twitter&style=social">
39
+ </a>
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the Roseflow OpenRouter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/roseflow-ai/roseflow-openrouter/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,14 @@
1
+ default: &default
2
+ referer: <YOUR REFERER URL>
3
+
4
+ development:
5
+ <<: *default
6
+ api_key: <YOUR OPENROUTER API KEY FOR DEVELOPMENT>
7
+
8
+ test:
9
+ <<: *default
10
+ api_key: <YOUR OPENROUTER API KEY FOR TEST>
11
+
12
+ production:
13
+ <<: *default
14
+ api_key: <YOUR OPENROUTER API KEY FOR PRODUCTION>
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday/retry"
5
+ require "roseflow/open_router/config"
6
+ require "roseflow/open_router/model"
7
+
8
+ FARADAY_RETRY_OPTIONS = {
9
+ max: 3,
10
+ interval: 0.05,
11
+ interval_randomness: 0.5,
12
+ backoff_factor: 2,
13
+ }
14
+
15
+ module Roseflow
16
+ module OpenRouter
17
+ class Client
18
+ def initialize(config = Config.new, provider = nil)
19
+ @config = config
20
+ @provider = provider
21
+ end
22
+
23
+ def models
24
+ response = connection.get("/api/v1/models")
25
+ body = JSON.parse(response.body)
26
+ body.fetch("data", []).map do |model|
27
+ OpenRouter::Model.new(model, self)
28
+ end
29
+ end
30
+
31
+ def completion(model:, prompt:, **options)
32
+ connection.post("/api/v1/chat/completions") do |request|
33
+ request.body = options.merge({
34
+ model: model.name,
35
+ messages: prompt,
36
+ })
37
+ end
38
+ end
39
+
40
+ def streaming_completion(model:, prompt:, **options, &block)
41
+ streamed = []
42
+ raw_ = !!options.delete(:raw)
43
+ options.delete(:streaming)
44
+
45
+ connection.post("/api/v1/chat/completions") do |request|
46
+ request.body = options.merge({
47
+ model: model.name,
48
+ messages: prompt,
49
+ stream: true,
50
+ })
51
+
52
+ request.options.on_data = Proc.new do |chunk|
53
+ if block_given?
54
+ yield streaming_chunk(chunk) unless raw_
55
+ yield chunk if raw_
56
+ end
57
+
58
+ streamed << chunk unless block_given?
59
+ end
60
+ end
61
+
62
+ streamed unless block_given?
63
+ end
64
+
65
+ private
66
+
67
+ attr_reader :config, :provider
68
+
69
+ def connection
70
+ @connection ||= Faraday.new(
71
+ url: Config::OPENROUTER_API_URL,
72
+ headers: {
73
+ "HTTP-Referer": config.referer,
74
+ },
75
+ ) do |faraday|
76
+ faraday.request :authorization, "Bearer", -> { config.api_key }
77
+ faraday.request :json
78
+ faraday.request :retry, FARADAY_RETRY_OPTIONS
79
+ faraday.adapter Faraday.default_adapter
80
+ end
81
+ end
82
+
83
+ def streaming_chunk(chunk)
84
+ return chunk unless chunk.match(/{.*}/)
85
+
86
+ chunk.scan(/{.*}/).map do |json|
87
+ JSON.parse(json).dig("choices", 0, "delta", "content")
88
+ end.join("")
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "anyway_config"
4
+
5
+ module Roseflow
6
+ module OpenRouter
7
+ class Config < Anyway::Config
8
+ config_name :openrouter
9
+
10
+ attr_config :api_key
11
+ attr_config :referer
12
+
13
+ required :api_key
14
+ required :referer
15
+
16
+ OPENROUTER_API_URL = "https://openrouter.ai/api/v1".freeze
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/struct"
4
+ require "active_support/core_ext/module/delegation"
5
+
6
+ module Types
7
+ include Dry.Types()
8
+ end
9
+
10
+ module Roseflow
11
+ module OpenRouter
12
+ class Model
13
+ attr_reader :name, :id, :context_length
14
+
15
+ def initialize(model, provider)
16
+ @model_ = model
17
+ @provider_ = provider
18
+ assign_attributes
19
+ end
20
+
21
+ def max_tokens
22
+ @context_length
23
+ end
24
+
25
+ private
26
+
27
+ def assign_attributes
28
+ @name = @model_.fetch("id")
29
+ @id = @model_.fetch("id")
30
+ @context_length = @model_.fetch("context_length")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Roseflow
4
+ module OpenRouter
5
+ class ModelRepository
6
+ attr_reader :models
7
+
8
+ delegate :each, :all, :first, :last, to: :models
9
+
10
+ def initialize(provider)
11
+ @provider = provider
12
+ @models = provider.client.models
13
+ end
14
+
15
+ def find(name)
16
+ @models.select { |model| model.name == name }.first
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Roseflow
4
+ module OpenRouter
5
+ class Provider
6
+ def initialize(config = Roseflow::OpenRouter::Config.new)
7
+ @config = config
8
+ end
9
+
10
+ def client
11
+ @client ||= Client.new(config, self)
12
+ end
13
+
14
+ def models
15
+ @models ||= ModelRepository.new(self)
16
+ end
17
+
18
+ def completion(model:, prompt:, **options)
19
+ streaming = options.fetch(:streaming, false)
20
+
21
+ if streaming
22
+ client.streaming_completion(model: model, prompt: prompt, **options)
23
+ else
24
+ client.completion(model: model, prompt: prompt, **options)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :config
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Roseflow
4
+ module OpenRouter
5
+ def self.gem_version
6
+ Gem::Version.new VERSION::STRING
7
+ end
8
+
9
+ module VERSION
10
+ MAJOR = 0
11
+ MINOR = 1
12
+ PATCH = 0
13
+ PRE = nil
14
+
15
+ STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "open_router/version"
4
+ require_relative "open_router/client"
5
+ require_relative "open_router/config"
6
+ require_relative "open_router/model_repository"
7
+ require_relative "open_router/model"
8
+ require_relative "open_router/provider"
9
+
10
+ module Roseflow
11
+ module OpenRouter
12
+ class Error < StandardError; end
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/roseflow/open_router/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "roseflow-openrouter"
7
+ spec.version = Roseflow::OpenRouter.gem_version
8
+ spec.authors = ["Lauri Jutila"]
9
+ spec.email = ["ljuti@users.noreply.github.com"]
10
+
11
+ spec.summary = "Roseflow meets OpenRouter"
12
+ spec.description = "OpenRouter integration and support for Roseflow"
13
+ spec.homepage = "https://github.com/roseflow-ai/roseflow-openrouter"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.2.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/roseflow-ai/roseflow-openrouter"
19
+ spec.metadata["changelog_uri"] = "https://github.com/roseflow-ai/roseflow-openrouter/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "activesupport"
33
+ spec.add_dependency "anyway_config", "~> 2.0"
34
+ spec.add_dependency "dry-struct"
35
+ spec.add_dependency "faraday"
36
+ spec.add_dependency "faraday-retry"
37
+
38
+ spec.add_development_dependency "awesome_print"
39
+ spec.add_development_dependency "pry"
40
+ spec.add_development_dependency "roseflow"
41
+ spec.add_development_dependency "webmock"
42
+ spec.add_development_dependency "vcr"
43
+ end
@@ -0,0 +1,6 @@
1
+ module Roseflow
2
+ module Openrouter
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roseflow-openrouter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lauri Jutila
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: anyway_config
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-struct
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday-retry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: roseflow
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: vcr
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: OpenRouter integration and support for Roseflow
154
+ email:
155
+ - ljuti@users.noreply.github.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".rspec"
161
+ - ".standard.yml"
162
+ - CHANGELOG.md
163
+ - CODE_OF_CONDUCT.md
164
+ - Gemfile
165
+ - LICENSE.txt
166
+ - README.md
167
+ - Rakefile
168
+ - config/openrouter.yml
169
+ - lib/roseflow/open_router.rb
170
+ - lib/roseflow/open_router/client.rb
171
+ - lib/roseflow/open_router/config.rb
172
+ - lib/roseflow/open_router/model.rb
173
+ - lib/roseflow/open_router/model_repository.rb
174
+ - lib/roseflow/open_router/provider.rb
175
+ - lib/roseflow/open_router/version.rb
176
+ - roseflow-openrouter.gemspec
177
+ - sig/roseflow/openrouter.rbs
178
+ homepage: https://github.com/roseflow-ai/roseflow-openrouter
179
+ licenses:
180
+ - MIT
181
+ metadata:
182
+ homepage_uri: https://github.com/roseflow-ai/roseflow-openrouter
183
+ source_code_uri: https://github.com/roseflow-ai/roseflow-openrouter
184
+ changelog_uri: https://github.com/roseflow-ai/roseflow-openrouter/blob/master/CHANGELOG.md
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: 3.2.0
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubygems_version: 3.4.1
201
+ signing_key:
202
+ specification_version: 4
203
+ summary: Roseflow meets OpenRouter
204
+ test_files: []