-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpt
164 lines (159 loc) · 4.23 KB
/
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
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
#!/bin/bash
### Subscribe to my YouTube channel to support me => [ https://www.youtube.com/@techghoshal ] ###
echo
#SET API KEY
OPENAI_API_KEY="API_KEY"
echo " 🤖️ Hi there! How can I help you?"
while true;
do
echo
read -p " 🧒️ " chat
#encode special characters
n="${#chat}"
mod=$chat
i=0
while (( i<=$n ));
do
char="${chat:$i:1}"
if [ "$char" == " " ]
then
mod=${mod// /%20}
elif [ "$char" == "!" ]
then
mod=${mod//!/%21}
elif [ "$char" == '"' ]
then
mod=${mod//'"'/%22}
elif [ "$char" == "#" ]
then
mod=${mod//#/%23}
elif [ "$char" == "$" ]
then
mod=${mod//"$"/%24}
elif [ "$char" == "%" ]
then
mod=${mod//"%"/%25}
elif [ "$char" == "&" ]
then
mod=${mod//"&"/%26}
elif [ "$char" == "'" ]
then
mod=${mod//"'"/%27}
elif [ "$char" == "(" ]
then
mod=${mod//"("/%28}
elif [ "$char" == ")" ]
then
mod=${mod//")"/%29}
elif [ "$char" == "+" ]
then
mod=${mod//"+"/%2B}
elif [ "$char" == "," ]
then
mod=${mod//","/%2C}
elif [ "$char" == "-" ]
then
mod=${mod//"-"/%2D}
elif [ "$char" == "." ]
then
mod=${mod//"."/%2E}
elif [ "$char" == ":" ]
then
mod=${mod//":"/%3A}
elif [ "$char" == ";" ]
then
mod=${mod//";"/%3B}
elif [ "$char" == "<" ]
then
mod=${mod//"<"/%3C}
elif [ "$char" == "=" ]
then
mod=${mod//"="/%3D}
elif [ "$char" == ">" ]
then
mod=${mod//">"/%3E}
elif [ "$char" == "?" ]
then
mod=${mod//"?"/%3F}
elif [ "$char" == "@" ]
then
mod=${mod//"@"/%40}
elif [ "$char" == "<" ]
then
mod=${mod//</%3C}
elif [ "$char" == "[" ]
then
mod=${mod//"["/%5B}
elif [ "$char" == '\' ]
then
mod=${mod//'\'/%5C}
elif [ "$char" == "]" ]
then
mod=${mod//"]"/%5D}
elif [ "$char" == "^" ]
then
mod=${mod//"^"/%5E}
elif [ "$char" == "_" ]
then
mod=${mod//"_"/%5F}
elif [ "$char" == '`' ]
then
mod=${mod//'`'/%60}
elif [ "$char" == "{" ]
then
mod=${mod//"{"/%7B}
elif [ "$char" == "|" ]
then
mod=${mod//"|"/%7C}
elif [ "$char" == "}" ]
then
mod=${mod//"}"/%7D}
elif [ "$char" == "~" ]
then
mod=${mod//"~"/%7E}
fi
((i++))
done
echo
#API request
res=$(curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "text-davinci-003",
"prompt": "'$mod'\nA:",
"temperature": 0,
"max_tokens": 100,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.0,
"stop": ["\n"]
}' -s)
##Filter JSON data
#Fetching finish_reason
fReason=$(echo $res | jq '.choices' | tr -d "[" | tr -d "]" | jq '.finish_reason' | tr -d '"')
#Checking on text that the text is empty or not empty
empty=$(echo $res | jq '.choices' | tr -d "[" | tr -d "]" | jq '.text')
#check error
error=$(echo $res | grep error | cut -d " " -f 2 | tr -d '"' | tr -d ":" )
#Check error condition
if [ "$error" == "error" ]
then
echo " 🤖️ Somthing went Worng! Please try again "
echo
echo $res | tr -d "{","}"
#Check text vlaue is empty or not empty condition
elif [ "$empty" == '""' ]
then
echo ' 🤖️ Somthing went Worng! AI did not reply [ text":"_blank" ] '
echo
echo $res
#Check fReason condition
elif [ "$fReason" == "stop" ]
then
ans=$(echo $res | jq '.choices' | tr -d "[" | tr -d "]" | jq '.text' | tr -d '"')
echo " 🤖️" $ans
else
echo " Somthing went Worng! Please try again "
fi
done