my $url = "https://api.openai.com/v1/completions"; my $model = "text-davinci-003"; ## other model implementations work too my $heat = "0.7"; ## ?? wtf my $uri = URI->new($url); my $ua = LWP::UserAgent->new; $textcall = Irssi::strip_codes($textcall); $textcall =~ s/\"/\\\"/g; my $askbuilt = "{\"model\": \"$model\",\"prompt\": \"$textcall\"," . "\"temperature\":$heat,\"max_tokens\": $tokenlimit," . "\"top_p\": 1,\"frequency_penalty\": 0,\"presence_" . "penalty\": 0}"; $ua->default_header("Content-Type" => "application/json"); $ua->default_header("Authorization" => "Bearer " . $apikey); my $res = $ua->post($uri, Content => $askbuilt); ## send the post request to the api if ($res->is_success) { my $said = decode_json($res->decoded_content())->{choices}[0]{text}; my $toks = decode_json($res->decoded_content())->{choices}[0]{total_tokens}; if (($said =~ m/^\s+$/) || ($said =~ m/^$/)) $said = ""; } $said =~ s/^\s+//; $said =~ s/^\n+//; $said =~ s/Franklin: //; $said =~ s/Reply: //; $said =~ s/My reply is: //; $said =~ s/^\s*[\?|.|-]\s*(\w)/$1/; ## if it spits out a question mark, this fixes it if ($said =~ m/^\s*\?\s*$/) { $said = ""; } unless ($said eq "") { my $hexfn = substr( ## the reencode fixes the utf8 bug Digest::MD5::md5_hex( utf8::is_utf8($said) ? Encode::encode_utf8($said) : $said ), 0, 8 ); umask(0133); my $cost = sprintf("%.5f", ($toks / 1000 * $price_per_k)); open(SAID, '>', "$httploc$hexfn" . ".txt") or Irssi::print "Could not open txt file for writing."; binmode(SAID, "encoding(UTF-8)"); print SAID "$nick asked $textcall_bare with hash $hexfn\n<---- snip ---->\n$said\n"; close(SAID); my $fg_top = ' Franklin, a ChatGPT bot

Franklin

'; my $fg_bottom = '
'; my $said_html = sanitize($said, html => 1); $textcall_bare = sanitize($textcall_bare, html => 1); $said_html =~ s/\n/
/g; open(SAIDHTML, '>', "$httploc$hexfn" . ".html") or Irssi::print "Couldn't open for writing."; binmode(SAIDHTML, "encoding(UTF-8)"); print SAIDHTML $fg_top . "
" . localtime() . "
Tokens used: $toks
Avg cost: \$$cost
" . "



$nick asked:
     $textcall_bare

" . $said_html . $fg_bottom; close SAIDHTML; my $said_cut = substr($said, 0, $hardlimit); $said_cut =~ s/\n/ /g; # fixes newlines for irc compat Irssi::print "Franklin: Reply: $said_cut $webaddr$hexfn" . ".html"; $server->command("msg $channel $said_cut TXID:$hexfn"); $retry++;