-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwe.sh
executable file
·189 lines (167 loc) · 5.53 KB
/
qwe.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
function qwe() {
# Set variables
pc_info=$(uname -s)
model_name="gpt-3.5-turbo"
max_tokens="255"
api_url="https://api.openai.com/v1/chat/completions"
help_string="
Usage: qwe [OPTIONS] 'What do you want to do'
This command interacts with the OpenAI API.
Options:
-h, --help Show this help message and exit
-v, --verbose Enable debug mode, providing additional output for troubleshooting
-t, --tokens TOKENS Set the maximum number of tokens for the API request (default: 255)
-m, --model MODEL Set the model for the API request (default: 'gpt-3.5-turbo')
-p, --pc_info PC_INFO Set the PC info (default: output of 'uname -s')
-s, --skip For debugging. Won't connect to API
-sk, --set_key API_KEY Save OpenAI key for future use in ~/.bash_profile
-k, -key API_KEY Set OpenAI key only for this command
Examples:
qwe -t 200 -m 'text-davinci-002'
qwe --verbose --pc_info 'My Custom PC Info and verbose mode'
"
verbose_mode=0
#This if for when debugging i dont wanna waste tokens
#0 = returns before curl request
#1 = runs normally
connect_to_api=1
#Clearning the PARAMS
PARAMS=""
# Parse options
while (( "$#" )); do
case "$1" in
-h|--help)
echo "$help_string"
return
;;
-v|--verbose)
verbose_mode=1
shift
;;
-t|--tokens)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
max_tokens=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
return
fi
;;
-m|--model)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
model_name=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
return
fi
;;
-p|--pc_info)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
pc_info=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
return
fi
;;
-s|--skip)
connect_to_api=0
shift
;;
-sk|--set_key)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
echo "export OPENAI_API_KEY=\"$2\"" >> ~/.bash_profile
source ~/.bash_profile
echo "OpenAI key saved."
shift 2
else
echo "Error: Argument for $1 is missing" >&2
return
fi
;;
-k|--key)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
OPENAI_API_KEY=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
return
fi
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
return
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
if [[ "$verbose_mode" == 1 ]]; then
echo "verbose (debug) mode on"
fi
# Check if the API key is set
if [ -z "$OPENAI_API_KEY" ]; then
source ~/.bash_profile
if [ -z "$OPENAI_API_KEY" ]; then
echo "Error: OPENAI_API_KEY is not set. Please set your OpenAI API key."
return
fi
fi
# Check if input is provided as a command-line argument
if [ -z "$PARAMS" ]; then
echo "qwe: try 'qwe --help' for more information"
return
fi
# Set variables
input_text="$PARAMS"
if [[ "$verbose_mode" == 1 ]]; then
echo "Variables before sending curl request"
echo "verbose_mode:$verbose_mode"
echo "input_text:$input_text"
echo "pc_info:$pc_info"
echo "model_name:$model_name"
echo "max_tokens:$max_tokens"
echo "api_url:$api_url"
fi
if [[ "$connect_to_api" == 0 ]]; then
echo "Ending the command"
return
fi
# Make the API request using curl
response=$(
curl -s -X POST "$api_url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"messages": [
{
"role": "user",
"content": "answer only with the command. This is what the command should do: '"$input_text"'. Info about the pc: '"$pc_info"'"
}
],
"model": "'"$model_name"'",
"max_tokens": '$max_tokens'
}'
)
# Extract and print the generated text from the response
command_to_run=$(echo $response | jq -r '.choices[0].message.content')
#Check if the command is not empty
if [[ "$command_to_run" == "null" ]]; then
echo "Request to openai failed"
return
fi
#Show user the command
echo "$command_to_run"
# Ask user if he wants to run command
printf 'Run this command (y/n)? '
read answer
# Run command if he says "y"
if [ "$answer" != "${answer#[Yy]}" ]; then
eval $command_to_run
fi
}
qwe "$@"