🤖lauren-ai
← Home
Export this page

Semantic Router

Intent-based routing using embedding similarity.

SemanticRouter

Route incoming queries to named handlers by embedding similarity.

Usage:

python
router = SemanticRouter(
    routes=[
        Route("weather", "Weather questions", examples=["Will it rain?"]),
        Route("travel", "Travel planning", examples=["Plan a trip to Rome"]),
    ],
    embed_fn=llm_service.embed,
    min_confidence=0.6,
    fallback_route="travel",
)
await router.compile()   # precompute embeddings (called at startup)

match = await router.route("Is it going to snow?")
print(match.route_name)   # "weather"

Route

A named route with description and optional example utterances.

Usage:

python
Route(
    name="weather",
    description="Questions about weather, forecasts, temperature",
    examples=["Will it rain?", "What is the temperature in Paris?"],
)

Parameters:

NameTypeDescription
namestrUnique route identifier.
descriptionstrHuman-readable description of what this route handles.
exampleslist[str]Optional list of example utterances used to compute the route centroid embedding.

RouteMatch

Result of a SemanticRouter.route() call.

Parameters:

NameTypeDescription
route_namestrName of the matched route (also accessible as route for API compatibility).
confidencefloatCosine-similarity score of the best match.
methodLiteral['embedding', 'llm_fallback', 'default']How the match was determined: "embedding", "llm_fallback", or "default".