DEV Community

Pizofreude
Pizofreude

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](https://pypi.org/project/ai-chat-tool/)
export CHATGPTKEY="<YOUR_CHATGPT_API_KEY>"
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:

echo $CHATGPTKEY
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!

Cheerios!

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 (7)

Collapse
 
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

Collapse
 
pizofreude profile image
Pizofreude

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!

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

Nice article @pizofreude!

Collapse
 
pizofreude profile image
Pizofreude

Thanks for reading ma-man!

Collapse
 
matoval profile image
matoval

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.

Collapse
 
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!

Collapse
 
pizofreude profile image
Pizofreude • Edited

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