New API design works!

This commit is contained in:
Ryan Voots 2024-03-06 15:47:17 -05:00
parent 4811272f5e
commit e47e503698
3 changed files with 12 additions and 13 deletions

View file

@ -375,24 +375,17 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
$response->protocol("HTTP/1.1"); $response->protocol("HTTP/1.1");
my $resp_string = $response->as_string("\r\n"); my $resp_string = $response->as_string("\r\n");
print STDERR "resp_string: $resp_string\n";
$req->write($resp_string); $req->write($resp_string);
# $req->write("\r\n"); # extra to end headers # $req->write("\r\n"); # extra to end headers
$req->write(sub { $req->write(sub {
print STDERR "About to shift\n";
my $body_obj = $queue->shift()->get(); my $body_obj = $queue->shift()->get();
use Data::Dumper;
print STDERR Dumper($body_obj);
if (defined $body_obj) { if (defined $body_obj) {
my $body = $body_obj->_serialize(); my $body = $body_obj->_serialize();
my $event_name = $body_obj->_event_name(); my $event_name = $body_obj->_event_name();
print STDERR Dumper($body);
if ($is_streaming) { if ($is_streaming) {
return sprintf "event: %s\ndata: %s\n\n", $event_name, $body; return sprintf "event: %s\ndata: %s\n\n", $event_name, $body;
} else { } else {
@ -400,8 +393,9 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
} }
} else { } else {
# Finished # Finished
print STDERR "Finished!\n";
$req->done(); $req->done();
$req->{conn}->close(); #TODO why is this needed?
return undef; return undef;
} }
}); });

View file

@ -52,11 +52,15 @@ sub mk_req($uri, $content) {
return $http_client->POST("http://127.0.0.1:$port/v1".$uri, $content_json, content_type => 'application/json'); return $http_client->POST("http://127.0.0.1:$port/v1".$uri, $content_json, content_type => 'application/json');
} }
my $res = await mk_req("/chat/completions", $chat_completion_input); my $res_fut = mk_req("/chat/completions", $chat_completion_input);
$loop->delay_future(after => 5)->get();
my $res = $res_fut->get();
my $content = $res->content; my $content = $res->content;
is($content, '{"choices":[],"created":"0","id":"24601","model":"GumbyBrain-llm","object":"text_completion","system_fingerprint":"SHODAN node 12 of 16 tertiary adjunct of unimatrix 42","usage":{"completion_tokens":9,"prompt_tokens":6,"total_tokens":42}}', "check marshalling of data directly"); is($content, '{"choices":[],"created":"0","id":"24601","model":"GumbyBrain-llm","object":"text_completion","system_fingerprint":"SHODAN node 12 of 16 tertiary adjunct of unimatrix 42","usage":{"completion_tokens":9,"prompt_tokens":6,"total_tokens":42}}', "check marshalling of data directly");
#$loop->delay_future(after => 120)->get();

View file

@ -29,11 +29,12 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) {
apply OpenAIAsync::Server::API::v1::ChatCompletion; apply OpenAIAsync::Server::API::v1::ChatCompletion;
use OpenAIAsync::Types::Results; use OpenAIAsync::Types::Results;
use Future::AsyncAwait; use Future::AsyncAwait;
no warnings 'experimental::builtin';
use builtin qw/false/; use builtin qw/false/;
async method chat_completion ($future_status, $queue, $ctx, $obj, $params) { async method chat_completion ($future_status, $queue, $ctx, $obj, $params) {
my $chained_future = $future_status->then(async sub { my $chained_future = $future_status->then(sub {
await $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new( $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new(
id => "24601", id => "24601",
choices => [], choices => [],
model => "GumbyBrain-llm", model => "GumbyBrain-llm",
@ -45,7 +46,7 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) {
}, },
object => "text_completion", object => "text_completion",
created => 0, created => 0,
)); ))->get();
$queue->finish(); $queue->finish();
} }