|
27 | 27 | import com.alibaba.cloud.ai.graph.agent.interceptor.InterceptorChain; |
28 | 28 |
|
29 | 29 | import org.springframework.ai.chat.client.ChatClient; |
| 30 | +import org.springframework.ai.chat.client.DefaultChatClient; |
30 | 31 | import org.springframework.ai.chat.client.advisor.api.Advisor; |
31 | 32 | import org.springframework.ai.chat.messages.AssistantMessage; |
32 | 33 | import org.springframework.ai.chat.messages.Message; |
|
40 | 41 | import org.springframework.ai.model.tool.ToolCallingChatOptions; |
41 | 42 | import org.springframework.ai.tool.ToolCallback; |
42 | 43 |
|
| 44 | +import org.springframework.lang.Nullable; |
43 | 45 | import org.springframework.util.StringUtils; |
44 | 46 |
|
45 | 47 | import org.slf4j.Logger; |
@@ -155,7 +157,7 @@ public Map<String, Object> apply(OverAllState state, RunnableConfig config) thro |
155 | 157 | // Create ModelRequest |
156 | 158 | ModelRequest.Builder requestBuilder = ModelRequest.builder() |
157 | 159 | .messages(messages) |
158 | | - .options(chatOptions.copy()) |
| 160 | + .options(this.chatOptions != null ? this.chatOptions.copy() : null) |
159 | 161 | .context(config.metadata().orElse(new HashMap<>())); |
160 | 162 |
|
161 | 163 | // Extract tool names and descriptions from toolCallbacks and pass them to ModelRequest |
@@ -319,12 +321,10 @@ private List<Message> appendSystemPromptIfNeeded(ModelRequest modelRequest) { |
319 | 321 | * @param toolCallbacks the tool callbacks to be included |
320 | 322 | * @return merged ToolCallingChatOptions |
321 | 323 | */ |
| 324 | + @Nullable |
322 | 325 | private ToolCallingChatOptions buildChatOptions(ChatOptions chatOptions, List<ToolCallback> toolCallbacks) { |
323 | 326 | if (chatOptions == null) { |
324 | | - return ToolCallingChatOptions.builder() |
325 | | - .toolCallbacks(toolCallbacks) |
326 | | - .internalToolExecutionEnabled(false) |
327 | | - .build(); |
| 327 | + return null; |
328 | 328 | } |
329 | 329 |
|
330 | 330 | if (chatOptions instanceof ToolCallingChatOptions builderToolCallingOptions) { |
@@ -457,21 +457,40 @@ private ChatClient.ChatClientRequestSpec buildChatClientRequestSpec(ModelRequest |
457 | 457 | List<ToolCallback> filteredToolCallbacks = filterToolCallbacks(modelRequest); |
458 | 458 | filteredToolCallbacks.addAll(modelRequest.getDynamicToolCallbacks()); |
459 | 459 |
|
460 | | - ToolCallingChatOptions chatOptions = modelRequest.getOptions(); |
461 | | - if (chatOptions == null) { |
462 | | - chatOptions = ToolCallingChatOptions.builder() |
463 | | - .toolCallbacks(filteredToolCallbacks) |
464 | | - .internalToolExecutionEnabled(false) |
465 | | - .build(); |
466 | | - } else { |
467 | | - chatOptions.setToolCallbacks(filteredToolCallbacks); |
468 | | - chatOptions.setInternalToolExecutionEnabled(false); |
469 | | - } |
| 460 | + var promptSpec = this.chatClient.prompt() |
| 461 | + .messages(messages) |
| 462 | + .advisors(this.advisors); |
| 463 | + |
| 464 | + ToolCallingChatOptions requestOptions = modelRequest.getOptions(); |
| 465 | + |
| 466 | + if (requestOptions != null) { |
| 467 | + requestOptions.setToolCallbacks(filteredToolCallbacks); |
| 468 | + // force disable internal tool execution to avoid conflict with Agent framework's tool execution management. |
| 469 | + requestOptions.setInternalToolExecutionEnabled(false); |
| 470 | + promptSpec.options(requestOptions); |
| 471 | + } else { |
| 472 | + // Check if user has set default options in ChatModel or ChatClient. |
| 473 | + if (promptSpec instanceof DefaultChatClient.DefaultChatClientRequestSpec defaultChatClientRequestSpec) { |
| 474 | + ChatOptions options = defaultChatClientRequestSpec.getChatOptions(); |
| 475 | + // If no default options set, create new ToolCallingChatOptions with filtered tool callbacks and toolExecution disabled. |
| 476 | + if (options == null) { |
| 477 | + options = ToolCallingChatOptions.builder() |
| 478 | + .toolCallbacks(filteredToolCallbacks) |
| 479 | + .internalToolExecutionEnabled(false) |
| 480 | + .build(); |
| 481 | + defaultChatClientRequestSpec.options(options); |
| 482 | + } |
| 483 | + // If options is ToolCallingChatOptions, set filtered tool callbacks and toolExecution disabled. |
| 484 | + else if (options instanceof ToolCallingChatOptions toolCallingChatOptions) { |
| 485 | + toolCallingChatOptions.setToolCallbacks(filteredToolCallbacks); |
| 486 | + toolCallingChatOptions.setInternalToolExecutionEnabled(false); |
| 487 | + } |
| 488 | + } else if (!filteredToolCallbacks.isEmpty()) { |
| 489 | + promptSpec.tools(filteredToolCallbacks); |
| 490 | + } |
| 491 | + } |
470 | 492 |
|
471 | | - return chatClient.prompt() |
472 | | - .options(chatOptions) |
473 | | - .messages(messages) |
474 | | - .advisors(advisors); |
| 493 | + return promptSpec; |
475 | 494 | } |
476 | 495 |
|
477 | 496 | public String getName() { |
|
0 commit comments