LLMMessagesRouter
Route chat messages to different pipeline branches using a language model for classification.
Key Features
- Uses a language model to classify messages and route them to different outputs.
- Works with general-purpose LLMs and specialized moderation models such as Llama Guard.
- Routes messages based on configurable output patterns (regular expressions).
- Supports a customizable system prompt to adjust the model's classification behavior.
Configuration
- Drag the
LLMMessagesRoutercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the Chat Generator to the LLM you want to use for classification. This can be any Haystack chat generator.
- Set Output Names to the list of output connection names. These names are used to connect the router to other components.
- Set Output Patterns to the list of regular expressions to match against the LLM's output. Each pattern corresponds to one output name. Patterns are evaluated in order. For moderation models, refer to the model card for the expected output format.
- Go to the Advanced tab to optionally set a System Prompt to customize the model's classification behavior. For moderation models, refer to the model card for supported customization.
Connections
LLMMessagesRouter receives a list of ChatMessage objects (only user and assistant messages are supported). Based on the LLM's classification output, it routes messages to the first output whose pattern matches. Connect each named output to the appropriate downstream component.
Source Code
To check this component's source code, open llm_messages_router.py in the Haystack repository.
Usage Examples
Basic Configuration
LLMMessagesRouter:
type: components.routers.llm_messages_router.LLMMessagesRouter
init_parameters: {}
components:
LLMMessagesRouter:
type: components.routers.llm_messages_router.LLMMessagesRouter
init_parameters:
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
messages | List[ChatMessage] | A list of chat messages to route. Only user and assistant messages are supported. |
Outputs
| Parameter | Type | Description |
|---|---|---|
| (per output name) | List[ChatMessage] | Messages routed to the output whose pattern matches the LLM's classification response. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
chat_generator | ChatGenerator | A chat generator instance representing the LLM used for classification. | |
output_names | List[str] | A list of output connection names used to connect the router to other components. | |
output_patterns | List[str] | A list of regular expressions matched against the LLM's output. Each pattern corresponds to one output name. Patterns are evaluated in order. | |
system_prompt | Optional[str] | None | An optional system prompt to customize the behavior of the LLM. For moderation models, refer to the model card for supported customization options. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
messages | List[ChatMessage] | A list of chat messages to route. Only user and assistant messages are supported. |
Was this page helpful?