Skip to content

Instantly share code, notes, and snippets.

@RA80533
Forked from dpoulopoulos/nlsh.py
Created May 8, 2024 01:20
Show Gist options
  • Save RA80533/e62d820c664611e92fa04e800ebb248b to your computer and use it in GitHub Desktop.
Save RA80533/e62d820c664611e92fa04e800ebb248b 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