├── .gitignore ├── .rspec ├── .rubocop.yml ├── CHANGELOG.md ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── exe └── openai_pipe ├── lib ├── openai_pipe.rb └── openai_pipe │ └── version.rb ├── openai_pipe.gemspec ├── sig └── openai_pipe.rbs └── spec ├── openai_pipe_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | NewCops: enable 3 | TargetRubyVersion: 3.2 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 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [Unreleased] 2 | 3 | ## [0.1.0] - 2022-12-23 4 | 5 | - Initial release 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in openai_pipe.gemspec 6 | gemspec 7 | 8 | gem "rake", "~> 13.0" 9 | 10 | gem "rspec", "~> 3.0" 11 | 12 | gem "rubocop", "~> 1.21" 13 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | openai_pipe (0.1.4) 5 | quick_openai (~> 0.3) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | addressable (2.8.7) 11 | public_suffix (>= 2.0.2, < 7.0) 12 | ast (2.4.3) 13 | diff-lcs (1.6.1) 14 | down (5.4.2) 15 | addressable (~> 2.8) 16 | event_stream_parser (1.0.0) 17 | faraday (2.13.0) 18 | faraday-net_http (>= 2.0, < 3.5) 19 | json 20 | logger 21 | faraday-multipart (1.1.0) 22 | multipart-post (~> 2.0) 23 | faraday-net_http (3.4.0) 24 | net-http (>= 0.5.0) 25 | json (2.10.2) 26 | language_server-protocol (3.17.0.4) 27 | lint_roller (1.1.0) 28 | logger (1.7.0) 29 | multipart-post (2.4.1) 30 | net-http (0.6.0) 31 | uri 32 | parallel (1.27.0) 33 | parser (3.3.8.0) 34 | ast (~> 2.4.1) 35 | racc 36 | prism (1.4.0) 37 | public_suffix (6.0.1) 38 | quick_openai (0.3.0) 39 | down (~> 5) 40 | ruby-openai (~> 8) 41 | racc (1.8.1) 42 | rainbow (3.1.1) 43 | rake (13.2.1) 44 | regexp_parser (2.10.0) 45 | rspec (3.13.0) 46 | rspec-core (~> 3.13.0) 47 | rspec-expectations (~> 3.13.0) 48 | rspec-mocks (~> 3.13.0) 49 | rspec-core (3.13.3) 50 | rspec-support (~> 3.13.0) 51 | rspec-expectations (3.13.3) 52 | diff-lcs (>= 1.2.0, < 2.0) 53 | rspec-support (~> 3.13.0) 54 | rspec-mocks (3.13.2) 55 | diff-lcs (>= 1.2.0, < 2.0) 56 | rspec-support (~> 3.13.0) 57 | rspec-support (3.13.2) 58 | rubocop (1.75.2) 59 | json (~> 2.3) 60 | language_server-protocol (~> 3.17.0.2) 61 | lint_roller (~> 1.1.0) 62 | parallel (~> 1.10) 63 | parser (>= 3.3.0.2) 64 | rainbow (>= 2.2.2, < 4.0) 65 | regexp_parser (>= 2.9.3, < 3.0) 66 | rubocop-ast (>= 1.44.0, < 2.0) 67 | ruby-progressbar (~> 1.7) 68 | unicode-display_width (>= 2.4.0, < 4.0) 69 | rubocop-ast (1.44.1) 70 | parser (>= 3.3.7.2) 71 | prism (~> 1.4) 72 | ruby-openai (8.1.0) 73 | event_stream_parser (>= 0.3.0, < 2.0.0) 74 | faraday (>= 1) 75 | faraday-multipart (>= 1) 76 | ruby-progressbar (1.13.0) 77 | unicode-display_width (3.1.4) 78 | unicode-emoji (~> 4.0, >= 4.0.4) 79 | unicode-emoji (4.0.4) 80 | uri (1.0.3) 81 | 82 | PLATFORMS 83 | x86_64-freebsd-13 84 | x86_64-linux 85 | 86 | DEPENDENCIES 87 | openai_pipe! 88 | rake (~> 13.0) 89 | rspec (~> 3.0) 90 | rubocop (~> 1.21) 91 | 92 | BUNDLED WITH 93 | 2.3.12 94 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenAIPipe 2 | 3 | This library provides a UNIX-ey interface to OpenAI. 4 | See [Installation](#installation) and [Setup](#setup) below, but first, some examples. 5 | 6 | ## Examples 7 | 8 | ```console 9 | $ ai what is two plus two 10 | Two plus two is equal to four. 11 | ``` 12 | 13 | ```console 14 | $ uptime | ai convert this to json 15 | { 16 | "time_of_measurement": "13:48:26", 17 | "up_time": "30 days, 18:07", 18 | "users": 3, 19 | "load_average": [ 20 | 0.46, 21 | 0.61, 22 | 0.79 23 | ] 24 | } 25 | ``` 26 | 27 | ```console 28 | $ ai list the nine planets as JSON | ai convert this to XML but in French | tee planets.fr.xml 29 | 30 | Mercure 31 | Vénus 32 | La Terre 33 | Mars 34 | Jupiter 35 | Saturne 36 | Uranus 37 | Neptune 38 | Pluton 39 | 40 | ``` 41 | 42 | ```console 43 | $ curl -sL "https://en.wikipedia.org/wiki/cats" | head -n 5 | ai extract just the title of this webpage | figlet 44 | ____ _ __ ___ _ _ _ _ 45 | / ___|__ _| |_ \ \ / (_) | _(_)_ __ ___ __| (_) __ _ 46 | | | / _` | __| _____ \ \ /\ / /| | |/ / | '_ \ / _ \/ _` | |/ _` | 47 | | |__| (_| | |_ |_____| \ V V / | | <| | |_) | __/ (_| | | (_| | 48 | \____\__,_|\__| \_/\_/ |_|_|\_\_| .__/ \___|\__,_|_|\__,_| 49 | |_| 50 | ``` 51 | 52 | ```console 53 | $ ls | ai What is this directory for? 54 | This directory contains the source code for a Ruby-based project called openai_pipe. It includes files related to the project's license (LICENSE.txt), changelog (CHANGELOG.md), dependencies (Gemfile and Gemfile.lock), executables (bin and exe), libraries (lib), signature (sig) and tests (spec). There is also a Rakefile and a README.md file which provide information about how to build and install the project, as well as its features and usage. Finally, it includes the openai_pipe-0.1.0.gem and openai_pipe.gemspec files which are used to build the gem which can be installed on other systems. 55 | ``` 56 | 57 | ```console 58 | $ ls -l | ai which of these are directories? 59 | bin, exe, lib, sig, spec 60 | ``` 61 | 62 | ```console 63 | $ ls | ai "For each of these files, provide a description of what is likely to be their contents?" 64 | bin - Likely contains compiled binary executable files. 65 | CHANGELOG.md - Likely contains a log of changes/modifications, such as bug fixes and new features, that have been made to the project. 66 | exe - Likely contains executable files. 67 | french_planets.xml - Likely contains an XML file containing data related to planets, likely in French. 68 | Gemfile - Likely contains Ruby code for the project's dependencies. 69 | Gemfile.lock - Likely contains a snapshot of the dependencies of the project and versions of those dependencies. 70 | lib - Likely contains the Ruby source code (e.g. classes and modules) for the project. 71 | LICENSE.txt - Likely contains the terms of use/license for the project. 72 | openai_pipe-0.1.0.gem - Likely contains a gem that gathers information from the OpenAI API. 73 | openai_pipe.gemspec - Likely contains configuration details for the gem. 74 | planets.lst - Likely contains a list of planets. 75 | poem.txt - Likely contains a text file containing a poem. 76 | Rakefile - Likely contains Ruby tasks and dependencies that can be used in projects. 77 | README.md - Likely contains general information about the project and usage instructions. 78 | reverse.lst - Likely contains a list of words or items that are in reverse order. 79 | sig - Likely contains digital signatures to validate individual files. 80 | spec - Likely contains Ruby specs (i.e. tests) for the project. 81 | uptime.json - Likely contains a file with information regarding system uptime of a computer. 82 | ``` 83 | 84 | ```console 85 | $ git commit -m "$(git status | ai write me a commit message for these changes)" 86 | [master 7d0271f] Add new files and modify README.md 87 | ``` 88 | 89 | ```console 90 | $ git status | tee /dev/tty | ai write me a sonnet about the status of this git repository 91 | On branch master 92 | Untracked files: 93 | (use "git add ..." to include in what will be committed) 94 | openai_pipe-0.1.0.gem 95 | 96 | nothing added to commit but untracked files present (use "git add" to track) 97 | 98 | My master branch may lack to thee its kin 99 | For change it holds the only force within 100 | Thé untracked files, they still remain unnamed 101 | ‘Tis fervent hope thé change will soon be claimed 102 | 103 | Fraught with the choice to leave or to persist 104 | The repository wavers ‘tween future and past 105 | The openai_pipe-0.1.0 gem stands out 106 | Waiting to be added, not yet about 107 | 108 | The commit awaits for brave new changes bold 109 | While time’s old force is ever unfurled 110 | Commit forth young mind, furrow not to crawl 111 | From untracked files, a future stands tall. 112 | ``` 113 | 114 | ```console 115 | % history | ai what was the last thing I did 116 | The last command you entered was 'history'. 117 | ``` 118 | n.b. somehow it sees history-esque output and determines that history was typed -- the history command does not itself include the history command in the output. 119 | 120 | ```console 121 | $ history | ai what was the last thing I did before typing history 122 | The last thing you did was amend a README.md file. 123 | ``` 124 | n.b. here it determines the amend was for README.md not from the previous command but from ones prior that edited README.md. 125 | 126 | ```console 127 | $ cat lib/openai_pipe/version.rb | ai rewrite this file with just the minor version incremented | sponge > lib/openai_pipe/version.rb 128 | $ git diff 129 | diff --git a/lib/openai_pipe/version.rb b/lib/openai_pipe/version.rb 130 | index 0f82357..cc57fab 100644 131 | --- a/lib/openai_pipe/version.rb 132 | +++ b/lib/openai_pipe/version.rb 133 | @@ -1,5 +1,5 @@ 134 | # frozen_string_literal: true 135 | 136 | module OpenAIPipe 137 | - VERSION = "0.1.0" 138 | + VERSION = "0.1.1" 139 | end 140 | ``` 141 | 142 | ```console 143 | $ ruby -e "$(ai write me a python script that prints the current month | ai translate this into ruby)" | ai translate this into French 144 | Le mois courant est Décembre. 145 | ``` 146 | 147 | ## Installation 148 | 149 | Install the gem by executing: 150 | 151 | $ gem install openai_pipe 152 | 153 | ## Setup 154 | 155 | This library uses [quick_openai](https://github.com/Aesthetikx/quick_openai) which itself uses [ruby-openai](https://github.com/alexrudall/ruby-openai), so you may want to familiarise yourself with those projects first. 156 | 157 | This library uses OpenAI GPT3 to generate responses, so you will need to have your access token available in ENV. In .bashrc or equivalent, 158 | ```bash 159 | export OPENAI_ACCESS_TOKEN=mytoken 160 | ``` 161 | 162 | By default the executable is called `openai_pipe`. It is reccommended to alias this command to something shorter in .bashrc or equivalent, e.g. 163 | ```bash 164 | alias ai="openai_pipe" 165 | ``` 166 | 167 | ## Notes 168 | 169 | Be aware that there is a cost associated every time GPT3 is invoked, so be mindful of your account usage. Also be wary of sending sensitive data to OpenAI, and also wary of arbitrarily executing scripts or programs that GPT3 generates. 170 | 171 | ## Development 172 | 173 | 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. 174 | 175 | 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). 176 | 177 | ## Contributing 178 | 179 | Bug reports and pull requests are welcome on GitHub at https://github.com/Aesthetikx/openai_pipe. 180 | 181 | ## License 182 | 183 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 184 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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] 13 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "openai_pipe" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require "irb" 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 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 9 | -------------------------------------------------------------------------------- /exe/openai_pipe: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "openai_pipe" 4 | 5 | OpenAIPipe.exe 6 | -------------------------------------------------------------------------------- /lib/openai_pipe.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "openai_pipe/version" 4 | 5 | require "quick_openai" 6 | 7 | module OpenAIPipe 8 | class Error < StandardError; end 9 | 10 | def self.exe 11 | input_from_pipe = $stdin.read if $stdin.stat.pipe? 12 | 13 | input_from_arguments = ARGV.join(" ") if ARGV.any? 14 | 15 | prompt = [input_from_arguments, input_from_pipe].compact.join("\n\n") 16 | 17 | puts prompt.gpt 18 | rescue QuickOpenAI::Error => e 19 | warn e.message 20 | exit 1 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /lib/openai_pipe/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module OpenAIPipe 4 | VERSION = "0.2.0" 5 | end 6 | -------------------------------------------------------------------------------- /openai_pipe.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/openai_pipe/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "openai_pipe" 7 | spec.version = OpenAIPipe::VERSION 8 | spec.authors = ["John DeSilva"] 9 | spec.email = ["desilvjo@umich.edu"] 10 | 11 | spec.summary = "A UNIX-ey interface to OpenAI" 12 | spec.description = "Provides pipe and redirect functionality to quick_openai" 13 | spec.homepage = "https://www.github.com/Aesthetikx/openai_pipe" 14 | spec.license = "MIT" 15 | spec.required_ruby_version = ">= 3.2" 16 | 17 | spec.metadata["homepage_uri"] = spec.homepage 18 | spec.metadata["source_code_uri"] = "https://www.github.com/Aesthetikx/openai_pipe" 19 | spec.metadata["changelog_uri"] = "https://www.github.com/Aesthetikx/openai_pipe/blob/master/CHANGELOG.md" 20 | spec.metadata["rubygems_mfa_required"] = "true" 21 | 22 | # Specify which files should be added to the gem when it is released. 23 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 24 | spec.files = Dir.chdir(__dir__) do 25 | `git ls-files -z`.split("\x0").reject do |f| 26 | (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) 27 | end 28 | end 29 | spec.bindir = "exe" 30 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 31 | spec.require_paths = ["lib"] 32 | 33 | spec.add_dependency "quick_openai", "~> 0.3" 34 | 35 | # For more information and examples about making a new gem, check out our 36 | # guide at: https://bundler.io/guides/creating_gem.html 37 | end 38 | -------------------------------------------------------------------------------- /sig/openai_pipe.rbs: -------------------------------------------------------------------------------- 1 | module OpenaiPipe 2 | VERSION: String 3 | # See the writing guide of rbs: https://github.com/ruby/rbs#guides 4 | end 5 | -------------------------------------------------------------------------------- /spec/openai_pipe_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | RSpec.describe OpenAIPipe do 4 | it "has a version number" do 5 | expect(OpenAIPipe::VERSION).not_to be nil 6 | end 7 | 8 | it "does something useful" do 9 | expect(true).to eq(true) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "openai_pipe" 4 | 5 | RSpec.configure do |config| 6 | # Enable flags like --only-failures and --next-failure 7 | config.example_status_persistence_file_path = ".rspec_status" 8 | 9 | # Disable RSpec exposing methods globally on `Module` and `main` 10 | config.disable_monkey_patching! 11 | 12 | config.expect_with :rspec do |c| 13 | c.syntax = :expect 14 | end 15 | end 16 | --------------------------------------------------------------------------------