Refactor hook registration and processing methods (#2853)

* Refactor hook registration and processing methods

- Refactored the `hook_lists` dictionary to use type hints for better readability.
- Updated the `register_hook` method signature to include type hints for the `hook` parameter.
- Added type hints to the `process_last_received_message` method parameters and return value.

This commit refactors the code related to hook registration and processing in the `conversable_agent.py` file. The changes improve code readability and maintainability by using type hints and updating method signatures.

* Refactor hook_lists initialization and add type hints

- Refactored the initialization of `hook_lists` to use a colon instead of an equal sign.
- Added type hints for the parameters and return types of `process_last_received_message` method.

* Refactor hook registration and processing in conversable_agent.py

- Refactored the `hook_lists` dictionary to use a more generic type for the list of hooks.
- Updated the signature check for `process_message_before_send`, `process_all_messages_before_reply`, and `process_last_received_message` hooks to ensure they are callable with the correct signatures.
- Added error handling to raise a ValueError or TypeError if any hook does not have the expected signature.

* Refactor hook processing in conversable_agent.py

- Simplify the code by removing unnecessary type checks and error handling.
- Consolidate the logic for processing hooks in `_process_message_before_send`, `process_all_messages_before_reply`, and `process_last_received_message` methods.

* Refactor register_hook method signature for flexibility

The commit changes the signature of the `register_hook` method in `conversable_agent.py`. The second argument, `hook`, is now of type `Callable` instead of `Callable[[List[Dict]], List[Dict]]`. This change allows for more flexibility when registering hooks.
This commit is contained in:
Diego Colombo 2024-06-03 15:16:58 -07:00 committed by GitHub
parent a959deeac6
commit 5f29d6b97d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -242,7 +242,7 @@ class ConversableAgent(LLMAgent):
# Registered hooks are kept in lists, indexed by hookable method, to be called in their order of registration. # Registered hooks are kept in lists, indexed by hookable method, to be called in their order of registration.
# New hookable methods should be added to this list as required to support new agent capabilities. # New hookable methods should be added to this list as required to support new agent capabilities.
self.hook_lists = { self.hook_lists: Dict[str, List[Callable]] = {
"process_last_received_message": [], "process_last_received_message": [],
"process_all_messages_before_reply": [], "process_all_messages_before_reply": [],
"process_message_before_send": [], "process_message_before_send": [],
@ -2724,7 +2724,7 @@ class ConversableAgent(LLMAgent):
processed_messages = hook(processed_messages) processed_messages = hook(processed_messages)
return processed_messages return processed_messages
def process_last_received_message(self, messages): def process_last_received_message(self, messages: List[Dict]) -> List[Dict]:
""" """
Calls any registered capability hooks to use and potentially modify the text of the last message, Calls any registered capability hooks to use and potentially modify the text of the last message,
as long as the last message is not a function call or exit command. as long as the last message is not a function call or exit command.
@ -2758,6 +2758,7 @@ class ConversableAgent(LLMAgent):
processed_user_content = user_content processed_user_content = user_content
for hook in hook_list: for hook in hook_list:
processed_user_content = hook(processed_user_content) processed_user_content = hook(processed_user_content)
if processed_user_content == user_content: if processed_user_content == user_content:
return messages # No hooks actually modified the user's message. return messages # No hooks actually modified the user's message.