From 931575bef075105f00fdaa32feef5de98a88792d Mon Sep 17 00:00:00 2001 From: Mizrak Date: Wed, 29 May 2024 12:58:56 +0300 Subject: [PATCH] messageData DTO is added to tests and readme --- README.md | 55 ++++++++-------- tests/OpenRouterAPITest.php | 121 ++++++++++++++---------------------- 2 files changed, 77 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index 458f531..9bd03a0 100644 --- a/README.md +++ b/README.md @@ -108,22 +108,22 @@ This is a sample chat data instance: ```php $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, + new MessageData([ + 'role' => RoleType::USER, 'content' => [ new TextContentData([ 'type' => TextContentData::ALLOWED_TYPE, 'text' => 'This is a sample text content.', ]), new ImageContentPartData([ - 'type' => ImageContentPartData::ALLOWED_TYPE, + 'type' => ImageContentPartData::ALLOWED_TYPE, 'image_url' => new ImageUrlData([ - 'url' => 'https://example.com/image.jpg', + 'url' => 'https://example.com/image.jpg', 'detail' => 'Sample image', ]), ]), ], - ], + ]), ], 'response_format' => new ResponseFormatData([ 'type' => 'json_object', @@ -149,9 +149,9 @@ $chatData = new ChatData([ 'models' => ['model1', 'model2'], 'route' => RouteType::FALLBACK, 'provider' => new ProviderPreferencesData([ - 'allow_fallbacks' => true, + 'allow_fallbacks' => true, 'require_parameters' => true, - 'data_collection' => DataCollectionType::ALLOW, + 'data_collection' => DataCollectionType::ALLOW, ]), ]); ``` @@ -162,13 +162,14 @@ To send a chat request, create an instance of `ChatData` and pass it to the `cha ```php $content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content $model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models) +$messageData = new MessageData([ + 'content' => $content, + 'role' => RoleType::USER, +]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $content, - ], + $messageData, ], 'model' => $model, 'max_tokens' => 100, // Adjust this value as needed @@ -181,13 +182,14 @@ To retrieve the cost of a generation, first make a `chat request` and obtain the ```php $content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content $model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models) +$messageData = new MessageData([ + 'content' => $content, + 'role' => RoleType::USER, +]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $content, - ], + $messageData, ], 'model' => $model, 'max_tokens' => 100, // Adjust this value as needed @@ -214,14 +216,16 @@ Similarly, to send a chat request, create an instance of `ChatData` and pass it ```php $content = 'Tell me a story about a rogue AI that falls in love with its creator.'; // Your desired prompt or content $model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models) +$messageData = new MessageData([ + 'content' => $content, + 'role' => RoleType::USER, +]); + $chatData = new ChatData([ - 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $content, - ], + 'messages' => [ + $messageData, ], - 'model' => $model, + 'model' => $model, 'max_tokens' => 100, // Adjust this value as needed ]); @@ -232,13 +236,14 @@ Similarly, to retrieve the cost of a generation, create a `chat request` to obta ```php $content = 'Tell me a story about a rogue AI that falls in love with its creator.'; $model = 'mistralai/mistral-7b-instruct:free'; // The OpenRouter model you want to use (https://openrouter.ai/docs#models) +$messageData = new MessageData([ + 'content' => $content, + 'role' => RoleType::USER, +]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $content, - ], + $messageData, ], 'model' => $model, 'max_tokens' => 100, // Adjust this value as needed diff --git a/tests/OpenRouterAPITest.php b/tests/OpenRouterAPITest.php index e3b1bfb..1eb9728 100644 --- a/tests/OpenRouterAPITest.php +++ b/tests/OpenRouterAPITest.php @@ -8,6 +8,7 @@ use MoeMizrak\LaravelOpenrouter\DTO\ImageContentPartData; use MoeMizrak\LaravelOpenrouter\DTO\ImageUrlData; use MoeMizrak\LaravelOpenrouter\DTO\LimitResponseData; +use MoeMizrak\LaravelOpenrouter\DTO\MessageData; use MoeMizrak\LaravelOpenrouter\DTO\ProviderPreferencesData; use MoeMizrak\LaravelOpenrouter\DTO\ResponseData; use MoeMizrak\LaravelOpenrouter\DTO\ResponseFormatData; @@ -28,6 +29,7 @@ class OpenRouterAPITest extends TestCase private int $maxTokens; private string $content; private string $prompt; + private MessageData $messageData; public function setUp(): void { @@ -37,6 +39,10 @@ public function setUp(): void $this->prompt = 'Why did the programmer go broke?'; $this->model = 'mistralai/mistral-7b-instruct:free'; $this->maxTokens = 100; + $this->messageData = new MessageData([ + 'content' => $this->content, + 'role' => RoleType::USER, + ]); $this->api = $this->app->make(OpenRouterRequest::class); } @@ -69,10 +75,7 @@ public function it_makes_a_basic_chat_completion_open_route_api_request() /* SETUP */ $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -133,10 +136,7 @@ public function it_throws_xor_validation_exception_when_both_message_and_prompt_ /* EXECUTE */ new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'prompt' => $this->prompt, 'model' => $this->model, @@ -154,12 +154,15 @@ public function it_successfully_sends_text_content_in_messages_in_the_open_route 'type' => TextContentData::ALLOWED_TYPE, // it can only take text for text content 'text' => $this->content, ]); + $messageData = new MessageData([ + 'role' => RoleType::USER, // text content is only for user role + 'content' => [ + $textContentData, + ], + ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, // text content is only for user role - 'content' => [$textContentData], // will be an array of text content data (it can take string, array or null) - ], + $messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -192,15 +195,16 @@ public function it_successfully_sends_image_and_text_content_in_messages_in_the_ 'type' => TextContentData::ALLOWED_TYPE, // it can only take text for text content 'text' => 'what is in the image?', ]); + $messageData = new MessageData([ + 'role' => RoleType::USER, // image content is only for user role + 'content' => [ + $textContentData, + $imageContentPartData, + ], + ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, // image content is only for user role - 'content' => [ - $textContentData, - $imageContentPartData, - ], - ], + $messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -229,15 +233,16 @@ public function it_successfully_sends_multiple_text_content_in_messages_in_the_o 'type' => TextContentData::ALLOWED_TYPE, // it can only take text for text content 'text' => 'Now, multiply the result with 10.', ]); + $messageData = new MessageData([ + 'role' => RoleType::USER, // Text content is only for user role + 'content' => [ + $textContentDataA, // First text content + $textContentDataB, // Second text content requires result from first content + ], + ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, // Text content is only for user role - 'content' => [ - $textContentDataA, // First text content - $textContentDataB, // Second text content requires result from first content - ], - ], + $messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -264,10 +269,7 @@ public function it_successfully_makes_a_basic_chat_completion_open_route_api_req // model is not set, so open router will use user default model $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, ]); @@ -295,10 +297,7 @@ public function it_makes_a_basic_chat_completion_open_route_api_request_with_res ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -322,12 +321,13 @@ public function it_makes_a_basic_chat_completion_open_route_api_request_with_sto /* SETUP */ $stop = ['bugs']; $content = 'Repeat this sentence: Function junction, where parameters meet, variables mingle, and bugs retreat.'; + $messageData = new MessageData([ + 'role' => RoleType::USER, + 'content' => $content, + ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $content, - ], + $messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -352,10 +352,7 @@ public function it_makes_cost_request_with_generation_id() /* SETUP */ $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens, @@ -405,10 +402,7 @@ public function it_makes_chat_completion_api_request_with_llm_parameters() $seed = 2; $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'model' => $this->model, 'max_tokens' => $maxTokens, @@ -448,10 +442,7 @@ public function it_makes_chat_completion_api_request_with_open_router_specific_p ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, 'transforms' => $transforms, @@ -496,10 +487,7 @@ public function it_makes_chat_completion_api_request_with_fallback_to_second_mod ]); $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, 'transforms' => $transforms, @@ -537,10 +525,7 @@ public function it_throws_xor_validation_exception_when_both_model_and_models_em /* EXECUTE */ new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, ]); @@ -559,10 +544,7 @@ public function it_throws_xor_validation_exception_when_both_model_and_models_ar /* EXECUTE */ new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, 'model' => $this->model, @@ -582,10 +564,7 @@ public function it_throws_validation_exception_when_NOT_ALLOWED_value_is_sent_fo /* EXECUTE */ new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, 'model' => $this->model, @@ -605,10 +584,7 @@ public function it_throws_validation_exception_when_NOT_ALLOWED_value_is_sent_fo /* EXECUTE */ new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'max_tokens' => $this->maxTokens, 'model' => $this->model, @@ -642,10 +618,7 @@ public function it_makes_a_open_route_api_request_by_using_facade() /* SETUP */ $chatData = new ChatData([ 'messages' => [ - [ - 'role' => RoleType::USER, - 'content' => $this->content, - ], + $this->messageData, ], 'model' => $this->model, 'max_tokens' => $this->maxTokens,