Skip to content

Lm based router

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

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.

The router delegates routing logic to a pluggable backend adapter (e.g., NativeLMBasedAdapter).

Attributes:

Name Type Description
backend BaseAdapter

The backend implementation handling routing logic.

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_kwargs=None, **kwargs) classmethod

Initialize the LM based router component using preset model configurations.

Examples:

router = LMBasedRouter.from_preset(modality="image")
router = LMBasedRouter.from_preset(
    modality="image",
    preset_kwargs={"default_route": "custom_route", "valid_routes": {"custom_route", "other"}}
)

Parameters:

Name Type Description Default
modality str

Type of modality input (e.g., "image").

required
preset_kwargs LMBasedRouterPresetKwargs | dict[str, Any] | None

Additional kwargs for the preset. Can be provided as a dict or as an LMBasedRouterPresetKwargs instance. When provided as a dict, unknown keys will raise a ValidationError. Defaults to None.

The following keys are allowed: 1. lm_invoker_kwargs: Configuration for the language model invoker. 2. prompt_builder_kwargs: Configuration for the prompt builder. 3. lm_output_key: The key in the language model's output that contains the route. 4. default_route: Default route to use if LM cannot determine route. 5. valid_routes: Set of valid routes for the router.

None
**kwargs Any

Additional arguments to pass for this class.

{}

Returns:

Name Type Description
LMBasedRouter LMBasedRouter

Initialized LM-based router component using preset model.

native(lm_request_processor, default_route, valid_routes, lm_output_key=DEFAULT_LM_OUTPUT_KEY) classmethod

Create LM-based router with native backend.

This is a convenience classmethod that creates an LMBasedRouter with a NativeLMBasedAdapter.

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

Returns:

Name Type Description
LMBasedRouter LMBasedRouter

Initialized router with native backend.

LMBasedRouterPresetKwargs

Bases: BaseModel

Schema for LMBasedRouter.from_preset preset_kwargs.

This model validates and documents the allowed parameters for the preset configuration.

Attributes:

Name Type Description
lm_invoker_kwargs dict[str, Any] | None

Configuration for the language model invoker.

prompt_builder_kwargs dict[str, Any] | None

Configuration for the prompt builder.

lm_output_key str

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

default_route str

Default route to use if LM cannot determine route.

valid_routes set[str]

Set of valid routes for the router.