|
That seems handy, maybe this way I can keep using jq on the command line without ever having to learn jq's syntax. I made a pure bash version since I don't always have php installed: gpt () {
api_key=$(cat ~/.config/openai.key)
verbose=false
if [[ $1 = -v ]]; then
verbose=true
shift
fi
userinput="$*"
# shellcheck disable=SC2016
system='You are an interactive shell command line shell agent.
You just get things done, rather than trying to explain.
Do your best to respond with 1 command that will meet the requirements.
Start a line with `$ ` to have it sent directly to the shell VERBATIM.
All other output is just echoed.
Favor 1 line shell commands.
Be terse.
Important: Every command you output will automatically be executed in this env: bash'
systemj="$(jq -Rs . <<<"${system}")"
userinputj="$(jq -Rs . <<<"${userinput}")"
payload='{ "model": "gpt-3.5-turbo",
"messages": [{"role":"system", "content": '"${systemj}"'},
{"role": "user", "content": '"${userinputj}"'}],
"temperature": 0.15,
"stream":false }'
if $verbose; then
set -x
fi
if ! res=$(curl -Ss -f https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${api_key}" \
-d "${payload}"); then
echo "ERROR: ${res}" >&2
return 1
else
$verbose && echo "RESULT: ${res}"
jq -r '.choices[0].message.content' <<<"${res}"
fi
set +x
}
-----I even tried using GPT to help me develop it! $ gpt -v get the actual completion from an openai /completions request using jq
RESULT: {
"id": "chatcmpl-8IZvb4lQaFgcDdnSnsU4t6zhFRrbN",
"object": "chat.completion",
"created": 1699438703,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "$ jq '.choices[0].text'"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 116,
"completion_tokens": 9,
"total_tokens": 125
}
}
… there's probably a lesson in there. |