GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — Agent Hooks - AgentScope Note Go to the end to download the full example code. Agent Hooks¶Hooks are extension points in AgentScope that allow developers to customize agent behaviors at specific execution points, providing a flexible way to modify or extend the agent’s functionality without changing its core implementation. In AgentScope, hooks are implemented around the agent’s core functions:
Tip Since hooks in AgentScope are implemented using a metaclass, they support inheritance. To simplify the usage, AgentScope provides unified signatures for all hooks. import asyncio from typing import Any, Type from agentscope.agent import ReActAgentBase, AgentBase from agentscope.message import Msg Hook Signature¶AgentScope provides unified hook signatures for all pre- and post-hooks as follows: Pre-Hook Signature
Note All positional arguments and keyword arguments of the core function are passed as a single A pre-hook template is defined as follows: def pre_hook_template( self: AgentBase | ReActAgentBase, kwargs: dict[str, Any], ) -> dict[str, Any] | None: # The modified displayed message """Pre hook template.""" pass Post-Hook Signature For post hooks, an additional
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 Hook Management¶AgentScope provides both instance- and class-level hooks, depending on the effective scope of the hooks. They execute in the following order:
AgentScope provides built-in methods to manage hooks at both instance and class levels as follows:
When using hooks, you MUST follow these rules: Important Execution Order
Return Value Handling
Important: Never call the core function (reply/speak/observe/_reasoning/_acting) within a hook to avoid infinite loops Taking the following agent as an example, we can see how to register, remove and clear hooks: # Create a simple test agent class class TestAgent(AgentBase): """A test agent for demonstrating hooks.""" async def reply(self, msg: Msg) -> Msg: """Reply to the message.""" return msg We create an instance-level hook and a class-level hook to modify the message content before replying. # Create two pre-reply hooks def instance_pre_reply_hook( self: AgentBase, kwargs: dict[str, Any], ) -> dict[str, Any]: """A pre-reply hook that modifies the message content.""" msg = kwargs["msg"] msg.content += "[instance-pre-reply]" # return modified kwargs return { **kwargs, "msg": msg, } def cls_pre_reply_hook( self: AgentBase, kwargs: dict[str, Any], ) -> dict[str, Any]: """A pre-reply hook that modifies the message content.""" msg = kwargs["msg"] msg.content += "[cls-pre-reply]" # return modified kwargs return { **kwargs, "msg": msg, } # Register class hook TestAgent.register_class_hook( hook_type="pre_reply", hook_name="test_pre_reply", hook=cls_pre_reply_hook, ) # Register instance hook agent = TestAgent() agent.register_instance_hook( hook_type="pre_reply", hook_name="test_pre_reply", hook=instance_pre_reply_hook, ) async def example_test_hook() -> None: """An example function to test the hooks.""" msg = Msg( name="user", content="Hello, world!", role="user", ) res = await agent(msg) print("Response content:", res.content) TestAgent.clear_class_hooks() asyncio.run(example_test_hook()) Response content: Hello, world![instance-pre-reply][cls-pre-reply] We can see that a “[instance-pre-reply]” and a “[cls-pre-reply]” are added to the message content. Total running time of the script: (0 minutes 0.002 seconds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |