Skip to content

Instantly share code, notes, and snippets.

@jjn1056
Last active November 9, 2023 21:50
Show Gist options
  • Save jjn1056/408dc4e1bec2951b19a83cbe1ec4202d to your computer and use it in GitHub Desktop.
Save jjn1056/408dc4e1bec2951b19a83cbe1ec4202d to your computer and use it in GitHub Desktop.
first pass server sent event based open AI chat API
my $ua = Mojo::UserAgent->new(max_response_size => 0);
my $tx = $ua->build_tx(
POST => 'https://api.openai.com/v1/chat/completions',
+{
Authorization => "Bearer $ENV{OPENAI_API_KEY}",
},
json => {
model => "gpt-3.5-turbo",
stream => \1,
messages => [
{
role => "user",
content => $prompt,
},
],
}
);
my $prior = '';
$tx->res->content->unsubscribe('read')->on(read => sub ($content, $bytes) {
if($prior) {
$bytes = $prior.$bytes;
$prior = '';
}
unless($bytes =~m/\n\n\z/) {
$prior = $bytes;
return 1;
}
my @messages = split 'data:', $bytes;
while(@messages) {
my $message = shift(@messages);
next if $message eq '';
next if $message =~m/\[DONE\]\n\n\z/;
my $res = $json->decode($message);
my $completion = $res->{choices}[0]{delta}{content};
if($completion) {
say $completion;
} else {
# is end of chat
}
}
});
$tx = $ua->start($tx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment