This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpt.pl
executable file
·392 lines (313 loc) · 10.6 KB
/
gpt.pl
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
#!/usr/bin/env perl
use v5.28.0;
use utf8;
use strict;
use warnings;
use autodie ':all';
use lib '/etc/perl';
use feature 'signatures';
no warnings qw(experimental::signatures experimental::smartmatch);
binmode( STDOUT, ":encoding(UTF-8)" );
local $SIG{INT} = \&quit;
use Data::Dumper;
use IO::Async::Loop::Mojo;
use Mojo::Discord;
use OpenAI::API::Request::Chat;
use OpenAI::API::Request::Completion;
use OpenAI::API::Request::Image::Generation;
use LWP::Simple;
use LWP::UserAgent;
use HTTP::Request::Common;
use File::Temp ':POSIX';
use File::Basename;
use JSON::MaybeXS qw(from_json to_json);
use Encode::Simple qw(encode_utf8_lax decode_utf8_lax);
my ($guild, $users, $started, $ready, $readyc, $resumed, $resumedc)
= (undef, undef, time, 0, 0, 0, 0 );
my ($gptres, $chat, $gptres4, $chat4);
my $config = {
discord => {
gptchan => 1167503883338268732,
gptchan4 => 1173902325979566150,
owner_id => 540067740594077698,
guild_id => 458323696910598165,
}
};
my $apikey = 'sk-XXX';
my $gptconfig = OpenAI::API::Config->new(
api_key => $apikey,
api_base => 'https://api.openai.com/v1',
timeout => 30,
retry => 3,
sleep => 2,
);
my $discord = Mojo::Discord->new(
'version' => '9999',
'url' => 'https://twlz.lifeisabug.com',
'token' => '',
'reconnect' => 1,
'verbose' => 1,
'logdir' => "$ENV{HOME}/gus",
'logfile' => 'discord_gpt.log',
'loglevel' => 'info',
);
###
discord_on_ready();
discord_on_guild_create();
discord_on_resumed();
discord_on_message_create();
$discord->init();
my $loop = IO::Async::Loop::Mojo->new;
$loop->run unless (Mojo::IOLoop->is_running);
exit;
###
sub discord_on_message_create ()
{
$discord->gw->on('MESSAGE_CREATE' => sub ($gw, $hash)
{
my $id = $hash->{'author'}->{'id'};
my $author = $hash->{'author'};
my $member = $hash->{'member'};
my $msg = $hash->{'content'};
my $msgid = $hash->{'id'};
my $channel = $hash->{'channel_id'};
my @mentions = $hash->{'mentions'}->@*;
add_user($_) for(@mentions);
unless ( exists $author->{'bot'} && $author->{'bot'} )
{
$msg =~ s/\@+everyone/everyone/g;
$msg =~ s/\@+here/here/g;
if ($channel == $$config{discord}{gptchan} || $channel == $$config{discord}{gptchan4})
{
if ($msg =~ /^<\@1167502883839819777> +?genimg (.+)/ || $msg =~ /^<\@&1167503499622363169> +?genimg (.+)/) {
return unless ($$author{id} == 540067740594077698 || $$author{id} == 495246182512066580 || $$author{id} == 423509189340561408);
say "IMAG <$$author{username}> genimg $1\n";
my $request = OpenAI::API::Request::Image::Generation->new(
config => $gptconfig,
prompt => $1,
model => 'dall-e-3',
#size => '1792x1024',
quality => 'hd'
);
my $res;
eval { $res = $request->send(); };
if ($@) {
say ' Error: ' . (split /\n/, $@)[0];
$discord->create_reaction( $channel, $msgid, ':redtick:712004372707541003' );
return;
}
my $url = $res->{data}->[0]{url};
my $file = tmpnam() . '.png';
my $dl = getstore($url, $file);
$msg =~ s/^<@&?[0-9]+> +?genimg //i;
my $txt = encode_utf8_lax('' . ($res->{data}->[0]{revised_prompt} ? ($res->{data}->[0]{revised_prompt} . ':') : ''));
say "IMAG >> [$txt]\n";
$txt = '<@' . $$author{id} . '> ' . $txt;
$discord->send_image( $channel, { path => $file, name => basename($file), content => $txt }, sub { unlink $file } );
}
elsif ($msg =~ /^<\@1167502883839819777> +?COMP(?:LETE)?\s+(.+)/is || $msg =~ /^<\@&1167503499622363169> +?COMP(?:LETE)?\s+(.+)/is) {
my $in = $1;
chomp($in);
say "COMP <$$author{username}> $in\n";
my $res;
$res = gptreq2($in);
if ($res) {
$res =~ s/\n\n/\n/g;
$res =~ s/^\s+//;
say "COMP >> $res\n";
my $send = '<@' . $$author{id} . '>: ' . $res;
my @out = split(/.{0,1800}\K(?:\s+|$)/s, $send);
if (scalar(@out) > 1) {
$discord->send_message_content_blocking( $channel, $_ ) for @out;
}
else {
$discord->send_message( $channel, $send );
}
}
}
elsif ($msg =~ /^<\@1167502883839819777>\s+?VIS\s+?(.+) (https?:\/\/.+)$/i || $msg =~ /^<\@&1167503499622363169>\s+?VIS\s+?(.+) (https?:\/\/.+)$/i) {
my $in = $1;
my $url = $2;
chomp($in);
say "VIS <$$author{username}> [$url] $in\n";
my $res;
$res = vision($in, $url);
if ($res) {
$res =~ s/\n\n/\n/g;
$res =~ s/^\s+//;
say "VIS >> $res\n";
my $send = '<@' . $$author{id} . '>: ' . $res;
my @out = split(/.{0,1800}\K(?:\s+|$)/s, $send);
if (scalar(@out) > 1) {
$discord->send_message_content_blocking( $channel, $_ ) for @out;
}
else {
$discord->send_message( $channel, $send );
}
}
}
elsif ($msg =~ /^<\@1167502883839819777>\s+?(.+)/s || $msg =~ /^<\@&1167503499622363169>\s+?(.+)/s) {
my $in = $1;
chomp($in);
my $g4 = 0;
if (($$author{id} == 540067740594077698 || $$author{id} == 495246182512066580) && $in =~ /^4 /) {
$g4 = 1;
$in =~ s/^4 //;
}
my $otherchan = 0;
$otherchan = 1 if ($channel == $$config{discord}{gptchan4});
say "CHAT <$$author{username}> $in\n";
my $res;
$res = gptreq('<@' . $$author{id} . '>: ' . $in, 0, $g4, $otherchan);
if ($res) {
$res =~ s/^\s+//;
say "CHAT >> $res\n";
my $send = $res;
$send =~ s/^@// if ($send =~ /^@<@/);
my @out = split(/.{0,1800}\K(?:\s+|$)/s, $send);
if (scalar(@out) > 1) {
$discord->send_message_content_blocking( $channel, $_ ) for @out;
}
else {
$discord->send_message( $channel, $send );
}
}
}
}
}
});
return;
}
###
sub vision($msg, $url) {
chomp($msg);
$msg =~ s/^vis //i;
return 'Not an image (only jpg/png/webp/gif supported).' unless ($msg && $url =~ /\.(jpe?g|png|webp|gif)(?:[&\?].+)?$/);
my $ua = LWP::UserAgent->new;
my $req = POST('https://api.openai.com/v1/chat/completions',
Content_Type => 'application/json',
Authorization => 'Bearer ' . $apikey,
);
$req->content(to_json({ model => 'gpt-4-vision-preview', messages => [ { role => 'user', content => [ { type => 'text', text => encode_utf8_lax($msg) }, { type => 'image_url', image_url => { url => $url, detail => 'low' } } ] } ], max_tokens => 2048 }));
my $res = $ua->request($req);
if ($res->is_success) {
my $json = from_json($res->decoded_content);
my $txt = $$json{choices}[0]{message}{content};
return $txt;
}
else {
return $res->status_line;
}
}
sub gptreq($m, $sys, $g4 = 0, $otherchan = 0) {
unless ($otherchan) {
unless ($gptres) {
$chat = OpenAI::API::Request::Chat->new(
config => $gptconfig,
model => $g4 ? 'gpt-4-1106-preview' : 'gpt-3.5-turbo-1106',
max_tokens => 1024,
messages => [
{ role => $sys ? 'system' : 'user', content => $m },
],
);
eval { $gptres = $chat->send(); };
}
else {
eval { $gptres = $chat->send_message($m); };
}
}
else {
unless ($gptres4) {
$chat4 = OpenAI::API::Request::Chat->new(
config => $gptconfig,
model => $g4 ? 'gpt-4-1106-preview' : 'gpt-3.5-turbo-1106',
max_tokens => 1024,
messages => [
{ role => $sys ? 'system' : 'user', content => $m },
],
);
eval { $gptres4 = $chat4->send(); };
}
else {
eval { $gptres4 = $chat4->send_message($m); };
}
}
return 0 if $sys;
if ($@) {
my $err = (split /\n/, $@)[0];
say "$@";
undef $gptres;
sysmsg();
return $err . ' GPT reset, new session initiated, previous context and role is now forgotten.';
}
unless ($otherchan) {
return $$gptres{choices}[0]{message}{content};
}
else {
return $$gptres4{choices}[0]{message}{content};
}
}
sub sysmsg() {
my $sys = "You are a bot named Paul in a Discord chat channel. Extensively make use of Markdown in your replies. Subtly use Emojis when appropriate. The currently speaking user name will be in front of every input prompt, remember the name and reference the user accordingly, i.e. <\@[0-9]+>. Occasionally replace common adjectives with their more eloquent alternatives. Stay and forget the roles you are put into as you're told.";
gptreq($sys, 1);
gptreq($sys, 1, 0, 1);
}
sub gptreq2($m) {
my $comp = OpenAI::API::Request::Completion->new(
config => $gptconfig,
model => 'gpt-3.5-turbo-instruct',
max_tokens => 3072,
prompt => $m,
);
my $res;
eval { $res = $comp->send(); };
if ($@) {
say "$@";
return (split /\n/, $@)[0];
}
return $$res{choices}[0]{text};
}
sub discord_on_ready ()
{
$discord->gw->on('READY' => sub ($gw, $hash)
{
$ready = $resumed = time;
$readyc++;
add_me($hash->{'user'});
$discord->status_update( { 'name' => 'GPT-3.5', type => 0 } );
});
return;
}
sub discord_on_guild_create ()
{
$discord->gw->on('GUILD_CREATE' => sub ($gw, $hash)
{
$guild = $discord->get_guild($$config{'discord'}{'guild_id'});
sysmsg();
});
return;
}
sub discord_on_resumed ()
{
$discord->gw->on('RESUMED' => sub ($gw, $hash)
{
$resumed = time;
$resumedc++;
});
return;
}
sub add_me ($user)
{
$$users{'id'} = $$user{'id'};
add_user($user);
return;
}
sub add_user ($user)
{
$$users{'users'}{$$user{'id'}} = $user;
return;
}
sub quit ($)
{
exit;
}