Skip to content

Instantly share code, notes, and snippets.

@foloinfo
Last active January 17, 2023 23:35
Show Gist options
  • Save foloinfo/3d868802c95080989b3e11bfa85699c4 to your computer and use it in GitHub Desktop.
Save foloinfo/3d868802c95080989b3e11bfa85699c4 to your computer and use it in GitHub Desktop.
OpenAI cli client script with streaming output
export OPENAI_API_KEY=sk-xxxxxxx # your API key
ask(){
ask_openai $1 | openai_parse_stream -
echo ''
}
ask_openai(){
curl -N \
-H "Accept: text/event-stream" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"text-davinci-003\",
\"prompt\": \"$1\",
\"temperature\": 0.7,
\"max_tokens\": 256,
\"top_p\": 1,
\"frequency_penalty\": 0,
\"presence_penalty\": 0,
\"stream\": true
}" https://api.openai.com/v1/completions \
-s
}
openai_parse_stream(){
cut -c 7- | sed 's/\[DONE\]//' | jq --stream -r -j 'fromstream(1|truncate_stream(inputs))[0].text'
}
$ ask 'What is jq library?'
jq is a lightweight and flexible command-line JSON processor. It is used for transforming and filtering JSON data. It can also be used to parse and inspect JSON data, making it a powerful tool for working with web APIs and other JSON data sources.%
@foloinfo
Copy link
Author

foloinfo commented Jan 13, 2023

I don't know how can I remove first two lines of \n without interrupting the stream.
It also seems slower than the actual stream, only output is visible few lines at a time.

If anyone know better solution, please let me know in the comment!

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