Skip to content

Instantly share code, notes, and snippets.

@nunodonato
Created November 25, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunodonato/a2d95026ac548b18d4140d60fcc36bc4 to your computer and use it in GitHub Desktop.
Save nunodonato/a2d95026ac548b18d4140d60fcc36bc4 to your computer and use it in GitHub Desktop.
Use openai codex to translate natural language into shell commands
import os
import openai
import sys
openai.api_key = "YOUR-OPENAI-KEY"
# get all command line arguments into one string
prompt = " ".join(sys.argv[1:])
response = openai.Completion.create(
engine="davinci-codex",
prompt="#!/bin/sh\n# command to "+prompt+"\n",
temperature=0,
max_tokens=64,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response = response.choices[0].text
command = ""
# print only the first non-empty line from response
for line in response.split("\n"):
if line:
# ignore comment lines
if line.startswith("#"):
continue
# trim line
line = line.strip()
# ignore lines with 0 length
if len(line) == 0:
continue
command = line
break
if command=="":
print("No command found")
else:
print (command)
# ask user to confirm
if input("Execute? [y/N] ").lower() == "y":
os.system(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment