From f54d3a89e679a2a8787d73192d9af6c79f6ae8d9 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Wed, 6 Mar 2024 14:58:38 -0500 Subject: [PATCH] Change full parameters of API and first try at tests --- lib/OpenAIAsync/Server.pm | 2 +- lib/OpenAIAsync/Server/API/v1/Audio.pm | 6 +-- .../Server/API/v1/ChatCompletion.pm | 2 +- lib/OpenAIAsync/Server/API/v1/Completions.pm | 2 +- lib/OpenAIAsync/Server/API/v1/Embeddings.pm | 2 +- lib/OpenAIAsync/Server/API/v1/File.pm | 10 ++--- lib/OpenAIAsync/Server/API/v1/Image.pm | 2 +- lib/OpenAIAsync/Server/API/v1/ModelList.pm | 2 +- lib/OpenAIAsync/Server/API/v1/Moderations.pm | 2 +- t/lib/OpenAIAsync/Server/API/Test/Audio.pm | 6 +-- .../Server/API/Test/ChatCompletion.pm | 42 ++++++++++++------- .../Server/API/Test/Completions.pm | 2 +- .../OpenAIAsync/Server/API/Test/Embeddings.pm | 2 +- t/lib/OpenAIAsync/Server/API/Test/File.pm | 10 ++--- t/lib/OpenAIAsync/Server/API/Test/Image.pm | 2 +- .../OpenAIAsync/Server/API/Test/ModelList.pm | 2 +- .../Server/API/Test/Moderations.pm | 2 +- 17 files changed, 55 insertions(+), 43 deletions(-) diff --git a/lib/OpenAIAsync/Server.pm b/lib/OpenAIAsync/Server.pm index 10623ab..f952be1 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($req, $future_status, $queue, $ctx, $obj, $params); + await $self->$route_method($future_status, $queue, $ctx, $obj, $params); my $status = await $future_status; my $is_streaming_event = $status->{is_streaming}; diff --git a/lib/OpenAIAsync/Server/API/v1/Audio.pm b/lib/OpenAIAsync/Server/API/v1/Audio.pm index 99ed8dc..5c0aee5 100644 --- a/lib/OpenAIAsync/Server/API/v1/Audio.pm +++ b/lib/OpenAIAsync/Server/API/v1/Audio.pm @@ -39,7 +39,7 @@ role OpenAIAsync::Server::API::v1::AudioTTS :strict(params) { ); } - async method audio_create_speech($req, $future_status, $queue, $ctx, $obj, $params); + async method audio_create_speech($future_status, $queue, $ctx, $obj, $params); } role OpenAIAsync::Server::API::v1::AudioSTT :strict(params) { @@ -53,7 +53,7 @@ role OpenAIAsync::Server::API::v1::AudioSTT :strict(params) { ); } - async method audio_create_transcript($req, $future_status, $queue, $ctx, $obj, $params); + async method audio_create_transcript($future_status, $queue, $ctx, $obj, $params); } role OpenAIAsync::Server::API::v1::AudioTranslate :strict(params) { @@ -67,7 +67,7 @@ role OpenAIAsync::Server::API::v1::AudioTranslate :strict(params) { ); } - async method audio_create_translation($req, $future_status, $queue, $ctx, $obj, $params); + async method audio_create_translation($future_status, $queue, $ctx, $obj, $params); } role OpenAIAsync::Server::API::v1::Audio :strict(params) { diff --git a/lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm b/lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm index 6c80eb0..1236f67 100644 --- a/lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm +++ b/lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm @@ -39,5 +39,5 @@ role OpenAIAsync::Server::API::v1::ChatCompletion :strict(params) { ); } - async method chat_completion($req, $future_status, $queue, $ctx, $obj, $params); + async method chat_completion($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/Completions.pm b/lib/OpenAIAsync/Server/API/v1/Completions.pm index ca7fe04..ed2d581 100644 --- a/lib/OpenAIAsync/Server/API/v1/Completions.pm +++ b/lib/OpenAIAsync/Server/API/v1/Completions.pm @@ -37,5 +37,5 @@ role OpenAIAsync::Server::API::v1::Completions :strict(params) { ); } - async method completion($req, $future_status, $queue, $ctx, $obj, $params); + async method completion($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/Embeddings.pm b/lib/OpenAIAsync/Server/API/v1/Embeddings.pm index f8377b3..9c2c5a4 100644 --- a/lib/OpenAIAsync/Server/API/v1/Embeddings.pm +++ b/lib/OpenAIAsync/Server/API/v1/Embeddings.pm @@ -37,5 +37,5 @@ role OpenAIAsync::Server::API::v1::Embeddings :strict(params) { ); } - async method embeddings($req, $future_status, $queue, $ctx, $obj, $params); + async method embeddings($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/File.pm b/lib/OpenAIAsync/Server/API/v1/File.pm index 757b4f6..7b7cc27 100644 --- a/lib/OpenAIAsync/Server/API/v1/File.pm +++ b/lib/OpenAIAsync/Server/API/v1/File.pm @@ -66,9 +66,9 @@ role OpenAIAsync::Server::API::v1::File :strict(params) { ); } - async method file_list($req, $future_status, $queue, $ctx, $obj, $params); - async method file_info($req, $future_status, $queue, $ctx, $obj, $params); - async method file_delete($req, $future_status, $queue, $ctx, $obj, $params); - async method file_upload($req, $future_status, $queue, $ctx, $obj, $params); - async method file_download($req, $future_status, $queue, $ctx, $obj, $params); + async method file_list($future_status, $queue, $ctx, $obj, $params); + async method file_info($future_status, $queue, $ctx, $obj, $params); + async method file_delete($future_status, $queue, $ctx, $obj, $params); + async method file_upload($future_status, $queue, $ctx, $obj, $params); + async method file_download($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/Image.pm b/lib/OpenAIAsync/Server/API/v1/Image.pm index c2b167c..22977cf 100644 --- a/lib/OpenAIAsync/Server/API/v1/Image.pm +++ b/lib/OpenAIAsync/Server/API/v1/Image.pm @@ -36,5 +36,5 @@ role OpenAIAsync::Server::API::v1::Image :strict(params) { ); } - async method create_image($req, $future_status, $queue, $ctx, $obj, $params); + async method create_image($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/ModelList.pm b/lib/OpenAIAsync/Server/API/v1/ModelList.pm index 2b99519..b8182f3 100644 --- a/lib/OpenAIAsync/Server/API/v1/ModelList.pm +++ b/lib/OpenAIAsync/Server/API/v1/ModelList.pm @@ -36,5 +36,5 @@ role OpenAIAsync::Server::API::v1::ModelList :strict(params) { ); } - async method model_list($req, $future_status, $queue, $ctx, $obj, $params); + async method model_list($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/lib/OpenAIAsync/Server/API/v1/Moderations.pm b/lib/OpenAIAsync/Server/API/v1/Moderations.pm index 9f64738..94dc83d 100644 --- a/lib/OpenAIAsync/Server/API/v1/Moderations.pm +++ b/lib/OpenAIAsync/Server/API/v1/Moderations.pm @@ -36,5 +36,5 @@ role OpenAIAsync::Server::API::v1::Moderations :strict(params) { ); } - async method moderations($req, $future_status, $queue, $ctx, $obj, $params); + async method moderations($future_status, $queue, $ctx, $obj, $params); } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/Audio.pm b/t/lib/OpenAIAsync/Server/API/Test/Audio.pm index 23b85fb..fe5d490 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/Audio.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/Audio.pm @@ -32,17 +32,17 @@ TODO document the subroles here, split up because TTS is much simpler to impleme role OpenAIAsync::Server::API::Test::AudioTTS :strict(params) { apply OpenAIAsync::Server::API::v1::AudioTTS; - async method audio_create_speech($obj, $http_req, $ctx) {...} + async method audio_create_speech($future_status, $queue, $ctx, $obj, $params) {...} } role OpenAIAsync::Server::API::Test::AudioSTT :strict(params) { apply OpenAIAsync::Server::API::v1::AudioSTT; - async method audio_create_transcript($obj, $http_req, $ctx) {...} + async method audio_create_transcript($future_status, $queue, $ctx, $obj, $params) {...} } role OpenAIAsync::Server::API::Test::AudioTranslate :strict(params) { apply OpenAIAsync::Server::API::v1::AudioTranslate; - async method audio_create_translation($obj, $http_req, $ctx) {...} + async method audio_create_translation($future_status, $queue, $ctx, $obj, $params) {...} } role OpenAIAsync::Server::API::Test::Audio :strict(params) { diff --git a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm index 7822357..d6e2cb2 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/ChatCompletion.pm @@ -30,19 +30,31 @@ role OpenAIAsync::Server::API::Test::ChatCompletion :strict(params) { use OpenAIAsync::Types::Results; use Future::AsyncAwait; - async method chat ($obj, $http_req, $ctx) { - return OpenAIAsync::Types::Results::ChatCompletion->new( - id => "24601", - choices => [], - model => "GumbyBrain-llm", - system_fingerprint => "SHODAN node 12 of 16 tertiary adjunct of unimatrix 42", - usage => { - total_tokens => 42, - prompt_tokens => 6, - completion_tokens => 9, - }, - object => "text_completion", - created => 0, - ) - } + async method chat ($future_status, $queue, $ctx, $obj, $params) { + my $chained_future = $future_status->then(sub { + return OpenAIAsync::Types::Results::ChatCompletion->new( + id => "24601", + choices => [], + model => "GumbyBrain-llm", + system_fingerprint => "SHODAN node 12 of 16 tertiary adjunct of unimatrix 42", + usage => { + total_tokens => 42, + prompt_tokens => 6, + completion_tokens => 9, + }, + object => "text_completion", + created => 0, + ) + } + ); + + $future_status->done({ + headers => {}, # TODO a way I can make this not leak and not expose the HTTP nature? + is_streaming => false, + content_type => "application/json", # TODO this should come from the object return type! + status_code => 200, + status_message => "OK", + }); + + return $chained_future; # TODO this might actually be wrong thanks to the async above? } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/Completions.pm b/t/lib/OpenAIAsync/Server/API/Test/Completions.pm index b873081..59ef2fc 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/Completions.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/Completions.pm @@ -28,5 +28,5 @@ OpenAIAsync::Server::API::Completions - Basic completion api role, consumed to i role OpenAIAsync::Server::API::Test::Completions :strict(params) { apply OpenAIAsync::Server::API::v1::Completions; - async method completion($obj, $http_req, $ctx) { ... } + async method completion($future_status, $queue, $ctx, $obj, $params) { ... } } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/Embeddings.pm b/t/lib/OpenAIAsync/Server/API/Test/Embeddings.pm index 7ec3b3a..2111460 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/Embeddings.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/Embeddings.pm @@ -28,5 +28,5 @@ OpenAIAsync::Server::API::Embeddings - Basic embeddings api role, consumed to im role OpenAIAsync::Server::API::Test::Embeddings { apply OpenAIAsync::Server::API::v1::Embeddings; - async method embeddings($obj, $http_req, $ctx) { ... } + async method embeddings($future_status, $queue, $ctx, $obj, $params) { ... } } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/File.pm b/t/lib/OpenAIAsync/Server/API/Test/File.pm index 14d8863..8ac3e36 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/File.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/File.pm @@ -28,9 +28,9 @@ OpenAIAsync::Server::API::File - Basic file api role, consumed to implement the role OpenAIAsync::Server::API::Test::File :strict(params) { apply OpenAIAsync::Server::API::v1::File; - async method file_list($http_req, $ctx) {...} - async method file_info($http_req, $ctx, $params) {...} - async method file_delete($http_req, $ctx, $params) {...} - async method file_upload($http_req, $ctx, $params) {...} - async method file_download($http_req, $ctx, $params) {...} + async method file_list($future_status, $queue, $ctx, $obj, $params) {...} + async method file_info($future_status, $queue, $ctx, $obj, $params) {...} + async method file_delete($future_status, $queue, $ctx, $obj, $params) {...} + async method file_upload($future_status, $queue, $ctx, $obj, $params) {...} + async method file_download($future_status, $queue, $ctx, $obj, $params) {...} } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/Image.pm b/t/lib/OpenAIAsync/Server/API/Test/Image.pm index 9349b09..8095b07 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/Image.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/Image.pm @@ -28,5 +28,5 @@ OpenAIAsync::Server::API::Image - Basic image role, consumed to implement the Op role OpenAIAsync::Server::API::Test::Image :strict(params) { apply OpenAIAsync::Server::API::v1::Image; - async method create_image($http_req, $ctx) {...} + async method create_image($future_status, $queue, $ctx, $obj, $params) {...} } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/ModelList.pm b/t/lib/OpenAIAsync/Server/API/Test/ModelList.pm index f0551a0..f171373 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/ModelList.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/ModelList.pm @@ -28,5 +28,5 @@ OpenAIAsync::Server::API::ModelList - Basic model list api role, consumed to imp role OpenAIAsync::Server::API::Test::ModelList :strict(params) { apply OpenAIAsync::Server::API::v1::ModelList; - async method model_list($obj, $http_req, $ctx) {...} + async method model_list($future_status, $queue, $ctx, $obj, $params) {...} } \ No newline at end of file diff --git a/t/lib/OpenAIAsync/Server/API/Test/Moderations.pm b/t/lib/OpenAIAsync/Server/API/Test/Moderations.pm index 597dab9..c7fe972 100644 --- a/t/lib/OpenAIAsync/Server/API/Test/Moderations.pm +++ b/t/lib/OpenAIAsync/Server/API/Test/Moderations.pm @@ -28,5 +28,5 @@ OpenAIAsync::Server::API::Moderations - Basic moderation api role, consumed to i role OpenAIAsync::Server::API::Test::Moderations :strict(params) { apply OpenAIAsync::Server::API::v1::Moderations; - async method moderations($obj, $http_req, $ctx) {...} + async method moderations($future_status, $queue, $ctx, $obj, $params) {...} } \ No newline at end of file