-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgpt.sh
executable file
Β·449 lines (395 loc) Β· 12.1 KB
/
gpt.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/bin/bash
#bash script to run gpt-4o
#cd to the directory of the script
cd "$(dirname "$0")"
#load from env variable
if [ -f .gitpmoji.env ]; then
source .gitpmoji.env
fi
# load from env variable
API_KEY=$GITPMOJI_API_KEY
API_BASE_URL=${GITPMOJI_API_BASE_URL:-https://api.openai.com/v1}
API_MODEL=${GITPMOJI_API_MODEL:-gpt-4o}
# check if API_KEY is set
if [ -z "$API_KEY" ]; then
echo "GITPMOJI_API_KEY is not set"
exit 1
fi
# check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq could not be found, please install it"
exit 1
fi
# Function to display help message
display_help() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -h Display this help message"
echo " -d Pipe diff to stdin"
echo " -f DIFF.file Specify the diff file. Will be used to generate the commit message"
echo " -g Generate the commit message (and emoji)"
echo " -a Assess the diff or file."
echo " -r Give starts the rating of the assessment only"
echo " -w Output the assessment in Markdown format"
echo " -m \"MESSAGE\" Specify the commit message"
echo " -e Will analyze message and add the emoji"
echo " -v Verbose mode"
echo
echo "Example:"
echo " $0 -e -m \"Implement new feature\""
}
# Parse command line arguments
DIFF_CONTENT=""
DIFF_FILE=""
GENERATE=false
ASSESS=false
RATING=false
MARKDOWN=false
MESSAGE=""
RESULT=""
VERBOSE=false
EMOJI=false
while getopts "hdf:garwm:ev" opt; do
case $opt in
h)
display_help
exit 0
;;
d)
DIFF_CONTENT=$(cat)
;;
f)
DIFF_FILE="$OPTARG"
;;
g)
GENERATE=true
;;
a)
ASSESS=true
;;
r)
RATING=true
;;
w)
MARKDOWN=true
;;
m)
MESSAGE="$OPTARG"
;;
e)
EMOJI=true
;;
v)
VERBOSE=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
display_help
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
display_help
exit 1
;;
esac
done
# Shift the parsed options out of the argument list
shift $((OPTIND-1))
if [ "$VERBOSE" = true ]; then
echo -e "DIFF_FILE: $DIFF_FILE"
echo -e "DIFF_CONTENT: $DIFF_CONTENT"
echo -e "ASSESS: $ASSESS"
echo -e "RATING: $RATING"
echo -e "MARKDOWN: $MARKDOWN"
echo -e "MESSAGE: $MESSAGE"
echo -e "EMOJI: $EMOJI"
fi
# Check if both emoji and message are provided
if [ "$GENERATE" = true ] && [ -z "$DIFF_CONTENT" ] && [ -z "$DIFF_FILE" ] && [ -z "$MESSAGE" ]; then
echo "At least one of the following options is required: -d, -f, -m"
display_help
exit 1
fi
# Check if both emoji and message are provided
if [ "$ASSESS" = true ] && [ -z "$DIFF_CONTENT" ] && [ -z "$DIFF_FILE" ]; then
echo "At least one of the following options is required: -d, -f for assessment"
display_help
exit 1
fi
get_diff_content() {
if [ "$VERBOSE" = true ]; then
echo -e "get_diff_content"
fi
#read diff from file
if [ ! "$DIFF_CONTENT" ] && [ "$DIFF_FILE" ]; then
if [ ! -f "$DIFF_FILE" ]; then
echo "no such file $DIFF_FILE"
exit 1
fi
DIFF_CONTENT=$(cat $DIFF_FILE)
fi
# Check the size of DIFF_CONTENT
if [ ${#DIFF_CONTENT} -gt 100000 ]; then
echo "Error: The diff is too large. Maximum allowed is 100000 characters. (30000 tokens)"
exit 1
fi
}
check_for_errors() {
local response=$1
ERROR=$(echo $response | jq -r '.error.message')
if [ "$ERROR" == 'null' ]; then
return
fi
if [ "$ERROR" ]; then
echo -e "ERROR: $ERROR"
echo -e "RESPONSE: $response"
exit 1
fi
}
generate_message() {
if [ "$VERBOSE" = true ]; then
echo -e "generate_message"
fi
get_diff_content
# Prepare the data for the API call
SYSTEM_PROMPT="You are a system that generates git commit messages from diff.
You will be given a diff and your task is to generate a git commit message.
You will provide only one commit message for each diff.
Your answer should contain only single commit message, nothing else.
Use english language only.
Use multiple lines for the response.
Try to use maximum 100 words in the response.
"
PREFIX_RX="\""
JSON='{
"model": $api_model,
"messages": [
{
"role": "system",
"content": $system_prompt
},
{
"role": "user",
"content": $prompt
}
],
"max_tokens": 200,
"temperature": 0.999,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}'
DATA=$(jq -n --arg system_prompt "$SYSTEM_PROMPT" --arg prompt "$DIFF_CONTENT" --arg api_model "$API_MODEL" "$JSON")
# Make the API call
RESPONSE=$(curl -s \
-X POST "$API_BASE_URL/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$DATA")
check_for_errors "$RESPONSE"
# Extract and display the answer
GPT_MESSAGE=$(echo $RESPONSE | jq -r '.choices[0].message.content' | sed 's/^"//;s/"$//')
if [ -z "$MESSAGE" ]; then
MESSAGE=$(echo -e "${GPT_MESSAGE}")
else
MESSAGE=$(echo -e "${MESSAGE}""${GPT_MESSAGE}")
fi
RESULT=$(echo -e "${MESSAGE}")
}
generate_emoji() {
if [ "$VERBOSE" = true ]; then
echo -e "generate_emoji"
fi
# Prepare the data for the API call
SYSTEM_PROMPT="You are a system that generates emoji for incoming messages.
You will be given a message and your task is to generate an emoji that best represents the message.
You will provide only one emoji for each message.
Your answer should contain only single emoji, nothing else.
If possible, use the emoji that is already in the message.
If possible, use the emoji from the list below:
| Emoji | Message |
|-------|-------------|
| π | Begin a project. start new priject. initial commit |
| πͺ² | Fix a bug. bugfix |
| π | Critical bug fix. hotfix. |
| β¨ | Introduce new features. |
| π | Add or update documentation. |
| π | Deploy stuff. |
| π | Add or update the UI and style files. |
| π¨ | Improve structure. cosmetic changes |
| π§Ή | Run linter or formatter |
| β‘ | Improve performance. |
| ποΈ | Deprecate code. Remove code or files.|
| β
| Add, update, or pass tests. unit-tests |
| π | Fix security issues. |
| π | Add or update secrets. |
| π | Release / Version tags. |
| π¨ | Fix compiler / linter warnings. |
| π§ | Work in progress. |
| π | Fix CI Build. |
| β¬οΈ | Downgrade dependencies. |
| β¬οΈ | Upgrade dependencies. |
| π | Pin dependencies to specific versions. |
| π· | Add or update CI build system. |
| π | Add or update analytics. |
| β»οΈ | Refactor code. |
| β | Add a dependency. |
| β | Remove a dependency. |
| π§ | Add or update configuration files. |
| π¨ | Add or update development scripts. |
| π | Internationalization and localization. |
| βοΈ | Fix typos. |
| βͺ | Revert changes. |
| π | Merge branches. |
| π¦ | Add or update compiled files or packages. |
| π½ | Update code due to external API changes. |
| π | Move or rename resources. |
| π | Add or update license. |
| π₯ | Introduce breaking changes. |
| π± | Add or update assets. |
| βΏ | Add or improve accessibility. |
| π‘ | Add or update comments in source code. |
| π― | Add or update text and literals. |
| π | Perform database changes. |
| π₯ | Add or update contributor(s). |
| πΈ | Improve user experience. |
| π | Make architectural changes. |
| π± | Work on responsive design. |
| π€‘ | Mock things. |
| π | Add or update a .gitignore file. |
| πΈ | Add or update snapshots. |
| π·οΈ | Add or update types. |
| π© | Add or update feature flags. |
| π₯
| Catch errors. |
| π« | Add or update animations. |
| π | Work on authorization. |
| π©Ή | Simple fix for a non-critical issue. |
| π§ | Data exploration/inspection. |
| β°οΈ | Remove dead code. |
| π§ͺ | Add a failing test. |
| π | Add or update business logic. |
| π©Ί | Add or update healthcheck. |
| 𧱠| Infrastructure changes. |
| π§βπ» | Improve developer experience. |
"
PREFIX_RX="\""
JSON='{
"model": $api_model,
"messages": [
{
"role": "system",
"content": $system_prompt
},
{
"role": "user",
"content": $prompt
}
],
"max_tokens": 100,
"temperature": 0.999,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}'
DATA=$(jq -n --arg system_prompt "$SYSTEM_PROMPT" --arg prompt "$MESSAGE" --arg api_model "$API_MODEL" "$JSON")
# Make the API call
RESPONSE=$(curl -s \
-X POST "$API_BASE_URL/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$DATA")
check_for_errors "$RESPONSE"
# Extract and display the answer
EMOJI=$(echo $RESPONSE | jq -r '.choices[0].message.content' | sed 's/^"//;s/"$//')
PREFIX="###"
# check if GITPMOJI_PREFIX_RX is set
GITPMOJI_PREFIX_RX=$GITPMOJI_PREFIX_RX
if [ -z "$GITPMOJI_PREFIX_RX" ]; then
PREFIX="###"
else
PREFIX=$GITPMOJI_PREFIX_RX
fi
RESULT=$(echo -e "${MESSAGE}" | sed "1s/^\($PREFIX\)\{0,1\}\(.*\)$/\1$EMOJI \2/")
}
assess_diff() {
if [ "$VERBOSE" = true ]; then
echo -e "assess_diff"
fi
get_diff_content
# Prepare the data for the API call
SYSTEM_PROMPT="You are a system that evaluate code quality and assesses the git diff.
You will be given a diff and your task is to assess this diff.
Analyze code changes in the diff and provide a detailed evaluation of the changes.
You will provide only one assessment for each diff.
Based on the provided git diff evaluate the code changes on several factors: code cleanliness, structure, readability, complexity, and overall code quality.
Use english language for the response only.
Use multiple lines for the response.
Try to use maximum 250 words in the response.
Add the final rating on the scale from 1 to 10 at the end of the response.
Use 10 emoji β and π© to indicate the rating.
For example: βββββββπ©π©π© means 7 out of 10 and ββββββββββ means 10 out of 10.
"
if [ "$RATING" = true ]; then
RATING_PROMPT="
Your answer should contain only the rating (10 emoji) and nothing else.
"
SYSTEM_PROMPT=$(echo -e "${SYSTEM_PROMPT}""${RATING_PROMPT}")
elif [ "$MARKDOWN" = true ]; then
RATING_PROMPT="
Provide all the answer in Markdown format.
"
SYSTEM_PROMPT=$(echo -e "${SYSTEM_PROMPT}""${RATING_PROMPT}")
else
RATING_PROMPT="
Provide the answer in plain text format no additional formatting required. Do not use any Markdown formatting.
"
SYSTEM_PROMPT=$(echo -e "${SYSTEM_PROMPT}""${RATING_PROMPT}")
fi
JSON='{
"model": $api_model,
"messages": [
{
"role": "system",
"content": $system_prompt
},
{
"role": "user",
"content": $prompt
}
],
"max_tokens": 500,
"temperature": 1,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}'
DATA=$(jq -n --arg system_prompt "$SYSTEM_PROMPT" --arg prompt "$DIFF_CONTENT" --arg api_model "$API_MODEL" "$JSON")
# Make the API call
RESPONSE=$(curl -s \
-X POST "$API_BASE_URL/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$DATA")
check_for_errors "$RESPONSE"
# Extract and display the answer
GPT_MESSAGE=$(echo $RESPONSE | jq -r '.choices[0].message.content' | sed 's/^"//;s/"$//')
if [ -z "$RESULT" ]; then
RESULT=$(echo -e "${GPT_MESSAGE}")
else
RESULT=$(echo -e "${RESULT}" && echo -e "${GPT_MESSAGE}")
fi
}
if [ "$GENERATE" = true ] && ([ "$DIFF_CONTENT" ] || [ "$DIFF_FILE" ]); then
generate_message
fi
if [ "$EMOJI" = true ]; then
generate_emoji
fi
if [ "$ASSESS" = true ]; then
assess_diff
fi
echo -e "${RESULT}"
exit 0