-
Notifications
You must be signed in to change notification settings - Fork 498
Add example of control flow composition with router agent and sequential executor #1550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thepatrickchin
wants to merge
7
commits into
NVIDIA:develop
Choose a base branch
from
thepatrickchin:control-flow-example
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,298
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f795134
feat: add example of control flow composition with router agent and s…
thepatrickchin e71a3f6
feat: add tests and documentation for mixed control flow example
thepatrickchin 8d14f82
fix: change config symlink to relative path
thepatrickchin 664e438
chore: remove version constraints from local dependencies
thepatrickchin 099473e
chore: switch to nemotron LLM
thepatrickchin 558c576
docs: update README with more detailed outputs following run command
thepatrickchin b62bef3
docs: update example complexity and add to examples listing
thepatrickchin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
300 changes: 300 additions & 0 deletions
300
examples/control_flow/mixture_of_control_flows/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # Mixture of Control Flows Example | ||
|
|
||
| **Complexity:** 🟢 Beginner | ||
|
|
||
| This example demonstrates how to compose [router agent](../router_agent/README.md) and [sequential executor](../sequential_executor/README.md) control flow patterns in the NeMo Agent Toolkit. | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Graph Structure](#graph-structure) | ||
| - [Configuration](#configuration) | ||
| - [Example Configuration](#example-configuration) | ||
| - [Installation and Setup](#installation-and-setup) | ||
| - [Install this Workflow](#install-this-workflow) | ||
| - [Set Up API Keys](#set-up-api-keys) | ||
| - [Run the Workflows](#run-the-workflows) | ||
| - [Router Agent to Sequential Executor](#router-agent-to-sequential-executor) | ||
| - [Router Agent to Sequential Executor with Router Agent](#router-agent-to-sequential-executor-with-router-agent) | ||
| - [Router Agent to Router Agent](#router-agent-to-router-agent) | ||
|
|
||
| ## Graph Structure | ||
|
|
||
| The following diagram illustrates an example workflow demonstrating three distinct patterns: routing to a sequential executor, routing to a sequential executor with an embedded router agent, and routing to a nested router agent for specialized tasks: | ||
|
|
||
| <div align="center"> | ||
| <img src="../../../docs/source/_static/mixture_of_control_flows.png" alt="Mixture of Control Flows Graph Structure" width="750" style="max-width: 100%; height: auto;"> | ||
| </div> | ||
|
|
||
| ## Configuration | ||
|
|
||
| The mixture of control flows is configured through the `config.yml` file. This example demonstrates how to combine multiple control flow components in a single workflow by reusing existing functions from other examples. | ||
|
|
||
| ### Example Configuration | ||
|
|
||
| ```yaml | ||
| llms: | ||
| nim_llm: | ||
| _type: nim | ||
| model_name: nvidia/nemotron-3-nano-30b-a3b | ||
| temperature: 0.0 | ||
| max_tokens: 4096 | ||
|
|
||
| functions: | ||
| mock_input_validator: | ||
| _type: mock_input_validator | ||
| mock_uppercase_converter: | ||
| _type: mock_uppercase_converter | ||
| mock_lowercase_converter: | ||
| _type: mock_lowercase_converter | ||
| text_processor: | ||
| _type: text_processor | ||
| data_analyzer: | ||
| _type: data_analyzer | ||
| report_generator: | ||
| _type: report_generator | ||
| mock_result_formatter: | ||
| _type: mock_result_formatter | ||
| fruit_advisor: | ||
| _type: mock_fruit_advisor | ||
| city_advisor: | ||
| _type: mock_city_advisor | ||
|
|
||
| # Router Agent -> Sequential Executor | ||
| text_analysis_pipeline: | ||
| _type: sequential_executor | ||
| tool_list: [text_processor, data_analyzer, report_generator] | ||
| raise_type_incompatibility: false | ||
| description: "Processes text, analyzes it, and generates a report" | ||
|
|
||
| # Router Agent -> Sequential Executor -> Router Agent | ||
| input_formatter: | ||
| _type: router_agent | ||
| branches: [mock_uppercase_converter, mock_lowercase_converter] | ||
| llm_name: nim_llm | ||
|
|
||
| text_formatting_pipeline: | ||
| _type: sequential_executor | ||
| tool_list: [mock_input_validator, input_formatter, mock_result_formatter] | ||
| raise_type_incompatibility: false | ||
| description: "Formats text by converting to uppercase or lowercase" | ||
|
|
||
| # Router Agent -> Router Agent | ||
| general_advisor: | ||
| _type: router_agent | ||
| branches: [fruit_advisor, city_advisor] | ||
| llm_name: nim_llm | ||
| description: "Provides advice about fruits or cities" | ||
|
|
||
| workflow: | ||
| _type: router_agent | ||
| branches: [text_analysis_pipeline, text_formatting_pipeline, general_advisor] | ||
| llm_name: nim_llm | ||
| detailed_logs: true | ||
| ``` | ||
|
|
||
| ## Installation and Setup | ||
|
|
||
| If you have not already done so, follow the instructions in the [Install Guide](../../../docs/source/get-started/installation.md#install-from-source) to create the development environment and install NeMo Agent Toolkit. | ||
|
|
||
| ### Install this Workflow | ||
|
|
||
| From the root directory of the NeMo Agent Toolkit library, run the following command: | ||
|
|
||
| ```bash | ||
| uv pip install -e examples/control_flow/mixture_of_control_flows | ||
| ``` | ||
|
|
||
| ### Set Up API Keys | ||
| If you have not already done so, follow the [Obtaining API Keys](../../../docs/source/get-started/quick-start.md#obtaining-api-keys) instructions to obtain an NVIDIA API key. You need to set your NVIDIA API key as an environment variable to access NVIDIA AI services: | ||
|
|
||
| ```bash | ||
| export NVIDIA_API_KEY=<YOUR_API_KEY> | ||
| ``` | ||
|
|
||
| ## Run the Workflows | ||
|
|
||
| This example demonstrates the power of combining different control flow patterns in the NeMo Agent Toolkit. The workflow routes different types of requests to appropriate handlers, where the main router agent intelligently determines whether to execute a text analysis pipeline, a text formatting pipeline with embedded routing, or route to a nested router agent for specialized recommendations based on the request content. | ||
|
|
||
| Run the following commands from the root of the NeMo Agent Toolkit repository to execute this workflow with different inputs: | ||
|
|
||
| ### Router Agent to Sequential Executor | ||
|
|
||
| Test the text analysis sequential pipeline, demonstrating flows from a router agent to a sequential executor: | ||
|
|
||
| ```bash | ||
| nat run --config_file=examples/control_flow/mixture_of_control_flows/configs/config.yml --input "Process this text: The NeMo Agent Toolkit provides powerful control flow capabilities for building sophisticated AI workflows" | ||
| ``` | ||
|
|
||
| **Expected Workflow Output:** | ||
|
|
||
| ```console | ||
| <snipped for brevity> | ||
| Configuration Summary: | ||
| -------------------- | ||
| Workflow Type: router_agent | ||
| Number of Functions: 13 | ||
| Number of Function Groups: 0 | ||
| Number of LLMs: 1 | ||
| Number of Embedders: 0 | ||
| Number of Memory: 0 | ||
| Number of Object Stores: 0 | ||
| Number of Retrievers: 0 | ||
| Number of TTC Strategies: 0 | ||
| Number of Authentication Providers: 0 | ||
|
|
||
| 2026-02-04 15:34:34 - INFO - nat.runtime.session:298 - Shared workflow built (entry_function=None) | ||
| 2026-02-04 15:34:45 - INFO - nat.front_ends.console.console_front_end_plugin:104 - | ||
| -------------------------------------------------- | ||
| Workflow Result: | ||
| ['=== TEXT ANALYSIS REPORT ===\n\nText Statistics:\n - Word Count: 17\n - Sentence Count: 0\n - Average Words per Sentence: 0\n - Text Complexity: Simple\n\nTop Words:\n 1. process\n 2. this\n 3. text\n 4. nemo\n 5. agent\n\nText Preview:\n Process this text The NeMo Agent Toolkit provides powerful control flow capabilities for building so...\n\nReport generated successfully.\n=========================='] | ||
| -------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Router Agent to Sequential Executor with Router Agent | ||
|
|
||
| Test the text formatting pipeline. In addition to flows from a router agent to a sequential executor, these examples demonstrate flows from a sequential executor to a nested router agent: | ||
|
|
||
| **Example 1: Uppercase conversion** | ||
|
|
||
| ```bash | ||
| nat run --config_file=examples/control_flow/mixture_of_control_flows/configs/config.yml --input "Convert this text to uppercase" | ||
| ``` | ||
|
|
||
| **Expected Workflow Output:** | ||
|
|
||
| ```console | ||
| <snipped for brevity> | ||
| Configuration Summary: | ||
| -------------------- | ||
| Workflow Type: router_agent | ||
| Number of Functions: 13 | ||
| Number of Function Groups: 0 | ||
| Number of LLMs: 1 | ||
| Number of Embedders: 0 | ||
| Number of Memory: 0 | ||
| Number of Object Stores: 0 | ||
| Number of Retrievers: 0 | ||
| Number of TTC Strategies: 0 | ||
| Number of Authentication Providers: 0 | ||
|
|
||
| 2026-02-04 15:37:31 - INFO - nat.runtime.session:298 - Shared workflow built (entry_function=None) | ||
| 2026-02-04 15:37:33 - INFO - nat.front_ends.console.console_front_end_plugin:104 - | ||
| -------------------------------------------------- | ||
| Workflow Result: | ||
| ['=== PROCESSED RESULT ===\n[VALIDATED] CONVERT THIS TEXT TO UPPERCASE\n========================'] | ||
| -------------------------------------------------- | ||
| ``` | ||
|
|
||
| **Example 2: lowercase conversion** | ||
|
|
||
| ```bash | ||
| nat run --config_file=examples/control_flow/mixture_of_control_flows/configs/config.yml --input "CONVERT THIS TEXT TO LOWERCASE" | ||
| ``` | ||
|
|
||
| **Expected Workflow Output:** | ||
|
|
||
| ```console | ||
| <snipped for brevity> | ||
| Configuration Summary: | ||
| -------------------- | ||
| Workflow Type: router_agent | ||
| Number of Functions: 13 | ||
| Number of Function Groups: 0 | ||
| Number of LLMs: 1 | ||
| Number of Embedders: 0 | ||
| Number of Memory: 0 | ||
| Number of Object Stores: 0 | ||
| Number of Retrievers: 0 | ||
| Number of TTC Strategies: 0 | ||
| Number of Authentication Providers: 0 | ||
|
|
||
| 2026-02-04 15:38:24 - INFO - nat.runtime.session:298 - Shared workflow built (entry_function=None) | ||
| 2026-02-04 15:38:27 - INFO - nat.front_ends.console.console_front_end_plugin:104 - | ||
| -------------------------------------------------- | ||
| Workflow Result: | ||
| ['=== PROCESSED RESULT ===\n[validated] convert this text to lowercase\n========================'] | ||
| -------------------------------------------------- | ||
| ``` | ||
|
|
||
| ### Router Agent to Router Agent | ||
|
|
||
| Test the nested router pattern where the main router delegates to a domain-specific sub-router for specialized advisory tasks: | ||
|
|
||
| **Example 1: Fruit advisor:** | ||
|
|
||
| ```bash | ||
| nat run --config_file=examples/control_flow/mixture_of_control_flows/configs/config.yml --input "What yellow fruit would you recommend?" | ||
| ``` | ||
|
|
||
| **Expected Workflow Output:** | ||
|
|
||
| ```console | ||
| <snipped for brevity> | ||
| Configuration Summary: | ||
| -------------------- | ||
| Workflow Type: router_agent | ||
| Number of Functions: 13 | ||
| Number of Function Groups: 0 | ||
| Number of LLMs: 1 | ||
| Number of Embedders: 0 | ||
| Number of Memory: 0 | ||
| Number of Object Stores: 0 | ||
| Number of Retrievers: 0 | ||
| Number of TTC Strategies: 0 | ||
| Number of Authentication Providers: 0 | ||
|
|
||
| 2026-02-04 15:39:33 - INFO - nat.runtime.session:298 - Shared workflow built (entry_function=None) | ||
| 2026-02-04 15:39:35 - INFO - nat.front_ends.console.console_front_end_plugin:104 - | ||
| -------------------------------------------------- | ||
| Workflow Result: | ||
| ['banana'] | ||
| -------------------------------------------------- | ||
| ``` | ||
|
|
||
| **Example 2: City advisor:** | ||
|
|
||
| ```bash | ||
| nat run --config_file=examples/control_flow/mixture_of_control_flows/configs/config.yml --input "What city should I visit in Canada?" | ||
| ``` | ||
|
|
||
| **Expected Workflow Output:** | ||
|
|
||
| ```console | ||
| <snipped for brevity> | ||
| Configuration Summary: | ||
| -------------------- | ||
| Workflow Type: router_agent | ||
| Number of Functions: 13 | ||
| Number of Function Groups: 0 | ||
| Number of LLMs: 1 | ||
| Number of Embedders: 0 | ||
| Number of Memory: 0 | ||
| Number of Object Stores: 0 | ||
| Number of Retrievers: 0 | ||
| Number of TTC Strategies: 0 | ||
| Number of Authentication Providers: 0 | ||
|
|
||
| 2026-02-04 15:40:08 - INFO - nat.runtime.session:298 - Shared workflow built (entry_function=None) | ||
| 2026-02-04 15:40:11 - INFO - nat.front_ends.console.console_front_end_plugin:104 - | ||
| -------------------------------------------------- | ||
| Workflow Result: | ||
| ['Toronto'] | ||
| -------------------------------------------------- | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| src/mixture_of_control_flows/configs |
48 changes: 48 additions & 0 deletions
48
examples/control_flow/mixture_of_control_flows/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| [build-system] | ||
| build-backend = "setuptools.build_meta" | ||
| requires = ["setuptools>=64", "setuptools-scm>=8", "setuptools_dynamic_dependencies>=1.0.0"] | ||
|
|
||
| [tool.setuptools_scm] | ||
| git_describe_command = "git describe --long --first-parent" | ||
| root = "../../.." | ||
|
|
||
| [tool.setuptools.packages.find] | ||
| where = ["src"] | ||
|
|
||
| [project] | ||
| name = "nat_mixture_of_control_flows" | ||
| dynamic = ["version", "dependencies"] | ||
| requires-python = ">=3.11,<3.14" | ||
| description = "Mixture of Control Flows example for NeMo Agent Toolkit" | ||
| classifiers = ["Programming Language :: Python"] | ||
|
|
||
| [tool.setuptools_dynamic_dependencies] | ||
| dependencies = [ | ||
| "nvidia-nat[langchain,test] == {version}", | ||
| "nat_sequential_executor", | ||
| "nat_router_agent", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| nvidia-nat = { path = "../../..", editable = true } | ||
| nat_sequential_executor = { path = "../sequential_executor", editable = true } | ||
| nat_router_agent = { path = "../router_agent", editable = true } | ||
|
|
||
| [project.entry-points.'nat.components'] | ||
| nat_mixture_of_control_flows = "mixture_of_control_flows.register" | ||
|
|
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One other request... if you update develop, please add
nat_mixture_of_control_flowsto the examples listing and reference it accordingly (and rerunuv lockin the root directory).We are trying to do better at tracking examples and their dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@willkill07 sure, updated the examples listing. I reran
uv lockbut there was no change