Skip to content

Commit bff158d

Browse files
authored
feat(model): Explicitly assign value for thinking mode and search mode (#380)
1 parent e2df6a5 commit bff158d

2 files changed

Lines changed: 111 additions & 7 deletions

File tree

agentscope-core/src/main/java/io/agentscope/core/model/DashScopeChatModel.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,19 @@ private void applyThinkingMode(DashScopeRequest request, GenerateOptions options
264264
+ ".defaultOptions(GenerateOptions.builder().thinkingBudget(1000).build())");
265265
}
266266

267-
if (Boolean.TRUE.equals(enableThinking)) {
268-
request.getParameters().setEnableThinking(true);
269-
if (options.getThinkingBudget() != null) {
270-
request.getParameters().setThinkingBudget(options.getThinkingBudget());
271-
}
267+
if (enableThinking != null) {
268+
// Explicitly assign value for thinking mode
269+
request.getParameters().setEnableThinking(enableThinking);
270+
}
271+
272+
if (Boolean.TRUE.equals(enableThinking) && options.getThinkingBudget() != null) {
273+
request.getParameters().setThinkingBudget(options.getThinkingBudget());
272274
}
273275

274276
// Model-specific settings for search mode
275-
if (Boolean.TRUE.equals(enableSearch)) {
276-
request.getParameters().setEnableSearch(Boolean.TRUE);
277+
if (enableSearch != null) {
278+
// Explicitly assign value for search mode
279+
request.getParameters().setEnableSearch(enableSearch);
277280
}
278281
}
279282

agentscope-core/src/test/java/io/agentscope/core/model/DashScopeChatModelTest.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616
package io.agentscope.core.model;
1717

1818
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;
1921
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;
2025

2126
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
2227
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;
2330
import io.agentscope.core.message.Msg;
2431
import io.agentscope.core.model.test.ModelTestUtils;
32+
import java.lang.reflect.InvocationTargetException;
33+
import java.lang.reflect.Method;
2534
import java.util.ArrayList;
2635
import java.util.List;
2736
import org.junit.jupiter.api.BeforeEach;
@@ -410,4 +419,96 @@ void testWithThinkingMode() {
410419

411420
assertNotNull(thinkingModel);
412421
}
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+
}
413514
}

0 commit comments

Comments
 (0)