Skip to content

Commit

Permalink
messageData DTO is added to tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-mizrak authored May 29, 2024
1 parent f06b4ee commit 931575b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 99 deletions.
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
]),
]);
```
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
]);

Expand All @@ -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
Expand Down
121 changes: 47 additions & 74 deletions tests/OpenRouterAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@ class OpenRouterAPITest extends TestCase
private int $maxTokens;
private string $content;
private string $prompt;
private MessageData $messageData;

public function setUp(): void
{
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
]);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 931575b

Please sign in to comment.