Class DecisionModelFilteringToolProvider
- All Implemented Interfaces:
ToolProvider
ToolProvider that passes on only the tools of another tool provider that are relevant to the conversation,
as decided by a DecisionModel. This keeps requests to the LLM small when a tool provider offers many tools,
for example an MCP server:
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.toolProvider(DecisionModelFilteringToolProvider.builder()
.toolProvider(mcpToolProvider)
.decisionModel(decisionModel)
.build())
.build();
Unlike a ToolSearchStrategy, the tools are selected before the first
LLM call, so no tool search round trip is needed. By default, every tool whose probability of being useful reaches
the minimum probability is passed on, and the selection is based on the last 3 messages of the conversation (see
DecisionModelFilteringToolProvider.Builder.maxMessages(Integer)), which helps with follow-up messages such as "do the same for Berlin".
Only the tools of the wrapped tool provider are filtered: tools configured directly on the AI Service, tools with
the SearchBehavior.ALWAYS_VISIBLE search behavior and the tools configured with
DecisionModelFilteringToolProvider.Builder.alwaysInclude(String...) are always passed on. Tools that were already called in the conversation are also always passed on, since some LLM providers
reject requests whose messages contain calls to tools that are not in the request. This is similar to a
ToolSearchStrategy, whose previously found tools stay available. The
previous messages, used both for this and for DecisionModelFilteringToolProvider.Builder.maxMessages(Integer), are only known if the caller
passes them in ToolProviderRequest.messages(), as LangChain4j AI Services do.
If the wrapped tool provider is dynamic, the tools are selected again, with a call
to the decision model, before each LLM call of the tool-calling loop. Since the messages sent to the decision model
usually do not change within a tool-calling loop, this only helps if the tools of the wrapped provider change.
The tools are selected for the user message as the user sent it: in AI Services, before retrieved content (RAG) and
output format instructions were added to it (see InvocationContext.originalUserMessage()). Otherwise, the
retrieved documents rather than the question would decide which tools are selected. The previous messages are sent
as they are stored in the chat memory, which by default includes the retrieved content.
If the user message has no text, all tools are passed on. If the decision model fails, the
DecisionModelFilteringToolProvider.FallbackStrategy applies: by default, all tools are passed on and a warning is logged.
- Since:
- 1.21.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumWhat the tool provider does when the decision model fails. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PromptTemplateThe default template of the question asked for each tool:"Would this tool help to handle the request?\nTool: {{name}}\nDescription: {{description}}". -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDecisionModelFilteringToolProvider(ToolProvider toolProvider, DecisionModel decisionModel) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()booleanWhether this tool provider is dynamic (i.e., should be re-evaluated before each LLM call in the tool execution loop).provideTools(ToolProviderRequest request) Provides tools for the request to the LLM.
-
Field Details
-
DEFAULT_QUESTION_TEMPLATE
The default template of the question asked for each tool:"Would this tool help to handle the request?\nTool: {{name}}\nDescription: {{description}}".
-
-
Constructor Details
-
DecisionModelFilteringToolProvider
-
DecisionModelFilteringToolProvider
-
-
Method Details
-
provideTools
Description copied from interface:ToolProviderProvides tools for the request to the LLM.- Specified by:
provideToolsin interfaceToolProvider- Parameters:
request- theToolProviderRequest, containsUserMessage,ChatMemoryID (seeMemoryId) andInvocationParameters.- Returns:
ToolProviderResultcontains tools that should be included in the request to the LLM.
-
isDynamic
public boolean isDynamic()Description copied from interface:ToolProviderWhether this tool provider is dynamic (i.e., should be re-evaluated before each LLM call in the tool execution loop).A static provider (default, returns
false) is called once per AI service invocation. A dynamic provider (returnstrue) has itsToolProvider.provideTools(ToolProviderRequest)called before each LLM call, allowing it to return different tools based on the current conversation state.- Specified by:
isDynamicin interfaceToolProvider- Returns:
trueif this provider should be re-evaluated before each LLM call
-
builder
-