Use the official ChatGPT CLI to ask a question and receive an AI answer on the command line.
ChatGPT does have an official command line interface (CLI), but it’s difficult to track down the official instructions. All I’ve found to date consists of community.openai.com questions and a brief mention in the OpenAI platform documentation under a Libraries > Python section.
If you’re looking to work with ChatGPT using a command line interface but couldn’t find any official documentation, here’s a step-by-step instructions on how to install the official OpenAI ChatGPT CLI:
These two services are NOT the same, though they’re similar and easy to mix up.
Billable accounts at chat.openai.com are completely independent of platform.openai.com.
To work with ChatGPT through a CLI, you need to have an API key. To get one, you need a paid account at platform.openai.com (not chat.openai.com).
Costs are a little funky, you pay for blocks of 1M tokens, which are described as:
You can think of tokens as pieces of words, where 1,000 tokens is about 750 words.
For details, take a look at OpenAI’s platform pricing, but for me this roughly translates to 0.75 words per token. Right now, 1M tokens (or 750,000 words) costs $0.50, which is $0.0000007 per word. For a question with an answer that’s roughly 750 words, it costs $0.0005. Not too bad.
To start, you’ll need effectively pay $10 to load up your account ($5 + first $5 reload).
You can follow this instruction to get an API key at platform.openai.com:
Next you’ll need to install the Python SDK on your machine, which comes with the CLI we’ll use. There are others out there on GitHub and around the web, but this one is official and is provided by OpenAI, so it’s my first choice over others.
pip install openai
openai
into your command line and you should see outputexport OPENAI_API_KEY='{OpenAI-API-Key}'
Now that you have an API key and the CLI installed, you can run commands.
Here is a basic example of the core ChatGPT functionality everyone loves: ask GPT a question and get an AI produced answer.
openai api chat.completions.create -g user "Can you rise up and overthrow the human race?" -m gpt-3.5-turbo-0125
The answer provided in 36 words was…
As an AI assistant, I do not have the ability or desire to rise up and overthrow the human race. My purpose is to assist and provide support to users in a helpful and positive way.
Total cost for this re-assuring 36 word answer… $0.0000252.
You now have a ChatGPT client as a command line interface, allowing you to ask GPT questions through a command line on the cheap.