Connecting GPT-3 to command line

I connected GPT-3 to my terminal via Node.js.

Assumptions:

  • You are using zsh on a mac
  • You are familiar with the basics of node and npm

Let’s say I want a command that lets me get a tldr summary from the command line.

I created a folder for prompts. Installed node stuff, including OpenAIApi. Created a file called tldr.js

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

async function start() {
  const myArgs = process.argv.slice(2);
  const paragraph = myArgs[0];
  const response = await openai.createCompletion("text-davinci-002", {
    prompt: "Summarize this for a five-year old child:\n\n" + paragraph,
    temperature: 0.7,
    max_tokens: 200,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
  });
  console.log(response.data.choices[0].text);
}

start();

I add the following to ~/.zshrc:

# OpenAI
export OPENAI_API_KEY=(YOUR API KEY HERE)

function tldr() {
  if [[ "$1" == "" ]]; then
    echo "tell me what to summarize"
  else
	node ~/(THE PATH TO YOUR FILE HERE)/tldr.js $1
  fi
}

Then I run the command (quotes are important!):

tldr “The intent of Ethereum is to create an alternative protocol for building decentralized applications, providing a different set of tradeoffs that we believe will be very useful for a large class of decentralized applications, with particular emphasis on situations where rapid development time, security for small and rarely used applications, and the ability of different applications to very efficiently interact, are important. Ethereum does this by building what is essentially the ultimate abstract foundational layer: a blockchain with a built-in Turing-complete programming language, allowing anyone to write smart contracts and decentralized applications where they can create their own arbitrary rules for ownership, transaction formats and state transition functions. A bare-bones version of Namecoin can be written in two lines of code, and other protocols like currencies and reputation systems can be built in under twenty. Smart contracts, cryptographic “boxes” that contain value and only unlock it if certain conditions are met, can also be built on top of the platform, with vastly more power than that offered by Bitcoin scripting because of the added powers of Turing-completeness, value-awareness, blockchain-awareness and state.”

Response:

Ethereum is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of fraud or third party interference.

In Ethereum, you can write code that controls money, and build applications accessible anywhere in the world.

6 Likes

I’m curious what your use case is?

3 Likes

Mostly “outsourcing” creative labor.

tldr is a common one: finding out if something is worth reading, skimming through paragraphs faster. (fun fact: I actually named it eli5 on my prompt but copied it as tldr)

Other uses, some still under construction:

  • generates clever titles around a concept
  • gives me an emoji expressing a term. I use emojis like :oil_drum: in technical documentation to state that something may have bugs. Sometimes I want something like :face_vomiting: but more appropriate for office use.
  • something that generates a verbose description around certain things
  • generating code around something simple (useful when I want to extract something from a specific string format or write unit tests)
  • a kind of game in the console whenever I get bored
  • something that turns bullet points into more verbose text
  • and a thing that converts mundane instructions to poetry

poetize “spaghetti alio olio” “olive oil, spaghetti, raw garlic”

In a pot of boiling water,
Add the spaghetti for al dente.
Meanwhile, in a heated pan,
Pour in some olive oil and raw garlic.
When the pasta is ready,
Add it to the pan of flavour.
Stir and serve,
Enjoy your simple but tasty meal.

2 Likes

I’m blown away that I didn’t know this technology existed until 2 weeks ago, and there’s some people using it here for half a year! Dang!

1 Like

If you came in from ChatGPT, you’re in for some fun. ChatGPT is sort of like “low code”, highly accessible to everyone with no experience. The tools here give you finer control over it; e.g. you can train it around the kind of poetry you like or snippets of your own code style. ChatGPT is also leaning very much towards safety, whereas you can do things like combat scenes for RPGs with direct access.

Also open sourced this content: GitHub - smuzani/openai-samples

Added some things on fine tuning, as well as some of the simpler prompts I use.

What do you mean by “direct access”? I did combat scenes already

nice -this is helpful! I wrote a similar shell script but ran into some issues when there were quotes inside the ‘paragraph’ portion of the request. essentially my script read a text file and then I had to use a sed command to remove any ’ " ’ in the text file. based on the poetize bit in the replies, it looks like your code processes the quotes perfectly. thanks for sharing.

Which easily shows that OpenAI is easily biased by the hype and sensationalism online :wink: