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.
Control loops will not start and action requests will not be processed until after this callback completes. Thus, it is safe to initialize resources in this callback that are needed by actions or loops.
See
Runtime.run_until_complete()
for more details on the startup sequence.
Source code in academy/agent.py
agent_on_shutdown
async
¶
Callback invoked at the beginning of an agent's shutdown sequence.
See
Runtime.run_until_complete()
for more details on the shutdown sequence.
Source code in academy/agent.py
agent_run_sync
async
¶
agent_run_sync(
function: Callable[P, R],
/,
*args: args,
**kwargs: kwargs,
) -> R
Run a blocking function in separate thread.
Example
Note
The max concurrency of the executor is configured in the
RuntimeConfig
. If all
executor workers are busy the function will be queued and a
warning will be logged.
Warning
This function does not support cancellation. For example, if you
wrap this call in asyncio.wait_for()
and a
timeout occurs, the task wrapping the coroutine will be cancelled
but the blocking function will continue running in its thread until
completion.
Parameters:
-
function
(Callable[P, R]
) –The blocking function to run.
-
*args
(args
, default:()
) –Positional arguments for the function.
-
**kwargs
(kwargs
, default:{}
) –Keyword arguments for the function.
Returns:
-
R
–The result of the function call.
Raises:
-
AgentNotInitializedError
–If the agent runtime has not been started.
-
Exception
–Any exception raised by the function.
Source code in academy/agent.py
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
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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
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: