Use ChatGPT in Terminal via Python
Tags: chatgpt, terminal, python, beginners
Published: 2023-01-08 07:59:30

## Table of Contents

1. [Python Package for ChatGPT](#Python Package for ChatGPT)
2. [Getting ChatGPT API Key](#Getting ChatGPT API Key)
3. [Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps!](#Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps!)
4. [Alias ChatGPT](#Alias ChatGPT)
5. [Update Software Package](#Update AI ChatBot Tool)

## Python Package for ChatGPT <a name="Python Package for ChatGPT"></a>

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

Check it out:

1. [Repo](https://github.com/matoval/ai-chat-tool)
2. [Python package](https://pypi.org/project/ai-chat-tool/)

## Getting ChatGPT API Key <a name="Getting ChatGPT API Key"></a>

To use this tool you will need a [ChatGPT API Key](https://beta.openai.com/account/api-keys).

## Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps! <a name="Save your ChatGPT API key like an SSH key in Git Bash in 2 Steps!"></a>

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

### Step 1 <a name="step1"></a>

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](https://beta.openai.com/account/api-keys) as the `varvalue`.

Here's the example:

```bash
# Source chatgptkey for [ai-chat-tool](https://pypi.org/project/ai-chat-tool/)
export CHATGPTKEY="<YOUR_CHATGPT_API_KEY>"
```

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

### Step 2

To validate the success of [Step 1](#step1), run:

```bash
echo $CHATGPTKEY
```

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:

```bash
python -m ai-chat-tool --api-key=$CHATGPTKEY
```

## Alias ChatGPT <a name='Alias ChatGPT'></a>

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](https://dev.to/pizofreude/efficient-way-to-invoke-all-editors-in-git-bash-on-windows-1j8i).

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

Cheerios!

## Update AI ChatBot Tool <a name='Update AI ChatBot Tool'></a>

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:

```bash
pip install --upgrade ai-chat-tool
```

- 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.