Skip to content

Commit 64a2f0f

Browse files
authored
feat(ReActAgent): add generateOptions support to ReActAgent builder (#968)
Allow users to configure LLM generation parameters via a new generateOptions() method on ReActAgent.Builder.
1 parent d9d977a commit 64a2f0f

2 files changed

Lines changed: 536 additions & 3 deletions

File tree

agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public class ReActAgent extends StructuredOutputCapableAgent {
138138
private final int maxIters;
139139
private final ExecutionConfig modelExecutionConfig;
140140
private final ExecutionConfig toolExecutionConfig;
141+
private final GenerateOptions generateOptions;
141142
private final PlanNotebook planNotebook;
142143
private final ToolExecutionContext toolExecutionContext;
143144
private final StatePersistence statePersistence;
@@ -159,6 +160,7 @@ private ReActAgent(Builder builder, Toolkit agentToolkit) {
159160
this.maxIters = builder.maxIters;
160161
this.modelExecutionConfig = builder.modelExecutionConfig;
161162
this.toolExecutionConfig = builder.toolExecutionConfig;
163+
this.generateOptions = builder.generateOptions;
162164
this.planNotebook = builder.planNotebook;
163165
this.toolExecutionContext = builder.toolExecutionContext;
164166
this.statePersistence =
@@ -787,11 +789,17 @@ private List<ToolUseBlock> extractPendingToolCalls() {
787789

788790
@Override
789791
protected GenerateOptions buildGenerateOptions() {
790-
GenerateOptions.Builder builder = GenerateOptions.builder();
792+
// Start with user-configured generateOptions if available
793+
GenerateOptions baseOptions = generateOptions;
794+
795+
// If modelExecutionConfig is set, merge it into the options
791796
if (modelExecutionConfig != null) {
792-
builder.executionConfig(modelExecutionConfig);
797+
GenerateOptions execConfigOptions =
798+
GenerateOptions.builder().executionConfig(modelExecutionConfig).build();
799+
baseOptions = GenerateOptions.mergeOptions(execConfigOptions, baseOptions);
793800
}
794-
return builder.build();
801+
802+
return baseOptions != null ? baseOptions : GenerateOptions.builder().build();
795803
}
796804

797805
// ==================== Hook Notification Methods ====================
@@ -963,6 +971,15 @@ public PlanNotebook getPlanNotebook() {
963971
return planNotebook;
964972
}
965973

974+
/**
975+
* Gets the configured generation options for this agent.
976+
*
977+
* @return The generation options, or null if not configured
978+
*/
979+
public GenerateOptions getGenerateOptions() {
980+
return generateOptions;
981+
}
982+
966983
public static Builder builder() {
967984
return new Builder();
968985
}
@@ -980,6 +997,7 @@ public static class Builder {
980997
private int maxIters = 10;
981998
private ExecutionConfig modelExecutionConfig;
982999
private ExecutionConfig toolExecutionConfig;
1000+
private GenerateOptions generateOptions;
9831001
private final Set<Hook> hooks = new LinkedHashSet<>();
9841002
private boolean enableMetaTool = false;
9851003
private StructuredOutputReminder structuredOutputReminder =
@@ -1157,6 +1175,39 @@ public Builder toolExecutionConfig(ExecutionConfig toolExecutionConfig) {
11571175
return this;
11581176
}
11591177

1178+
/**
1179+
* Sets the generation options for model API calls.
1180+
*
1181+
* <p>This configuration controls LLM generation parameters such as temperature, topP,
1182+
* maxTokens, frequencyPenalty, presencePenalty, etc. These options are passed to the
1183+
* model during the reasoning phase.
1184+
*
1185+
* <p><b>Example usage:</b>
1186+
* <pre>{@code
1187+
* ReActAgent agent = ReActAgent.builder()
1188+
* .name("assistant")
1189+
* .model(model)
1190+
* .generateOptions(GenerateOptions.builder()
1191+
* .temperature(0.7)
1192+
* .topP(0.9)
1193+
* .maxTokens(1000)
1194+
* .build())
1195+
* .build();
1196+
* }</pre>
1197+
*
1198+
* <p><b>Note:</b> If both generateOptions and modelExecutionConfig are set,
1199+
* the modelExecutionConfig's executionConfig will be merged into the generateOptions,
1200+
* with modelExecutionConfig taking precedence for execution settings.
1201+
*
1202+
* @param generateOptions The generation options for model calls, can be null
1203+
* @return This builder instance for method chaining
1204+
* @see GenerateOptions
1205+
*/
1206+
public Builder generateOptions(GenerateOptions generateOptions) {
1207+
this.generateOptions = generateOptions;
1208+
return this;
1209+
}
1210+
11601211
/**
11611212
* Sets the structured output enforcement mode.
11621213
*

0 commit comments

Comments
 (0)