academy.agent¶
ActionDescription
pydantic-model
¶
AgentDescription
pydantic-model
¶
Bases: BaseModel
Typed description of an Agent.
Fields:
-
description(str) -
actions(dict[str, ActionDescription])
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
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
@actionare 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
@loopare 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.
agent_launch_alongside
async
¶
agent_launch_alongside(
agent: AgentT | type[AgentT],
*,
args: tuple[Any, ...] | None = None,
kwargs: dict[str, Any] | None = None
) -> Handle[AgentT]
Launch a child agent in the current event loop.
Parameters:
-
agent(AgentT | type[AgentT]) –An agent instance to launch, or an agent class to be instantiated before launching using
argsandkwargs. -
args(tuple[Any, ...] | None, default:None) –Positional arguments used to initialize the agent. Ignored if
agentis already an instance. -
kwargs(dict[str, Any] | None, default:None) –Keyword arguments used to initialize the agent. Ignored if
agentis already an instance.
Source code in academy/agent.py
agent_describe
async
¶
agent_describe() -> AgentDescription
Returns a description of the agent.
Returns:
-
AgentDescription–A AgentDescription created from the class documentation.
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
contextwill be provided at runtime as a keyword argument.
Raises:
-
TypeError–If the decorated function is not a coroutine.
-
TypeError–If
context=Trueand the method does not have a parameter namedcontextor ifcontextis a positional only argument.
Source code in academy/agent.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
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
ControlLoopprotocol.
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.Eventto wait on.
Raises:
-
AttributeError–Raised at runtime if no attribute named
nameexists on the agent. -
TypeError–Raised at runtime if the attribute named
nameis not aasyncio.Event.
Source code in academy/agent.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 | |
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: