Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pycckuu/576fefd979db2c5cafc60553ceec08a1 to your computer and use it in GitHub Desktop.
Save pycckuu/576fefd979db2c5cafc60553ceec08a1 to your computer and use it in GitHub Desktop.
This script defines two functions that allow a user to interact with OpenAI's GPT models via the command line: gpt(): This function takes a query as an argument, sends it to the GPT-3.5 model, and returns a response. It uses the API key stored in the environment variable OPENAI_API_KEY to authenticate the request. The response is printed in the …
export OPENAI_API_KEY=sk-...
# ask gpt anything in the terminal and get a response
function gpt() {
local url="https://api.openai.com/v1/chat/completions"
local model="gpt-3.5-turbo"
local system="Offer a brief reply to the question. Format everything using ANSI escape codes for color, bold, and underline. Do not use backticks."
echo using $model model "\n"
local body="{\"model\":\"$model\", \"messages\":[{\"role\": \"system\", \"content\": \"$system\"}, {\"role\": \"user\", \"content\": \" Answer the query: $1\"}]}"
local headers="Content-Type: application/json"
local auth="Authorization: Bearer ${OPENAI_API_KEY}"
local resp=$(curl -s -H "$headers" -H "$auth" -d "$body" "$url" | jq -r '.choices[0].message.content')
echo -e $resp
}
# ask gpt terminal commands. Script asks if you want to execute it
function gptc() {
local url="https://api.openai.com/v1/chat/completions"
local model="gpt-4"
local system="Act as a zsh terminal transactor, I give an instruction in English and you reply with a shell command that accomplishes that instruction. Output only the command. Do not use backticks."
echo using $model model "\n"
local body="{\"model\":\"$model\", \"temperature\": "0", \"messages\":[{\"role\": \"system\", \"content\": \"$system\"}, {\"role\": \"user\", \"content\": \" Answer the query: $1\"}]}"
local headers="Content-Type: application/json"
local auth="Authorization: Bearer ${OPENAI_API_KEY}"
local resp=$(curl -s -H "$headers" -H "$auth" -d "$body" "$url" | jq -r '.choices[0].message.content')
echo -e $resp
echo -n "Execute?: "
read answer
if [[ $answer == "y" ]];
then eval "${resp}";
else echo "Command not executed";
fi
}
@pycckuu
Copy link
Author

pycckuu commented May 4, 2023

This script defines two functions that allow a user to interact with OpenAI's GPT models via the command line:

gpt(): This function takes a query as an argument, sends it to the GPT-3.5 model, and returns a response. It uses the API key stored in the environment variable OPENAI_API_KEY to authenticate the request. The response is printed in the terminal.

gptc(): This function is similar to gpt(), but it is designed to provide terminal commands instead of responses to text queries. After the command is generated, the function prompts the user to confirm if they want to execute it. If the user enters "y", the command is executed in the terminal. If the user enters anything else, the command is not executed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment