Class DecisionModelChatModelRouter
- All Implemented Interfaces:
ChatModelRouter
ChatModelRouter that uses a DecisionModel to select the route: the decision model chooses the
route whose description fits the last user message best.
ChatModel chatModel = RoutingChatModel.builder()
.route("simple", "Greetings, short factual questions, simple lookups", smallModel)
.route("complex", "Multi-step reasoning, code, analysis", largeModel)
.router(new DecisionModelChatModelRouter(decisionModel))
.defaultRoute("complex")
.build();
A route without a description is described by its name.
The decision model receives the last messages of the request (3 by default, see
DecisionModelChatModelRouter.Builder.maxMessages(Integer)), up to and including the last user message, as sent to the chat model, so that
short follow-ups such as "yes, go ahead" are understood. In an AI Service, the last user message is the one after
the prompt template and retrieved content were added to it. Content other than text is represented by a marker,
such as [attached image], so that a route whose description mentions images can be chosen for it.
The router selects the default route of the routing chat model (ChatModelRoutingResult.defaultRoute()) when
the request contains
no user message, and when the probability of the chosen route is below DecisionModelChatModelRouter.Builder.minProbability(Double).
A minimum probability requires a decision model that reports probabilities: otherwise the call fails with an
IllegalStateException, whatever the DecisionModelChatModelRouter.FallbackStrategy. When the decision model fails, the
DecisionModelChatModelRouter.FallbackStrategy applies: by default, the default route is used and a warning is logged. When only one
route can handle the request (for example, the only route supporting a JSON schema response format), that route is
selected without calling the decision model.
routeAsync(ChatModelRoutingRequest) uses DecisionModel.decideAsync(DecisionRequest), so it fails
with an AsyncNotSupportedException if the decision model does not support
asynchronous calls.
- Since:
- 1.21.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic enumWhat the router does when the decision model fails. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe default question asked to choose between the routes. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDecisionModelChatModelRouter(DecisionModel decisionModel) -
Method Summary
Modifier and TypeMethodDescriptionbuilder()route(ChatModelRoutingRequest request) Selects the route that handles the request.routeAsync(ChatModelRoutingRequest request) Non-blocking counterpart ofChatModelRouter.route(ChatModelRoutingRequest), used by the asynchronous and streaming methods of the routing chat models.voidvalidate(List<ChatModelRoute> routes) Checks that this router can route between the given routes, for example that every route has a description.
-
Field Details
-
DEFAULT_QUESTION
The default question asked to choose between the routes.- See Also:
-
-
Constructor Details
-
DecisionModelChatModelRouter
-
DecisionModelChatModelRouter
-
-
Method Details
-
validate
Description copied from interface:ChatModelRouterChecks that this router can route between the given routes, for example that every route has a description. Called when aRoutingChatModelorRoutingStreamingChatModelis created, so misconfigurations fail early. The default does nothing.- Specified by:
validatein interfaceChatModelRouter- Parameters:
routes- all routes, in the order in which they were configured.
-
route
Description copied from interface:ChatModelRouterSelects the route that handles the request.- Specified by:
routein interfaceChatModelRouter- Parameters:
request- the chat request to route and the routes that can handle it. Nevernull.- Returns:
- one of the routes (
ChatModelRoutingResult.route(String)), or the default route (ChatModelRoutingResult.defaultRoute()). Nevernull.
-
routeAsync
Description copied from interface:ChatModelRouterNon-blocking counterpart ofChatModelRouter.route(ChatModelRoutingRequest), used by the asynchronous and streaming methods of the routing chat models. Routers that can route without blocking, such as routers that call a model asynchronously, override it.The default returns a future failed with an
AsyncNotSupportedException: the non-blocking calls of the routing chat models (chatAsync(...), and streaming to aPublisher) then fail. A router that decides without blocking, for example with simple rules, can implement this method by returningCompletableFuture.completedFuture(route(request)); a router that blocks, for example one that looks up the user in a database, can runChatModelRouter.route(ChatModelRoutingRequest)on an executor of its choice.- Specified by:
routeAsyncin interfaceChatModelRouter- Parameters:
request- the chat request to route and the routes that can handle it. Nevernull.- Returns:
- a future of the result, see
ChatModelRouter.route(ChatModelRoutingRequest).
-
builder
-