Skip to content

Instantly share code, notes, and snippets.

@dpoulopoulos
Last active June 14, 2023 11:25
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dpoulopoulos/24d52f888988b58c86547a21001422a2 to your computer and use it in GitHub Desktop.
Save dpoulopoulos/24d52f888988b58c86547a21001422a2 to your computer and use it in GitHub Desktop.
OpenAI natural language shell example.
prompt = """
Input: Print the current directory
Output: pwd
Input: List files
Output: ls -l
Input: Change directory to /tmp
Output: cd /tmp
Input: Count files
Output: ls -l | wc -l
Input: Replace foo with bar in all python files
Output: sed -i .bak -- 's/foo/bar/g' *.py
Input: Push to master
Output: git push origin master
"""
template = """
Input: {}
Output:
"""
import os, click, openai
while True:
request = input(click.style('nlsh> ', 'red', bold=True))
prompt += template.format(request)
result = openai.Completion.create(
model='davinci', prompt=prompt, stop='/n', max_tokens=100, temperature=.0
)
command = result.choices[0]['text']
prompt += command
if click.confirm(f'>>> Run: {click.style(command, "blue")}', default=True):
os.system(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment