Skip to content

Instantly share code, notes, and snippets.

@kxzk
Last active April 2, 2023 20:49
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 kxzk/69faf0f3c9988efd47e6739af87fe5e6 to your computer and use it in GitHub Desktop.
Save kxzk/69faf0f3c9988efd47e6739af87fe5e6 to your computer and use it in GitHub Desktop.
function hey_gpt
# usage: hey_gpt tell me a joke about a dog
set prompt \'(echo $argv | string join ' ')\'
set gpt (curl https://api.openai.com/v1/chat/completions -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_KEY" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "'$prompt'"}],
"temperature": 0.7,
"stream": true
}')
for text in $gpt
if test $text = 'data: [DONE]'
break
else if string match -q --regex "role" $text
continue
else if string match -q --regex "content" $text
echo -n $text | string replace 'data: ' '' | jq -r -j '.choices[0].delta.content'
else
continue
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment