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)
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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.