Use ChatGPT UI with your own API key

Overview

ChatGPT has become increasingly popular in recent months. At the time of writing, version 3.5 is in active use by the public and the even more advanced version 4 is being integrated by large companies such as Microsoft. Compared to ChatGPT-3.5 the latest version is able to process a much larger input, up to 25.000 words and makes fewer mistakes called 'hallucinations'. The most convenient way to get started with ChatGPT is to register as a new user on the OpenAI website for free. However, a major drawback is the lack of performance when using the official ChatGPT UI platform with the free account. That's a pity, because the strength of ChatGPT is the speed of generating a response. There are several options to take advantage of the high speed. You can either subscribe to ChatGPT and use a paid plan for $24 p/m or you can get started yourself by using an API key in combination with a self-hosted, open source, user interface.

As a new user you get $5 user credit for the first 3 months to use the API. I use ChatGPT-3.5 myself and as soon as your credit is used you pay per use. This costs $0.002 / 1K tokens where 1K equals 750 words.

Getting started

First, sign up to the OpenAI platform or sign in with your existing account if you have one already. Next, here you can generate an API key on the OpenAI platform. Just hit the "Create new secret key" button, copy the value and save it somewhere safe. Great! Now that you have an API key we can use it to use ChatGPT-3.5 under the hood.

I'll be using an open source ChatGPT UI of mckaywrigley/chatbot-ui which has become increasingly popular over time and is continuously updated. Clone the Git repository to your desired destination where you will run ChatGPT UI on a Docker engine.

Prepare docker

Edit the docker-compose.yml file and replace the content with the example below. Choose your preferred container name and choose a different port mapping if needed. Usually you'll want that if another container is already running on port 3000. If that's the case you can use another host port, like 3001:3000 (host -> container port).

 1version: "3.6"
 2
 3services:
 4  chatgpt-ui:
 5    container_name: chatgpt-ui
 6    build: .
 7    restart: unless-stopped
 8    volumes:
 9      - ./.env:/app/.env
10    ports:
11      - 3000:3000

Create new .env file

Now craete a new .env file in the same location where you cloned the Git repository and use the content below. You can leave the DEFAULT_MODEL and DEFAULT_SYSTEM_PROMPT as is and put your generated API key after the OPENAI_API_KEY.

DEFAULT_MODEL=gpt-3.5-turbo
DEFAULT_SYSTEM_PROMPT=You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.
OPENAI_API_KEY=YOUR_KEY

Start ChatGPT UI

Now that everything is in place you can execute the command docker compose up -d. This will instruct the Docker engine to build a docker image based on the Dockerfile and once succeeded to create a docker container and start it in the background.

Once started you should be able to access the ChatGPT UI on http://localhost:3000 if running locally or replace localhost with the correct server name.