How to register a hook
Use the ConversableAgent.register_hook method to register a function for a hook using the hook’s string name.1. “process_message_before_send”
This hook is the only hook not in ConversableAgent’s generate_reply method and is designed to intercept a message before it is sent to another agent. You can change the message with the hook and the change will be permanent. This is because this hook is called before a message is displayed or added to the message list. Signature:Mike
agent will have this hook registered, so all their messages will have this text added.
2. “update_agent_state”
The first of three hooks that run in the ConversableAgent.generate_reply method before the reply functions are evaluated. This hook is designed to be used to update an agent’s state, typically their system message, before replying. As the system message is a key message for an LLM to consider, it’s useful to be able to make sure that pertinent information is there. A couple of examples of where it is used:- DocAgent to update the internal summary agent’s system message with the context from the ingestions and queries.
- Swarms to hide/show conditional hand-offs.
Calendar_Agent
is able to provide the current date and time. Furthermore, their system message is updated.
3. “process_last_received_message”
This is the second of the three hooks that run in the ConversableAgent.generate_reply method before the reply functions are evaluated. This hook is used to process the last message in the messages list (as opposed to all messages, handled by the next hook). If the last message is a function call or is “exit” then it will not execute the associated function. Changing the message will result in permanent changes to the chat messages of the agent with the hook, but not other agents. So, changes to the messages will be present in future generate_reply calls for the agent with the hook, but not other agent’s calls to generate_reply. A couple of examples of where it is used:- The Teachability capability appends any relevant memos to the last message.
- The Vision capability will update the last message if it contains an image with a caption for the image using an LLM call.
4. “process_all_messages_before_reply”
The final hook that runs in the ConversableAgent.generate_reply method before the reply functions are evaluated. This hook is used to work on all messages before replying. The changes to these messages will be used for the replying but will not persist beyond the generate_reply call. An example of the use of this hook is the TransformMessages capability where it carries out all transforms (such as limiting the number messages, filtering sensitive information, truncating individual messages) on messages before an agent replies. Signature:- The messages are updated before Mike replies.
- The ChatResult’s chat history does not include these temporary changes.
- LLMs have an inclination to mimic formats used, so we can see the LLM added “:Mike \nMike said:” to the last message it created (this wasn’t created by our hook).