Hooks
API Reference: ConversableAgent.register_hook
Hooks are a means to update/review agent state before replying, and messages before sending and replying.
You can define functions and associate them with these hooks and they will execute at certain points in the workflow, see the initiate_chat page to see where these occur (blue circles).
How to register a hook
Use the ConversableAgent.register_hook method to register a function for a hook using the hook’s string name.
There are four hooks available on a ConversableAgent, let’s look at them and how you use them.
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:
Let’s look at how we could prepend a message with who is sending the message (sender) and to whom it’s going (recipient).
In this example, our Mike
agent will have this hook registered, so all their messages will have this text added.
Here’s the output, you can see Mike’s messages (including the initial message) now have our text prepended and the chat history has the permanently changed messages:
This is a good hook if you need to edit the message before it is added to the messages list, such as for redacting text.
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.
Signature:
In the following example we will update an agent’s system message to have the current date in it.
We can see from the output that 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.
Signature:
Here’s an example that changes any instances of the word “blue” to “red” in the last message.
We can see from the output that although the question asks why the sky is blue, it is updated and changed to red before the LLM responds.
We can see the main chat messages are unchanged but the Scientist’s chat history with Bob shows that the question was changed to red.
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:
Here’s an example that adds the agent’s name to the start of each message.
From the output we can observe:
- 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).