Created
November 25, 2021 10:32
-
-
Save nunodonato/a2d95026ac548b18d4140d60fcc36bc4 to your computer and use it in GitHub Desktop.
Use openai codex to translate natural language into shell commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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