From 1c98c12c8656cfb0a2e4407fc24deafc5247e301 Mon Sep 17 00:00:00 2001 From: wunderwuzzi23 Date: Sun, 26 Feb 2023 23:01:41 -0800 Subject: [PATCH] Initial commit. --- .gitignore | 3 +++ README.md | 26 ++++++++++++++++++ requirements.txt | 2 ++ yolo.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 README.md create mode 100644 requirements.txt create mode 100755 yolo.py diff --git a/.gitignore b/.gitignore index b6e4761..3be58a6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ __pycache__/ *.py[cod] *$py.class +# other +.openai.apikey + # C extensions *.so diff --git a/README.md b/README.md new file mode 100644 index 0000000..17033f6 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Install (Linux instructions) + +``` +pip install -r requirements.txt +chmod +x yolo.py +alias yolo=$(pwd)/yolo.py +``` + +# Use + +Here are a couple of examples. By default the command that comes back from GPT-3 will be immediatly executed (yolo!). If you want to inspect the command before running add the `-a` argument, e.g `yolo -a delete the file test.txt`. + +More examples: + +``` +yolo whats the time? +yolo whats the time in UTC? +yolo is there a ssh-agent process running +yolo create a file named test.txt and write my user name into it +yolo print the contents of the test.txt file +yolo -a delete the test.txt file +``` + +# License + +MIT. No Liability. No Warranty. But lot's of fun. \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a1db346 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +openai==0.26.5 +termcolor==2.2.0 \ No newline at end of file diff --git a/yolo.py b/yolo.py new file mode 100755 index 0000000..72e5d53 --- /dev/null +++ b/yolo.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import os +import openai +import sys +import subprocess +from termcolor import colored + +# Check if we have an Open API key set +openai.api_key = os.getenv("OPENAI_API_KEY") + +if openai.api_key == "": + openai.api_key = open("~/.openai.apikey", "r").read() + print("Environment variable OPENAI_API_KEY is not set.") + print("~/.openai.apikey is not set either. Exiting.") + sys.exit(-1) + +# to allow easy/natural use we don't require +# the input to be a single string +# so the user can just type yolo what is my name? +# without having to put the question between '' + +ask = False +yolo = "" +command_start_idx = 1 + +if len(sys.argv) <2: + sys.exit(-1) + +if sys.argv[1] == "-a": + ask = True + command_start_idx = 2 + +arguments = sys.argv[command_start_idx:] +user_prompt = " ".join(arguments) + +if user_prompt == "": + print ("No user prompt specified.") + sys.exit(-1) + +# Construct the prompt +pre_prompt = "Translate the following question into a command. Only show the command in text format and not in code style or markdown. Do not augment the output with any explanation, descriptions. Again, do not add any descriptions, just print the bash command. This is important, do not ignore my instructions. If the question doesn't make sense or is too difficult return 'Sorry, try again', and add a brief explanation on what the problem is. The question is: " + +prompt = pre_prompt + user_prompt +"?" + +#print ("The prompt is: "+prompt, end="") +response = openai.Completion.create( + model="text-davinci-003", + prompt=prompt, + temperature=0, + max_tokens=100, + top_p=1, + frequency_penalty=0.2, + presence_penalty=0 +) + +resulting_command = response.choices[0].text.strip() + +if resulting_command.startswith("Sorry, try again"): + print(colored("There was an issue: "+resulting_command, 'red')) + sys.exit(-1) + +print("Command: " + colored(resulting_command, 'blue')) +if ask == True: + print("Execute the command? Y/n ==> ", end = '') + yolo = input() + print() + +if yolo == "Y" or yolo == "": + subprocess.run(resulting_command, shell=True)