Class DecisionModelQueryRouter

java.lang.Object
dev.langchain4j.rag.query.router.DecisionModelQueryRouter
All Implemented Interfaces:
QueryRouter

@Experimental public class DecisionModelQueryRouter extends Object implements QueryRouter
A 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