quick_openai 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: c100b819268f04a4bf818f9b011a35d0fce11652398bdab7d837594eefa09ecb
4
+ data.tar.gz: 2b8d7e92373f869306c6c02e4194635b7676422a8362239df619f47ea9c8a7d5
5
+ SHA512:
6
+ metadata.gz: 63970fcd91f0802d14ce0fcdd62ae515f6d73d2076f0450c67131ea19b689466b06901d0b7e0473f9579aedc8da0fa8f0a03e0fdab01718e030ecd1701220ab4
7
+ data.tar.gz: e268e1471966d8adad75b3ea609df8faa562f5aadb5948ffa80ebec1c89ce6420b2c63c6e2911449e2dc0bc85560172c4e4a1df151c5690d56341de9a270011b
data/.env.test ADDED
@@ -0,0 +1 @@
1
+ OPENAI_ACCESS_TOKEN=fakeopenaiaccesstoken
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-14
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in quick_openai.gemspec
6
+ gemspec
7
+
8
+ gem "dotenv"
9
+
10
+ gem "rake", "~> 13.0"
11
+
12
+ gem "rspec", "~> 3.0"
13
+
14
+ gem "rubocop", "~> 1.21"
15
+
16
+ gem "vcr", "~> 6"
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ quick_openai (0.1.0)
5
+ down (~> 5)
6
+ ruby-openai (~> 2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.1)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ diff-lcs (1.5.0)
15
+ dotenv (2.8.1)
16
+ down (5.3.1)
17
+ addressable (~> 2.8)
18
+ httparty (0.20.0)
19
+ mime-types (~> 3.0)
20
+ multi_xml (>= 0.5.2)
21
+ json (2.6.3)
22
+ mime-types (3.4.1)
23
+ mime-types-data (~> 3.2015)
24
+ mime-types-data (3.2022.0105)
25
+ multi_xml (0.6.0)
26
+ parallel (1.22.1)
27
+ parser (3.1.3.0)
28
+ ast (~> 2.4.1)
29
+ public_suffix (5.0.1)
30
+ rainbow (3.1.1)
31
+ rake (13.0.6)
32
+ regexp_parser (2.6.1)
33
+ rexml (3.2.5)
34
+ rspec (3.12.0)
35
+ rspec-core (~> 3.12.0)
36
+ rspec-expectations (~> 3.12.0)
37
+ rspec-mocks (~> 3.12.0)
38
+ rspec-core (3.12.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-expectations (3.12.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.12.0)
43
+ rspec-mocks (3.12.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.12.0)
46
+ rspec-support (3.12.0)
47
+ rubocop (1.40.0)
48
+ json (~> 2.3)
49
+ parallel (~> 1.10)
50
+ parser (>= 3.1.2.1)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.8, < 3.0)
53
+ rexml (>= 3.2.5, < 4.0)
54
+ rubocop-ast (>= 1.23.0, < 2.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 3.0)
57
+ rubocop-ast (1.24.0)
58
+ parser (>= 3.1.1.0)
59
+ ruby-openai (2.1.0)
60
+ dotenv (>= 2.7.6, < 2.9.0)
61
+ httparty (>= 0.18.1, < 0.21.0)
62
+ ruby-progressbar (1.11.0)
63
+ unicode-display_width (2.3.0)
64
+ vcr (6.1.0)
65
+
66
+ PLATFORMS
67
+ x86_64-linux
68
+
69
+ DEPENDENCIES
70
+ dotenv
71
+ quick_openai!
72
+ rake (~> 13.0)
73
+ rspec (~> 3.0)
74
+ rubocop (~> 1.21)
75
+ vcr (~> 6)
76
+
77
+ BUNDLED WITH
78
+ 2.3.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 John DeSilva
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,68 @@
1
+ # QuickOpenAI
2
+
3
+ This gem is a wrapper of [ruby-openai](https://github.com/alexrudall/ruby-openai), providing quick convenience methods for experimenting with OpenAI language and image models.
4
+
5
+ ## Usage
6
+
7
+ Quickly get results from GPT3:
8
+
9
+ ```ruby
10
+ puts "Hello, what is your name?".gpt3
11
+ # => My name is Aileen.
12
+ ```
13
+
14
+ Quickly get images from DALLE2:
15
+
16
+ ```ruby
17
+ "a drawing of a cat in a hat".dalle2.then { |tempfile|
18
+ File.write('./cat.png', tempfile.read)
19
+ }
20
+ ```
21
+ <img src="https://raw.githubusercontent.com/Aesthetikx/quick_openai/master/.github/cat.png" height=256 width=256></img>
22
+
23
+ ## Installation
24
+
25
+ Install the gem and add to the application's Gemfile by executing:
26
+
27
+ $ bundle add quick_openai
28
+
29
+ If bundler is not being used to manage dependencies, install the gem by executing:
30
+
31
+ $ gem install quick_openai
32
+
33
+ ## Setup
34
+
35
+ Make sure you have your OpenAI access token in ENV, and then require QuickOpenAI to include String extensions.
36
+ ```ruby
37
+ ENV['OPENAI_ACCESS_TOKEN'] = '12345'
38
+
39
+ require 'quick_openai'
40
+ ```
41
+
42
+ ## Options
43
+
44
+ You can pass options supported by `ruby-openai` to the convenience methods:
45
+
46
+ ```ruby
47
+ "a drawing of a cat in a hat".dalle2(n: 2) # => An array of two tempfiles
48
+
49
+ "count to ten in italian".gpt3(model: 'text-ada-001') # Use a different model
50
+
51
+ "write me an essay on napoleon".gpt3(max_tokens: 2048) # Produce more or less output. The default is 2048.
52
+ ```
53
+
54
+ Read more about GPT3 models [here](https://beta.openai.com/docs/models/gpt-3).
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aesthetikx/quick_openai.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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 "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QuickOpenAI
4
+ module Extensions
5
+ module String
6
+ def gpt3(**options)
7
+ QuickOpenAI::Gpt3.gpt3(self, **options)
8
+ end
9
+
10
+ def dalle2(**options)
11
+ QuickOpenAI::Dalle2.dalle2(self, **options)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QuickOpenAI
4
+ module Gpt3
5
+ def self.gpt3(prompt, **options)
6
+ parameters = {
7
+ model: "text-davinci-003",
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QuickOpenAI
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "quick_openai/version"
4
+ require_relative "quick_openai/dalle2"
5
+ require_relative "quick_openai/gpt3"
6
+ require_relative "quick_openai/extensions/string"
7
+
8
+ module QuickOpenAI
9
+ class Error < StandardError; end
10
+
11
+ def self.client
12
+ OpenAI::Client.new
13
+ end
14
+
15
+ def self.fetch_response_from_client
16
+ begin
17
+ response = yield client
18
+ rescue StandardError
19
+ raise QuickOpenAI::Error, "Unable to fetch response."
20
+ end
21
+
22
+ if (error = response.dig("error", "message"))
23
+ raise QuickOpenAI::Error, "Unable to fetch response: #{error}"
24
+ end
25
+
26
+ response
27
+ end
28
+ end
29
+
30
+ require "down"
31
+ require "ruby/openai"
32
+ require "tempfile"
33
+
34
+ class String
35
+ include QuickOpenAI::Extensions::String
36
+ end
@@ -0,0 +1,4 @@
1
+ module QuickOpenai
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quick_openai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John DeSilva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: down
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-openai
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ description: Provides monkey patches for quick usage of GPT3 and DALLE2
42
+ email:
43
+ - desilvjo@umich.edu
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".env.test"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - CHANGELOG.md
52
+ - Gemfile
53
+ - Gemfile.lock
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/quick_openai.rb
58
+ - lib/quick_openai/dalle2.rb
59
+ - lib/quick_openai/extensions/string.rb
60
+ - lib/quick_openai/gpt3.rb
61
+ - lib/quick_openai/version.rb
62
+ - sig/quick_openai.rbs
63
+ homepage: https://github.com/Aesthetikx/quick_openai
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/Aesthetikx/quick_openai
68
+ source_code_uri: https://github.com/Aesthetikx/quick_openai
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.7.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.3.7
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Convenience extensions for using OpenAI with Ruby
88
+ test_files: []