DEV Community


Posted on • Updated on


Use ChatGPT in Terminal via Python

Table of Contents

  1. Python Package for ChatGPT
  2. Getting ChatGPT API Key
  3. Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps!
  4. Alias ChatGPT
  5. Update Software Package

Python Package for ChatGPT

There is a Python package that brings #ChatGPT to the command line!

Check it out:

  1. Repo
  2. Python package

Getting ChatGPT API Key

To use this tool you will need a ChatGPT API Key.

Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps!

Rather than manually key in the API key, we can save it like an SSH key as follows:

Step 1

Pass the env var inside your .bashrc file.

To pass the env var, set an environment variable in Git Bash, you can use the export command. For example, to set the environment variable VARNAME to the value varvalue.

In this case, we use: CHATGPTKEY as the VARNAME and ChatGPT api key as the varvalue.

Here's the example:

# Source chatgptkey for [ai-chat-tool](
Enter fullscreen mode Exit fullscreen mode

Save the bash.bashrc file, exit Git Bash and rerun it.

Step 2

To validate the success of Step 1, run:

Enter fullscreen mode Exit fullscreen mode

Your ChatGPT API key should be shown in the Terminal.

Voilà, Bob's your uncle!

You can simply run the ai-chat-tool in the Terminal literally as follows:

python -m ai-chat-tool --api-key=$CHATGPTKEY
Enter fullscreen mode Exit fullscreen mode

Alias ChatGPT

Create alias for ease of use. What is your favourite alias for ChatGPT in Python? Comment down below!

For more info on creating aliases, please refer to my post here.

No more manual API input, ease of use, and happy days!


Update AI ChatBot Tool

Here's a short instruction on how to update the "ai-chat-tool" Python package using pip:

  • Open your terminal or command prompt.
  • Type the following command:
pip install --upgrade ai-chat-tool
Enter fullscreen mode Exit fullscreen mode
  • Press Enter to execute the command.
  • If the package is already installed on your system, pip will check if there is a newer version available and upgrade it if necessary.
  • Wait for the upgrade process to complete. Depending on your internet connection and the size of the package, this may take a few seconds to several minutes.
  • Once the upgrade is complete, you should see a message indicating the version of the package that was installed or upgraded.

That's it! Your ai-chat-tool package should now be updated to the latest version. You can confirm this by running the command "pip show ai-chat-tool" to see the current version installed on your system.

Top comments (5)

pengeszikra profile image
Peter Vivo

Thanks for this one is te easiest use of chatgpt!

I added to my .zshrc

alias gg="python3 -m ai-chat-tool --api-key=<CHAT-GPI-KEY> --max-tokens=2000"
Enter fullscreen mode Exit fullscreen mode

for short use

pizofreude profile image

version 0.0.9 already released and more features coming soon already in dev's roadmap. I like it when someone uses useful shortcut with zero nonsense & just for the sake of show off, cheers!

matoval profile image

Thanks @pizofreude for writing this blog! I really appreciate the support. I just pushed version 0.0.11 which added the ability to write ChatGPT output to a file at a specific line.

pizofreude profile image
Pizofreude • Edited

My pleasure, I'm thrilled ever since the inception release. 🙂 You're killin' it man, congrats and thanks for your awesome contribution!

pizofreude profile image
Pizofreude • Edited

Tested with Python3.11, not sure whether it will work with prior release of Python2.

The AI Brief

AI generated git commit messages

Minimize the struggle of remembering what you just coded. AI-generated commits make it easier to manage projects and keep track of changes. The Nutlope/aicommits project demonstrates how AI can improve commit messages.

I open sourced an AI that creates any UI in seconds

Make AI-generated user interfaces a breeze. This open-source project harnesses the power of generative AI technologies like chatGPT to create versatile, quick, and intuitive UI components.

Use AI to commit like a PRO in 1 second

Upgrade your commit message game with AI. Boost your productivity by using ChatGPT to generate commit messages and avoid context switching. OpenCommit is an open-source library that helps you achieve this easily.

Build your own ChatGPT starter kit

Train AI models on custom data for improved domain-specific knowledge. Combine the power of WebView technologies and this starter kit to train your ChatGPT model on specific websites, allowing for better-optimized outcomes.