Running ChatGPT Client Locally using Docker Desktop

Ajeet Singh Raina - Jan 8 '23 - - Dev Community

It is possible to run Chat GPT Client locally on your own computer. To do this, you will first need to understand how to install and configure the OpenAI API client. Then, try to see how we can build a simple chatbot system similar to ChatGPT.

Please note that this is the first part of the blog series where you will see how to get started with OpenAI and build a simple Pet Name Generator app. In the 2nd part of the blog series, you will see how to build a chatbot system. Let's get started.

First, you will need to obtain an API key from OpenAI. Visit the OpenAI API site and generate a secret key. (as shown below)

Next, create the below sample Node.js script that demonstrates how you can use the OpenAI API client to run Chat GPT locally:



const openai = require('openai');

// Set your OpenAI API key
openai.apiKey = "YOUR_API_KEY";

// Set the prompt for Chat GPT
const prompt = "What's your favorite color?";

// Set the model to use (in this case, Chat GPT)
const model = "chatbot";

// Generate a response from Chat GPT
openai.completions.create({
  engine: model,
  prompt: prompt,
  max_tokens: 2048,
  n: 1,
  stop: '.',
  temperature: 0.5,
}, (error, response) => {
  console.log(response.choices[0].text);
});


Enter fullscreen mode Exit fullscreen mode

This script uses the openai.completions.create() method to generate a response from Chat GPT based on the provided prompt. You can adjust the max_tokens and temperature parameters to control the length and creativity of the response, respectively.

In order to test drive how OpenAI actually works, I forked the Pet name generator app repository and tried to create my own version of Dockerized solution. First, I tried to bring up the app without using Docker just to ensure that it works on my local machine.

1. Clone the repository



git clone https://github.com/ajeetraina/openai-quickstart-node


Enter fullscreen mode Exit fullscreen mode

2. Navigate into the project directory



cd openai-quickstart-node


Enter fullscreen mode Exit fullscreen mode

3. Install the requirements



npm install


Enter fullscreen mode Exit fullscreen mode

4. Make a copy of the example environment variables file



cp .env.example .env


Enter fullscreen mode Exit fullscreen mode

Add your API key to the newly created .env file

4. Run the app



npm run build


Enter fullscreen mode Exit fullscreen mode

You should now be able to access the app at http://localhost:3000!

Image12

Running Pet Name Generator app using Docker Desktop

Let us try to run the Pet Name Generator app in a Docker container. To do this, you will need to install Docker locally in your system. I recommend using Docker Desktop which is free of cost for personal usage.

Create a Dockerfile

Create a Dockerfile: In your project directory, create a file called Dockerfile and add the following content to it:



# Use the official Node.js 10 image as the base
FROM node:14

# Create a working directory
RUN mkdir -p /usr/src/app

# Set the working directory
WORKDIR /usr/src/app

# Copy the package.json and package-lock.json files
COPY package*.json /usr/src/app/

# Install the dependencies
RUN npm install

# Copy the rest of the application code
COPY . /usr/src/app

# Expose the application's port
EXPOSE 8080

# Run the application
CMD [ "npm", "run", "dev" ]


Enter fullscreen mode Exit fullscreen mode

This Dockerfile specifies the base image (node:14) to use for the Docker container and installs the OpenAI API client. It also copies the app code to the container and sets the working directory to the app code.

Building the Chatbot Docker Image



docker build -t ajeetraina/chatbot-docker .


Enter fullscreen mode Exit fullscreen mode

Running the Chatbot container



docker run -d -p 3000:3000 ajeetraina/chatbot-docker


Enter fullscreen mode Exit fullscreen mode

Image description

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .