|
13 | 13 | use MoeMizrak\LaravelOpenrouter\DTO\LimitResponseData; |
14 | 14 | use MoeMizrak\LaravelOpenrouter\DTO\MessageData; |
15 | 15 | use MoeMizrak\LaravelOpenrouter\DTO\ProviderPreferencesData; |
| 16 | +use MoeMizrak\LaravelOpenrouter\DTO\ReasoningData; |
16 | 17 | use MoeMizrak\LaravelOpenrouter\DTO\ResponseData; |
17 | 18 | use MoeMizrak\LaravelOpenrouter\DTO\ResponseFormatData; |
18 | 19 | use MoeMizrak\LaravelOpenrouter\DTO\TextContentData; |
19 | 20 | use MoeMizrak\LaravelOpenrouter\Exceptions\OpenRouterValidationException; |
20 | 21 | use MoeMizrak\LaravelOpenrouter\Facades\LaravelOpenRouter; |
21 | 22 | use MoeMizrak\LaravelOpenrouter\OpenRouterRequest; |
22 | 23 | use MoeMizrak\LaravelOpenrouter\Types\DataCollectionType; |
| 24 | +use MoeMizrak\LaravelOpenrouter\Types\EffortType; |
23 | 25 | use MoeMizrak\LaravelOpenrouter\Types\RoleType; |
24 | 26 | use MoeMizrak\LaravelOpenrouter\Types\RouteType; |
25 | 27 | use PHPUnit\Framework\Attributes\Test; |
@@ -80,6 +82,37 @@ private function mockBasicBody(): array |
80 | 82 | ]; |
81 | 83 | } |
82 | 84 |
|
| 85 | + /** |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + private function mockReasoning(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'id' => 'gen-QcWgjEtiEDNHgomV2jjoQpCZlkRZ', |
| 92 | + 'provider' => 'HuggingFace', |
| 93 | + 'model' => $this->model, |
| 94 | + 'object' => 'chat.completion', |
| 95 | + 'created' => 1718888436, |
| 96 | + 'choices' => [ |
| 97 | + [ |
| 98 | + 'index' => 0, |
| 99 | + 'message' => [ |
| 100 | + 'role' => RoleType::ASSISTANT, |
| 101 | + 'content' => 'Some random content', |
| 102 | + 'reasoning' => 'The reasoning behind the answer is...', |
| 103 | + ], |
| 104 | + 'finish_reason' => 'stop', |
| 105 | + ], |
| 106 | + ], |
| 107 | + 'usage' => [ |
| 108 | + 'prompt_tokens' => 23, |
| 109 | + 'completion_tokens' => 100, |
| 110 | + 'total_tokens' => 123, |
| 111 | + 'cost' => 0.00000114, |
| 112 | + ], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
83 | 116 | /** |
84 | 117 | * @return array[] |
85 | 118 | */ |
@@ -190,6 +223,71 @@ public function it_makes_a_basic_chat_completion_open_route_api_request() |
190 | 223 | $this->assertNotNull(Arr::get($response->choices[0], 'message.content')); |
191 | 224 | } |
192 | 225 |
|
| 226 | + #[Test] |
| 227 | + public function it_makes_a_basic_chat_completion_open_route_api_request_with_reasoning_param() |
| 228 | + { |
| 229 | + /* SETUP */ |
| 230 | + $chatData = new ChatData( |
| 231 | + messages: [ |
| 232 | + $this->messageData, |
| 233 | + ], |
| 234 | + model: $this->model, |
| 235 | + max_tokens: $this->maxTokens, |
| 236 | + reasoning: new ReasoningData( |
| 237 | + effort: EffortType::HIGH, |
| 238 | + exclude: false, // Reasoning should not be excluded |
| 239 | + ), |
| 240 | + ); |
| 241 | + $this->mockOpenRouter($this->mockReasoning()); |
| 242 | + |
| 243 | + /* EXECUTE */ |
| 244 | + $response = $this->api->chatRequest($chatData); |
| 245 | + /* ASSERT */ |
| 246 | + $this->generalTestAssertions($response); |
| 247 | + $this->assertNotNull(Arr::get($response->choices[0], 'message.reasoning')); |
| 248 | + } |
| 249 | + |
| 250 | + #[Test] |
| 251 | + public function it_tests_chat_data_with_legacy_include_reasoning_param_if_mapping_to_reasoning() |
| 252 | + { |
| 253 | + /* SETUP */ |
| 254 | + // Legacy parameter `include_reasoning` is set to true, so it should be mapped to reasoning |
| 255 | + $firstChatData = new ChatData( |
| 256 | + messages: [ |
| 257 | + $this->messageData, |
| 258 | + ], |
| 259 | + model: $this->model, |
| 260 | + max_tokens: $this->maxTokens, |
| 261 | + include_reasoning: true, // Legacy parameter |
| 262 | + ); |
| 263 | + // neither include_reasoning nor reasoning is set, so it should not be mapped to reasoning |
| 264 | + $secondChatData = new ChatData( |
| 265 | + messages: [ |
| 266 | + $this->messageData, |
| 267 | + ], |
| 268 | + model: $this->model, |
| 269 | + max_tokens: $this->maxTokens, |
| 270 | + ); |
| 271 | + // reasoning is set, so it should ignore legacy parameter |
| 272 | + $thirdChatData = new ChatData( |
| 273 | + messages: [ |
| 274 | + $this->messageData, |
| 275 | + ], |
| 276 | + model: $this->model, |
| 277 | + max_tokens: $this->maxTokens, |
| 278 | + include_reasoning: false, |
| 279 | + reasoning: new ReasoningData( |
| 280 | + effort: EffortType::HIGH, |
| 281 | + exclude: false, |
| 282 | + ), |
| 283 | + ); |
| 284 | + |
| 285 | + /* ASSERT */ |
| 286 | + $this->assertFalse($firstChatData->reasoning->exclude); |
| 287 | + $this->assertTrue($secondChatData->reasoning->exclude); |
| 288 | + $this->assertFalse($thirdChatData->reasoning->exclude); |
| 289 | + } |
| 290 | + |
193 | 291 | #[Test] |
194 | 292 | public function it_makes_a_basic_chat_completion_open_route_api_request_with_historical_data() |
195 | 293 | { |
|
0 commit comments