mirror of https://github.com/ashikslab/smartsh.git
Compare commits
5 Commits
a81d229399
...
f8d599bfa2
Author | SHA1 | Date |
---|---|---|
Ashik K | f8d599bfa2 | |
Ashik K | 23b69fe325 | |
Ashik K | 7942c95229 | |
Ashik K | e061d198de | |
Ashik K | 7e93eb08c4 |
|
@ -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
|
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_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.
|
||||||
|
|
38
smartsh.py
38
smartsh.py
|
@ -10,13 +10,23 @@ if openai.api_key is None:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
api_model = os.environ.get("OPENAI_MODEL_ID")
|
api_model = os.environ.get("OPENAI_MODEL_ID")
|
||||||
|
smarsh_dont_warn = os.environ.get("SMARTSH_SILENT_MODE") == "1"
|
||||||
if api_model is None:
|
if api_model is None:
|
||||||
api_model = "gpt-3.5-turbo"
|
api_model = "gpt-3.5-turbo"
|
||||||
|
if smarsh_dont_warn != True:
|
||||||
print("Warning: OPENAI_MODEL_ID not set. Supported models are text-davinci-003, 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)
|
print("Using default model " + api_model)
|
||||||
|
|
||||||
smarsh_debug_mode = os.environ.get("SMARTSH_DEBUG")
|
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":
|
if smarsh_debug_mode == "1" or smarsh_debug_mode == "true":
|
||||||
print("Debug mode enabled")
|
print("Debug mode enabled")
|
||||||
print("OpenAI API key: " + openai.api_key)
|
print("OpenAI API key: " + openai.api_key)
|
||||||
|
@ -24,10 +34,15 @@ if smarsh_debug_mode == "1" or smarsh_debug_mode == "true":
|
||||||
|
|
||||||
# argcmd contains the entire command line arguments as a space separated string
|
# argcmd contains the entire command line arguments as a space separated string
|
||||||
argcmd = " ".join(sys.argv)
|
argcmd = " ".join(sys.argv)
|
||||||
|
prompttxt = ""
|
||||||
prompttxt = "Suggest a linux shell command to accomplish the following: " + argcmd
|
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
|
completion = None
|
||||||
|
apioutput = None
|
||||||
if api_model == "text-davinci-003":
|
if api_model == "text-davinci-003":
|
||||||
|
if smarsh_dont_warn != True:
|
||||||
print("Using model " + api_model)
|
print("Using model " + api_model)
|
||||||
# Get the completion from OpenAI
|
# Get the completion from OpenAI
|
||||||
completion = openai.Completion.create(
|
completion = openai.Completion.create(
|
||||||
|
@ -39,16 +54,27 @@ if api_model == "text-davinci-003":
|
||||||
frequency_penalty=0,
|
frequency_penalty=0,
|
||||||
presence_penalty=0,
|
presence_penalty=0,
|
||||||
)
|
)
|
||||||
# print the output from davinci model to stdout
|
apioutput = completion['choices'][0]['text'].strip()
|
||||||
print(completion['choices'][0]['text'].strip())
|
|
||||||
elif api_model == "gpt-3.5-turbo":
|
elif api_model == "gpt-3.5-turbo":
|
||||||
completion = openai.ChatCompletion.create(
|
completion = openai.ChatCompletion.create(
|
||||||
model = api_model,
|
model = api_model,
|
||||||
messages = [
|
messages = [
|
||||||
{'role': 'user', 'content': prompttxt}
|
{'role': 'system', 'content': prompttxt}
|
||||||
],
|
],
|
||||||
temperature = 0
|
temperature = 0
|
||||||
)
|
)
|
||||||
# print the response to stdout
|
# 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)
|
||||||
|
|
Loading…
Reference in New Issue