From 4811272f5e5c57a63f3894740be46a6dc01304d1 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Wed, 6 Mar 2024 15:29:30 -0500 Subject: [PATCH] Ok so it now runs, but doesnt exit the test --- lib/OpenAIAsync/Server.pm | 27 +++++++++++++------ lib/OpenAIAsync/Types/Results.pm | 1 + .../Server/API/Test/ChatCompletion.pm | 8 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/OpenAIAsync/Server.pm b/lib/OpenAIAsync/Server.pm index c090778..f86c3d7 100644 --- a/lib/OpenAIAsync/Server.pm +++ b/lib/OpenAIAsync/Server.pm @@ -361,7 +361,7 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) { # TODO can I redo this to eliminate the $future_status? I want it for internal chaining inside the handler # that is needed for it to persist some code that's running in the future that populates the queue my $route_method = $route->{handle}; - await $self->$route_method($future_status, $queue, $ctx, $obj, $params); + $self->$route_method($future_status, $queue, $ctx, $obj, $params); my $status = await $future_status; my $is_streaming = $status->{is_streaming}; @@ -372,16 +372,26 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) { # TODO others? ); my $response = HTTP::Response->new($status->{status_code}, $status->{status_message}, $headers); - - $req->write($response->as_string("\r\n")); - $req->write("\r\n"); # extra to end headers + $response->protocol("HTTP/1.1"); - $req->write(async sub { - my $body_obj = await $queue->pull(); + 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(); + 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; @@ -390,6 +400,7 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) { } } else { # Finished + print STDERR "Finished!\n"; $req->done(); return undef; } diff --git a/lib/OpenAIAsync/Types/Results.pm b/lib/OpenAIAsync/Types/Results.pm index 4201d37..6c01eac 100644 --- a/lib/OpenAIAsync/Types/Results.pm +++ b/lib/OpenAIAsync/Types/Results.pm @@ -22,6 +22,7 @@ role OpenAIAsync::Types::Results::Encoder::JSON { } method _content_type() {"application/json"} + method _event_name() {"event"} } role OpenAIAsync::Types::Results::Encoder::Raw { diff --git a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm index 3e46eed..32a779d 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm @@ -32,8 +32,8 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) { use builtin qw/false/; async method chat_completion ($future_status, $queue, $ctx, $obj, $params) { - my $chained_future = $future_status->then(sub { - return OpenAIAsync::Types::Results::ChatCompletion->new( + my $chained_future = $future_status->then(async sub { + await $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new( id => "24601", choices => [], model => "GumbyBrain-llm", @@ -45,7 +45,9 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) { }, object => "text_completion", created => 0, - ) + )); + + $queue->finish(); } );