From e47e503698520b128900837fd2d5d35387c14921 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Wed, 6 Mar 2024 15:47:17 -0500 Subject: [PATCH] New API design works! --- lib/OpenAIAsync/Server.pm | 10 ++-------- t/03-create-server.t | 8 ++++++-- t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm | 7 ++++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/OpenAIAsync/Server.pm b/lib/OpenAIAsync/Server.pm index f86c3d7..6b76075 100644 --- a/lib/OpenAIAsync/Server.pm +++ b/lib/OpenAIAsync/Server.pm @@ -375,24 +375,17 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) { $response->protocol("HTTP/1.1"); my $resp_string = $response->as_string("\r\n"); - print STDERR "resp_string: $resp_string\n"; $req->write($resp_string); # $req->write("\r\n"); # extra to end headers $req->write(sub { - print STDERR "About to shift\n"; my $body_obj = $queue->shift()->get(); - use Data::Dumper; - print STDERR Dumper($body_obj); - if (defined $body_obj) { my $body = $body_obj->_serialize(); my $event_name = $body_obj->_event_name(); - print STDERR Dumper($body); - if ($is_streaming) { return sprintf "event: %s\ndata: %s\n\n", $event_name, $body; } else { @@ -400,8 +393,9 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) { } } else { # Finished - print STDERR "Finished!\n"; + $req->done(); + $req->{conn}->close(); #TODO why is this needed? return undef; } }); diff --git a/t/03-create-server.t b/t/03-create-server.t index 501955e..8a6998d 100644 --- a/t/03-create-server.t +++ b/t/03-create-server.t @@ -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'); } -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; 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(); + diff --git a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm index 32a779d..d8f9060 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm @@ -29,11 +29,12 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) { apply OpenAIAsync::Server::API::v1::ChatCompletion; use OpenAIAsync::Types::Results; use Future::AsyncAwait; + no warnings 'experimental::builtin'; use builtin qw/false/; async method chat_completion ($future_status, $queue, $ctx, $obj, $params) { - my $chained_future = $future_status->then(async sub { - await $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new( + my $chained_future = $future_status->then(sub { + $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new( id => "24601", choices => [], model => "GumbyBrain-llm", @@ -45,7 +46,7 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) { }, object => "text_completion", created => 0, - )); + ))->get(); $queue->finish(); }