Simple ChatGPT client in the terminal
I stumbled upon a tutorial on how to make a simple ChatGPT client, but they seemed a bit outdated so I fiddled until I had the very simple thing I wanted.
Firstly important: this does not really compute the AI answer on your machine, it is just a client interface to the OpenAI cloud solution.
You will need a API-key from OpenAI.
There might be a billing model attached by now; look that up before you spam the AI.
Approach
npm install -g openai
test.js:
const openai = require('openai'); const readline = require('node:readline/promises'); const process = require('node:process'); // Set your OpenAI API key const apiKey = "<<< your api key goes here >>>"; const configuration = new openai.Configuration({ apiKey, }); const AI = new openai.OpenAIApi(configuration); function queryInput() { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.question("Ask me: ").then(ans => { rl.close(); processQuery(ans); }); } function processQuery(query) { const model = "text-davinci-003"; AI.createCompletion({ model, prompt: query, temperature: 0.5, }).then((response) => { console.log(response.data.choices[0].text); queryInput(); }); } queryInput();
# node test.js Ask me: when did avryl lavigne change her hair color to blonde? Avril Lavigne dyed her hair blonde in 2001
The above is based on this tutorial I found and the OpenAI client example application:
https://dev.to/docker/running-chatgpt-locally-using-docker-desktop-2i31
and additionally on official documentation since I wanted to run it like a script.
You can run the author's openai client example to generate animal names like this:
git clone https://github.com/ajeetraina/openai-quickstart-node . cp .env.example .env # Add your API key to .env npm install npm run dev
Or the same thing as docker container:
git clone https://github.com/ajeetraina/openai-quickstart-node . cp .env.example .env # Add your API key to .env docker build -t chatbot-docker . docker run -d -p 3000:3000 chatbot-docker -> http://localhost:3000/
Discussion