Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdothtml committed May 30, 2024
0 parents commit 6dbdea1
Show file tree
Hide file tree
Showing 14 changed files with 883 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ignore these files when generating release tarballs
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
CONTRIBUTING.md export-ignore
install.sh export-ignore
Pipfile export-ignore
Pipfile.lock export-ignore
README.md export-ignore
release.sh export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
**/__pycache__/
vendor/
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

## Running locally

First, install the dependencies (**note**: make sure you're using python 3 and pip 3):

```sh
# install pipenv
brew install pipenv

# install both runtime and dev dependencies
pipenv install --dev

# activate the virtual env
pipenv shell
```

Now you can run the tool via:

```sh
python -m gpt_cmd [...]
```

## Cutting a release

Currently this just updates the `vendor` tarball (only needed if deps were added/upgraded):

```sh
./release.sh
```

This script requires all the tools mentioned in the [Running locally](#running-locally) section.
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
openai = "*"

[dev-packages]
black = "*"

[requires]
python_version = "3"
281 changes: 281 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# gpt-cmd

> Sit back and let ChatGPT figure out what commands need to be run
> [!WARNING]
> It's always risky to execute commands directly from a third party. Use responsibly and at your own risk.
```sh
gpt_cmd "Install python3 and pip3 and symlink them to python and pip"

gpt_cmd "You're running on an alpine linux distro that already has wget and bash installed. Install kafka"

gpt_cmd "Install SQLite and write a script to verify it's working correctly"
```

### Problem

> I'm working on a fresh Docker image, and I can't remember the specific commands needed to install tools. I can ask ChatGPT, but it might give me an answer that doesn't work with the exact environment I'm running in (e.g. differences between linux distros). Why can't ChatGPT just figure all that out for me?
The main problem is that, a lot of the time, to determine what exactly needs to be run (e.g. to install kafka in a Docker image), you need to try running commands to see if they fail, or run commands to probe the system to determine what tools are available.

For example, sometimes `sudo` doesn't exist in Docker, and since it runs as the root user by default, you can just leave it out. ChatGPT has no problem deducing that it can just remove the `sudo` in those cases.

### Solution

Let ChatGPT iteratively run commands for you to achieve your end goal.

The way it works is that you provide some end goal (e.g. 'Install python3 and pip3'), and ChatGPT will respond with a list of commands it wants to run. The tool will execute these commands and respond to ChatGPT with the stdout and exit code of each. Once ChatGPT thinks it's done, it'll respond accordingly and the loop will end.

With this approach, ChatGPT is able to probe your system and try running commands, responding to potential failures as they happen with alternative commands.

## Install

> [!WARNING]
> In light of my other warning above, this install script is pulled directly from my GitHub repo, and is a potential vulnerability if the repo (or GitHub) becomes compromised. Always inspect scripts for shady behavior before running them on your device (even mine: [install.sh](https://raw.githubusercontent.com/chrisdothtml/gpt-cmd/main/install.sh)).
**NOTE**: the install script and tool require: `python` (v3), `bash`, and either `curl` or `wget`

```sh
curl -s https://raw.githubusercontent.com/chrisdothtml/gpt-cmd/main/install.sh | bash

# or if you prefer wget
wget -qO- https://raw.githubusercontent.com/chrisdothtml/gpt-cmd/main/install.sh | bash
```

See [Env var overrides](#env-var-overrides) section for changing the install dir.

The install script will write to your `.profile` file to expose it to your `$PATH`, but if that doesn't work, you'll have to manually add it your path:

```sh
# replace `$HOME` with your custom install dir if you used one
export PATH="$HOME/gpt_cmd/bin:$PATH"
```

## Use

Before running, you need to create an `~/OPENAI_TOKEN` file and put your token in it.

```sh
gpt_cmd <goal>

# see `Env var overrides` section below for full list
GPT_CMD_MODEL="gpt-4-turbo" gpt_cmd <goal>
GPT_CMD_TOKEN_FILE_PATH="/my/token/file" gpt_cmd <goal>

# print path to the dir containing message logs for your previous runs
gpt_cmd --get-convos-dir
```

The `goal` can be literally anything you can achieve via a terminal (which is a lot). Even if it takes dozens of commands to get there, it'll eventually get there (maybe). You can be as descriptive or vague as you want, and list as many different tasks as you want (e.g. 'Install [some tool] and then write a starter script with some examples of how to use it').

## Env var overrides

Enironment vars that you can provide to change the behavior of the tool.

### `GPT_CMD_INSTALL_DIR`

Override the dir the installer puts the tool in.

**Default**: home dir

**Example**:

```sh
curl -s [...] | GPT_CMD_INSTALL_DIR="/my/custom/dir" bash
```

### `GPT_CMD_MODEL`

Override the gpt model used by the tool.

**Default**: `gpt-4o`

### `GPT_CMD_TOKEN_FILE_PATH`

Override the file path the tool gets your OpenAI token from.

**Default**: `~/OPENAI_TOKEN`

### `GPT_CMD_DANGEROUSLY_SKIP_PROMPTS`

By default, the tool will prompt you before running each command, as giving an AI unfettered access to run commands on your system is usually a bad idea. However, if you're on a throwaway machine/container and don't care what happens to it, you can disable the prompts via `GPT_CMD_DANGEROUSLY_SKIP_PROMPTS=true`.

## License

[MIT](license)
31 changes: 31 additions & 0 deletions bin/gpt_cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e

project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

function quit_python_required() {
echo "ERROR: python 3 is required to run gpt_cmd" >&2
exit 1
}

function get_python_bin() {
if command -v python3 >/dev/null; then
echo "python3"
return 0
elif command -v python >/dev/null; then
major_version="$(python --version | grep -oE '[0-9]+' | head -1)"
if [ "$major_version" -ne 3 ]; then
quit_python_required
fi

echo "python"
return 0
fi

quit_python_required
}

export PYTHONPATH="$project_root/vendor"
export GPT_CMD_PROJECT_ROOT="$project_root"
$(get_python_bin) "$project_root/gpt_cmd.py" "$@"
Loading

0 comments on commit 6dbdea1

Please sign in to comment.