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
.envfile 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 ./)
that won't work for me, since I generally store it in the
.envfile that I read in the lineset -o allexport; source .env; set +o allexport