ChatGPT interactive command line REPL, using the OpenAI API for per usage billing.
An experiment inspired by two tweets.
- Bring your own API key
- Shell like REPL with history access and commands
- Stream responses
- Controllable conversation context
- Clipboard copying
- Local API usage tracking
- Access input history from previous sessions
See the current roadmap for plans and ideas, including issues with a help wanted label. Feel free to open an issue if you encounter any bugs or have suggestions.
Use your favourite package manager to install the chatgpt-repl command globally. With npm:
npm install -g chatgpt-replOnce installed:
- Get an OpenAI API key.
- Run
chatgpt-repl. - Provided API key when asked.
- Enter a ChatGPT prompt ('Should Pluto be a planet?') and hit enter.
- See available commands with
.help. - Hit Ctrl-C or enter
.exitto end the session when sufficiently filled with knowledge. - Use the up/down arrows to access previously entered prompts or commands.
Responses are streamed in by default. This can be disabled with the .stream false command or the --stream false CLI option. Streamed responses currently don't include token usage from the API and not included in usage tracking (issue to estimate usage).
Cancel a request in progress with Ctrl-C at any time, even when streaming.
REPL input is saved into a history file (~/.chatgpt-repl-history or based onCHATGPTREPL_HISTORYFILE env). This can be disabled with the --history false CLI option. The last 100 (or CHATGPTREPL_HISTORYSIZE) history entries are loaded on startup.
Currently, the default model is gpt-3.5-turbo. Use the .models command to see the list of supported models.
Supported models are retrieved from the OpenAI API and cached locally for 3 days. Run .models update to force an update.
GPT-4 was announced on March 14th, 2023 and API support for it started out with a waitlist. If it's available to you, provide an alternate model name via --model gpt-4 (or the .model gpt-4 command).
Note however that GPT-4's pricing appears to be significantly higher than GPT-3's. The current API usage costs shown by this tool is based on GPT-3's pricing (issue for model specific pricing).
-
-v, --versionShow version number -
-h, --helpShow help -
-c, --clipboard <boolean>Enable/disable copying the latest response to the clipboard as it is shown (default: 'false' orCHATGPTREPL_CLIPBOARDenv) -
-k, --api-key <key>Set (and save) OpenAI API key (CHATGPTREPL_APIKEYenv) -
-l, --conversation-lengthSet conversation history length (default: 3 orCHATGPTREPL_CONVERSATIONLENGTHenv) -
--history-sizeSet history size (default: 100 orCHATGPTREPL_HISTORYSIZEenv) -
--history-fileHistory file location (default:~/.chatgpt-repl.historyorCHATGPTREPL_HISTORYFILEenv) -
-m, --modelSet the OpenAI model (default: gpt-3.5-turbo orCHATGPTREPL_MODELenv) -
-t, --temperatureSet the temperature for more 'random' responses (default: 1 orCHATGPTREPL_TEMPERATUREenv) -
-s, --systemSet the system to set the tone of the response (default: 'You are a helpful assistant' orCHATGPTREPL_SYSTEMenv) -
-r, --streamEnable/disable streamed responses (default: 'true' orCHATGPTREPL_STREAMenv) -
-w, --word-wrap <boolean>Enable/disable automatic word wrapping in response output (default: 'true' orCHATGPTREPL_WORDWRAPenv) -
-x, --max-tokens <num>Set the max tokens to use and control costs (default: 1024 orCHATGPTREPL_MAXTOKENSenv)
Defaults can be overridden with environment variables where indicated ('env').
OpenAI API usage is paid after a free trial, but extremely cheap. 1,000 tokens (currently) cost $0.002 for the gpt-3.5-turbo model used by this tool by default. Each word in a question and response uses at least 1 token.
By default, the last 5 prompts/responses in a session are used in addition to a new prompt, to provide ChatGPT with additional context. This allows for follow-up prompts that reference a previous response, but also increases costs by using more API tokens.
The -l <num> option (or CHATGPTREPL_CONVERSATIONLENGTH environment variable or .conversationlength command) can be used to change this conversation length, by indicating how much of an earlier conversation to reuse. So -l 0 would not send any previous conversation context back and -l 1 would only use the most recent prompt/response for context.
Thus with a history length of zero, you couldn't ask 'What is the Sun?' and later 'How far away from the Earth is it?', since it would have no frame of reference for 'it'.
Enhancements to conversation tracking are planned.
Instead of entering a prompt, you can also use a number of commands to modify settings or perform other actions. Use .help for the full list:
.settingsshows all the current settings.resetresets one or all settings to when you launched the current session.model,.temperature,.maxtokens,.conversationlengthor.systemfollowed by a value will change a setting, or show it without a value.models [update]shows/force updates the list of supported models.retryreruns the last prompt with the latest settings.lastshows the last response again without running it through ChatGPT again (applies new word wrap/clipboard settings).clearclears the conversation history.copycopies the last response to a prompt to your system clipboard.clipboardenabled/disabled clipboard copying for every response (off by default).wordwrapenables/disables word wrapping for the response output (on by default).historyenables/disables input logging to history file (on by default).streamenables/disables streamed responses (on by default).usageshows current and total API usage.exitends the current session
Values to set boolean fields can include true, false, 1, 0, on, off etc.
Hitting tab will attempt to complete a command.
When asked 'What are the benefits of a ChatGPT command line interface?', it wholeheartedly endorsed this tool:
There are several benefits of ChatGPT command line interface, including:
Faster and More Efficient Communication: With command line interface, users can communicate with the ChatGPT model much faster and efficiently than with a graphical interface.
Improved Productivity: With command line interface, users can execute multiple commands in quick succession, which can help increase productivity.
Ease of Use: Command line interface is generally easier to use for users who are familiar with the command line interface. The syntax, while it may look complicated for beginners, is generally quicker than using a graphical interface.
Customizability: By using a command line interface, users can customize their experience with ChatGPT based on their needs and preferences.
Script Automation: ChatGPT command line interface can be used to create scripts that can be used to automate certain tasks, which can help save time and increase productivity.
Some of the libraries used:
- clack: some of the prompt UI
- commander: command line options
- OpenAI Node.js library: ChatGPT API responses
- streamed-chatgpt-api: streaming ChatGPT responses (currently using fork to cancel requests)
- node-clipboardy: copy responses to the system clipboard
- zod: type safe validation
- tsx: local TypeScript development
REPL prompt plus history uses the built-in Node readline API.
