Skip to content

Router

Defines a base class for routers used in Gen AI applications.

Authors

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

References

NONE

BaseRouter(default_route, valid_routes)

Bases: Component, ABC

An abstract base class for the routers used in Gen AI applications.

This class provides a foundation for building routers in Gen AI applications. It includes initialization for default and valid routes and abstract routing logic that subclasses must implement.

Attributes:

Name Type Description
default_route str

The default route to use.

valid_routes set[str]

A set of valid routes for the router.

Initializes a new instance of the BaseRouter class.

Parameters:

Name Type Description Default
default_route str

The default route to use.

required
valid_routes set[str]

A set of valid routes for the router.

required

Raises:

Type Description
ValueError

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

route(source, route_filter=None) async

Routes an input source to the appropriate path.

This method is a wrapper around the _select_route method. It first calls _select_route to get the selected route and then checks if the route is in the set of valid routes. If it is not, it returns the default route.

Parameters:

Name Type Description Default
source str | bytes

The input source to be routed.

required
route_filter set[str] | None

An optional set of allowed routes. If provided, only the routes in the set are considered valid. Defaults to None.

None

Returns:

Name Type Description
str str

The selected route for the input source.

Raises:

Type Description
ValueError

If the provided route filter contains invalid routes or if the default route is not included in the route filter.