├── LICENSE ├── README.md ├── gpt ├── gpti └── images ├── example1.png └── fenway.png /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gpt-bash-cli 2 | 3 | An extremely simple interface to the openai API, written in bash. Here's an example usage: 4 | 5 | **status**: this command works, but I've transitioned to using [simonw/llm](https://github.com/simonw/llm) which supports other models and functions similarly to this command. I will consider PRs if they're filed but it's not under active development 6 | 7 | ```` 8 | $ gpt how do I output red text in the console using tput 9 | To output red text in the console using tput, you can use the following command: 10 | 11 | ``` 12 | tput setaf 1; echo "This text is in red"; tput sgr0 13 | ``` 14 | 15 | Explanation: 16 | 17 | - `tput setaf 1` sets the foreground color to red (1 is the code for red) 18 | - `echo "This text is in red"` outputs the message "This text is in red" in the console 19 | - `tput sgr0` resets the console color to its default state 20 | 21 | You can replace "This text is in red" with any message you want to output in red. 22 | ```` 23 | 24 | the `gpt` command stores your chat log in a sqlite database, and will use your 25 | most recent messages and openai's responses as context, so you can have a 26 | chat-like interface just by calling it repeatedly. 27 | 28 | Here's an example; first we ask gpt for a fibonacci-generating function, then 29 | we ask it to rewrite the previous answer in a recursive style: 30 | 31 | ```` 32 | $ gpt write a function in python that outputs fibonacci numbers 33 | Sure, here's an example of a Python function that outputs Fibonacci numbers: 34 | 35 | ```python 36 | def fibonacci(n): 37 | """Generate the first n Fibonacci numbers.""" 38 | fib = [0, 1] # initialize the sequence with the first two numbers 39 | for i in range(2, n): 40 | fib.append(fib[i-1] + fib[i-2]) # add the previous two numbers to generate the next one 41 | return fib 42 | ``` 43 | 44 | This function takes the number `n` as an argument and returns a list of the first `n` Fibonacci numbers. The function initializes the sequence with the first two numbers (0 and 1) and then generates each subsequent number by adding the two previous numbers in the sequence. The resulting list of Fibonacci numbers is returned. 45 | 46 | You can call this function with an argument `n` to generate the first `n` Fibonacci numbers like this: 47 | 48 | ```python 49 | fib = fibonacci(10) 50 | print(fib) # output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 51 | ``` 52 | 53 | This will generate the first 10 Fibonacci numbers and print them to the console. 54 | 55 | $ gpt now rewrite that function to be recursive 56 | Sure, here's an example of a recursive function that generates Fibonacci numbers in Python: 57 | 58 | ```python 59 | def fibonacci(n): 60 | """Generate the n-th Fibonacci number.""" 61 | if n <= 1: 62 | return n 63 | else: 64 | return fibonacci(n-1) + fibonacci(n-2) 65 | ``` 66 | 67 | This function takes the number `n` as an argument and returns the `n`-th Fibonacci number. If `n` is less than or equal to 1, the function simply returns `n`. Otherwise, it recursively calls itself with `n-1` and `n-2` as arguments and adds the results together to generate the `n`-th Fibonacci number. 68 | 69 | You can call this function with an argument `n` to generate the `n`-th Fibonacci number like this: 70 | 71 | ```python 72 | fib = fibonacci(10) 73 | print(fib) # output: 55 74 | ``` 75 | 76 | This will generate the 10th Fibonacci number (which is 55) and print it to the console. 77 | ```` 78 | 79 | Because we send the previous chat log to gpt as context, it knows what we mean 80 | by "that function" in the second command. 81 | 82 | ## Stdin 83 | 84 | If you pipe data to gpt, it will be appended to the end of your query. This lets 85 | you do something like this: 86 | 87 | ``` 88 | $ git diff | gpt write a commit message for this diff 89 | ``` 90 | 91 | or: 92 | 93 | ``` 94 | $ ls *.png | gpt give me an imagemagick command that will concatenate these images vertically: 95 | ``` 96 | 97 | ## Quotes 98 | 99 | `gpt` allows you to call it without quoting your arguments, but if you want to 100 | include a quote character in your queries that won't work. In that case, you 101 | can quote your argument to it and it will work as expected: 102 | 103 | ``` 104 | $ gpt "tell me why bash's quoting rules are the best" 105 | ``` 106 | 107 | will work, but: 108 | 109 | ``` 110 | $ gpt tell me why bash's quoting rules are the best 111 | > 112 | ``` 113 | 114 | will leave bash waiting for you to finish your quoted string. 115 | 116 | ## Prerequisites 117 | 118 | These scripts assume you have [`curl`](https://curl.se) and 119 | [`jq`](https://stedolan.github.io/jq/) available. 120 | 121 | ## OpenAI API keys 122 | 123 | You will need an OpenAI api key. Get one from 124 | https://platform.openai.com/account/api-keys 125 | 126 | On its first run, the script will ask you for your key, and attempt to store it 127 | in your system's key store. 128 | 129 | - On a mac, you shouldn't need to install anything. The script will use 130 | `security` to save the key to your system keychain 131 | - On linux and windows, you should install 132 | [keyring](https://github.com/jaraco/keyring#installation---linux) 133 | 134 | ## Installation 135 | 136 | There are two scripts in this repository, `gpt` and `gpti`. To install them, 137 | copy them to somewhere on your path and make sure they're executable (with 138 | `chmod a+x gpt`, for example) 139 | 140 | **NOTE**: mac systems have a `gpt` binary installed in `/usr/sbin` by default. 141 | Either put the directory containing `gpt` in a path that gets searched before 142 | `/usr/sbin`, or give it another name 143 | 144 | ## Storage 145 | 146 | Each script stores all requests and responses in a sqlite database, located 147 | in your `XDG_DATA_HOME` directory, which defaults to `$HOME/.local/share`. 148 | 149 | For most people the database will be located at 150 | `~/.local/share/gpt-bash/openai.sqlite3` 151 | 152 | ## Usage 153 | 154 | Each script has help output documenting all options. 155 | 156 | ### gpt 157 | 158 | ``` 159 | gpt [-hpv] [-nc] [-m ] [-t ] 160 | 161 | chat with openai's /chat/completions endpoint 162 | 163 | FLAGS: 164 | 165 | -h, --help: print this help and exit 166 | -m, --model: set the model you want to use. Defaults to $MODEL 167 | -nc, --no-context: do not send recent message context to openai 168 | -p, --api-key: print the openai API key found by this script and exit 169 | -t, --temperature: set the temperature. Defaults to $TEMPERATURE 170 | -v, --verbose: print verbose debugging information 171 | 172 | STORAGE: 173 | 174 | This script will store all requests to and responses from openai in a 175 | sqlite database in $DATA_DIR, in the "streaming_chat_completions" table 176 | 177 | EXAMPLE USAGE: 178 | 179 | you don't need to quote the arguments to gpt: 180 | 181 | gpt write a bash script that uses curl to access the openai API 182 | 183 | you can pipe input into it, and it will be appended to the end of your 184 | query: 185 | 186 | git diff | gpt -nc write a commit message for the following diff: 187 | 188 | if you have quotes in your query, you may need to quote it: 189 | 190 | gpt "tell me why bash's quote handling is great" 191 | ``` 192 | 193 | ### gpti 194 | 195 | ``` 196 | gpti [-vhp] 197 | 198 | generates an image via openai's /images/generations endpoint 199 | 200 | FLAGS: 201 | 202 | -h, --help: print this help and exit 203 | -p, --api-key: print the openai API key found by this script and exit 204 | -v, --verbose: print the URL of the image and the filename when done 205 | 206 | EXAMPLE USAGE: 207 | 208 | gpti a drone photo of fenway park on opening day 209 | 210 | STORAGE: 211 | 212 | This script will store all requests to and responses from openai in a 213 | sqlite database in $DATA_DIR, in the "images_generations" table 214 | 215 | NOTE: 216 | 217 | All images are downloaded into your temp directory and the filename 218 | will begin with `gpti__