2023-04-09 15:43:18 +00:00
import openai
import os
import sys
#Get the OpenAI API key from environment variable
openai . api_key = os . environ . get ( " OPENAI_API_KEY " )
2023-04-10 06:20:38 +00:00
# check if the API key is set
if openai . api_key is None :
print ( " Please set the OPENAI_API_KEY environment variable " )
sys . exit ( 1 )
api_model = os . environ . get ( " OPENAI_MODEL_ID " )
2023-04-10 08:50:49 +00:00
smarsh_dont_warn = os . environ . get ( " SMARTSH_SILENT_MODE " ) == " 1 "
2023-04-10 06:20:38 +00:00
if api_model is None :
api_model = " gpt-3.5-turbo "
2023-04-10 08:48:27 +00:00
if smarsh_dont_warn != True :
2023-04-10 08:34:10 +00:00
print ( " Warning: OPENAI_MODEL_ID not set. Supported models are text-davinci-003, gpt-3.5-turbo " )
print ( " Using default model " + api_model )
2023-04-10 06:20:38 +00:00
smarsh_debug_mode = os . environ . get ( " SMARTSH_DEBUG " )
2023-04-10 21:05:58 +00:00
tune_for_powershell = os . environ . get ( " SMARTSH_TUNE_FOR_POWERSHELL " ) == " 1 " or os . environ . get ( " SMARTSH_TUNE_FOR_POWERSHELL " ) == " true "
colorize_output = os . environ . get ( " SMARTSH_COLORIZE_OUTPUT " ) == " 1 " or os . environ . get ( " SMARTSH_COLORIZE_OUTPUT " ) == " true "
2023-04-10 08:48:27 +00:00
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
2023-04-10 06:20:38 +00:00
if smarsh_debug_mode == " 1 " or smarsh_debug_mode == " true " :
print ( " Debug mode enabled " )
print ( " OpenAI API key: " + openai . api_key )
print ( " OpenAI model: " + api_model )
2023-04-09 15:43:18 +00:00
# argcmd contains the entire command line arguments as a space separated string
argcmd = " " . join ( sys . argv )
2023-04-10 08:48:27 +00:00
prompttxt = " "
if is_in_teacher_mode :
2023-04-10 21:05:58 +00:00
if tune_for_powershell :
prompttxt = " You suggest a valid PowerShell command to accomplish the following, together with an explanation: " + argcmd
else :
prompttxt = " You suggest a valid shell command to accomplish the following, together with an explanation: " + argcmd
2023-04-10 08:48:27 +00:00
else :
2023-04-10 21:05:58 +00:00
if tune_for_powershell :
prompttxt = " You suggest a valid and correct PowerShell command to accomplish the following. You shall not provide any further explanation or additional text: " + argcmd
else :
prompttxt = " You suggest a valid and correct { os.environ.get( ' SHELL ' )} command to accomplish the following. You shall not provide any further explanation or additional text: " + argcmd
2023-04-10 06:20:38 +00:00
completion = None
2023-04-10 08:48:27 +00:00
apioutput = None
2023-04-10 06:20:38 +00:00
if api_model == " text-davinci-003 " :
2023-04-10 09:03:33 +00:00
if smarsh_dont_warn != True :
print ( " Using model " + api_model )
2023-04-10 06:20:38 +00:00
# Get the completion from OpenAI
completion = openai . Completion . create (
model = " text-davinci-003 " ,
prompt = prompttxt ,
temperature = 0 ,
max_tokens = 50 ,
top_p = 1 ,
frequency_penalty = 0 ,
presence_penalty = 0 ,
)
2023-04-10 08:48:27 +00:00
apioutput = completion [ ' choices ' ] [ 0 ] [ ' text ' ] . strip ( )
2023-04-10 06:20:38 +00:00
elif api_model == " gpt-3.5-turbo " :
completion = openai . ChatCompletion . create (
model = api_model ,
2023-04-09 15:43:18 +00:00
messages = [
2023-04-10 08:34:10 +00:00
{ ' role ' : ' system ' , ' content ' : prompttxt }
2023-04-09 15:43:18 +00:00
] ,
temperature = 0
2023-04-10 06:20:38 +00:00
)
# print the response to stdout
2023-04-10 08:48:27 +00:00
apioutput = completion [ ' choices ' ] [ 0 ] [ ' message ' ] [ ' content ' ] . strip ( )
2023-04-09 15:43:18 +00:00
2023-04-10 08:34:10 +00:00
# Ask the user if the suggested command shall be executed
2023-04-10 08:48:27 +00:00
if is_in_teacher_mode == False and apioutput is not None :
2023-04-10 21:05:58 +00:00
# print the suggested command in bright red
if colorize_output :
print ( " suggested command: \033 [91m " + apioutput + " \033 [0m " )
else :
print ( " suggested command: " + apioutput )
2023-04-10 08:34:10 +00:00
print ( " Do you want to execute this command? (y/n) " )
user_input = input ( )
if user_input == ' y ' :
2023-04-10 08:48:27 +00:00
print ( " Executing command: " + apioutput )
2023-04-10 21:05:58 +00:00
if colorize_output :
print ( " \033 [93m " )
2023-04-10 21:16:22 +00:00
if tune_for_powershell :
2023-04-10 22:35:01 +00:00
print ( " ***** PowerShell support is experimental***** " )
print ( " ***** You may have better luck if you copy the command and paste it into a PowerShell window ***** " )
2023-04-10 21:16:22 +00:00
os . system ( " powershell.exe -Command " + apioutput )
else :
os . system ( apioutput )
2023-04-10 21:05:58 +00:00
if colorize_output :
print ( " \033 [0m " )
2023-04-10 08:34:10 +00:00
else :
print ( " Command not executed " )
2023-04-10 08:48:27 +00:00
else :
2023-04-10 08:50:49 +00:00
print ( apioutput )