This gpt()
function reads input from the command line and then sends a formatted JSON object to the OpenAI API for the GPT-4 model. It properly escapes special characters and checks for the .env
file containing the OpenAI API key.
Usage:
- Load the function into your environment:
source gpt.sh
. - Make sure to have an
.env
file in the current directory with theOPENAI_API_KEY
. - Pipe anything itno it, and also add a prompt, example:
git diff main | gpt "As a programmer, review this diff. Provide feedback only if necessary. Be brief"
Or, to specify the model:
git diff main | gpt -m gpt-4-32k -p "As a programmer, review this diff. Provide feedback only if necessary. Be brief"
Or you can use <<<
to make it easier to switch what you want to pipe into the gpt command:
gpt -m gpt-4-32k -p "As a programmer, review this diff. Provide feedback only if necessary. Be brief" <<< $(git diff main ./)
My silly patch is to make the check for the OPENAI_API_KEY first thing, don't bother doing any work if this isn't set.