Introduction to ChatGPT CLI
ChatGPT CLI is a simple and efficient tool designed to facilitate interaction with ChatGPT directly from the command line. Utilizing the official API released on March 1, 2023, this tool allows users to leverage ChatGPT at optimal speed and reduced cost compared to a full ChatGPT Plus subscription, making it economical for the average user.
Obtaining an API Key
To start using ChatGPT CLI, users must first obtain an API Key. This requires creating an account with OpenAI at platform.openai.com. After logging in, navigate to the top-right corner, click on your name's initial, and select "View API keys." Click on "Create new secret key" to generate a key. Adding a payment method might also be necessary, although new accounts often come with free credits. Users should check the billing section for more details on usage policies and charges.
Installation and Configuration
ChatGPT CLI requires Python (version 3.10 or higher), Pip, and Git to be installed on your system. It's crucial to update Pip to the latest version to ensure smooth installation using the command:
pip install -U pip
Users can then install ChatGPT CLI with:
pip install git+https://github.com/marcolardera/chatgpt-cli
Once installed, users need to configure their API Key. This can be done through various methods:
- Directly in the config.yaml configuration file.
- By setting the environment variable
OPENAI_API_KEY
. - Using command line options
--key
or-k
.
If multiple keys are provided, the command line option takes precedence, followed by the environment variable, and then the configuration file.
Configuration File Details
The configuration file, named config.yaml, is located in the default config directory specified by the XDG Base Directory Specification. On Linux and MacOS, this is found using the $XDG_CONFIG_HOME
variable. A template is automatically generated upon first use, with any missing fields assigned default values.
Suppliers and Models
ChatGPT CLI supports two suppliers: openai
(default) and azure
, configurable in the config.yaml file. The tool defaults to using the gpt-3.5-turbo
model, but users can specify other models either in the configuration file or via the command line. Each model has different pricing, which is based on input and output tokens. Visit OpenAI's model documentation for more details.
Basic Usage
To initiate a chat session, type chatgpt-cli
in the terminal. The session will display the number of tokens used, which is an indicator of the session's cost and complexity. Use the /q
command to exit and receive a summary of token usage and estimated cost.
Additional commands include /copy
or /c
to copy code blocks from the output. These commands help manage content efficiently, especially when dealing with coding tasks or lengthy responses. The /help
command provides a list of all available commands.
Advanced Features
- Multiline Input: Activate with
--multiline
or-ml
for composing longer inputs. - Contextual Input: Use
--context <FILE PATH>
or-c
to load initial context from a file, useful for tasks like code analysis or text translation. - Markdown Rendering: ChatGPT CLI supports Markdown formatting by default, which can be disabled via the configuration file if needed.
- Session History: All chat sessions are saved and can be restored using
--restore <timestamp>
or--restore last
. - Piping and JSON Mode: Incorporate ChatGPT CLI into shell scripts using
--non-interactive
(-n
) mode and request JSON responses with--json
(-j
).
External Tools and Contributions
For Linux users, a feature facilitates starting a chat using highlighted text as context. Additionally, users can combine ChatGPT CLI with other CLI tools such as DALL-E CLI for creative tasks, exemplified by generating an image prompt from natural language input.
Developers and enthusiasts are welcome to contribute to the project, with guidelines available in the CONTRIBUTING.md file.
In summary, ChatGPT CLI provides a robust and flexible tool for interacting with ChatGPT via the command line, extending its capabilities with various input methods, model configurations, and advanced features to suit a wide range of user needs.