Class DecisionModelQueryRouter
- All Implemented Interfaces:
QueryRouter
QueryRouter that uses a DecisionModel to decide which ContentRetrievers can help answer
the query.
For each retriever, the decision model answers a yes/no question about whether the retriever, described by its
description, could help answer the query (see DEFAULT_QUESTION_TEMPLATE), all in a single call. The query is
routed to every retriever whose probability of "yes" reaches the minimum probability (0.5 by default). When no
retriever qualifies, no retrieval is performed, which is useful for queries that do not need retrieval at all
(e.g. "Hi!"). The decision model receives the query ({"query": ...}) and, when the query comes from a
conversation, the previous messages ("messages", see DecisionModelQueryRouter.Builder.maxMessages(Integer)), so that
follow-up questions such as "and for contractors?" are understood.
QueryRouter queryRouter = DecisionModelQueryRouter.builder()
.decisionModel(decisionModel)
.retrieverToDescription(Map.of(
hrRetriever, "HR policies: leave, benefits, expenses",
wikiRetriever, "Engineering wiki: services, deployments, on-call"))
.build();
If the decision model fails, the DecisionModelQueryRouter.FallbackStrategy applies: by default, no content is retrieved and a warning
is logged.- 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 PromptTemplateThe default template of the question asked for each retriever:"Could the following data source contain information that helps answer the query?\n{{description}}". -
Constructor Summary
ConstructorsModifierConstructorDescriptionDecisionModelQueryRouter(DecisionModel decisionModel, Map<ContentRetriever, String> retrieverToDescription) protected -
Method Summary
Modifier and TypeMethodDescriptionbuilder()protected Collection<ContentRetriever> Routes the givenQueryto one or multipleContentRetrievers.routeAsync(Query query) Non-blocking counterpart ofQueryRouter.route(Query), invoked by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service modes when RAG is configured.
-
Field Details
-
DEFAULT_QUESTION_TEMPLATE
The default template of the question asked for each retriever:"Could the following data source contain information that helps answer the query?\n{{description}}".
-
-
Constructor Details
-
DecisionModelQueryRouter
public DecisionModelQueryRouter(DecisionModel decisionModel, Map<ContentRetriever, String> retrieverToDescription) -
DecisionModelQueryRouter
-
-
Method Details
-
route
Description copied from interface:QueryRouterRoutes the givenQueryto one or multipleContentRetrievers.- Specified by:
routein interfaceQueryRouter- Parameters:
query- TheQueryto be routed.- Returns:
- A collection of one or more
ContentRetrievers to which theQueryshould be routed.
-
routeAsync
Description copied from interface:QueryRouterNon-blocking counterpart ofQueryRouter.route(Query), invoked by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service modes when RAG is configured.The default implementation returns a failed future carrying
AsyncNotSupportedException: a router backed by a blocking LLM call (e.g.LanguageModelQueryRouter) must opt in by overriding this method to stay off the calling thread. A router that cannot be made non-blocking is still usable from these modes viaDefaultRetrievalAugmentor, which offloads the blockingQueryRouter.route(Query)to its executor.- Specified by:
routeAsyncin interfaceQueryRouter- Parameters:
query- TheQueryto be routed.- Returns:
- A
CompletableFutureof one or moreContentRetrievers to route theQueryto.
-
fallback
-
builder
-