|
16 | 16 | package io.agentscope.core.model; |
17 | 17 |
|
18 | 18 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
19 | 21 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
20 | 25 |
|
21 | 26 | import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter; |
22 | 27 | import io.agentscope.core.formatter.dashscope.DashScopeMultiAgentFormatter; |
| 28 | +import io.agentscope.core.formatter.dashscope.dto.DashScopeParameters; |
| 29 | +import io.agentscope.core.formatter.dashscope.dto.DashScopeRequest; |
23 | 30 | import io.agentscope.core.message.Msg; |
24 | 31 | import io.agentscope.core.model.test.ModelTestUtils; |
| 32 | +import java.lang.reflect.InvocationTargetException; |
| 33 | +import java.lang.reflect.Method; |
25 | 34 | import java.util.ArrayList; |
26 | 35 | import java.util.List; |
27 | 36 | import org.junit.jupiter.api.BeforeEach; |
@@ -410,4 +419,96 @@ void testWithThinkingMode() { |
410 | 419 |
|
411 | 420 | assertNotNull(thinkingModel); |
412 | 421 | } |
| 422 | + |
| 423 | + @Test |
| 424 | + @DisplayName("DashScope chat model apply thinking mode") |
| 425 | + void testApplyThinkingMode() { |
| 426 | + DashScopeChatModel chatModel = |
| 427 | + DashScopeChatModel.builder() |
| 428 | + .apiKey(mockApiKey) |
| 429 | + .modelName("qwen-plus") |
| 430 | + .enableThinking(true) |
| 431 | + .enableSearch(false) |
| 432 | + .build(); |
| 433 | + |
| 434 | + DashScopeRequest request = |
| 435 | + DashScopeRequest.builder() |
| 436 | + .parameters(DashScopeParameters.builder().build()) |
| 437 | + .build(); |
| 438 | + |
| 439 | + GenerateOptions options = GenerateOptions.builder().thinkingBudget(100).build(); |
| 440 | + |
| 441 | + assertDoesNotThrow(() -> invokeApplyThinkingMode(chatModel, request, options)); |
| 442 | + |
| 443 | + assertTrue(request.getParameters().getEnableThinking()); |
| 444 | + assertFalse(request.getParameters().getEnableSearch()); |
| 445 | + assertEquals(100, request.getParameters().getThinkingBudget()); |
| 446 | + } |
| 447 | + |
| 448 | + @Test |
| 449 | + @DisplayName("DashScope chat model apply thinking mode with null") |
| 450 | + void testApplyThinkingModeWithNull() { |
| 451 | + DashScopeChatModel chatModel = |
| 452 | + DashScopeChatModel.builder().apiKey(mockApiKey).modelName("qwen-plus").build(); |
| 453 | + |
| 454 | + DashScopeRequest request = |
| 455 | + DashScopeRequest.builder() |
| 456 | + .parameters(DashScopeParameters.builder().build()) |
| 457 | + .build(); |
| 458 | + |
| 459 | + GenerateOptions options = GenerateOptions.builder().build(); |
| 460 | + |
| 461 | + assertDoesNotThrow(() -> invokeApplyThinkingMode(chatModel, request, options)); |
| 462 | + |
| 463 | + assertNull(request.getParameters().getEnableThinking()); |
| 464 | + assertNull(request.getParameters().getEnableSearch()); |
| 465 | + assertNull(request.getParameters().getThinkingBudget()); |
| 466 | + } |
| 467 | + |
| 468 | + @Test |
| 469 | + @DisplayName( |
| 470 | + "Should throw an IllegalStateException when setting thinkingBudget while thinking mode" |
| 471 | + + " is disabled") |
| 472 | + void testApplyThinkingModeValidation() { |
| 473 | + DashScopeChatModel chatModel = |
| 474 | + DashScopeChatModel.builder() |
| 475 | + .apiKey(mockApiKey) |
| 476 | + .modelName("qwen-plus") |
| 477 | + .enableThinking(false) |
| 478 | + .enableSearch(false) |
| 479 | + .build(); |
| 480 | + |
| 481 | + DashScopeRequest request = |
| 482 | + DashScopeRequest.builder() |
| 483 | + .parameters(DashScopeParameters.builder().build()) |
| 484 | + .build(); |
| 485 | + |
| 486 | + GenerateOptions options = GenerateOptions.builder().thinkingBudget(100).build(); |
| 487 | + |
| 488 | + assertThrows( |
| 489 | + IllegalStateException.class, |
| 490 | + () -> invokeApplyThinkingMode(chatModel, request, options)); |
| 491 | + } |
| 492 | + |
| 493 | + /** |
| 494 | + * Use reflection to invoke applyThinkingMode |
| 495 | + * |
| 496 | + * @param model the dashscope model |
| 497 | + * @param request the dashscope API request DTO |
| 498 | + * @param options the generation options for LLM models |
| 499 | + * @throws Throwable throw the target exception |
| 500 | + */ |
| 501 | + private void invokeApplyThinkingMode( |
| 502 | + DashScopeChatModel model, DashScopeRequest request, GenerateOptions options) |
| 503 | + throws Throwable { |
| 504 | + try { |
| 505 | + Method method = |
| 506 | + DashScopeChatModel.class.getDeclaredMethod( |
| 507 | + "applyThinkingMode", DashScopeRequest.class, GenerateOptions.class); |
| 508 | + method.setAccessible(true); |
| 509 | + method.invoke(model, request, options); |
| 510 | + } catch (InvocationTargetException e) { |
| 511 | + throw e.getTargetException(); |
| 512 | + } |
| 513 | + } |
413 | 514 | } |
0 commit comments