open_router 0.2.2 → 0.3.1
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 +4 -4
- data/CHANGELOG.md +8 -1
- data/Gemfile +5 -5
- data/Gemfile.lock +1 -1
- data/README.md +26 -0
- data/lib/open_router/client.rb +3 -3
- data/lib/open_router/http.rb +14 -18
- data/lib/open_router/version.rb +1 -1
- data/lib/open_router.rb +11 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5b9e865727928cf848ac7358a9d5b4fc5531885151549d4f872088761497a5e
|
4
|
+
data.tar.gz: 79696ceb293c5123529bdd50bc5ed99bbd324e0b68cc1a174ecec5f53db15026
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70738504ab35189048b1eb76d7f52c7e3421876b3bab31792af0afbe013ca0d2d27abaafd2cd07fc7c7d2942333b68d8170b51729bb5a32606188f702d4a6eca
|
7
|
+
data.tar.gz: 47155d2a9ef577daaa979776838a51ae9107294d8e370e4a7ad3fc3b8099589def7f40d96f44f2bf2fcdd9126a46562d7d54a7ab43778906c13d6ab409228cb7
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
## [
|
1
|
+
## [0.3.0] - 2024-05-03
|
2
|
+
|
3
|
+
- Uses Faraday's built-in JSON mode
|
4
|
+
- Added support for configuring Faraday and its middleware
|
5
|
+
- Spec creates a STDOUT logger by default (headers, bodies, errors)
|
6
|
+
- Spec filters Bearer token from logs by default
|
2
7
|
|
3
8
|
## [0.1.0] - 2024-03-19
|
4
9
|
|
5
10
|
- Initial release
|
11
|
+
|
12
|
+
## [Unreleased]
|
data/Gemfile
CHANGED
@@ -6,13 +6,13 @@ source "https://rubygems.org"
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
gem "activesupport", ">= 6.0"
|
9
|
-
gem "dotenv", ">= 2"
|
10
|
-
gem "pry", ">= 0.14"
|
11
|
-
gem "rake", "~> 13.0"
|
12
|
-
gem "rspec", "~> 3.0"
|
13
|
-
gem "rubocop", "~> 1.21"
|
14
9
|
|
15
10
|
group :development do
|
11
|
+
gem "dotenv", ">= 2"
|
12
|
+
gem "pry", ">= 0.14"
|
13
|
+
gem "rake", "~> 13.0"
|
14
|
+
gem "rspec", "~> 3.0"
|
15
|
+
gem "rubocop", "~> 1.21"
|
16
16
|
gem "solargraph-rails", "~> 0.2.0.pre"
|
17
17
|
gem "sorbet"
|
18
18
|
gem "tapioca", require: false
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -63,6 +63,32 @@ Then you can create a client like this:
|
|
63
63
|
client = OpenRouter::Client.new
|
64
64
|
```
|
65
65
|
|
66
|
+
#### Configure Faraday
|
67
|
+
|
68
|
+
The configuration object exposes a [`faraday`](https://github.com/lostisland/faraday-retry) method that you can pass a block to configure Faraday settings and middleware.
|
69
|
+
|
70
|
+
This example adds `faraday-retry` and a logger that redacts the api key so it doesn't get leaked to logs.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
require 'faraday/retry'
|
74
|
+
|
75
|
+
retry_options = {
|
76
|
+
max: 2,
|
77
|
+
interval: 0.05,
|
78
|
+
interval_randomness: 0.5,
|
79
|
+
backoff_factor: 2
|
80
|
+
}
|
81
|
+
|
82
|
+
OpenRouter::Client.new(access_token: ENV["ACCESS_TOKEN"]) do |config|
|
83
|
+
config.faraday do |f|
|
84
|
+
f.request :retry, retry_options
|
85
|
+
f.response :logger, ::Logger.new($stdout), { headers: true, bodies: true, errors: true } do |logger|
|
86
|
+
logger.filter(/(Bearer) (\S+)/, '\1[REDACTED]')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
66
92
|
#### Change version or timeout
|
67
93
|
|
68
94
|
The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client.
|
data/lib/open_router/client.rb
CHANGED
@@ -41,9 +41,9 @@ module OpenRouter
|
|
41
41
|
parameters[:stream] = stream if stream
|
42
42
|
parameters.merge!(extras)
|
43
43
|
|
44
|
-
|
45
|
-
raise ServerError,
|
46
|
-
raise ServerError, response.
|
44
|
+
post(path: "/chat/completions", parameters:).tap do |response|
|
45
|
+
raise ServerError, response.dig("error", "message") if response.presence&.dig("error", "message").present?
|
46
|
+
raise ServerError, "Empty response from OpenRouter. Might be worth retrying once or twice." if stream.blank? && response.blank?
|
47
47
|
end.with_indifferent_access
|
48
48
|
end
|
49
49
|
|
data/lib/open_router/http.rb
CHANGED
@@ -3,13 +3,13 @@
|
|
3
3
|
module OpenRouter
|
4
4
|
module HTTP
|
5
5
|
def get(path:)
|
6
|
-
|
6
|
+
conn.get(uri(path:)) do |req|
|
7
7
|
req.headers = headers
|
8
|
-
end&.body
|
8
|
+
end&.body
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
11
|
+
def post(path:, parameters:)
|
12
|
+
conn.post(uri(path:)) do |req|
|
13
13
|
if parameters[:stream].respond_to?(:call)
|
14
14
|
req.options.on_data = to_json_stream(user_proc: parameters[:stream])
|
15
15
|
parameters[:stream] = true # Necessary to tell OpenRouter to stream.
|
@@ -17,33 +17,24 @@ module OpenRouter
|
|
17
17
|
|
18
18
|
req.headers = headers
|
19
19
|
req.body = parameters.to_json
|
20
|
-
end&.body
|
20
|
+
end&.body
|
21
21
|
end
|
22
22
|
|
23
23
|
def multipart_post(path:, parameters: nil)
|
24
|
-
|
24
|
+
conn(multipart: true).post(uri(path:)) do |req|
|
25
25
|
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
|
26
26
|
req.body = multipart_parameters(parameters)
|
27
|
-
end&.body
|
27
|
+
end&.body
|
28
28
|
end
|
29
29
|
|
30
30
|
def delete(path:)
|
31
|
-
|
31
|
+
conn.delete(uri(path:)) do |req|
|
32
32
|
req.headers = headers
|
33
|
-
end&.body
|
33
|
+
end&.body
|
34
34
|
end
|
35
35
|
|
36
36
|
private
|
37
37
|
|
38
|
-
def to_json(string)
|
39
|
-
return unless string
|
40
|
-
|
41
|
-
JSON.parse(string)
|
42
|
-
rescue JSON::ParserError
|
43
|
-
# Convert a multiline string of JSON objects to a JSON array.
|
44
|
-
JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
|
45
|
-
end
|
46
|
-
|
47
38
|
# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
|
48
39
|
# For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
|
49
40
|
# be a data object or an error object as described in the OpenRouter API documentation.
|
@@ -66,6 +57,11 @@ module OpenRouter
|
|
66
57
|
Faraday.new do |f|
|
67
58
|
f.options[:timeout] = OpenRouter.configuration.request_timeout
|
68
59
|
f.request(:multipart) if multipart
|
60
|
+
f.use MiddlewareErrors if @log_errors
|
61
|
+
f.response :raise_error
|
62
|
+
f.response :json
|
63
|
+
|
64
|
+
OpenRouter.configuration.faraday_config&.call(f)
|
69
65
|
end
|
70
66
|
end
|
71
67
|
|
data/lib/open_router/version.rb
CHANGED
data/lib/open_router.rb
CHANGED
@@ -13,18 +13,19 @@ module OpenRouter
|
|
13
13
|
|
14
14
|
class Configuration
|
15
15
|
attr_writer :access_token
|
16
|
-
attr_accessor :api_version, :extra_headers, :request_timeout, :uri_base
|
16
|
+
attr_accessor :api_version, :extra_headers, :faraday_config, :log_errors, :request_timeout, :uri_base
|
17
17
|
|
18
18
|
DEFAULT_API_VERSION = "v1"
|
19
19
|
DEFAULT_REQUEST_TIMEOUT = 120
|
20
20
|
DEFAULT_URI_BASE = "https://openrouter.ai/api"
|
21
21
|
|
22
22
|
def initialize
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
self.access_token = nil
|
24
|
+
self.api_version = DEFAULT_API_VERSION
|
25
|
+
self.extra_headers = {}
|
26
|
+
self.log_errors = false
|
27
|
+
self.request_timeout = DEFAULT_REQUEST_TIMEOUT
|
28
|
+
self.uri_base = DEFAULT_URI_BASE
|
28
29
|
end
|
29
30
|
|
30
31
|
def access_token
|
@@ -33,6 +34,10 @@ module OpenRouter
|
|
33
34
|
raise ConfigurationError, "OpenRouter access token missing!"
|
34
35
|
end
|
35
36
|
|
37
|
+
def faraday(&block)
|
38
|
+
self.faraday_config = block
|
39
|
+
end
|
40
|
+
|
36
41
|
def site_name=(value)
|
37
42
|
@extra_headers["X-Title"] = value
|
38
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Obie Fernandez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|