commit 2824ab6089802c69f1f39d145d000930eae85bf3 Author: Ashik K Date: Sun Apr 9 17:43:18 2023 +0200 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b1dc7b --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +You need to add the following to your .bashrc + +``` +export OPENAI_API_KEY="your_api_key" + +export SMARTSH_PATH="path_to_dir_with_smartsh.py" +command_not_found_handle () { + echo "Let's get help from OpenAI API!" + python3 $SMARTSH_PATH/smartsh.py "$@" +} +``` diff --git a/smartsh.py b/smartsh.py new file mode 100755 index 0000000..a47bf08 --- /dev/null +++ b/smartsh.py @@ -0,0 +1,20 @@ +import openai +import os +import sys + +#Get the OpenAI API key from environment variable +openai.api_key = os.environ.get("OPENAI_API_KEY") +# argcmd contains the entire command line arguments as a space separated string +argcmd = " ".join(sys.argv) + +prompt = "Suggest a linux shell command to accomplish the following. If the entered string is already a valid command, suggest installing the required packages: " + argcmd +completion = openai.ChatCompletion.create( + model = 'gpt-3.5-turbo', + messages = [ + {'role': 'user', 'content': prompt} + ], + temperature = 0 +) + +# print the response to stdout +print(completion['choices'][0]['message']['content'])