list-checkRule-Based Router

gllm-pipelinearrow-up-right | Tutorial: Rule-Based Router | API Referencearrow-up-right

The Rule-Based Router routes queries based on keyword matching and pattern rules. It's ideal for deterministic routing logic where you want explicit control over routing decisions.

chevron-rightPrerequisiteshashtag

This tutorial does not require external dependencies. It uses only the core routing framework.

Installation

pip install gllm-pipeline

Basic Usage

Step 1: Define routing rules

import asyncio
from gllm_pipeline.router import RuleBasedRouter
from gllm_pipeline.router.rule_based_router import RouterRule, RouterRuleset

# Define rules for each route
billing_rules = RouterRuleset(
    rules=[
        RouterRule(
            keywords=["payment", "invoice", "billing", "charge", "refund"],
            allow_substring=True,
            case_sensitive=False,
        ),
    ],
    match_all=False,  # Match any rule
)

tech_support_rules = RouterRuleset(
    rules=[
        RouterRule(
            keywords=["crash", "error", "bug", "broken", "not working"],
            allow_substring=True,
            case_sensitive=False,
        ),
    ],
    match_all=False,
)

faq_rules = RouterRuleset(
    rules=[
        RouterRule(
            keywords=["hours", "location", "contact", "help", "guide"],
            allow_substring=True,
            case_sensitive=False,
        ),
    ],
    match_all=False,
)

# Map routes to rulesets
ruleset_map = {
    "billing": billing_rules,
    "tech_support": tech_support_rules,
    "faq": faq_rules,
}

Step 2: Create the router

Step 3: Route queries

Advanced Rule Configuration

Exact Keyword Matching

Match complete words only:

Case-Sensitive Matching

Input Splitting

Split input before matching:

Multiple Rules (AND/OR Logic)

Complete Example

Configuration Options

Alphanumeric Filtering

Only consider alphanumeric characters:

Complex Splitting Patterns

Route Filtering

Best Practices

  1. Keyword Selection: Choose keywords that are specific to each route

  2. Avoid Overlap: Minimize keyword overlap between routes

  3. Test Coverage: Test with diverse queries including edge cases

  4. Fallback Route: Set a sensible default for unmatched queries

  5. Maintenance: Document rules and update as new patterns emerge

  6. Performance: Rule-based routing is very fast; use for high-throughput scenarios

Troubleshooting

Routes not matching?

  • Add more keywords to the rule

  • Enable substring matching if needed

  • Check case sensitivity settings

  • Verify split rules are working correctly

Too many false positives?

  • Use stricter matching (exact words, case-sensitive)

  • Reduce keyword overlap

  • Use AND logic (match_all=True) for multiple conditions

Ambiguous queries matching multiple routes?

  • Define rule priority (order matters)

  • Use more specific keywords

  • Add additional rules to disambiguate

See Also

Last updated

Was this helpful?