Runners¶
There are two ways to manage the agents in RAI:
AgentRunner
- a class for starting and stopping the agentswait_for_shutdown
- a function for waiting for interruption signals
Usage of Runners
is optional
You can start and stop the agents manually for more control over the agents lifecycle.
AgentRunner class definition¶
rai.agents.runner.AgentRunner
¶
Runs the agents in the background.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agents
|
List[BaseAgent]
|
List of agent instances |
required |
Source code in src/rai_core/rai/agents/runner.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
__init__(agents)
¶
Initialize the AgentRunner with a list of agents.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agents
|
List[BaseAgent]
|
List of agent instances to be managed by the runner. |
required |
Source code in src/rai_core/rai/agents/runner.py
79 80 81 82 83 84 85 86 87 88 |
|
run()
¶
Run all agents in the background.
Notes
This method starts all agents by calling their run
method.
It is experimental; if agents do not run properly, consider running them in separate processes.
Source code in src/rai_core/rai/agents/runner.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
run_and_wait_for_shutdown()
¶
Run all agents and block until a shutdown signal is received.
Notes
This method starts all agents and waits for a shutdown signal (SIGINT or SIGTERM), ensuring graceful termination.
Source code in src/rai_core/rai/agents/runner.py
106 107 108 109 110 111 112 113 114 |
|
stop()
¶
Stop all managed agents by calling their stop
method.
Source code in src/rai_core/rai/agents/runner.py
137 138 139 140 |
|
wait_for_shutdown()
¶
Block until a shutdown signal (SIGINT or SIGTERM) is received, ensuring graceful termination.
Notes
Installs signal handlers to capture SIGINT and SIGTERM. On receiving a signal, stops all managed agents.
Source code in src/rai_core/rai/agents/runner.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
wait_for_shutdown function definition¶
rai.agents.runner.wait_for_shutdown(agents)
¶
Block until a shutdown signal (SIGINT or SIGTERM) is received, ensuring graceful termination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agents
|
List[BaseAgent]
|
List of running agents to be stopped on shutdown. |
required |
Notes
This method ensures a graceful shutdown of both the agent and the ROS2 node upon receiving
an interrupt signal (SIGINT, e.g., Ctrl+C) or SIGTERM. It installs signal handlers to
capture these events and invokes the agent's stop()
method as part of the shutdown process.
Source code in src/rai_core/rai/agents/runner.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
Usage examples¶
AgentRunner¶
from rai.agents import AgentRunner
from rai.agents.ros2 import ROS2StateBasedAgent
from rai.agents import ReActAgent
state_based_agent = ROS2StateBasedAgent()
react_agent = ReActAgent()
runner = AgentRunner([state_based_agent, react_agent])
runner.run_and_wait_for_shutdown() # starts the agents and blocks until the shutdown signal (Ctrl+C or SIGTERM)
wait_for_shutdown¶
from rai.agents import wait_for_shutdown
from rai.agents.ros2 import ROS2StateBasedAgent
from rai.agents import ReActAgent
state_based_agent = ROS2StateBasedAgent()
react_agent = ReActAgent()
# start the agents manually
state_based_agent.run()
react_agent.run()
# blocks until the shutdown signal (Ctrl+C or SIGTERM)
wait_for_shutdown([state_based_agent, react_agent])
See Also¶
- Agents: For more information on the different types of agents in RAI
- Aggregators: For more information on the different types of aggregators in RAI
- Connectors: For more information on the different types of connectors in RAI
- Langchain Integration: For more information on the LangChain integration within RAI
- Multimodal messages: For more information on the multimodal LangChain messages in RAI