From 502433f9a1f56feb4cd759d626cf44429db4223b Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Wed, 18 Dec 2024 11:07:10 -0500 Subject: [PATCH] beginning rename of things --- lib/{OpenAIAsync.pm => Net/Async/xLM.pm} | 0 lib/{OpenAIAsync.pod => Net/Async/xLM.pod} | 0 .../Async/xLM/API}/Client.pm | 0 .../Async/xLM/API}/Client/Stream.pm | 0 .../Async/xLM/API}/Server.pm | 0 .../Async/xLM/API}/Server/API/v1/Audio.pm | 0 .../xLM/API}/Server/API/v1/ChatCompletion.pm | 0 .../xLM/API}/Server/API/v1/Completions.pm | 0 .../xLM/API}/Server/API/v1/Embeddings.pm | 0 .../Async/xLM/API}/Server/API/v1/File.pm | 0 .../Async/xLM/API}/Server/API/v1/Image.pm | 0 .../Async/xLM/API}/Server/API/v1/ModelList.pm | 0 .../xLM/API}/Server/API/v1/Moderations.pm | 0 .../Async/xLM/API}/Server/Stream.pm | 0 .../Async/xLM/API}/Types.pm | 0 .../Async/xLM/API}/Types/Requests.pm | 1 - .../API}/Types/Requests/ChatCompletion.pod | 0 .../xLM/API}/Types/Requests/Completion.pod | 0 .../xLM/API}/Types/Requests/Embedding.pod | 0 .../Async/xLM/API}/Types/Results.pm | 0 .../xLM/API}/Types/Results/ChatCompletion.pod | 0 .../xLM/API}/Types/Results/Completion.pod | 0 .../API}/Types/Results/CompletionChoices.pod | 0 .../xLM/API}/Types/Results/Embedding.pod | 0 .../Async/xLM/API}/Types/Results/LogProbs.pod | 0 .../Async/xLM/API}/Types/Results/Usage.pod | 0 .../Async/xLM/API}/Types/Shared.pm | 0 t/03-create-server.t | 41 +++++++++++++++++++ 28 files changed, 41 insertions(+), 1 deletion(-) rename lib/{OpenAIAsync.pm => Net/Async/xLM.pm} (100%) rename lib/{OpenAIAsync.pod => Net/Async/xLM.pod} (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Client.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Client/Stream.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/Audio.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/ChatCompletion.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/Completions.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/Embeddings.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/File.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/Image.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/ModelList.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/API/v1/Moderations.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Server/Stream.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Requests.pm (99%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Requests/ChatCompletion.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Requests/Completion.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Requests/Embedding.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results.pm (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/ChatCompletion.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/Completion.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/CompletionChoices.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/Embedding.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/LogProbs.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Results/Usage.pod (100%) rename lib/{OpenAIAsync => Net/Async/xLM/API}/Types/Shared.pm (100%) diff --git a/lib/OpenAIAsync.pm b/lib/Net/Async/xLM.pm similarity index 100% rename from lib/OpenAIAsync.pm rename to lib/Net/Async/xLM.pm diff --git a/lib/OpenAIAsync.pod b/lib/Net/Async/xLM.pod similarity index 100% rename from lib/OpenAIAsync.pod rename to lib/Net/Async/xLM.pod diff --git a/lib/OpenAIAsync/Client.pm b/lib/Net/Async/xLM/API/Client.pm similarity index 100% rename from lib/OpenAIAsync/Client.pm rename to lib/Net/Async/xLM/API/Client.pm diff --git a/lib/OpenAIAsync/Client/Stream.pm b/lib/Net/Async/xLM/API/Client/Stream.pm similarity index 100% rename from lib/OpenAIAsync/Client/Stream.pm rename to lib/Net/Async/xLM/API/Client/Stream.pm diff --git a/lib/OpenAIAsync/Server.pm b/lib/Net/Async/xLM/API/Server.pm similarity index 100% rename from lib/OpenAIAsync/Server.pm rename to lib/Net/Async/xLM/API/Server.pm diff --git a/lib/OpenAIAsync/Server/API/v1/Audio.pm b/lib/Net/Async/xLM/API/Server/API/v1/Audio.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/Audio.pm rename to lib/Net/Async/xLM/API/Server/API/v1/Audio.pm diff --git a/lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm b/lib/Net/Async/xLM/API/Server/API/v1/ChatCompletion.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/ChatCompletion.pm rename to lib/Net/Async/xLM/API/Server/API/v1/ChatCompletion.pm diff --git a/lib/OpenAIAsync/Server/API/v1/Completions.pm b/lib/Net/Async/xLM/API/Server/API/v1/Completions.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/Completions.pm rename to lib/Net/Async/xLM/API/Server/API/v1/Completions.pm diff --git a/lib/OpenAIAsync/Server/API/v1/Embeddings.pm b/lib/Net/Async/xLM/API/Server/API/v1/Embeddings.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/Embeddings.pm rename to lib/Net/Async/xLM/API/Server/API/v1/Embeddings.pm diff --git a/lib/OpenAIAsync/Server/API/v1/File.pm b/lib/Net/Async/xLM/API/Server/API/v1/File.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/File.pm rename to lib/Net/Async/xLM/API/Server/API/v1/File.pm diff --git a/lib/OpenAIAsync/Server/API/v1/Image.pm b/lib/Net/Async/xLM/API/Server/API/v1/Image.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/Image.pm rename to lib/Net/Async/xLM/API/Server/API/v1/Image.pm diff --git a/lib/OpenAIAsync/Server/API/v1/ModelList.pm b/lib/Net/Async/xLM/API/Server/API/v1/ModelList.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/ModelList.pm rename to lib/Net/Async/xLM/API/Server/API/v1/ModelList.pm diff --git a/lib/OpenAIAsync/Server/API/v1/Moderations.pm b/lib/Net/Async/xLM/API/Server/API/v1/Moderations.pm similarity index 100% rename from lib/OpenAIAsync/Server/API/v1/Moderations.pm rename to lib/Net/Async/xLM/API/Server/API/v1/Moderations.pm diff --git a/lib/OpenAIAsync/Server/Stream.pm b/lib/Net/Async/xLM/API/Server/Stream.pm similarity index 100% rename from lib/OpenAIAsync/Server/Stream.pm rename to lib/Net/Async/xLM/API/Server/Stream.pm diff --git a/lib/OpenAIAsync/Types.pm b/lib/Net/Async/xLM/API/Types.pm similarity index 100% rename from lib/OpenAIAsync/Types.pm rename to lib/Net/Async/xLM/API/Types.pm diff --git a/lib/OpenAIAsync/Types/Requests.pm b/lib/Net/Async/xLM/API/Types/Requests.pm similarity index 99% rename from lib/OpenAIAsync/Types/Requests.pm rename to lib/Net/Async/xLM/API/Types/Requests.pm index 9fcb069..086251c 100644 --- a/lib/OpenAIAsync/Types/Requests.pm +++ b/lib/Net/Async/xLM/API/Types/Requests.pm @@ -47,7 +47,6 @@ class OpenAIAsync::Types::Requests::ChatCompletion :Struct { field $function_call :JSONExclude = undef; field $functions :JSONExclude = undef; } - class OpenAIAsync::Types::Requests::Completion :Struct { apply OpenAIAsync::Types::Requests::Base; diff --git a/lib/OpenAIAsync/Types/Requests/ChatCompletion.pod b/lib/Net/Async/xLM/API/Types/Requests/ChatCompletion.pod similarity index 100% rename from lib/OpenAIAsync/Types/Requests/ChatCompletion.pod rename to lib/Net/Async/xLM/API/Types/Requests/ChatCompletion.pod diff --git a/lib/OpenAIAsync/Types/Requests/Completion.pod b/lib/Net/Async/xLM/API/Types/Requests/Completion.pod similarity index 100% rename from lib/OpenAIAsync/Types/Requests/Completion.pod rename to lib/Net/Async/xLM/API/Types/Requests/Completion.pod diff --git a/lib/OpenAIAsync/Types/Requests/Embedding.pod b/lib/Net/Async/xLM/API/Types/Requests/Embedding.pod similarity index 100% rename from lib/OpenAIAsync/Types/Requests/Embedding.pod rename to lib/Net/Async/xLM/API/Types/Requests/Embedding.pod diff --git a/lib/OpenAIAsync/Types/Results.pm b/lib/Net/Async/xLM/API/Types/Results.pm similarity index 100% rename from lib/OpenAIAsync/Types/Results.pm rename to lib/Net/Async/xLM/API/Types/Results.pm diff --git a/lib/OpenAIAsync/Types/Results/ChatCompletion.pod b/lib/Net/Async/xLM/API/Types/Results/ChatCompletion.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/ChatCompletion.pod rename to lib/Net/Async/xLM/API/Types/Results/ChatCompletion.pod diff --git a/lib/OpenAIAsync/Types/Results/Completion.pod b/lib/Net/Async/xLM/API/Types/Results/Completion.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/Completion.pod rename to lib/Net/Async/xLM/API/Types/Results/Completion.pod diff --git a/lib/OpenAIAsync/Types/Results/CompletionChoices.pod b/lib/Net/Async/xLM/API/Types/Results/CompletionChoices.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/CompletionChoices.pod rename to lib/Net/Async/xLM/API/Types/Results/CompletionChoices.pod diff --git a/lib/OpenAIAsync/Types/Results/Embedding.pod b/lib/Net/Async/xLM/API/Types/Results/Embedding.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/Embedding.pod rename to lib/Net/Async/xLM/API/Types/Results/Embedding.pod diff --git a/lib/OpenAIAsync/Types/Results/LogProbs.pod b/lib/Net/Async/xLM/API/Types/Results/LogProbs.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/LogProbs.pod rename to lib/Net/Async/xLM/API/Types/Results/LogProbs.pod diff --git a/lib/OpenAIAsync/Types/Results/Usage.pod b/lib/Net/Async/xLM/API/Types/Results/Usage.pod similarity index 100% rename from lib/OpenAIAsync/Types/Results/Usage.pod rename to lib/Net/Async/xLM/API/Types/Results/Usage.pod diff --git a/lib/OpenAIAsync/Types/Shared.pm b/lib/Net/Async/xLM/API/Types/Shared.pm similarity index 100% rename from lib/OpenAIAsync/Types/Shared.pm rename to lib/Net/Async/xLM/API/Types/Shared.pm diff --git a/t/03-create-server.t b/t/03-create-server.t index 8a6998d..14e2280 100644 --- a/t/03-create-server.t +++ b/t/03-create-server.t @@ -1,26 +1,48 @@ +# test for OpenAIAsync perl module use strict; use warnings; +# Use the Test2::V0 module for testing use Test2::V0; use OpenAIAsync::Server; +# Import the OpenAIAsync::Server module +use OpenAIAsync::Server; + +# Use Object::Pad for object-oriented programming use Object::Pad; + +# Import IO::Async::Loop for managing loops and event loops use IO::Async::Loop; + +# Use Future::AsyncAwait for easier handling of asynchronous operations use Future::AsyncAwait; + +# Import JSON::MaybeXS for encoding and decoding JSON data use JSON::MaybeXS; + +# Import Net::Async::HTTP for asynchronous HTTP requests use Net::Async::HTTP; +# Use the relative path './lib' for module lookup use lib::relative './lib'; +# Create an instance of IO::Async::Loop my $loop = IO::Async::Loop->new(); +# Set the OPENAI_API_KEY environment variable if it is not set or not equal to "12345" BEGIN { no warnings 'uninitialized'; $ENV{OPENAI_API_KEY}="12345" unless $ENV{OPENAI_API_KEY}eq"12345"; + $ENV{OPENAI_API_KEY} = "12345" unless $ENV{OPENAI_API_KEY} eq "12345"; } +# Define a TestServer class that inherits from OpenAIAsync::Server class TestServer { + # Inherit methods and properties from OpenAIAsync::Server inherit OpenAIAsync::Server; + + # Apply methods from various OpenAIAsync::Server::API::Test modules apply OpenAIAsync::Server::API::Test::ChatCompletion; apply OpenAIAsync::Server::API::Test::Audio; apply OpenAIAsync::Server::API::Test::Completions; @@ -33,12 +55,20 @@ class TestServer { # Pick a random high port, TODO better scheme for this my $port = int(2048+rand(20480)); +# Pick a random high port number between 2048 and 22528 +my $port = int(2048 + rand(20480)); +# Create an instance of the TestServer class that listens on localhost with the chosen port my $server = TestServer->new(listen => '127.0.0.1', port => $port); + +# Create an instance of Net::Async::HTTP for making HTTP requests my $http_client = Net::Async::HTTP->new(); + +# Add the $http_client and $server instances to the event loop $loop->add($http_client); $loop->add($server); +# Define a hash ref for the chat completion request data my $chat_completion_input = { "model" => "gpt-3.5-turbo", "messages" => [ @@ -47,17 +77,24 @@ my $chat_completion_input = { "temperature" => 0.7 }; +# Subroutine to make an HTTP POST request to the server 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, content_type => 'application/json'); + my $content_json = encode_json($content); # Encode the content data as JSON + return $http_client->POST("http://127.0.0.1:$port/v1{$uri}", $content_json, content_type => 'application/json'); # Make the POST request } +# Make an HTTP POST request to the chat completions endpoint with the chat completion input data my $res_fut = mk_req("/chat/completions", $chat_completion_input); +# Delay the loop for 5 seconds to allow the request to complete $loop->delay_future(after => 5)->get(); +# Get the response object from the future my $res = $res_fut->get(); +# Extract the response content my $content = $res->content; is($content, '{"choices":[],"created":"0","id":"24601","model":"GumbyBrain-llm","object":"text_completion","system_fingerprint":"SHODAN node 12 of 16 tertiary adjunct of unimatrix 42","usage":{"completion_tokens":9,"prompt_tokens":6,"total_tokens":42}}', "check marshalling of data directly"); @@ -65,3 +102,7 @@ is($content, '{"choices":[],"created":"0","id":"24601","model":"GumbyBrain-llm", done_testing(); + + +# Compare the response content to the expected content and provide a test message +is($content, '{"choices":[],"created":"0","id":"24601","model":"GumbyBrain-llm","object":"text_completion","