Skip to content

Rule based router

Defines a router that routes the input text to the appropriate path based on a set of rules.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id) Obryan Ramadhan (obryan.ramadhan@gdplabs.id)

References

NONE

RouterRule

Bases: BaseModel

Configuration class for defining keyword matching rules in rule-based router operations.

Attributes:

Name Type Description
keywords list[str]

A list of keywords to match against the input string.

allow_substring bool

If True, allows matching if a keyword is a substring of the input. If False, requires an exact match between a keyword and either the full input or any of its split parts. Defaults to True.

case_sensitive bool

If True, keyword matching will be case-sensitive. Defaults to True.

alphanumeric_only bool

If True, only alphanumeric and whitespace characters will be considered during matching. Defaults to True.

split_rule list[RouterSplitRule] | None

A list of RouterSplitRule objects that define how to split the input string before matching. If multiple are provided, the splits will be done sequentially. If None, no splitting will be applied. Defaults to None.

RouterRuleset

Bases: BaseModel

Configuration class for defining a set of rules in rule-based router operations.

Attributes:

Name Type Description
rules list[RouterRule]

A list of RouterRule objects that define individual routing rules.

match_all bool

If True, all rules must be matched for the ruleset to return True. If False, matching any rule will result in the ruleset returning True. Defaults to True.

RouterSplitRule

Bases: BaseModel

Configuration class for defining input string splitting rules in rule-based router operations.

Attributes:

Name Type Description
splitter list[str]

A list of string delimiters used to split the input string. Defaults to [" "].

beg_index int | None

Optional beginning index of the portion of the split result to keep. If None, the split result will be kept from the first element. Defaults to None.

end_index int | None

Optional ending index of the portion of the split result to keep. If None, the split result will be kept until the last element. Defaults to None.

RuleBasedRouter(ruleset_map, default_route, valid_routes)

Bases: BaseRouter

A rule-based router that directs the input text to an appropriate route based on a set of rules.

The RuleBasedRouter routes incoming input text to different paths by evaluating a set of rules encapsulated in RouterRuleset objects. Each ruleset consists of multiple RouterRule objects, and the router determines which route to take based on the input text and the rules defined in the ruleset.

If match_all is True in a RouterRuleset, all rules in that ruleset must match the input text for the associated route to be selected. If False, matching any rule in the ruleset will cause that route to be selected. If no match is found, the router defaults to the default_route.

Attributes:

Name Type Description
ruleset_map dict[str, RouterRuleset]

A mapping of route names to their corresponding rulesets.

default_route str

The default route to be taken if no ruleset matches the input text.

valid_routes set[str]

A set of valid routes that the router can direct to.

Initializes a new instance of the RuleBasedRouter class.

Parameters:

Name Type Description Default
ruleset_map dict[str, RouterRuleset]

A mapping of route names to their corresponding rulesets.

required
default_route str

The default route to be taken if no ruleset matches the input text.

required
valid_routes set[str]

A set of valid routes for the router.

required

Raises:

Type Description
ValueError
  1. If the ruleset_map contains routes that are not in the set of valid routes.
  2. If the provided default route is not in the set of valid routes.