-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatgpt.sh
executable file
·71 lines (63 loc) · 1.33 KB
/
chatgpt.sh
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
# shellcheck disable=SC2046
export $(grep -v '^#' "${DIR}/.env" | xargs)
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--max-tokens)
MAX_TOKENS="$2"
shift
shift
;;
-m|--model)
MODEL="$2"
shift
shift
;;
-n)
N_COMPLETIONS="$2"
shift
shift
;;
-t|--temperature)
TEMPERATURE="$2"
shift
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
USER_MD5=$(echo "$USER" | md5sum | cut -f1 -d" ")
if [ "$#" -gt 0 ]; then
INPUT="$1"
else
read -d '' INPUT
fi
INPUT=${INPUT//$'\n'/' '}
curl https://api.openai.com/v1/completions \
-s \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer ${OPENAI_API_KEY}" \
-d '{
"model": "'"$MODEL"'",
"prompt": "'"$INPUT"'",
"max_tokens": '"$MAX_TOKENS"',
"n": '"$N_COMPLETIONS"',
"temperature": '"$TEMPERATURE"',
"user": "'"$USER_MD5"'"
}' | jq -r ".choices[].text"