Compare commits

...

5 Commits

2 changed files with 39 additions and 9 deletions

View File

@ -46,3 +46,7 @@ You can set the following additional environment variables which smartsh can mak
If the variable is not set, we use "gpt-3.5-turbo" by default
`SMARTSH_DEBUG` : Will print additional debug info when running smartsh
`SMARTSH_SILENT_MODE` When set to 1, smartsh will try not to print warnings about missing environment variables, current mode etc. Off by default.
`SMARTSH_TEACHER_MODE` When set to 1, smartsh will provide you an explanation about the command it synthesized. Note that this will disable the prompt to execute the synthesized command. Off by default.

View File

@ -10,13 +10,23 @@ if openai.api_key is None:
sys.exit(1)
api_model = os.environ.get("OPENAI_MODEL_ID")
smarsh_dont_warn = os.environ.get("SMARTSH_SILENT_MODE") == "1"
if api_model is None:
api_model = "gpt-3.5-turbo"
print("Warning: OPENAI_MODEL_ID not set. Supported models are text-davinci-003, gpt-3.5-turbo")
print("Using default model " + api_model)
if smarsh_dont_warn != True:
print("Warning: OPENAI_MODEL_ID not set. Supported models are text-davinci-003, gpt-3.5-turbo")
print("Using default model " + api_model)
smarsh_debug_mode = os.environ.get("SMARTSH_DEBUG")
is_in_teacher_mode = False
smartsh_teacher_mode = os.environ.get("SMARTSH_TEACHER_MODE")
if smartsh_teacher_mode == "1" or smartsh_teacher_mode == "true":
if smarsh_dont_warn != True:
print("Teacher mode enabled")
is_in_teacher_mode = True
if smarsh_debug_mode == "1" or smarsh_debug_mode == "true":
print("Debug mode enabled")
print("OpenAI API key: " + openai.api_key)
@ -24,11 +34,16 @@ if smarsh_debug_mode == "1" or smarsh_debug_mode == "true":
# argcmd contains the entire command line arguments as a space separated string
argcmd = " ".join(sys.argv)
prompttxt = "Suggest a linux shell command to accomplish the following: " + argcmd
prompttxt = ""
if is_in_teacher_mode:
prompttxt = "You suggest a valid shell command to accomplish the following, together with an explanation: " + argcmd
else:
prompttxt = "You suggest a valid and correct {os.environ.get('SHELL')} command to accomplish the following, without any further explanation or additional text: " + argcmd
completion = None
apioutput = None
if api_model == "text-davinci-003":
print("Using model " + api_model)
if smarsh_dont_warn != True:
print("Using model " + api_model)
# Get the completion from OpenAI
completion = openai.Completion.create(
model="text-davinci-003",
@ -39,16 +54,27 @@ if api_model == "text-davinci-003":
frequency_penalty=0,
presence_penalty=0,
)
# print the output from davinci model to stdout
print(completion['choices'][0]['text'].strip())
apioutput = completion['choices'][0]['text'].strip()
elif api_model == "gpt-3.5-turbo":
completion = openai.ChatCompletion.create(
model = api_model,
messages = [
{'role': 'user', 'content': prompttxt}
{'role': 'system', 'content': prompttxt}
],
temperature = 0
)
# print the response to stdout
print(completion['choices'][0]['message']['content'])
apioutput = completion['choices'][0]['message']['content'].strip()
# Ask the user if the suggested command shall be executed
if is_in_teacher_mode == False and apioutput is not None:
print("Suggested command: " + apioutput)
print("Do you want to execute this command? (y/n)")
user_input = input()
if user_input == 'y':
print("Executing command: " + apioutput)
os.system(apioutput)
else:
print("Command not executed")
else:
print(apioutput)