brainLM-Based Router

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

The LM-Based Router uses a language model to intelligently determine the appropriate route for a given input. This approach leverages the reasoning capabilities of LMs for flexible, context-aware routing decisions.

chevron-rightPrerequisiteshashtag

This tutorial requires familiarity with these concepts:

  1. Language Model (LM) Invoker - For understanding language model invocation

  2. LM Request Processor (LMRP) - For configuring LM request processing

Installation

pip install gllm-pipeline gllm-inference

Basic Usage

Step 1: Set up the LM Request Processor

import asyncio
from gllm_inference.request_processor import build_lm_request_processor
from gllm_pipeline.router import LMBasedRouter

# Create an LM request processor
lm_processor = build_lm_request_processor(
    lm_invoker_kwargs={
        "model_id": "openai/gpt-5-nano",
        "credentials": "<YOUR_OPENAI_API_KEY>"
    },
    prompt_builder_kwargs={
        "system_template": "You are a customer support routing assistant.",
        "user_template": "Route this query to the appropriate department: {source}"
    }
)

Step 2: Create the router

Step 3: Route queries

Advanced Configuration

Custom Output Parsing

Configure how the LM output is parsed:

Multi-Step Routing

Use the LM for more complex routing logic:

Complete Example

Configuration Options

LM Model Selection

Choose different models for different routing complexity:

Route Filtering

Restrict available routes at runtime:

Best Practices

  1. Clear Instructions: Provide explicit routing instructions in the system prompt

  2. Route Descriptions: Describe each route clearly so the LM understands the distinctions

  3. JSON Responses: Use structured JSON output for reliable parsing

  4. Fallback Handling: Set a sensible default route for edge cases

  5. Testing: Test with diverse queries to ensure consistent routing

  6. Cost Monitoring: LM-based routing incurs API costs; monitor usage

Troubleshooting

Router selecting wrong routes?

  • Improve the system prompt with clearer route descriptions

  • Add examples of queries for each route

  • Use a more capable model (e.g., GPT-4 instead of GPT-3.5)

Parsing errors?

  • Ensure LM output format matches lm_output_key

  • Add explicit format instructions to the prompt

  • Use structured output formats (JSON)

High latency?

  • Use faster models (GPT-3.5-turbo)

  • Cache LM responses if routing patterns are repetitive

  • Consider hybrid approaches (rules + LM)

See Also

Last updated

Was this helpful?