Skip to content

Commit c6ca598

Browse files
authored
validateXorFields is modified and model-models xor relation is added, tests are added for chatData parameters (moe-mizrak#6)
* Image content DTOs are added and tests are improved for variety scenarios * main.yml is moved to workflows directory * phpunit.xml is removed from gitignore * Cache Composer Dependencies in main.yml is modified * bug is resolved: php version in main.yml is fixed * endpoint for open router api is fixed * main.yml is fixed for rate limitation on requests * main.yml is fixed for rate limitation on requests * tests for response format and stop param are added * cost reguest is added along with tests and DTO for cost response * validateXorFields is modified and model-models xor relation is added, tests are added for chatData parameters
1 parent 50a102c commit c6ca598

File tree

4 files changed

+276
-31
lines changed

4 files changed

+276
-31
lines changed

src/DTO/ChatData.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,38 @@ class ChatData extends DataTransferObject
2525
* @throws \Spatie\DataTransferObject\Exceptions\UnknownProperties
2626
*/
2727
public function __construct($params)
28+
{
29+
$this->validateXorFields($params);
30+
31+
parent::__construct($params);
32+
}
33+
34+
/**
35+
* Validate the XOR fields and throw an exception if not valid.
36+
*
37+
* @param array $params
38+
* @throws XorValidationException
39+
*/
40+
private function validateXorFields(array $params): void
2841
{
2942
/**
30-
* Handle validation for XOR fields here and throw an exception if NOT valid:
43+
* Set the fields that have xor relation.
3144
*/
32-
$validatorMessagesAndPrompt = new XORFields('messages', 'prompt'); // messages and prompt fields are XOR gated
33-
$validationResultMessagesAndPrompt = $validatorMessagesAndPrompt->validate($params); // Validate params
34-
35-
if (! $validationResultMessagesAndPrompt->isValid) {
36-
throw new XorValidationException($validationResultMessagesAndPrompt->message);
45+
$xorFields = [
46+
['messages', 'prompt'], // messages and prompt fields are XOR gated
47+
['model', 'models'], // model and models fields are XOR gated
48+
];
49+
50+
// Loop through the xor fields and validate
51+
foreach ($xorFields as [$firstField, $secondField]) {
52+
$validator = new XORFields($firstField, $secondField);
53+
$validationResult = $validator->validate($params);
54+
55+
// Throw exception in case validation is failed.
56+
if (!$validationResult->isValid) {
57+
throw new XorValidationException($validationResult->message);
58+
}
3759
}
38-
39-
parent::__construct($params);
4060
}
4161

4262
/**
@@ -100,10 +120,10 @@ public function __construct($params)
100120
/**
101121
* Only natively supported by OpenAI models. For others, we submit a YAML-formatted string with these tools at the end of the prompt.
102122
*
103-
* @var string|ToolCallData|null
123+
* @var string|array|null
104124
*/
105125
#[AllowedValues([ToolChoiceType::AUTO, ToolChoiceType::NONE])]
106-
public string|ToolCallData|null $tool_choice; // none|auto or ToolCallData as {"type": "function", "function": {"name": "my_function"}}
126+
public string|array|null $tool_choice; // none|auto or ToolCallData as {"type": "function", "function": {"name": "my_function"}}
107127

108128
/**
109129
* Tool calls (also known as function calling) allow you to give an LLM access to external tools.
@@ -116,7 +136,7 @@ public function __construct($params)
116136
/**
117137
* Modify the likelihood of specified tokens appearing in the completion. e.g. {"50256": -100}
118138
*/
119-
public ?array $logit_bias; // todo json object, map => check openai doc and test it for array also
139+
public ?array $logit_bias;
120140

121141
// OpenRouter-only parameters
122142
/**

src/DTO/ToolCallData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class ToolCallData extends DataTransferObject
2323
/**
2424
* Name of the tool. (i.e. function)
2525
*
26-
* @var string
26+
* @var string|null
2727
*/
28-
public string $type;
28+
public ?string $type;
2929

3030
/**
3131
* Function DTO object.

src/Rules/AllowedValues.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MoeMizrak\LaravelOpenrouter\Rules;
44

5-
use Spatie\DataTransferObject\DataTransferObject;
65
use Spatie\DataTransferObject\Validation\ValidationResult;
76
use Spatie\DataTransferObject\Validator;
87

@@ -29,7 +28,7 @@ public function __construct(protected array $acceptableValues = [])
2928
*/
3029
public function validate(mixed $value): ValidationResult
3130
{
32-
if (in_array($value, $this->acceptableValues) || is_null($value) || $value instanceof DataTransferObject) {
31+
if (in_array($value, $this->acceptableValues) || is_null($value) || is_array($value)) {
3332
return ValidationResult::valid();
3433
}
3534

0 commit comments

Comments
 (0)