Running ChatGPT Locally using Docker Desktop

It is possible to run Chat GPT locally on your own computer. To do this, you will need to install the OpenAI API client.

But before that, you will need to obtain an API key from OpenAI. To do this, you need to visit the OpenAI API site and generate a …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ajeet Singh Raina

It is possible to run Chat GPT locally on your own computer. To do this, you will need to install the OpenAI API client.

But before that, you will need to obtain an API key from OpenAI. To do this, you need to visit the OpenAI API site and generate a secret key.(as shown below)

Image2

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);
});

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

2. Navigate into the project directory

cd openai-quickstart-node

3. Install the requirements

npm install

4. Make a copy of the example environment variables file

cp .env.example .env

Add your API key to the newly created .env file

4. Run the app

npm run build

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

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 new directory

Open a terminal window and navigate to the location where you want to create your project directory. Then, run the following command to create a new directory:

mkdir chatbot

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

FROM node:14

## Install the OpenAI API client
RUN npm install openai

## Copy the app code to the container
COPY . /app

# Set the working directory to the app code
WORKDIR /app

# Run the app
CMD node app.js

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.

Create an app.js file: In your project directory, create a file called app.js and add the following content to it:

const openai = require('openai');

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

Building the Chat GPT Docker Image

docker build -t ajeetraina/chatbot-docker .

Running the Chat GPT container

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

In my next blog post, I will show how to run OpenAI as a Kubernetes Pod.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ajeet Singh Raina


Print Share Comment Cite Upload Translate Updates
APA

Ajeet Singh Raina | Sciencx (2023-01-08T03:22:21+00:00) Running ChatGPT Locally using Docker Desktop. Retrieved from https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/

MLA
" » Running ChatGPT Locally using Docker Desktop." Ajeet Singh Raina | Sciencx - Sunday January 8, 2023, https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/
HARVARD
Ajeet Singh Raina | Sciencx Sunday January 8, 2023 » Running ChatGPT Locally using Docker Desktop., viewed ,<https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/>
VANCOUVER
Ajeet Singh Raina | Sciencx - » Running ChatGPT Locally using Docker Desktop. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/
CHICAGO
" » Running ChatGPT Locally using Docker Desktop." Ajeet Singh Raina | Sciencx - Accessed . https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/
IEEE
" » Running ChatGPT Locally using Docker Desktop." Ajeet Singh Raina | Sciencx [Online]. Available: https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/. [Accessed: ]
rf:citation
» Running ChatGPT Locally using Docker Desktop | Ajeet Singh Raina | Sciencx | https://www.scien.cx/2023/01/08/running-chatgpt-locally-using-docker-desktop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.