Class ToolCallingAdvisor

java.lang.Object
org.springframework.ai.chat.client.advisor.ToolCallingAdvisor
All Implemented Interfaces:
Advisor, CallAdvisor, StreamAdvisor, ToolAdvisor, org.springframework.core.Ordered
Direct Known Subclasses:
ToolCallAdvisor

public class ToolCallingAdvisor extends Object implements CallAdvisor, StreamAdvisor, ToolAdvisor
Recursive Advisor that disables the internal tool execution flow and instead implements the tool calling loop as part of the advisor chain.

It uses the CallAdvisorChainUtil to implement looping advisor chain calls.

This enables intercepting the tool calling loop by the rest of the advisors next in the chain.

When the prompt options do not implement ToolCallingChatOptions (e.g. for models that don't support tool calling), this advisor passes the request through to the next advisor without modification.

Since:
2.0.0
Author:
Christian Tzolov
  • Field Details

    • DEFAULT_ORDER

      public static final int DEFAULT_ORDER
      Default advisor order. Higher than Advisor.DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDER (+200) so that memory advisors at their default order are placed outside this advisor and do not participate in each tool-call iteration.
      See Also:
    • DEFAULT_TOOL_EXECUTION_ELIGIBILITY_CHECKER

      protected static final org.springframework.ai.model.tool.ToolExecutionEligibilityChecker DEFAULT_TOOL_EXECUTION_ELIGIBILITY_CHECKER
    • toolCallingManager

      protected final org.springframework.ai.model.tool.ToolCallingManager toolCallingManager
  • Constructor Details

    • ToolCallingAdvisor

      protected ToolCallingAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, org.springframework.ai.model.tool.ToolExecutionEligibilityChecker toolExecutionEligibilityChecker, int advisorOrder, boolean conversationHistoryEnabled)
  • Method Details

    • getName

      public String getName()
      Description copied from interface: Advisor
      Return the name of the advisor.
      Specified by:
      getName in interface Advisor
      Returns:
      the advisor name.
    • getOrder

      public int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered
    • adviseCall

      public ChatClientResponse adviseCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain)
      Specified by:
      adviseCall in interface CallAdvisor
    • doGetNextInstructionsForToolCall

      protected List<org.springframework.ai.chat.messages.Message> doGetNextInstructionsForToolCall(ChatClientRequest chatClientRequest, ChatClientResponse chatClientResponse, org.springframework.ai.model.tool.ToolExecutionResult toolExecutionResult)
    • doFinalizeLoop

      protected ChatClientResponse doFinalizeLoop(ChatClientResponse chatClientResponse, CallAdvisorChain callAdvisorChain)
    • doInitializeLoop

      protected ChatClientRequest doInitializeLoop(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain)
    • doBeforeCall

      protected ChatClientRequest doBeforeCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain)
    • doAfterCall

      protected ChatClientResponse doAfterCall(ChatClientResponse chatClientResponse, CallAdvisorChain callAdvisorChain)
    • adviseStream

      public reactor.core.publisher.Flux<ChatClientResponse> adviseStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain)
      Specified by:
      adviseStream in interface StreamAdvisor
    • doInitializeLoopStream

      protected ChatClientRequest doInitializeLoopStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain)
      Hook method called at the start of the streaming tool call loop. Subclasses can override to customize initialization behavior.
      Parameters:
      chatClientRequest - the initial request
      streamAdvisorChain - the stream advisor chain
      Returns:
      the potentially modified request
    • doBeforeStream

      protected ChatClientRequest doBeforeStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain)
      Hook method called before each streaming call in the tool call loop. Subclasses can override to customize pre-call behavior.
      Parameters:
      chatClientRequest - the request about to be processed
      streamAdvisorChain - the stream advisor chain
      Returns:
      the potentially modified request
    • doAfterStream

      protected ChatClientResponse doAfterStream(ChatClientResponse chatClientResponse, StreamAdvisorChain streamAdvisorChain)
      Hook method called after each streaming call in the tool call loop. Subclasses can override to customize post-call behavior.
      Parameters:
      chatClientResponse - the response from the call
      streamAdvisorChain - the stream advisor chain
      Returns:
      the potentially modified response
    • doFinalizeLoopStream

      protected reactor.core.publisher.Flux<ChatClientResponse> doFinalizeLoopStream(reactor.core.publisher.Flux<ChatClientResponse> chatClientResponseFlux, StreamAdvisorChain streamAdvisorChain)
      Hook method called at the end of the streaming tool call loop to finalize the response. Subclasses can override to customize finalization behavior.
      Parameters:
      chatClientResponseFlux - the flux of collected response chunks to emit
      streamAdvisorChain - the stream advisor chain
      Returns:
      the potentially modified flux of responses
    • doGetNextInstructionsForToolCallStream

      protected List<org.springframework.ai.chat.messages.Message> doGetNextInstructionsForToolCallStream(ChatClientRequest chatClientRequest, ChatClientResponse chatClientResponse, org.springframework.ai.model.tool.ToolExecutionResult toolExecutionResult)
      Hook method to determine the next instructions for a tool call iteration in streaming mode. Subclasses can override to customize conversation history handling.
      Parameters:
      chatClientRequest - the current request
      chatClientResponse - the current response
      toolExecutionResult - the result of tool execution
      Returns:
      the list of messages to use as instructions for the next iteration
    • builder

      public static ToolCallingAdvisor.Builder<?> builder()
      Creates a new Builder instance for constructing a ToolCallingAdvisor.
      Returns:
      a new Builder instance