Class ToolCallAdvisor

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

public class ToolCallAdvisor extends Object implements CallAdvisor, StreamAdvisor
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.

Author:
Christian Tzolov
  • Field Details

    • toolCallingManager

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

    • ToolCallAdvisor

      protected ToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder)
    • ToolCallAdvisor

      protected ToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder, boolean conversationHistoryEnabled)
    • ToolCallAdvisor

      protected ToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder, boolean conversationHistoryEnabled, boolean streamToolCallResponses)
  • 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 ToolCallAdvisor.Builder<?> builder()
      Creates a new Builder instance for constructing a ToolCallAdvisor.
      Returns:
      a new Builder instance