#ref. https://platform.openai.com/docs/api-reference/completions/create OPENAI_API_KEY=**** FILE="${1:-chatgpt-file.txt}" chat() { PROMPT="$1" RESPONSE=$( \ curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "'"$PROMPT"'"}], "temperature": 0.7 }' \ | jq '.choices[0].message.content' \ | perl -pe 's/^"// | s/"$//' \ ) echo "answer: $RESPONSE" >> $FILE echo >> $FILE echo "$RESPONSE" echo } file_contents=$(cat $FILE | perl -pe 's/\n/\\n/g' ) chat "$file_contents"