-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpt
executable file
·46 lines (38 loc) · 931 Bytes
/
gpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
set -o errexit
model="gpt-3.5-turbo"
curl_gpt() {
local prompt="$(jq -R -s '.' <<< "$1")"
local json=$(cat <<-EOT
{
"model": "$model",
"messages": [{"role": "user", "content": $prompt}],
"temperature": 0.7,
"stream": true
}
EOT
)
curl https://api.openai.com/v1/chat/completions -s \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_KEY" \
-d "$json"
}
prompt="$*"
if [[ $# == 0 ]]; then
prompt=$(</dev/stdin)
fi
data=$(curl_gpt "$prompt" | tee gpt.txt)
# data=$(cat gpt.txt)
readarray -t lines <<<"$data"
for line in "${lines[@]}"; do
case "$line" in
*DONE*)
break;;
*role*)
continue;;
*content*)
echo -n "$line" | sed 's/data: //' | tee -a a.txt | jq -r -j '.choices[0].delta.content';;
*)
continue;;
esac
done