ruby-openai 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 61d4334878bbc6975011c4a27e0b7e9670d4b0abe37ac02dcd9059e2c98d3ecb
4
+ data.tar.gz: db0896a130eb8931d4847127f1b165e234706c9d402728da89c326b3702ac919
5
+ SHA512:
6
+ metadata.gz: f2d419f61441f4afb4d2aaaeb9d11b14d7ac0db905bc6f886d9069c3b9575a59657586b9a0094cf2f99c9bf1390d46a663cf4b19ea85aca89a91e78ccc59053b
7
+ data.tar.gz: 6b539d35aab4aee91b8ecdd2055f26c2854435589bfd6b8665689b58983eb7bfadd0e62ff425cc1d73b4d5ef15451dcf3d41e29f00bb289a4c42937c30b04855
@@ -0,0 +1,38 @@
1
+ version: 2.1 # Use 2.1 to enable using orbs and other features.
2
+
3
+ # Declare the orbs that we'll use in our config.
4
+ # read more about orbs: https://circleci.com/docs/2.0/using-orbs/
5
+ orbs:
6
+ ruby: circleci/ruby@1.0
7
+
8
+ jobs:
9
+ build: # our first job, named "build"
10
+ docker:
11
+ - image: cimg/ruby:2.7-node # use a tailored CircleCI docker image.
12
+ steps:
13
+ - checkout # pull down our git code.
14
+ - ruby/install-deps # use the ruby orb to install dependencies
15
+
16
+ test: # our next job, called "test"
17
+ # we can run "parallel job containers" to enable speeding up our tests;
18
+ # this splits our tests across multiple containers.
19
+ parallelism: 1
20
+ docker:
21
+ - image: cimg/ruby:2.7-node # this is our primary docker image, where step commands run.
22
+ # A series of steps to run, some are similar to those in "build".
23
+ steps:
24
+ - checkout
25
+ - ruby/install-deps
26
+ - run:
27
+ name: Run tests
28
+ command: bundle exec rspec -fd
29
+
30
+ # We use workflows to orchestrate the jobs that we declared above.
31
+ workflows:
32
+ version: 2
33
+ build_and_test: # The name of our workflow is "build_and_test"
34
+ jobs: # The list of jobs we run as part of this workflow.
35
+ - build # Run build first.
36
+ - test: # Then run test,
37
+ requires: # Test requires that build passes for it to run.
38
+ - build # Finally, run the build job.
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
14
+ .env
15
+
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ NewCops: enable
5
+
6
+ Style/Documentation:
7
+ # Skips checking to make sure top level modules / classes have a comment.
8
+ Enabled: false
9
+
10
+ Layout/LineLength:
11
+ Max: 100
12
+
13
+ Metrics/BlockLength:
14
+ Exclude:
15
+ - "Rakefile"
16
+ - "**/*.rake"
17
+ - "spec/**/*.rb"
18
+
19
+ Style/StringLiterals:
20
+ EnforcedStyle: double_quotes
21
+
22
+ Style/FrozenStringLiteralComment:
23
+ Enabled: false
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2020-09-06
11
+
12
+ ### Added
13
+
14
+ - Initialise repository.
15
+ - Add OpenAI::Client to connect to OpenAI API using user credentials.
16
+ - Add spec for Client with a cached response using VCR.
17
+ - Add CircleCI to run the specs on pull requests.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at alexrudall@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Include gem dependencies from ruby-openai.gemspec
4
+ gemspec
5
+
6
+ gem "byebug", "~> 11.1.3"
7
+ gem "rake", "~> 13.0"
8
+ gem "rspec", "~> 3.0"
9
+ gem "rubocop", "~> 0.90.0"
10
+ gem "vcr", "~> 6.0.0"
11
+ gem "webmock", "~> 3.8.3"
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-openai (0.1.0)
5
+ dotenv (~> 2.7.6)
6
+ httparty (~> 0.18.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.1)
14
+ byebug (11.1.3)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.4.4)
18
+ dotenv (2.7.6)
19
+ hashdiff (1.0.1)
20
+ httparty (0.18.1)
21
+ mime-types (~> 3.0)
22
+ multi_xml (>= 0.5.2)
23
+ mime-types (3.3.1)
24
+ mime-types-data (~> 3.2015)
25
+ mime-types-data (3.2020.0512)
26
+ multi_xml (0.6.0)
27
+ parallel (1.19.2)
28
+ parser (2.7.1.4)
29
+ ast (~> 2.4.1)
30
+ public_suffix (4.0.5)
31
+ rainbow (3.0.0)
32
+ rake (13.0.1)
33
+ regexp_parser (1.7.1)
34
+ rexml (3.2.4)
35
+ rspec (3.9.0)
36
+ rspec-core (~> 3.9.0)
37
+ rspec-expectations (~> 3.9.0)
38
+ rspec-mocks (~> 3.9.0)
39
+ rspec-core (3.9.2)
40
+ rspec-support (~> 3.9.3)
41
+ rspec-expectations (3.9.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-mocks (3.9.1)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.9.0)
47
+ rspec-support (3.9.3)
48
+ rubocop (0.90.0)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.7.1.1)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.7)
53
+ rexml
54
+ rubocop-ast (>= 0.3.0, < 1.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 2.0)
57
+ rubocop-ast (0.3.0)
58
+ parser (>= 2.7.1.4)
59
+ ruby-progressbar (1.10.1)
60
+ safe_yaml (1.0.5)
61
+ unicode-display_width (1.7.0)
62
+ vcr (6.0.0)
63
+ webmock (3.8.3)
64
+ addressable (>= 2.3.6)
65
+ crack (>= 0.3.2)
66
+ hashdiff (>= 0.4.0, < 2.0.0)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ byebug (~> 11.1.3)
73
+ rake (~> 13.0)
74
+ rspec (~> 3.0)
75
+ rubocop (~> 0.90.0)
76
+ ruby-openai!
77
+ vcr (~> 6.0.0)
78
+ webmock (~> 3.8.3)
79
+
80
+ BUNDLED WITH
81
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alex
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.
@@ -0,0 +1,63 @@
1
+ # Ruby::OpenAI
2
+
3
+ [![CircleCI Build Status](https://circleci.com/gh/alexrudall/ruby-openai.svg?style=shield)](https://circleci.com/gh/alexrudall/ruby-openai)
4
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/ruby-openai/blob/master/LICENSE.txt)
5
+
6
+ A simple Ruby wrapper for the [OpenAI GPT-3 API](https://openai.com/blog/openai-api/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ruby-openai'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ruby-openai
23
+
24
+ ## Usage
25
+
26
+ Get your API key from [https://beta.openai.com/docs/developer-quickstart/your-api-keys](https://beta.openai.com/docs/developer-quickstart/your-api-keys)
27
+
28
+ Add your secret key to your .env file:
29
+
30
+ ```
31
+ OPENAI_ACCESS_TOKEN=secretkeygoeshere
32
+ ```
33
+
34
+ Create a client:
35
+
36
+ ```
37
+ client = OpenAI::Client.new
38
+ ```
39
+
40
+ Use it to hit the OpenAI API for a completion:
41
+
42
+ ```
43
+ response = client.call(engine: "davinci", prompt: "Once upon a time", max_tokens: 5)
44
+ response.parsed_response['choices'].map{ |c| c["text"] }
45
+ ```
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/ruby-openai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/master/CODE_OF_CONDUCT.md).
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
60
+
61
+ ## Code of Conduct
62
+
63
+ Everyone interacting in the Ruby::OpenAI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/alexrudall/ruby-openai/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby/openai"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require "httparty"
2
+ require "ruby/openai/client"
3
+ require "ruby/openai/version"
4
+ require "dotenv/load"
5
+
6
+ module Ruby
7
+ module OpenAI
8
+ class Error < StandardError; end
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ module OpenAI
2
+ class Client
3
+ include HTTParty
4
+ base_uri "https://api.openai.com/v1/engines"
5
+
6
+ def initialize(access_token: nil)
7
+ @access_token = access_token || ENV["OPENAI_ACCESS_TOKEN"]
8
+ end
9
+
10
+ def call(engine:, prompt:, max_tokens:)
11
+ self.class.post(
12
+ "/#{engine}/completions",
13
+ headers: {
14
+ "Content-Type" => "application/json",
15
+ "Authorization" => "Bearer #{@access_token}"
16
+ },
17
+ body: {
18
+ prompt: prompt, max_tokens: max_tokens
19
+ }.to_json
20
+ )
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Ruby
2
+ module OpenAI
3
+ VERSION = "0.1.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "lib/ruby/openai/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ruby-openai"
5
+ spec.version = Ruby::OpenAI::VERSION
6
+ spec.authors = ["Alex"]
7
+ spec.email = ["alexrudall@users.noreply.github.com"]
8
+
9
+ spec.summary = "A Ruby gem for the OpenAI GPT-3 API"
10
+ spec.homepage = "https://github.com/alexrudall/ruby-openai"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/alexrudall/ruby-openai"
16
+ spec.metadata["changelog_uri"] = "https://github.com/alexrudall/ruby-openai/blob/master/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "dotenv", "~> 2.7.6"
28
+ spec.add_dependency "httparty", "~> 0.18.1"
29
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-openai
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.18.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.18.1
41
+ description:
42
+ email:
43
+ - alexrudall@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - ".rubocop.yml"
52
+ - ".travis.yml"
53
+ - CHANGELOG.md
54
+ - CODE_OF_CONDUCT.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - bin/console
61
+ - bin/setup
62
+ - lib/ruby/openai.rb
63
+ - lib/ruby/openai/client.rb
64
+ - lib/ruby/openai/version.rb
65
+ - ruby-openai.gemspec
66
+ homepage: https://github.com/alexrudall/ruby-openai
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/alexrudall/ruby-openai
71
+ source_code_uri: https://github.com/alexrudall/ruby-openai
72
+ changelog_uri: https://github.com/alexrudall/ruby-openai/blob/master/CHANGELOG.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.3.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.1.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A Ruby gem for the OpenAI GPT-3 API
92
+ test_files: []