Access ChatGPT from Command Line

Duane May
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨
6 min readJul 9, 2023

--

The ChatGPT Command Line Interface (CLI) allows for direct interaction with ChatGPT from your terminal. You now have the power of ChatGPT on your command line, to answer your queries, or edit your files. Look at what you can do!

Interactive Mode

The interactive mode allows direct chat with ChatGPT, much like using the web interface, you can even continue a session at a later date.

chatgpt-cli in action

Let’s walk through this example step by step. Here we start a session by typing chatgpt-cli chat then we ask ChatGPT to translate the word Hello into Japanese. You can see it responds こんにちは!

(✨Non-subscribers continue reading here)

$ chatgpt-cli chat
ChatGPT CLI v1.8.0
model: gpt-4, role: user, temp: 1.0, maxtok: 0, topp: 1.0
- Press TAB after entering a message to send.
- Press TAB or CTRL+C with a blank message to terminate the session without sending.
to continue with this session, use: --session-file chatgpt-cli-2023-07-09T06:50:02Z.json
Enter Message [Press tab to submit] :
say Hello in Japanese
ChatGPT response:
こんにちは
Enter Message [Press tab to submit] :
No Message to Send, exiting...

By default, the CLI uses the gpt-4 model. This, along with other defaults, can be altered by setting the CHATGPT_MODEL environment variable or using the --model flag.

The CLI automatically saves your session history, offering the ability to continue the session later using the --session-file flag.

Here we continue the session that we started earlier, by providing the session file we were given on the last command. This time we just say “and in Korean” note we did not tell it what to translate this time, but it responded correctly 안녕하세요.

$ chatgpt-cli chat --session-file chatgpt-cli-2023-07-09T06:50:02Z.json
...
Enter Message [Press tab to submit] :
and in Korean
ChatGPT response:
안녕하세요
Enter Message [Press tab to submit] :
No Message to Send, exiting...

Non-Interactive Mode

While interactive mode is powerful in itself, the CLI’s non-interactive mode is where great power lies. You can add the power of ChatGPT to your shell scripts. You can pass it commands from a file or standard input.

Let’s say we have a README file that needs some editing. Let’s send it to ChatGPT, and tell it “Make it easy to read and informative”.

echo "Rewrite this README file as a user guide. Make it easy to read and informative. Use a helpful and clear style" \
| chatgpt-cli chat < README.md > README-new.md

You might be surprised to find our that the Project’s README file was edited using this exact command.

For the next example, what if you have a bunch of files, and you don’t want to compare to see which is the largest, sure you could sort on size. But what fun is that? (this is just an example to spark your imagination) Also, to make it a bit more challenging, I used the long listing and human readable format -lh , so it takes a bit more

{ echo "What is the largest file in this directory?"; ls -lh } \
| chatgpt-cli chat

The largest file in this directory is 'chatgpt-cli' which is 11 MB.

So how about something a little more involved. Here we have a directory of notes, written in markdown format. We are using the note taking application, Obsidian and want to apply a similar style across all the notes and make them a bit more clear and easier to read. Here I broke up the prompt into multiple printf statements to make it easier to edit. The prompt ends up on one line, followed by the contents of the file.

for file in notes/*.md; do
printf "\nEditing '%s'\n============================\n" "$file"
{
printf "Revise my notes. "
printf "Use Markdown format. "
printf "Revise the text of the note below to use a clear and informative style. "
printf "Use newlines to keep line length less than 120 characters.\n\n"
cat "${file}"
} | chatgpt-cli chat > "${file}.new"

if [ $? -ne 0 ]; then
printf " Request Failed: '%s'\n" "${file}"
rm "${file}.new"
else
mv "${file}.new" "${file}"
fi
done

Just like that a directory of a hundred files are edited and formatted.

Image Generation

Image Generation using DALL-E is a new feature for the CLI. The DALL-E 3 model is a huge improvement over the previous DALLE-2 model. Like the chat counterpart, it can be used in scripted settings, which makes this a powerful addition. It supports both DALL-E 2 and 3 models, the options depend on the model you are using. For DALL-E 2, you are able to request between 1 to 10 image variations, and in the sizes of 256x256, 512x512, or 1024x1024. These are the current limitations from the DALL-E API. For DALL-E 3, you are able to modify the image quality, style, and select from sizes of 1024x1024, 1792x1024, or 1024x1792.

chatgpt-cli image generation in action

Image Generation, Non-Interactive Mode

echo "Monkey in a banana costume" | ./chatgpt-cli image

Which gives us the resulting image.

A machine generated image of a cute monkey wearing a banana costume.
DALL-E 3 Result for requesting an image “Monkey in a banana costume”

A similar request using --model dall-e-2 gives a very different type of result.

DALL-E 2 Result for requesting an image “Monkey in a banana costume”

Power Tools: Combining Chat and Image Generation

Let’s now go back to our directory of a hundred markdown files. This time we will use ChatGPT to summarize and suggest a description of an image. Then we will use DALL-E to create the cover image for each note.


for file in notes/*.md; do
dir=$(dirname "${file}")
filename=$(basename "${file}")
base_filename=${filename%.*}

printf "\nCreating Images '%s'\n============================\n" "$file"
{
printf "Describe the contents of an image that would make a good cover image for the blog post below.\n\n"
cat "${file}"
} | ./chatgpt-cli chat --system-message "As an expert creator of blog posts" > "${dir}/${base_filename}-img-description.txt"

if [ $? -ne 0 ]; then
printf " Request Failed: '%s'\n" "${file}"
rm "${dir}/${base_filename}-img-description.txt"
continue
fi

cat "${dir}/${base_filename}-img-description.txt" | ./chatgpt-cli image -o "${dir}/${base_filename}-img"
done

Once again, the possibilities are limitless. You can combine the use ChatGPT and DALL-E to to automatically revise your notes, and generate related images for each.

These examples are only meant to get you thinking about how you would use ChatGPT from the command line. If you want to try it for your self, the next sections cover Installation and setup.

Installation

The application is available to download from the Github releases page for Mac, Linux, and Windows. If you are on Mac or Linux and use Homebrew, Installation is easy with these commands:

brew tap duanemay/tap
brew install chatgpt-cli

Setup

A ChatGPT API key is needed to use the CLI. Sign up on the OpenAI platform to obtain one.

We recommend creating a .chatgpt-cli file in your home directory—or wherever you intend to run the CLI— for inputting your ChatGPT API key and any other default settings.

API_KEY=sk-mysupersecretAPIkey

For details on the different CLI options, refer to the Project README.

Conclusion

Not having to copy and paste text to ChatGPT in a browser window has been a game changer for me. I am finding more and more ways to use it each day, and ways to incorporate ChatGPT into my shell scripts. Be creative, explore. Feel free to share examples of how you have been able to use ChatGPT-CLI in the comments below!

In addition, the project is open source, so if you have additional features you would like to see submit PRs, or open a enhancement request. The project’s Github page is accessible here.

Revisions

2023–07 - Original version written

2023–08 - Updated examples

2023–09 - Added Image command

2023–11 - Updated with DALL-E 3 information and examples.

--

--

Duane May
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨

Father, Software Engineer, Technology Tinkerer, Drummer, Bass & Ukulele player.