Ok so it now runs, but doesnt exit the test

This commit is contained in:
Ryan Voots 2024-03-06 15:29:30 -05:00
parent a6fd7dc043
commit 4811272f5e
3 changed files with 25 additions and 11 deletions

View file

@ -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 # 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 # that is needed for it to persist some code that's running in the future that populates the queue
my $route_method = $route->{handle}; 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 $status = await $future_status;
my $is_streaming = $status->{is_streaming}; my $is_streaming = $status->{is_streaming};
@ -372,16 +372,26 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
# TODO others? # TODO others?
); );
my $response = HTTP::Response->new($status->{status_code}, $status->{status_message}, $headers); my $response = HTTP::Response->new($status->{status_code}, $status->{status_message}, $headers);
$response->protocol("HTTP/1.1");
$req->write($response->as_string("\r\n"));
$req->write("\r\n"); # extra to end headers
$req->write(async sub { my $resp_string = $response->as_string("\r\n");
my $body_obj = await $queue->pull(); 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) { 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;
@ -390,6 +400,7 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
} }
} else { } else {
# Finished # Finished
print STDERR "Finished!\n";
$req->done(); $req->done();
return undef; return undef;
} }

View file

@ -22,6 +22,7 @@ role OpenAIAsync::Types::Results::Encoder::JSON {
} }
method _content_type() {"application/json"} method _content_type() {"application/json"}
method _event_name() {"event"}
} }
role OpenAIAsync::Types::Results::Encoder::Raw { role OpenAIAsync::Types::Results::Encoder::Raw {

View file

@ -32,8 +32,8 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) {
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(sub { my $chained_future = $future_status->then(async sub {
return OpenAIAsync::Types::Results::ChatCompletion->new( await $queue->push(OpenAIAsync::Types::Results::ChatCompletion->new(
id => "24601", id => "24601",
choices => [], choices => [],
model => "GumbyBrain-llm", model => "GumbyBrain-llm",
@ -45,7 +45,9 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) {
}, },
object => "text_completion", object => "text_completion",
created => 0, created => 0,
) ));
$queue->finish();
} }
); );