-
Notifications
You must be signed in to change notification settings - Fork 0
/
gencmd.sh
131 lines (113 loc) · 3.46 KB
/
gencmd.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
#!/bin/bash
# generate cmd line commands with MakerSuite/Palm/Google Cloud
function usage {
echo "Usage: $(basename $0) [-h] [-l NUMBER] [-o OS]" 2>&1
echo 'Generate a cmd:'
echo ' -o OS Operating system for the cmd. Default is unix.'
echo ' -n NUMBER Number of suggestions you want to see. Default is 4.'
echo ' -t TEMP Temperature [0 to 1.0]. Default is 0.5.'
echo ' -v Verbose. Shows JSON and curl output. Default is off.'
echo ' -l Line numbers in output. Default is off.'
echo ' -e Show a few examples.'
echo ' -h Help with usage.'
echo ''
echo 'Written by Sathish VJ'
exit 1
}
function examples {
echo "Ex: bash $(basename $0) convert the first 10 seconds of an mp4 video into a gif" 2>&1
echo "Ex: bash $(basename $0) find files that contain the text html" 2>&1
echo "Ex: bash $(basename $0) find files that has extension pdf" 2>&1
exit 0
}
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed. Please install jq.' >&2
exit 1
fi
# if no input argument found, exit the script with usage
if [[ ${#} -eq 0 ]]; then
usage
fi
# Set default values
OS="unix"
NUMBER=4
TEMP=0.5
VERBOSE=0
LINES=0
# Define list of arguments expected in the input
#optstring=":onh:"
optstring=":o:n:t:vlhe"
while getopts ${optstring} arg; do
case ${arg} in
o)
OS="${OPTARG}"
;;
n)
NUMBER=${OPTARG}
;;
t)
TEMP=${OPTARG}
;;
v)
VERBOSE=1
;;
l)
LINES=1
;;
h)
usage
;;
e)
examples
;;
?)
echo "Invalid option: -${OPTARG}."
echo
usage
;;
esac
done
if [ -z "$MAKERSUITE_API_KEY" ]; then
echo "MAKERSUITE_API_KEY is empty. Do 'export MAKERSUITE_API_KEY=<key you got from Maker Suite for your project.>'"
exit 2
fi
#echo "Before shift:"
#echo "number=$NUMBER"
#echo "os=$OS"
#echo "others=$@"
#echo "VERBOSE=$VERBOSE"
#exit 0
shift "$(( OPTIND-1 ))"
#echo "After shift:"
#echo "number=$NUMBER"
#echo "os=$OS"
#echo "others=$@"
#echo "VERBOSE=$VERBOSE"
#exit 0
prompt="You are very good at generating the $OS command line commands and options to accomplish a given task. Given a description of what the user wants to get done, you should generate the appropriate command line options. The description of the task is: $@"
#echo "prompt=$prompt"
d="{ 'prompt': { 'text': '$prompt'}, 'temperature': $TEMP, 'top_k': 40, 'top_p': 0.95, 'candidate_count': ${NUMBER}, 'max_output_tokens': 1024, 'stop_sequences': [], 'safety_settings': [{'category':'HARM_CATEGORY_DEROGATORY','threshold':1},{'category':'HARM_CATEGORY_TOXICITY','threshold':1},{'category':'HARM_CATEGORY_VIOLENCE','threshold':2},{'category':'HARM_CATEGORY_SEXUAL','threshold':2},{'category':'HARM_CATEGORY_MEDICAL','threshold':2},{'category':'HARM_CATEGORY_DANGEROUS','threshold':2}]}"
#echo "d=$d"
SILENT_OPTION="--silent"
if [ $VERBOSE -eq 1 ]; then
SILENT_OPTION=""
fi
output=$(curl $SILENT_OPTION \
-H 'Content-Type: application/json' \
-X POST 'https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText?key='${MAKERSUITE_API_KEY} \
-d "${d}")
if [ $VERBOSE -eq 1 ]; then
echo $output | jq
echo
fi
i=1
echo $output | jq -c '.candidates[].output' | while read object; do
object=${object/\"\`\`\`n/}
object=${object/n\`\`\`\"/}
if [ $LINES -eq 1 ]; then
echo "$i: $object"
i=$((i+1))
else
echo "$object"
fi
done