ruby-leonardoai 0.1.0 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 267d56d7aa704aabc1811a935781aeb4a76fd33a6190814fa24d0205094e4a0a
4
- data.tar.gz: 257de0b0042ce3f39de55fbb49b6df8a04b011852627cf6c3ae040c5ae76c3c9
3
+ metadata.gz: ffeeea7f957abbf1047c55db4b65e787b4c881c5bb3025cfad2b773403bcf6c9
4
+ data.tar.gz: aafa39eba2314b23210262dba8ccbb60b68dc688cd6f111ede64c50b641b6f5d
5
5
  SHA512:
6
- metadata.gz: a35af039b1a1a22e5b34d64e9fa7698dca4ecde84205699218bba8bb3a9ac8e769afb87342b97bc0a2374a1496b7ee4f8def929e356422acb476aebda0f3f69c
7
- data.tar.gz: aa680397e3e68cf6e442b5c0932d09ed0423352e94c7ad53d1fa560bf6bfb4e5a50b00d2dcac6573c8d8063e890ab072103ae35e83dbddf2c2dd2273df6b8dc3
6
+ metadata.gz: 3e0ed3ba12d42f629cb319784c6c53b8c911a0c7c1d11d07e6b1485c26293d7a44bd1b1132515adb19207641191589d4733e84bd5cda1e4a83f323611f0c9ff4
7
+ data.tar.gz: 5144d1f07fb74fe88ef233255ff8e970e0985facbaa5ab56377188727e84816f740be883f10d8a607a588abe36e6d0e7924ae90a31138a5391ae8a3a9a141c68
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-leonardoai (0.1.3)
5
+ faraday (>= 1)
6
+ faraday-multipart (>= 1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.5)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ dotenv (2.8.1)
19
+ faraday (2.7.10)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ json (2.6.3)
27
+ multipart-post (2.3.0)
28
+ parallel (1.23.0)
29
+ parser (3.2.2.4)
30
+ ast (~> 2.4.1)
31
+ racc
32
+ public_suffix (5.0.3)
33
+ racc (1.7.1)
34
+ rainbow (3.1.1)
35
+ rake (13.0.6)
36
+ regexp_parser (2.8.1)
37
+ rexml (3.2.6)
38
+ rspec (3.12.0)
39
+ rspec-core (~> 3.12.0)
40
+ rspec-expectations (~> 3.12.0)
41
+ rspec-mocks (~> 3.12.0)
42
+ rspec-core (3.12.2)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-expectations (3.12.3)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-mocks (3.12.6)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-support (3.12.1)
51
+ rubocop (1.50.2)
52
+ json (~> 2.3)
53
+ parallel (~> 1.10)
54
+ parser (>= 3.2.0.0)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8, < 3.0)
57
+ rexml (>= 3.2.5, < 4.0)
58
+ rubocop-ast (>= 1.28.0, < 2.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 2.4.0, < 3.0)
61
+ rubocop-ast (1.29.0)
62
+ parser (>= 3.2.1.0)
63
+ ruby-progressbar (1.13.0)
64
+ ruby2_keywords (0.0.5)
65
+ unicode-display_width (2.5.0)
66
+ vcr (6.1.0)
67
+ webmock (3.18.1)
68
+ addressable (>= 2.8.0)
69
+ crack (>= 0.3.2)
70
+ hashdiff (>= 0.4.0, < 2.0.0)
71
+
72
+ PLATFORMS
73
+ x86_64-darwin-19
74
+
75
+ DEPENDENCIES
76
+ byebug (~> 11.1.3)
77
+ dotenv (~> 2.8.1)
78
+ rake (~> 13.0)
79
+ rspec (~> 3.12)
80
+ rubocop (~> 1.50.2)
81
+ ruby-leonardoai!
82
+ vcr (~> 6.1.0)
83
+ webmock (~> 3.18.1)
84
+
85
+ BUNDLED WITH
86
+ 2.4.20
@@ -0,0 +1,31 @@
1
+ module LeonardoAI
2
+ class Client
3
+ include LeonardoAI::HTTP
4
+
5
+ CONFIG_KEYS = %i[
6
+ api_type
7
+ api_version
8
+ access_token
9
+ uri_base
10
+ request_timeout
11
+ extra_headers
12
+ ].freeze
13
+ attr_reader *CONFIG_KEYS
14
+
15
+ def initialize(config = {})
16
+ CONFIG_KEYS.each do |key|
17
+ # Set instance variables like api_type & access_token. Fall back to global config
18
+ # if not present.
19
+ instance_variable_set("@#{key}", config[key] || LeonardoAI.configuration.send(key))
20
+ end
21
+ end
22
+
23
+ def chat(parameters: {})
24
+ json_post(path: "/chat/completions", parameters: parameters)
25
+ end
26
+
27
+ def generations
28
+ @generations ||= LeonardoAI::Generations.new(client: self)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module LeonardoAI
3
+ VERSION = ::LeonardoAI::VERSION
4
+
5
+ Error = ::LeonardoAI::Error
6
+ ConfigurationError = ::LeonardoAI::ConfigurationError
7
+ Configuration = ::LeonardoAI::Configuration
8
+ end
9
+ end
@@ -5,11 +5,11 @@ module LeonardoAI
5
5
  end
6
6
 
7
7
  def generate(parameters: {})
8
- @client.json_post(path: "/images/generations", parameters: parameters)
8
+ @client.json_post(path: "/generations", parameters: parameters)
9
9
  end
10
10
 
11
11
  def edit(parameters: {})
12
- @client.multipart_post(path: "/images/edits", parameters: open_files(parameters))
12
+ @client.multipart_post(path: "/edits", parameters: open_files(parameters))
13
13
  end
14
14
 
15
15
  private
@@ -19,7 +19,5 @@ module LeonardoAI
19
19
  parameters = parameters.merge(mask: File.open(parameters[:mask])) if parameters[:mask]
20
20
  parameters
21
21
  end
22
-
23
- def
24
22
  end
25
23
  end
@@ -0,0 +1,73 @@
1
+ module LeonardoAI
2
+ module HTTP
3
+ def get(path:)
4
+ to_json(conn.get(uri(path: path)) do |req|
5
+ req.headers = headers
6
+ end&.body)
7
+ end
8
+
9
+ def json_post(path:, parameters:)
10
+ to_json(conn.post(uri(path: path)) do |req|
11
+ req.headers = headers
12
+ req.body = parameters.to_json
13
+ end&.body)
14
+ end
15
+
16
+ def multipart_post(path:, parameters: nil)
17
+ to_json(conn(multipart: true).post(uri(path: path)) do |req|
18
+ req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
19
+ req.body = multipart_parameters(parameters)
20
+ end&.body)
21
+ end
22
+
23
+ def delete(path:)
24
+ to_json(conn.delete(uri(path: path)) do |req|
25
+ req.headers = headers
26
+ end&.body)
27
+ end
28
+
29
+ private
30
+
31
+ def to_json(string)
32
+ return unless string
33
+
34
+ JSON.parse(string)
35
+ rescue JSON::ParserError
36
+ # Convert a multiline string of JSON objects to a JSON array.
37
+ JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
38
+ end
39
+
40
+ def conn(multipart: false)
41
+ Faraday.new do |f|
42
+ f.options[:timeout] = @request_timeout
43
+ f.request(:multipart) if multipart
44
+ end
45
+ end
46
+
47
+ def uri(path:)
48
+ File.join(@uri_base, @api_version, path)
49
+ end
50
+
51
+ def headers
52
+ leonardoai_headers.merge(@extra_headers || {})
53
+ end
54
+
55
+ def leonardoai_headers
56
+ {
57
+ "Content-Type" => "application/json",
58
+ "Authorization" => "Bearer #{@access_token}",
59
+ }
60
+ end
61
+
62
+ def multipart_parameters(parameters)
63
+ parameters&.transform_values do |value|
64
+ next value unless value.respond_to?(:close) # File or IO object.
65
+
66
+ # Doesn't seem like OpenAI needs mime_type yet, so not worth
67
+ # the library to figure this out. Hence the empty string
68
+ # as the second argument.
69
+ Faraday::UploadIO.new(value, "", value.path)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module LeonardoAI
2
+ VERSION = "0.1.3"
3
+ end
data/lib/leonardoai.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require "faraday"
2
2
  require "faraday/multipart"
3
+ require_relative "leonardoai/http"
4
+ require_relative "leonardoai/client"
5
+ require_relative "leonardoai/generations"
3
6
  require_relative "leonardoai/version"
4
7
 
5
-
6
- module Leonardoai
8
+ module LeonardoAI
7
9
  class Error < StandardError; end
8
10
  class ConfigurationError < Error; end
9
11
 
@@ -13,7 +15,7 @@ module Leonardoai
13
15
  :extra_headers
14
16
 
15
17
  DEFAULT_API_VERSION = "v1".freeze
16
- DEFAULT_URI_BASE = "https://cloud.leonardo.ai/api/rest/v1/".freeze
18
+ DEFAULT_URI_BASE = "https://cloud.leonardo.ai/api/rest/".freeze
17
19
  DEFAULT_REQUEST_TIMEOUT = 120
18
20
 
19
21
  def initialize
@@ -35,5 +37,17 @@ module Leonardoai
35
37
  raise ConfigurationError, error_text
36
38
  end
37
39
  end
40
+
41
+ class << self
42
+ attr_writer :configuration
43
+ end
44
+
45
+ def self.configuration
46
+ @configuration ||= LeonardoAI::Configuration.new
47
+ end
48
+
49
+ def self.configure
50
+ yield(configuration)
51
+ end
38
52
  end
39
53
 
@@ -0,0 +1,2 @@
1
+ require_relative "../leonardoai"
2
+ require_relative "../leonardoai/compatibility"
@@ -1,10 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/ruby/leonardoai/version"
1
+ require_relative "lib/leonardoai/version"
4
2
 
5
3
  Gem::Specification.new do |spec|
6
4
  spec.name = "ruby-leonardoai"
7
- spec.version = Ruby::Leonardoai::VERSION
5
+ spec.version = LeonardoAI::VERSION
8
6
  spec.authors = ["Donald Lee"]
9
7
  spec.email = ["donaldlee50@gmail.com"]
10
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-leonardoai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donald Lee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-07 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -49,14 +49,19 @@ files:
49
49
  - CHANGELOG.md
50
50
  - CODE_OF_CONDUCT.md
51
51
  - Gemfile
52
+ - Gemfile.lock
52
53
  - LICENSE.txt
53
54
  - README.md
54
55
  - Rakefile
55
56
  - bin/console
56
57
  - bin/setup
57
58
  - lib/leonardoai.rb
58
- - lib/ruby/leonardoai/generations.rb
59
- - lib/ruby/leonardoai/version.rb
59
+ - lib/leonardoai/client.rb
60
+ - lib/leonardoai/compatibility.rb
61
+ - lib/leonardoai/generations.rb
62
+ - lib/leonardoai/http.rb
63
+ - lib/leonardoai/version.rb
64
+ - lib/ruby/leonardoai.rb
60
65
  - ruby-leonardoai.gemspec
61
66
  - sig/ruby/leonardoai.rbs
62
67
  homepage: https://github.com/royalgiant/ruby-leonardoai
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Ruby
4
- module Leonardoai
5
- VERSION = "0.1.0"
6
- end
7
- end