Class ToolCallAdvisor
java.lang.Object
org.springframework.ai.chat.client.advisor.ToolCallAdvisor
- All Implemented Interfaces:
Advisor,CallAdvisor,StreamAdvisor,org.springframework.core.Ordered
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classToolCallAdvisor.Builder<T extends ToolCallAdvisor.Builder<T>>Builder for creating instances of ToolCallAdvisor. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.springframework.ai.model.tool.ToolCallingManagerFields inherited from interface org.springframework.ai.chat.client.advisor.api.Advisor
DEFAULT_CHAT_MEMORY_PRECEDENCE_ORDERFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder) protectedToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder, boolean conversationHistoryEnabled) protectedToolCallAdvisor(org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, int advisorOrder, boolean conversationHistoryEnabled, boolean streamToolCallResponses) -
Method Summary
Modifier and TypeMethodDescriptionadviseCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) reactor.core.publisher.Flux<ChatClientResponse>adviseStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain) static ToolCallAdvisor.Builder<?>builder()Creates a new Builder instance for constructing a ToolCallAdvisor.protected ChatClientResponsedoAfterCall(ChatClientResponse chatClientResponse, CallAdvisorChain callAdvisorChain) protected ChatClientResponsedoAfterStream(ChatClientResponse chatClientResponse, StreamAdvisorChain streamAdvisorChain) Hook method called after each streaming call in the tool call loop.protected ChatClientRequestdoBeforeCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) protected ChatClientRequestdoBeforeStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain) Hook method called before each streaming call in the tool call loop.protected ChatClientResponsedoFinalizeLoop(ChatClientResponse chatClientResponse, CallAdvisorChain callAdvisorChain) 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.protected List<org.springframework.ai.chat.messages.Message>doGetNextInstructionsForToolCall(ChatClientRequest chatClientRequest, ChatClientResponse chatClientResponse, org.springframework.ai.model.tool.ToolExecutionResult toolExecutionResult) 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.protected ChatClientRequestdoInitializeLoop(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) protected ChatClientRequestdoInitializeLoopStream(ChatClientRequest chatClientRequest, StreamAdvisorChain streamAdvisorChain) Hook method called at the start of the streaming tool call loop.getName()Return the name of the advisor.intgetOrder()
-
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
Description copied from interface:AdvisorReturn the name of the advisor. -
getOrder
public int getOrder()- Specified by:
getOrderin interfaceorg.springframework.core.Ordered
-
adviseCall
public ChatClientResponse adviseCall(ChatClientRequest chatClientRequest, CallAdvisorChain callAdvisorChain) - Specified by:
adviseCallin interfaceCallAdvisor
-
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:
adviseStreamin interfaceStreamAdvisor
-
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 requeststreamAdvisorChain- 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 processedstreamAdvisorChain- 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 callstreamAdvisorChain- 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 emitstreamAdvisorChain- 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 requestchatClientResponse- the current responsetoolExecutionResult- the result of tool execution- Returns:
- the list of messages to use as instructions for the next iteration
-
builder
Creates a new Builder instance for constructing a ToolCallAdvisor.- Returns:
- a new Builder instance
-