Skip to content

Commit

Permalink
prompt to run command by default
Browse files Browse the repository at this point in the history
  • Loading branch information
johannr-dev committed Mar 5, 2023
1 parent 75bca24 commit 2101c4d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2023 wunderwuzzi23
Greetings from Seattle!

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ cd yolo-ai-cmdbot
pip install -r requirements.txt
chmod +x yolo.py
alias yolo=$(pwd)/yolo.py
#source launch.sh
alias computer=$(pwd)/yolo.py #optional
```

Another option is to run `source install.sh` after cloning the repo. That does the following:
1. Copies the necessary files to `~/yolo-ai-cmdbot/`
2. Creates two aliases `yolo` and `computer` pointint to `~/yolo-ai-cmdbot/yolo.py`


# macOS

I haven't tested it yet, but it should just work :)

# Windows

Windows is less tested, it does work though and will use PowerShell.
Expand All @@ -25,37 +34,53 @@ There are two ways to configure the key:

# Using yolo

Here are a couple of examples on how this utility can be used.
By default `yolo` will prompt the user before executing commands.

**WARNING**: By default the command that comes back from ChatGPT will be immediatly executed (yolo!).
## Disabling the safety switch!

If you want to inspect the command that is executed, add the `-a` argument, e.g `yolo -a delete the file test.txt`.
To disable the default behavior and have yolo run commands right away when they come back from ChatGPT create a file named `~/.yolo-safety-off`

A simple command to do that on Linux would be:

```
touch ~/.yolo-safety-off
```

If you still want to inspect the command that is executed when safety is off, add the `-a` argument, e.g `yolo -a delete the file test.txt`.

Let's go!

## Examples

Here are a couple of examples on how this utility can be used.

More examples:

```
yolo whats the time?
yolo whats the time in UTC
yolo whats the date and time in Vienna Austria
yolo show me some unicode characters
yolo what is my user name and what's my machine name?
yolo what is my user name and whats my machine name?
yolo is there a nano process running
yolo download the homepage of ycombinator.com and store it in index.html
yolo find all unique urls in index.html
yolo create a file named test.txt and write my user name into it
yolo print the contents of the test.txt file
yolo -a delete the test.txt file
yolo whats the current price of Bitcoin in USD
yolo whats the current price of Bitcoin in USD. Extract the price only
yolo whats the current price of Bitcoin in USD. Ext the price only
yolo look at the ssh logs to see if any suspicious logons accured
yolo look at the ssh logs and show me all recent logins
yolo is the user hacker logged on right now?
yolo do i have a firewall running?
yolo create a hostnames.txt file and add 10 typical hostnames based on planet names to it, line by line, then show me the contents
yolo find any file with the name yolo.py. do not show permission denied errors
yolo write a new bash script file called scan.sh, with the contents to iterate over hostnames.txt and invokes a default nmap scan on each host. then show me the file.
yolo write a new bash script file called scan.sh, with the contents to iterate over hostnames.txt and invokes a default nmap scan on each host. then show me the file. Make it over multiple lines with comments and annotiations.
```

# Thanks!

# License

MIT. No Liability. No Warranty. But lot's of fun.
13 changes: 12 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
alias yolo=$(pwd)/yolo.py
# Installs yolo in the user's home directory

TARGET_DIR=~/yolo-ai-cmdbot
TARGET_FULLPATH=$TARGET_DIR/yolo.py

mkdir -p $TARGET_DIR
cp yolo.py prompt.txt $TARGET_DIR
chmod +x $TARGET_FULLPATH

# Creates two aliases for use
alias yolo=$TARGET_FULLPATH
alias computer=$TARGET_FULLPATH
48 changes: 35 additions & 13 deletions yolo.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
#!/usr/bin/env python3

# MIT License
# Copyright (c) 2023 wunderwuzzi23
# Greetings from Seattle!

import os
import openai
import sys
import subprocess
from termcolor import colored
from colorama import init


ask = False # safety switch -a
yolo = "" # user's answer to safety switch
command_start_idx = 1 # command starts at which argv index?
yolo_safety_switch = True # by default, user is prompted to run each command
ask_flag = False # safety switch -a command line argument
yolo = "" # user's answer to safety switch (-a) question y/n
command_start_idx = 1 # command starts at which argv index?
home_path = os.path.expanduser("~")

# Two options for the user to specify they openai api key
openai.api_key = os.getenv("OPENAI_API_KEY")
home_path = os.path.expanduser("~")
openai.api_key_path = home_path+"/.openai.apikey"
openai.api_key_path = os.path.join(home_path,".openai.apikey")

# Check if the user globally disabled the safety switch
yolo_safety_off_path = os.path.join(home_path,".yolo-safety-off")
if os.path.exists(yolo_safety_off_path):
yolo_safety_switch = False
else:
yolo_safety_switch = True

# parsing weirdness
# Parsing weirdness
if len(sys.argv) < 2:
print("Usage Example: yolo [-a] list the current directory information")
print("Yolo 0.1 - by @wunderwuzzi23")
print()
print("Usage: yolo [-a] list the current directory information")
print("Argument: -a: Prompt the user before running the command")
print()
print("Current safety switch setting (~/.yolo-safety-off) is " + str(yolo_safety_switch))
sys.exit(-1)

# safety switch (no yolo mode)
Expand All @@ -45,14 +59,22 @@
shell = os.environ.get("SHELL", "powershell.exe")

# Construct the prompt
pre_prompt = open("prompt.txt","r").read()

## Find the executing directory (e.g. in case an alias is set)
## So we can find the prompt.txt file
yolo_path = os.path.abspath(__file__)
prompt_path = os.path.dirname(yolo_path)

## Load the prompt and prep it
prompt_file = os.path.join(prompt_path, "prompt.txt")
pre_prompt = open(prompt_file,"r").read()
pre_prompt = pre_prompt.replace("{}", shell)
prompt = pre_prompt + user_prompt

#make the first line also the system prompt
## make the first line also the system prompt
system_prompt = pre_prompt[1]

#be nice and make it a question
# be nice and make it a question
if prompt[-1:] != "?":
prompt+="?"

Expand Down Expand Up @@ -80,13 +102,13 @@
print(colored("There was an issue: "+res_command, 'red'))
sys.exit(-1)

#odd corner case, sometimes ChatCompletion returns markdown
# odd corner case, sometimes ChatCompletion returns markdown
if res_command.count("```",2):
print(colored("The proposed command contains markdown, so I thought to not execute the response directly: \n", 'red')+res_command)
sys.exit(-1)

print("Command: " + colored(res_command, 'blue'))
if ask == True:
if yolo_safety_switch == True or ask_flag == True:
print("Execute the command? Y/n ==> ", end = '')
yolo = input()
print()
Expand Down

0 comments on commit 2101c4d

Please sign in to comment.