base classes modified, completely new api here and change the handle => to use method names and avoid a useless coderef

This commit is contained in:
Ryan Voots 2024-03-05 09:33:22 -05:00
parent 8f33acb3b9
commit 213278513b
9 changed files with 34 additions and 33 deletions

View file

@ -360,7 +360,8 @@ 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
await $route->{handle}->($req, $future_status, $queue, $ctx, $obj, $params); my $route_method = $route->{handle};
await $self->$route_method($req, $future_status, $queue, $ctx, $obj, $params);
my $status = await $future_status; my $status = await $future_status;
my $is_streaming_event = $status->{is_streaming}; my $is_streaming_event = $status->{is_streaming};

View file

@ -33,13 +33,13 @@ role OpenAIAsync::Server::API::v1::AudioTTS :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/audio/speech$}, url => qr{^/v1/audio/speech$},
handle => async sub($req, $ctx, $obj, $params) {await $self->audio_create_speech($obj, $req, $ctx)}, handle => "audio_create_speech",
request_class => "OpenAIAsync::Type::Requests::CreateSpeech", request_class => "OpenAIAsync::Type::Requests::CreateSpeech",
result_class => "", # This gives back a file of audio data result_class => "", # This gives back a file of audio data
); );
} }
async method audio_create_speech($obj, $http_req, $ctx); async method audio_create_speech($req, $future_status, $queue, $ctx, $obj, $params);
} }
role OpenAIAsync::Server::API::v1::AudioSTT :strict(params) { role OpenAIAsync::Server::API::v1::AudioSTT :strict(params) {
@ -47,13 +47,13 @@ role OpenAIAsync::Server::API::v1::AudioSTT :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/audio/transcription$}, url => qr{^/v1/audio/transcription$},
handle => async sub($req, $ctx, $obj, $params) {await $self->audio_create_transcript($obj, $req, $ctx)}, handle => "audio_create_transcript",
request_class => "OpenAIAsync::Type::Requests::CreateTranscription", request_class => "OpenAIAsync::Type::Requests::CreateTranscription",
result_class => "", # This gives back a file, in the requested format result_class => "OpenAIAsync::Type::Response::AudioFile",
); );
} }
async method audio_create_transcript($obj, $http_req, $ctx); async method audio_create_transcript($req, $future_status, $queue, $ctx, $obj, $params);
} }
role OpenAIAsync::Server::API::v1::AudioTranslate :strict(params) { role OpenAIAsync::Server::API::v1::AudioTranslate :strict(params) {
@ -61,13 +61,13 @@ role OpenAIAsync::Server::API::v1::AudioTranslate :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/$}, url => qr{^/v1/$},
handle => async sub($req, $ctx, $obj, $params) {await $self->audio_create_translation($obj, $req, $ctx)}, handle => "audio_create_translation",
request_class => "OpenAIAsync::Type::Requests::CreateTranslation", request_class => "OpenAIAsync::Type::Requests::CreateTranslation",
result_class => "", # This gives back a file, in the requested format result_class => "OpenAIAsync::Type::Response::AudioFile",
); );
} }
async method audio_create_translation($obj, $http_req, $ctx); async method audio_create_translation($req, $future_status, $queue, $ctx, $obj, $params);
} }
role OpenAIAsync::Server::API::v1::Audio :strict(params) { role OpenAIAsync::Server::API::v1::Audio :strict(params) {

View file

@ -32,12 +32,12 @@ role OpenAIAsync::Server::API::v1::ChatCompletion :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/chat/completions$}, url => qr{^/v1/chat/completions$},
handle => async sub($req, $ctx, $obj, $params) {await $self->chat($obj, $req, $ctx)}, handle => "chat_completion",
request_class => "OpenAIAsync::Types::Requests::ChatCompletion", request_class => "OpenAIAsync::Types::Requests::ChatCompletion",
result_class => "OpenAIAsync::Types::Results::ChatCompletion", result_class => "OpenAIAsync::Types::Results::ChatCompletion",
decoder => 'json', # default is json, we need this for this api decoder => 'json', # default is json, we need this for this api
); );
} }
async method chat($obj, $http_req, $ctx); async method chat_completion($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,12 +30,12 @@ role OpenAIAsync::Server::API::v1::Completions :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/completions$}, url => qr{^/v1/completions$},
handle => async sub($req, $ctx, $obj, $params) {await $self->completion($obj, $req, $ctx)}, handle => "completion",
request_class => "OpenAIAsync::Type::Request::Completion", request_class => "OpenAIAsync::Type::Request::Completion",
result_class => "OpenAIAsync::Type::Result::Completion", result_class => "OpenAIAsync::Type::Result::Completion",
decoder => 'www-form-urlencoded', # default is json, we need this for this api decoder => 'www-form-urlencoded', # default is json, we need this for this api
); );
} }
async method completion($obj, $http_req, $ctx); async method completion($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,12 +30,12 @@ role OpenAIAsync::Server::API::v1::Embeddings :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/embeddings$}, url => qr{^/v1/embeddings$},
handle => async sub($req, $ctx, $obj, $params) {await $self->embeddings($obj, $req, $ctx)}, handle => "embeddings",
request_class => "OpenAIAsync::Type::Request::Embeddings", request_class => "OpenAIAsync::Type::Request::Embeddings",
result_class => "OpenAIAsync::Type::Result::Embeddings", result_class => "OpenAIAsync::Type::Result::Embeddings",
decoder => 'www-form-urlencoded', # default is json, we need this for this api decoder => 'www-form-urlencoded', # default is json, we need this for this api
); );
} }
async method embeddings($obj, $http_req, $ctx); async method embeddings($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,7 +30,7 @@ role OpenAIAsync::Server::API::v1::File :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/files$}, url => qr{^/v1/files$},
handle => async sub($req, $ctx, $obj, $params) {await $self->file_upload($obj, $req, $ctx)}, handle => "file_upload",
request_class => "OpenAIAsync::Type::Request::FileUpload", request_class => "OpenAIAsync::Type::Request::FileUpload",
result_class => "OpenAIAsync::Type::Shared::File", result_class => "OpenAIAsync::Type::Shared::File",
decoder => 'www-form-urlencoded', # default is json, we need this for this api decoder => 'www-form-urlencoded', # default is json, we need this for this api
@ -38,37 +38,37 @@ role OpenAIAsync::Server::API::v1::File :strict(params) {
$self->register_url( $self->register_url(
method => 'GET', method => 'GET',
url => qr{^/v1/files/(?<file_id>[^/]+)/content$}, url => qr{^/v1/files/(?<file_id>[^/]+)/content$},
handle => async sub($req, $ctx, $obj, $params) {await $self->file_download($obj, $req, $ctx, $params)}, handle => "file_download",
request_class => "", # No req type here request_class => "", # No req type here
result_class => "", # TODO this should be special, it's raw content, make it undef? leave it off? result_class => "OpenAIAsync::Type::Results::RawFile",
); );
$self->register_url( $self->register_url(
method => 'GET', method => 'GET',
url => qr{^/v1/files/(?<file_id>[^/]+)$}, url => qr{^/v1/files/(?<file_id>[^/]+)$},
handle => async sub($req, $ctx, $obj, $params) {await $self->file_info($obj, $req, $ctx, $params)}, handle => "file_info",
request_class => "", # No req type here request_class => "", # No req type here
result_class => "OpenAIAsync::Type::Shared::File", result_class => "OpenAIAsync::Type::Shared::File",
); );
$self->register_url( $self->register_url(
method => 'DELETE', method => 'DELETE',
url => qr{^/v1/files/(?<file_id>[^/]+)$}, url => qr{^/v1/files/(?<file_id>[^/]+)$},
handle => async sub($req, $ctx, $obj, $params) {await $self->file_delete($obj, $req, $ctx, $params)}, handle => "file_delete",
request_class => "", # No req type here request_class => "", # No req type here
result_class => "OpenAIAsync::Type::Results::FileDeletion", result_class => "OpenAIAsync::Type::Results::FileDeletion",
); );
$self->register_url( $self->register_url(
method => 'GET', method => 'GET',
url => qr{^/v1/files$}, url => qr{^/v1/files$},
handle => async sub($req, $ctx, $obj, $params) {await $self->file_list($obj, $req, $ctx)}, handle => "file_list",
request_class => "OpenAIAsync::Type::Request::FileList", request_class => "OpenAIAsync::Type::Request::FileList",
result_class => "OpenAIAsync::Type::Results::FileList", result_class => "OpenAIAsync::Type::Results::FileList",
decoder => 'optional_json', # this API input is OPTIONAL, if it's not present then we create a blank object to use. decoder => 'optional_json', # this API input is OPTIONAL, if it's not present then we create a blank object to use.
); );
} }
async method file_list($http_req, $ctx); async method file_list($req, $future_status, $queue, $ctx, $obj, $params);
async method file_info($http_req, $ctx, $params); async method file_info($req, $future_status, $queue, $ctx, $obj, $params);
async method file_delete($http_req, $ctx, $params); async method file_delete($req, $future_status, $queue, $ctx, $obj, $params);
async method file_upload($http_req, $ctx, $params); async method file_upload($req, $future_status, $queue, $ctx, $obj, $params);
async method file_download($http_req, $ctx, $params); async method file_download($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,11 +30,11 @@ role OpenAIAsync::Server::API::v1::Image :strict(params) {
$self->register_url( $self->register_url(
method => 'GET', method => 'GET',
url => qr{^/v1/files$}, url => qr{^/v1/files$},
handle => async sub($req, $ctx, $obj, $params) {await $self->create_image($obj, $req, $ctx)}, handle => "create_image",
request_class => "OpenAIAsync::Type::Requests::GenerateImage", request_class => "OpenAIAsync::Type::Requests::GenerateImage",
result_class => "", result_class => "OpenAIAsync::Type::Results::RawFile", # TOOD image class?
); );
} }
async method create_image($http_req, $ctx); async method create_image($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,11 +30,11 @@ role OpenAIAsync::Server::API::v1::ModelList :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/models$}, url => qr{^/v1/models$},
handle => async sub($req, $ctx, $obj, $params) {await $self->model_list($obj, $req, $ctx)}, handle => "model_list",
request_class => "", request_class => "",
result_class => "OpenAIAsync::Type::Result::ModelList", result_class => "OpenAIAsync::Type::Result::ModelList",
); );
} }
async method model_list($obj, $http_req, $ctx); async method model_list($req, $future_status, $queue, $ctx, $obj, $params);
} }

View file

@ -30,11 +30,11 @@ role OpenAIAsync::Server::API::v1::Moderations :strict(params) {
$self->register_url( $self->register_url(
method => 'POST', method => 'POST',
url => qr{^/v1/moderations$}, url => qr{^/v1/moderations$},
handle => async sub($req, $ctx, $obj, $params) {await $self->moderations($obj, $req, $ctx)}, handle => "moderations",
request_class => "OpenAIAsync::Type::Requests::CreateModeration", request_class => "OpenAIAsync::Type::Requests::CreateModeration",
result_class => "OpenAIAsync::Type::Results::Moderations", result_class => "OpenAIAsync::Type::Results::Moderations",
); );
} }
async method moderations($obj, $http_req, $ctx); async method moderations($req, $future_status, $queue, $ctx, $obj, $params);
} }