TransformersTextRouter
Route text strings to different connections based on a category label predicted by a Hugging Face text classification model. Use this component to branch pipelines by language, intent, or other categories defined by the model.
Key Features
- Routes text to dynamically named outputs based on the predicted label.
- Uses any Hugging Face text classification model.
- Fetches label names from the model configuration when
labelsis not provided. - Supports configurable device placement and Hugging Face pipeline kwargs.
Configuration
- Drag the
TransformersTextRoutercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the
modelto a Hugging Face text classification model (for example,papluca/xlm-roberta-base-language-detection). - Optionally set
labelsto override the labels fetched from the model configuration.
- Set the
- Go to the Advanced tab to configure
device,token, andhuggingface_pipeline_kwargs.
The labels available as outputs depend on the model you choose. Check the model card on Hugging Face for the supported categories.
Connections
TransformersTextRouter accepts a text string as input. It outputs the text to a dynamically created output named after the predicted label. Connect each labeled output to the appropriate downstream component.
Source Code
To check this component's source code, open text_router.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
TransformersTextRouter:
type: haystack_integrations.components.routers.transformers.text_router.TransformersTextRouter
init_parameters:
model: papluca/xlm-roberta-base-language-detection
Using the Component in a Pipeline
# haystack-pipeline
components:
TransformersTextRouter:
type: haystack_integrations.components.routers.transformers.text_router.TransformersTextRouter
init_parameters:
model: papluca/xlm-roberta-base-language-detection
english_prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Answer the question: {{query}}\nAnswer:"
german_prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Beantworte die Frage: {{query}}\nAntwort:"
connections:
- sender: TransformersTextRouter.en
receiver: english_prompt_builder.query
- sender: TransformersTextRouter.de
receiver: german_prompt_builder.query
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
text | str | A string of text to route. |
Outputs
| Parameter | Type | Description |
|---|---|---|
| Dynamic label outputs | str | One output per model label. The output name matches the predicted label and carries the input text. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | str | The name or path of a Hugging Face model for text classification. | |
labels | Optional[List[str]] | None | The list of labels. If not provided, the component fetches labels from the model configuration on Hugging Face. |
device | Optional[ComponentDevice] | None | The device for loading the model. If None, automatically selects the default device. |
token | Optional[Secret] | Secret.from_env_var(["HF_API_TOKEN", "HF_TOKEN"], strict=False) | The API token used to download private models from Hugging Face. |
huggingface_pipeline_kwargs | Optional[Dict[str, Any]] | None | Keyword arguments for initializing the Hugging Face text classification pipeline. |
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 |
|---|---|---|---|
text | str | A string of text to route. |
Related Information
Was this page helpful?