Last active
November 9, 2023 21:50
-
-
Save jjn1056/408dc4e1bec2951b19a83cbe1ec4202d to your computer and use it in GitHub Desktop.
first pass server sent event based open AI chat API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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