It is recommended to study this section together with the tutorial Branch Structure.
Function
In complex RAG reasoning tasks, it is often necessary to dynamically decide the subsequent execution path based on intermediate results (such as the model’s current generated content or retrieval results). The Router Server is a key component designed for this purpose — it judges the current state based on input information and returns a custom branch label (state identifier) to drive branch jumps and dynamic control in the Pipeline.Implementation Example
The following example shows how to implement a Router Tool. Suppose in the current RAG process, the model needs to judge whether the currently retrieved documents contain enough information to answer the question: if the information is sufficient, end the process, otherwise continue retrieval. You can implement a Router Tool like this:servers/router/src/router.py
continue: Insufficient information, need to continue retrieval;stop: Information is sufficient, terminate the process.
Usage Example
The definedRouter Tool needs to be used in conjunction with branch structures branch: and router: to jointly realize dynamic jumps based on state labels.
router.check_model_state judges that the model output contains the <search> identifier, it enters the continue branch to continue retrieval;
otherwise, it enters the stop branch to directly end the loop.