All post-hooks have an additional output argument, which is the output of the core function or the previous hook. If the core function has no output, the output will be None.
Name
Description
Arguments
self
The agent instance, which can be used to access the agent’s internal state and methods.
kwargs
The input keyword arguments.
output
The output of the target function or the most recent non-None return value from previous hooks
Returns
Any
The modified output to be returned to the next hook or the caller. If None, the original output will be used.
The post-hook template looks like this:
def post_hook_template( self: AgentBase | ReActAgentBase, kwargs: dict[str, Any], output: Any, # The output of the target function) -> Any: # The modified output """Post hook template.""" pass
Then we can register the hook function to a specific agent instance:
from agentscope.agent import ReActAgentagent = ReActAgent(...)agent.register_instance_hook( hook_type="pre_reply", hook_name="test_pre_reply", hook=instance_pre_reply_hook)
Now, when the agent is called to reply a message, the instance_pre_reply_hook will be executed before the reply, and
the message content will be modified accordingly.