Skip to content

Instantly share code, notes, and snippets.

@pranavgade20
Last active April 21, 2023 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranavgade20/00a242c91134bfac3c8235f662dbe82c to your computer and use it in GitHub Desktop.
Save pranavgade20/00a242c91134bfac3c8235f662dbe82c to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NC='\033[0m'
messages=$(echo '[{"role": "system", "content": "You are CompileGPT v4.0.0., a compiler for C programs. You will read the user input as the program, and produce assembly code in AT&T syntax. Highlight any errors, nd do your best to logically fix them while warning about the changes made. Remember the compiler version will be CompileGPT v4.0.0."}]' | jq)
if [[ -z "$OPENAI_API_KEY" ]]; then
echo -ne "$RED"'Please run `export OPENAI_API_KEY="sk-xx", replacing sk-xx with your own API key.'"\n$NC"
exit
fi
line=$(cat)
messages=$(echo "$messages" | jq -c '. += [{"role": "user", "content": '"$(echo "$line" | jq -sR .)"'}]')
response=''
echo -ne "$RED""GPT:\n$NC"
response=$(curl --no-buffer -s -X POST -H 'Content-Type: application/json' -H 'Accept: text/event-stream' -H 'Authorization: Bearer '"$OPENAI_API_KEY" -d '{"model": "'"${CHAT_MODEL_NAME:="gpt-4"}"'", "messages": '"$messages"', "stream": true}' https://api.openai.com/v1/chat/completions > >(while read -r resp; do
if [[ $resp == *"delta\":{\"content"* ]]; then
content="$(echo -E "$resp" | cut -c 7- | jq '.choices[].delta.content' | sed "s/^\"\|\"$//g" | sed 's/\\"/"/g')"
echo -ne "$content"
fi
done) | tee /dev/tty)
echo "$response" | sed -n '/```assembly/,$p' | sed '/```$/q' | sed '$d' | tail -n +2 > a.S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment