Created
May 4, 2023 19:34
-
-
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 …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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