academy.agent¶
AgentRunConfig
dataclass
¶
AgentRunConfig(
close_exchange_on_exit: bool = True,
max_action_concurrency: int | None = None,
terminate_on_error: bool = True,
terminate_on_exit: bool = True,
)
Agent run configuration.
Attributes:
-
close_exchange_on_exit
(bool
) –Close the exchange interface when the agent exits. Typically this should be
True
to clean up resources, except when multiple agents are running in the same process and sharing an exchange. -
max_action_concurrency
(int | None
) –Maximum size of the thread pool used to concurrently execute action requests.
-
terminate_on_error
(bool
) –Terminate the agent by closing its mailbox permanently if the agent fails.
-
terminate_on_exit
(bool
) –Terminate the agent by closing its mailbox permanently after the agent exits.
Agent
¶
Agent(
behavior: BehaviorT,
*,
agent_id: AgentId[BehaviorT],
exchange: Exchange,
config: AgentRunConfig | None = None
)
Bases: Generic[BehaviorT]
Executable agent.
An agent executes predefined Behavior
. An
agent can operate independently or as part of a broader multi-agent
system.
Note
An agent can only be run once. After shutdown()
is called, later
operations will raise a RuntimeError
.
Note
If any @loop
method raises an error, the agent will be signaled
to shutdown.
Parameters:
-
behavior
(BehaviorT
) –Behavior that the agent will exhibit.
-
agent_id
(AgentId[BehaviorT]
) –EntityId of this agent in a multi-agent system.
-
exchange
(Exchange
) –Message exchange of multi-agent system. The agent will close the exchange when it finished running.
-
config
(AgentRunConfig | None
, default:None
) –Agent execution parameters.
Source code in academy/agent.py
__call__
¶
action
¶
Invoke an action of the agent.
Parameters:
-
action
(str
) –Name of action to invoke.
-
args
(Any
) –Tuple of positional arguments.
-
kwargs
(Any
) –Dictionary of keyword arguments.
Returns:
-
Any
–Result of the action.
Raises:
-
AttributeError
–if an action with this name is not implemented by the behavior of the agent.
Source code in academy/agent.py
run
¶
Run the agent.
Starts the agent, waits for another thread to call signal_shutdown()
,
and then shuts down the agent.
Raises:
-
Exception
–Any exceptions raised inside threads.
Source code in academy/agent.py
start
¶
Start the agent.
Note
This method is idempotent; it will return if the agent is already running. However, it will raise an error if the agent is shutdown.
- Binds all unbound handles to remote agents to this agent.
- Calls
Behavior.on_setup()
. - Starts threads for all control loops defined on the agent's
Behavior
. - Starts a thread for listening to messages from the
Exchange
(if provided).
Raises:
-
RuntimeError
–If the agent has been shutdown.
Source code in academy/agent.py
shutdown
¶
Shutdown the agent.
Note
This method is idempotent.
- Sets the shutdown
Event
passed to all control loops. - Waits for any currently executing actions to complete.
- Closes the agent's mailbox indicating that no further messages will be processed.
- Waits for the control loop and message listener threads to exit.
- Optionally closes the exchange.
- Calls
Behavior.on_shutdown()
.
Raises:
-
Exception
–Any exceptions raised inside threads.
Source code in academy/agent.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
|
signal_shutdown
¶
signal_shutdown(expected: bool = True) -> None
Signal that the agent should exit.
If the agent has not started, this will cause the agent to immediately shutdown when next started. If the agent is shutdown, this has no effect.