Last active
January 17, 2023 23:35
-
-
Save foloinfo/3d868802c95080989b3e11bfa85699c4 to your computer and use it in GitHub Desktop.
OpenAI cli client script with streaming output
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-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' | |
} |
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
$ 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.% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!