academy.agent¶
Agent
¶
Agent base class.
An agent is composed of three parts:
- The
agent_on_startup()
andagent_on_shutdown()
methods define callbacks that are invoked once at the start and end of an agent's execution, respectively. The methods should be used to initialize and cleanup stateful resources. Resource initialization should not be performed in__init__
. - Action methods annotated with
@action
are methods that other agents can invoke on this agent. An agent may also call it's own action methods as normal methods. - Control loop methods annotated with
@loop
are executed in separate threads when the agent is executed.
The Runtime
is used to execute an agent
definition.
Warning
This class cannot be instantiated directly and must be subclassed.
Source code in academy/agent.py
agent_context
property
¶
agent_context: AgentContext[Self]
Agent runtime context.
Raises:
-
AgentNotInitializedError
–If the agent runtime implementing this agent has not been started.
agent_id
property
¶
agent_id: AgentId[Self]
Agent Id.
Raises:
-
AgentNotInitializedError
–If the agent runtime implementing this agent has not been started.
agent_exchange_client
property
¶
agent_exchange_client: AgentExchangeClient[Self, Any]
Agent exchange client.
Raises:
-
AgentNotInitializedError
–If the agent runtime implementing this agent has not been started.
agent_on_startup
async
¶
Callback invoked at the end of an agent's startup sequence.
See Runtime.run()
for more
details on the startup sequence.
agent_on_shutdown
async
¶
Callback invoked at the beginning of an agent's shutdown sequence.
See Runtime.run()
for more
details on the shutdown sequence.
agent_shutdown
¶
Request the agent to shutdown.
Raises:
-
AgentNotInitializedError
–If the agent runtime implementing this agent has not been started.
Action
¶
ControlLoop
¶
Bases: Protocol
Control loop method protocol.
__call__
async
¶
__call__(shutdown: Event) -> None
Expected signature of methods decorated as a control loop.
Parameters:
-
shutdown
(Event
) –Event indicating that the agent has been instructed to shutdown and all control loops should exit.
Returns:
-
None
–Control loops should not return anything.
Source code in academy/agent.py
action
¶
action(
method: ActionMethod[P, R] | None = None,
*,
allow_protected_name: bool = False,
context: bool = False
) -> (
ActionMethod[P, R]
| Callable[[ActionMethod[P, R]], ActionMethod[P, R]]
)
Decorator that annotates a method of a agent as an action.
Marking a method of a agent as an action makes the method available to other agents. I.e., peers within a multi-agent system can only invoke methods marked as actions on each other. This enables agents to define "private" methods.
Example
Warning
A warning will be emitted if the decorated method's name clashed
with a method of Handle
because it would
not be possible to invoke this action remotely via attribute
lookup on a handle. This warning can be suppressed with
allow_protected_name=True
, and the action must be invoked via
Handle.action()
.
Parameters:
-
method
(ActionMethod[P, R] | None
, default:None
) –Method to decorate as an action.
-
allow_protected_name
(bool
, default:False
) –Allow decorating a method as an action when the name of the method clashes with a protected method name of
Handle
. This flag silences the emitted warning. -
context
(bool
, default:False
) –Specify that the action method expects a context argument. The
context
will be provided at runtime as a keyword argument.
Raises:
-
TypeError
–If
context=True
and the method does not have a parameter namedcontext
or ifcontext
is a positional only argument.
Source code in academy/agent.py
318 319 320 321 322 323 324 325 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 408 |
|
loop
¶
Decorator that annotates a method of a agent as a control loop.
Control loop methods of a agent are run as threads when an agent
starts. A control loop can run for a well-defined period of time or
indefinitely, provided the control loop exits when the shutdown
event, passed as a parameter to all control loop methods, is set.
Example
Raises:
-
TypeError
–if the method signature does not conform to the
ControlLoop
protocol.
Source code in academy/agent.py
event
¶
event(
name: str,
) -> Callable[
[Callable[[AgentT], Coroutine[None, None, None]]],
LoopMethod[AgentT],
]
Decorator that annotates a method of a agent as an event loop.
An event loop is a special type of control loop that runs when a
asyncio.Event
is set. The event is cleared
after the loop runs.
Example
Parameters:
-
name
(str
) –Attribute name of the
asyncio.Event
to wait on.
Raises:
-
AttributeError
–Raised at runtime if no attribute named
name
exists on the agent. -
TypeError
–Raised at runtime if the attribute named
name
is not aasyncio.Event
.
Source code in academy/agent.py
timer
¶
timer(
interval: float | timedelta,
) -> Callable[
[Callable[[AgentT], Coroutine[None, None, None]]],
LoopMethod[AgentT],
]
Decorator that annotates a method of a agent as a timer loop.
A timer loop is a special type of control loop that runs at a set interval. The method will always be called once before the first sleep.
Example
Parameters: