quick_openai 0.1.3 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20a679365b256c1f19ce2f5809fc033dcfe3e99e0113c630556d5c5f87ca7988
4
- data.tar.gz: 7c197baa51a408238f1834cdd5c290bd3b39d918caf379d32ead233248bf2d99
3
+ metadata.gz: d0c8f01b890c1bac117c4ec75c84cc42cedec5f392ffb53427902da947f38dae
4
+ data.tar.gz: 7c099581a01de52fbebba1c57c3900f819d78ee38c93277dd8e370327bba3fcc
5
5
  SHA512:
6
- metadata.gz: 988a59c07722e607b52282074cc47fdd380033315afa9a66e14b3170c81ddd901946c87dc95c1a46e42781bbf0a25d05a7ca9c4688f100e23dbca103446e1c16
7
- data.tar.gz: 7af2b69216ea5210c9e3043d037b7c25150354118cbb453f7e975b1890a113b6f9b81273e2e4daa6b294332883f6c537bc2dd720cfa7126726269d95e59fb3c7
6
+ metadata.gz: 2911d2d5aa90500dbb47d5b9abcbf82baec63d9698beb5f631d29692ec5adb1988c480dfaae8f64da4fb455b3b2d8151b2b6fd99acf2fe7d99fc52784c01d712
7
+ data.tar.gz: 2d6c7a326874c9ede4e62f39c1055f701b2fc53b1daf1ed8cce57bb5f710279c00ca2de147e50ab114b0537d10125fcaf24e7abd2a32ed924f996748e76ebca2
data/.env.test CHANGED
@@ -1 +1 @@
1
- OPENAI_ACCESS_TOKEN=fakeopenaiaccesstoken
1
+ OPENAI_ACCESS_TOKEN=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
data/.rubocop.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  AllCops:
2
2
  NewCops: enable
3
- TargetRubyVersion: 2.7
3
+ TargetRubyVersion: 3.2
4
4
 
5
5
  Style/StringLiterals:
6
6
  Enabled: true
data/Gemfile.lock CHANGED
@@ -1,72 +1,92 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- quick_openai (0.1.3)
4
+ quick_openai (0.3.0)
5
5
  down (~> 5)
6
- ruby-openai (~> 3)
6
+ ruby-openai (~> 8)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- addressable (2.8.6)
12
- public_suffix (>= 2.0.2, < 6.0)
13
- ast (2.4.2)
14
- crack (0.4.5)
11
+ addressable (2.8.7)
12
+ public_suffix (>= 2.0.2, < 7.0)
13
+ ast (2.4.3)
14
+ base64 (0.2.0)
15
+ bigdecimal (3.1.9)
16
+ crack (1.0.0)
17
+ bigdecimal
15
18
  rexml
16
- diff-lcs (1.5.0)
17
- dotenv (2.8.1)
18
- down (5.4.1)
19
+ diff-lcs (1.6.1)
20
+ dotenv (3.1.8)
21
+ down (5.4.2)
19
22
  addressable (~> 2.8)
20
- hashdiff (1.1.0)
21
- httparty (0.21.0)
22
- mini_mime (>= 1.0.0)
23
- multi_xml (>= 0.5.2)
24
- json (2.7.1)
25
- language_server-protocol (3.17.0.3)
26
- mini_mime (1.1.5)
27
- multi_xml (0.6.0)
28
- parallel (1.24.0)
29
- parser (3.3.0.3)
23
+ event_stream_parser (1.0.0)
24
+ faraday (2.13.0)
25
+ faraday-net_http (>= 2.0, < 3.5)
26
+ json
27
+ logger
28
+ faraday-multipart (1.1.0)
29
+ multipart-post (~> 2.0)
30
+ faraday-net_http (3.4.0)
31
+ net-http (>= 0.5.0)
32
+ hashdiff (1.1.2)
33
+ json (2.10.2)
34
+ language_server-protocol (3.17.0.4)
35
+ lint_roller (1.1.0)
36
+ logger (1.7.0)
37
+ multipart-post (2.4.1)
38
+ net-http (0.6.0)
39
+ uri
40
+ parallel (1.27.0)
41
+ parser (3.3.8.0)
30
42
  ast (~> 2.4.1)
31
43
  racc
32
- public_suffix (5.0.4)
33
- racc (1.7.3)
44
+ prism (1.4.0)
45
+ public_suffix (6.0.1)
46
+ racc (1.8.1)
34
47
  rainbow (3.1.1)
35
- rake (13.1.0)
36
- regexp_parser (2.9.0)
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)
48
+ rake (13.2.1)
49
+ regexp_parser (2.10.0)
50
+ rexml (3.4.1)
51
+ rspec (3.13.0)
52
+ rspec-core (~> 3.13.0)
53
+ rspec-expectations (~> 3.13.0)
54
+ rspec-mocks (~> 3.13.0)
55
+ rspec-core (3.13.3)
56
+ rspec-support (~> 3.13.0)
57
+ rspec-expectations (3.13.3)
45
58
  diff-lcs (>= 1.2.0, < 2.0)
46
- rspec-support (~> 3.12.0)
47
- rspec-mocks (3.12.6)
59
+ rspec-support (~> 3.13.0)
60
+ rspec-mocks (3.13.2)
48
61
  diff-lcs (>= 1.2.0, < 2.0)
49
- rspec-support (~> 3.12.0)
50
- rspec-support (3.12.1)
51
- rubocop (1.59.0)
62
+ rspec-support (~> 3.13.0)
63
+ rspec-support (3.13.2)
64
+ rubocop (1.75.2)
52
65
  json (~> 2.3)
53
- language_server-protocol (>= 3.17.0)
66
+ language_server-protocol (~> 3.17.0.2)
67
+ lint_roller (~> 1.1.0)
54
68
  parallel (~> 1.10)
55
- parser (>= 3.2.2.4)
69
+ parser (>= 3.3.0.2)
56
70
  rainbow (>= 2.2.2, < 4.0)
57
- regexp_parser (>= 1.8, < 3.0)
58
- rexml (>= 3.2.5, < 4.0)
59
- rubocop-ast (>= 1.30.0, < 2.0)
71
+ regexp_parser (>= 2.9.3, < 3.0)
72
+ rubocop-ast (>= 1.44.0, < 2.0)
60
73
  ruby-progressbar (~> 1.7)
61
- unicode-display_width (>= 2.4.0, < 3.0)
62
- rubocop-ast (1.30.0)
63
- parser (>= 3.2.1.0)
64
- ruby-openai (3.7.0)
65
- httparty (>= 0.18.1)
74
+ unicode-display_width (>= 2.4.0, < 4.0)
75
+ rubocop-ast (1.44.1)
76
+ parser (>= 3.3.7.2)
77
+ prism (~> 1.4)
78
+ ruby-openai (8.1.0)
79
+ event_stream_parser (>= 0.3.0, < 2.0.0)
80
+ faraday (>= 1)
81
+ faraday-multipart (>= 1)
66
82
  ruby-progressbar (1.13.0)
67
- unicode-display_width (2.5.0)
68
- vcr (6.2.0)
69
- webmock (3.19.1)
83
+ unicode-display_width (3.1.4)
84
+ unicode-emoji (~> 4.0, >= 4.0.4)
85
+ unicode-emoji (4.0.4)
86
+ uri (1.0.3)
87
+ vcr (6.3.1)
88
+ base64
89
+ webmock (3.25.1)
70
90
  addressable (>= 2.8.0)
71
91
  crack (>= 0.3.2)
72
92
  hashdiff (>= 0.4.0, < 2.0.0)
data/README.md CHANGED
@@ -4,17 +4,17 @@ This gem is a wrapper of [ruby-openai](https://github.com/alexrudall/ruby-openai
4
4
 
5
5
  ## Usage
6
6
 
7
- Quickly get results from GPT3:
7
+ Quickly get results from GPT:
8
8
 
9
9
  ```ruby
10
- puts "Hello, what is your name?".gpt3
10
+ puts "Hello, what is your name?".gpt
11
11
  # => My name is Aileen.
12
12
  ```
13
13
 
14
- Quickly get images from DALLE2:
14
+ Quickly get images from DALLE:
15
15
 
16
16
  ```ruby
17
- "a drawing of a cat in a hat".dalle2.then { |tempfile|
17
+ "a drawing of a cat in a hat".dalle.then { |tempfile|
18
18
  File.write('./cat.png', tempfile.read)
19
19
  }
20
20
  ```
@@ -44,14 +44,14 @@ require 'quick_openai'
44
44
  You can pass options supported by `ruby-openai` to the convenience methods:
45
45
 
46
46
  ```ruby
47
- "a drawing of a cat in a hat".dalle2(n: 2) # => An array of two tempfiles
47
+ "a drawing of a cat in a hat".dalle(n: 2) # => An array of two tempfiles
48
48
 
49
- "count to ten in italian".gpt3(model: 'text-ada-001') # Use a different model
49
+ "count to ten in italian".gpt(model: 'gpt-4.5-preview') # Use a different model
50
50
 
51
- "write me an essay on napoleon".gpt3(max_tokens: 2048) # Produce more or less output. The default is 2048.
51
+ "write me an essay on napoleon".gpt(max_tokens: 2048) # Produce more or less output. The default is 2048.
52
52
  ```
53
53
 
54
- Read more about GPT3 models [here](https://beta.openai.com/docs/models/gpt-3).
54
+ Read more about GPT models [here](https://beta.openai.com/docs/models/gpt-3).
55
55
 
56
56
  ## Development
57
57
 
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QuickOpenAI
4
+ module Dalle
5
+ class << self
6
+ def dalle2(prompt, **)
7
+ generic_dalle(prompt, **)
8
+ end
9
+
10
+ def dalle3(prompt, **)
11
+ default_size = "1024x1024"
12
+
13
+ generic_dalle(prompt, size: default_size, **)
14
+ end
15
+
16
+ alias dalle dalle3
17
+
18
+ private
19
+
20
+ def generic_dalle(prompt, **options)
21
+ parameters = {
22
+ prompt:,
23
+ **options
24
+ }
25
+
26
+ response = QuickOpenAI.fetch_response_from_client do |client|
27
+ client.images.generate(parameters:)
28
+ end
29
+
30
+ results = response.fetch("data")
31
+
32
+ tempfiles = results.map do |result|
33
+ url = result["url"]
34
+
35
+ Down.download(url)
36
+ end
37
+
38
+ if tempfiles.size == 1
39
+ tempfiles.first
40
+ else
41
+ tempfiles
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -3,13 +3,17 @@
3
3
  module QuickOpenAI
4
4
  module Extensions
5
5
  module String
6
- def gpt3(**options)
7
- QuickOpenAI::Gpt3.gpt3(self, **options)
8
- end
6
+ def gpt(**) = QuickOpenAI::Gpt.gpt(self, **)
9
7
 
10
- def dalle2(**options)
11
- QuickOpenAI::Dalle2.dalle2(self, **options)
12
- end
8
+ def gpt3(**) = QuickOpenAI::Gpt.gpt3(self, **)
9
+
10
+ def gpt4(**) = QuickOpenAI::Gpt.gpt4(self, **)
11
+
12
+ def dalle(**) = QuickOpenAI::Dalle.dalle(self, **)
13
+
14
+ def dalle2(**) = QuickOpenAI::Dalle.dalle2(self, **)
15
+
16
+ def dalle3(**) = QuickOpenAI::Dalle.dalle3(self, **)
13
17
  end
14
18
  end
15
19
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QuickOpenAI
4
+ module Gpt
5
+ class << self
6
+ def gpt3(prompt, **)
7
+ model = "gpt-3.5-turbo"
8
+
9
+ generic_gpt(prompt, model:, **)
10
+ end
11
+
12
+ def gpt4(prompt, **)
13
+ model = "gpt-4.1"
14
+
15
+ generic_gpt(prompt, model:, **)
16
+ end
17
+
18
+ alias gpt gpt4
19
+
20
+ private
21
+
22
+ def generic_gpt(prompt, **options)
23
+ parameters = {
24
+ max_tokens: 2048,
25
+ messages: [{ role: "user", content: prompt }],
26
+ **options
27
+ }
28
+
29
+ response = QuickOpenAI.fetch_response_from_client do |client|
30
+ client.chat(parameters:)
31
+ end
32
+
33
+ text = response.dig("choices", 0, "message", "content")
34
+
35
+ raise QuickOpenAI::Error, "Unable to parse response." if text.nil? || text.empty?
36
+
37
+ text.chomp.strip
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QuickOpenAI
4
- VERSION = "0.1.3"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/quick_openai.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "quick_openai/version"
4
- require_relative "quick_openai/dalle2"
5
- require_relative "quick_openai/gpt3"
4
+ require_relative "quick_openai/dalle"
5
+ require_relative "quick_openai/gpt"
6
6
  require_relative "quick_openai/extensions/string"
7
7
 
8
8
  module QuickOpenAI
@@ -10,7 +10,7 @@ module QuickOpenAI
10
10
 
11
11
  class << self
12
12
  def client
13
- OpenAI::Client.new(access_token: ENV["OPENAI_ACCESS_TOKEN"])
13
+ OpenAI::Client.new(access_token:)
14
14
  end
15
15
 
16
16
  def fetch_response_from_client
@@ -40,6 +40,10 @@ module QuickOpenAI
40
40
 
41
41
  raise QuickOpenAI::Error, "Unable to fetch response: #{error}"
42
42
  end
43
+
44
+ def access_token
45
+ ENV.fetch("OPENAI_ACCESS_TOKEN")
46
+ end
43
47
  end
44
48
  end
45
49
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quick_openai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John DeSilva
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-01-13 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: down
@@ -30,14 +29,14 @@ dependencies:
30
29
  requirements:
31
30
  - - "~>"
32
31
  - !ruby/object:Gem::Version
33
- version: '3'
32
+ version: '8'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
- version: '3'
39
+ version: '8'
41
40
  description: Provides monkey patches for quick usage of GPT3 and DALLE2
42
41
  email:
43
42
  - desilvjo@umich.edu
@@ -55,18 +54,18 @@ files:
55
54
  - README.md
56
55
  - Rakefile
57
56
  - lib/quick_openai.rb
58
- - lib/quick_openai/dalle2.rb
57
+ - lib/quick_openai/dalle.rb
59
58
  - lib/quick_openai/extensions/string.rb
60
- - lib/quick_openai/gpt3.rb
59
+ - lib/quick_openai/gpt.rb
61
60
  - lib/quick_openai/version.rb
62
61
  - sig/quick_openai.rbs
63
62
  homepage: https://github.com/Aesthetikx/quick_openai
64
63
  licenses:
65
64
  - MIT
66
65
  metadata:
66
+ rubygems_mfa_required: 'true'
67
67
  homepage_uri: https://github.com/Aesthetikx/quick_openai
68
68
  source_code_uri: https://github.com/Aesthetikx/quick_openai
69
- post_install_message:
70
69
  rdoc_options: []
71
70
  require_paths:
72
71
  - lib
@@ -74,15 +73,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
73
  requirements:
75
74
  - - ">="
76
75
  - !ruby/object:Gem::Version
77
- version: 2.7.0
76
+ version: '3.2'
78
77
  required_rubygems_version: !ruby/object:Gem::Requirement
79
78
  requirements:
80
79
  - - ">="
81
80
  - !ruby/object:Gem::Version
82
81
  version: '0'
83
82
  requirements: []
84
- rubygems_version: 3.4.10
85
- signing_key:
83
+ rubygems_version: 3.6.7
86
84
  specification_version: 4
87
85
  summary: Convenience extensions for using OpenAI with Ruby
88
86
  test_files: []
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module QuickOpenAI
4
- module Dalle2
5
- def self.dalle2(prompt, **options)
6
- parameters = {
7
- prompt: prompt,
8
- **options
9
- }
10
-
11
- response = QuickOpenAI.fetch_response_from_client do |client|
12
- client.images.generate(parameters: parameters)
13
- end
14
-
15
- results = response.fetch("data")
16
-
17
- tempfiles = results.map do |result|
18
- url = result["url"]
19
-
20
- Down.download(url)
21
- end
22
-
23
- if tempfiles.size == 1
24
- tempfiles.first
25
- else
26
- tempfiles
27
- end
28
- end
29
- end
30
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module QuickOpenAI
4
- module Gpt3
5
- def self.gpt3(prompt, **options)
6
- parameters = {
7
- model: "gpt-3.5-turbo-instruct",
8
- max_tokens: 2048,
9
- prompt: prompt,
10
- **options
11
- }
12
-
13
- response = QuickOpenAI.fetch_response_from_client do |client|
14
- client.completions(parameters: parameters)
15
- end
16
-
17
- text = response.dig("choices", 0, "text")
18
-
19
- raise QuickOpenAI::Error, "Unable to parse response." if text.nil? || text.empty?
20
-
21
- text.chomp.strip
22
- end
23
- end
24
- end