Many things better now, just needs to do some other stuff

This commit is contained in:
Ryan Voots 2024-01-26 18:48:43 -05:00
parent 75a359bd6d
commit 78184a015b
4 changed files with 18 additions and 15 deletions

View file

@ -222,6 +222,9 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
use HTTP::Response;
use HTTP::Request;
use OpenAIAsync::Types::Requests;
use OpenAIAsync::Types::Results;
field $_json = JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1);
field $http_server;
@ -280,7 +283,7 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
method register_url(%opts) {
# TODO check params
use Data::Dumper;
#use Data::Dumper;
#say Dumper("Got url registered", \%opts);
push $routes->@*, \%opts;
}
@ -288,7 +291,6 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
async method _route_request($req, $ctx) {
my $method = $req->method();
my $path = $req->path;
say "Got request ", $method, " => ", $path;
try {
@ -303,20 +305,20 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
my $obj;
if ($route->{decoder} eq "www-form-urlencoded") {
my %data = WWW::Form::UrlEncoded::parse_urlencoded($req->decoded_content);
my %data = WWW::Form::UrlEncoded::parse_urlencoded($req->body);
$obj = $route->{request_class}->new(%data);
} elsif ($route->{decoder} eq "json") {
my $data = $_json->decode($req->decoded_content);
my $data = $_json->decode($req->body);
$obj = $route->{request_class}->new(%$data);
} elsif ($route->{decoder} eq "null") {
$obj = $route->{request_class}->new();
} else { # Try to detect based on content-type, then fail
my $content_type = $req->header("Content-Type");
if ($content_type eq 'application/json') {
my $data = $_json->decode($req->decoded_content);
my $data = $_json->decode($req->body);
$obj = $route->{request_class}->new(%$data);
} elsif ($content_type eq 'application/x-www-form-urlencoded') {
my %data = WWW::Form::UrlEncoded::parse_urlencoded($req->decoded_content);
my %data = WWW::Form::UrlEncoded::parse_urlencoded($req->body);
$obj = $route->{request_class}->new(%data);
} else {
die "Unsupported content-type for URI: $content_type";
@ -324,11 +326,12 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
}
try {
my ($result, @extra) = (await $route->{handle}->($req, $ctx, $obj, $params))->get();
my ($result, @extra) = (await $route->{handle}->($req, $ctx, $obj, $params));
if ($route->{result_class}) {
my $out_obj = $result;
unless ($out_obj isa $route->{result_object}) {
unless ($out_obj isa $route->{result_class}) {
$out_obj = $route->{result_class}->new(%$result);
}

View file

@ -33,9 +33,9 @@ role OpenAIAsync::Server::API::v1::ChatCompletion :strict(params) {
method => 'POST',
url => qr{^/v1/chat/completions$},
handle => async sub($req, $ctx, $obj, $params) {await $self->chat($obj, $req, $ctx)},
request_class => "OpenAIAsync::Type::Request::ChatCompletion",
result_class => "OpenAIAsync::Type::Result::ChatCompletion",
decoder => 'www-form-urlencoded', # default is json, we need this for this api
request_class => "OpenAIAsync::Types::Requests::ChatCompletion",
result_class => "OpenAIAsync::Types::Results::ChatCompletion",
decoder => 'json', # default is json, we need this for this api
);
}

View file

@ -49,7 +49,7 @@ my $chat_completion_input = {
sub mk_req($uri, $content) {
my $content_json = encode_json($content);
return $http_client->POST("http://127.0.0.1:$port/v1".$uri, $content_json, headers => {"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);

View file

@ -37,9 +37,9 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) {
model => "GumbyBrain-llm",
system_fingerprint => "SHODAN node 12 of 16 tertiary adjunct of unimatrix 42",
usage => {
42,
6,
9,
total_tokens => 42,
prompt_tokens => 6,
completion_tokens => 9,
},
object => "text_completion",
created => 0,