Skip to content

Lm based router

Defines a router that utilizes a language model to route the input source to the appropriate path.

Authors

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

References

NONE

LMBasedRouter(lm_request_processor, default_route, valid_routes, lm_output_key=DEFAULT_LM_OUTPUT_KEY)

Bases: BaseRouter, UsesLM

A router that utilizes a language model to determine the appropriate route for an input source.

This class routes a given input source to an appropriate path based on the output of a language model. If the determined route is not valid, it defaults to a predefined route.

Attributes:

Name Type Description
lm_request_processor LMRequestProcessor

The request processor that handles requests to the language model.

default_route str

The default route to be used if the language model's output is invalid.

valid_routes set[str]

A set of valid routes for the router.

lm_output_key str

The key in the language model's output that contains the route.

Notes

The lm_request_processor must be configured to: 1. Take a "source" key as input. The input source of the router should be passed as the value of this "source" key. 2. Return a JSON object which contains the selected route as a string. The key of the route is specified by the lm_output_key attribute. Furthermore, the selected route must be present in the valid_routes set.

Output example, assuming the lm_output_key is "route": { "route": "" }

Initializes a new instance of the LMBasedRouter class.

Parameters:

Name Type Description Default
lm_request_processor LMRequestProcessor

The request processor that handles requests to the language model.

required
default_route str

The default route to be used if the language model's output is invalid.

required
valid_routes set[str]

A set of valid routes for the router.

required
lm_output_key str

The key in the language model's output that contains the route. Defaults to DEFAULT_LM_OUTPUT_KEY.

DEFAULT_LM_OUTPUT_KEY

Raises:

Type Description
ValueError

If the provided default route is not in the set of valid routes.

from_preset(modality, preset_name, preset_kwargs=None, **kwargs) classmethod

Initialize the LM based router component using preset model configurations.

Parameters:

Name Type Description Default
modality str

type of modality input.

required
preset_name str

Name of the preset to use.

required
preset_kwargs dict | None

placeholder for preset additional arguments.

None
**kwargs Any

Additional arguments to pass for this class.

{}

Returns:

Name Type Description
LMBasedRouter LMBasedRouter

Initialized lm based router component using preset model.