Interface ChatModelRouter
- All Known Implementing Classes:
DecisionModelChatModelRouter
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Decides which route of a
RoutingChatModel or RoutingStreamingChatModel handles a chat request.
A router can be a simple rule:
ChatModelRouter router = request -> ChatModelRoutingResult.route(
request.chatRequest().messages().size() > 20 ? "complex" : "simple");
or use a model, such as DecisionModelChatModelRouter.- Since:
- 1.21.0
-
Method Summary
Modifier and TypeMethodDescriptionroute(ChatModelRoutingRequest request) Selects the route that handles the request.default CompletableFuture<ChatModelRoutingResult> routeAsync(ChatModelRoutingRequest request) Non-blocking counterpart ofroute(ChatModelRoutingRequest), used by the asynchronous and streaming methods of the routing chat models.default voidvalidate(List<ChatModelRoute> routes) Checks that this router can route between the given routes, for example that every route has a description.
-
Method Details
-
route
Selects the route that handles the request.- 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
Non-blocking counterpart ofroute(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 runroute(ChatModelRoutingRequest)on an executor of its choice.- Parameters:
request- the chat request to route and the routes that can handle it. Nevernull.- Returns:
- a future of the result, see
route(ChatModelRoutingRequest).
-
validate
Checks 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.- Parameters:
routes- all routes, in the order in which they were configured.- Throws:
IllegalArgumentException- if the router cannot route between these routes.
-