diff --git a/autogen/agentchat/contrib/swarm_agent.py b/autogen/agentchat/contrib/swarm_agent.py index fcb6db08a1..c525c53db3 100644 --- a/autogen/agentchat/contrib/swarm_agent.py +++ b/autogen/agentchat/contrib/swarm_agent.py @@ -350,7 +350,7 @@ def __init__( human_input_mode: Literal["ALWAYS", "NEVER", "TERMINATE"] = "NEVER", description: Optional[str] = None, code_execution_config=False, - update_agent_before_reply: Optional[ + update_agent_state_before_reply: Optional[ Union[List[Union[Callable, UPDATE_SYSTEM_MESSAGE]], Callable, UPDATE_SYSTEM_MESSAGE] ] = None, **kwargs, @@ -385,9 +385,9 @@ def __init__( # List of Dictionaries containing the nested_chats and condition self._nested_chat_handoffs = [] - self.register_update_agent_before_reply(update_agent_before_reply) + self.register_update_agent_state_before_reply(update_agent_state_before_reply) - def register_update_agent_before_reply(self, functions: Optional[Union[List[Callable], Callable]]): + def register_update_agent_state_before_reply(self, functions: Optional[Union[List[Callable], Callable]]): """ Register functions that will be called when the agent is selected and before it speaks. You can add your own validation or precondition functions here. diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index db69574f96..ffd6923721 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -2093,7 +2093,7 @@ def generate_reply( messages = self._oai_messages[sender] # Call the hookable method that gives registered hooks a chance to update agent state, used for their context variables. - self.process_update_agent_states(messages) + self.update_agent_state_before_reply(messages) # Call the hookable method that gives registered hooks a chance to process the last message. # Message modifications do not affect the incoming messages or self._oai_messages. @@ -2166,7 +2166,7 @@ async def a_generate_reply( messages = self._oai_messages[sender] # Call the hookable method that gives registered hooks a chance to update agent state, used for their context variables. - self.process_update_agent_states(messages) + self.update_agent_state_before_reply(messages) # Call the hookable method that gives registered hooks a chance to process all messages. # Message modifications do not affect the incoming messages or self._oai_messages. @@ -2854,7 +2854,7 @@ def register_hook(self, hookable_method: str, hook: Callable): assert hook not in hook_list, f"{hook} is already registered as a hook." hook_list.append(hook) - def process_update_agent_states(self, messages: List[Dict]) -> None: + def update_agent_state_before_reply(self, messages: List[Dict]) -> None: """ Calls any registered capability hooks to update the agent's state. Primarily used to update context variables. diff --git a/website/docs/topics/swarm.ipynb b/website/docs/topics/swarm.ipynb index 2edd76fcde..82a0a3cc83 100644 --- a/website/docs/topics/swarm.ipynb +++ b/website/docs/topics/swarm.ipynb @@ -168,11 +168,11 @@ "source": [ "### Update Agent state before replying\n", "\n", - "It can be useful to update an agent's state before they reply, particularly their system message/prompt.\n", + "It can be useful to update a swarm agent's state before they reply. For example, using an agent's context variables you could change their system message based on the state of the workflow.\n", "\n", - "When initialising an agent you can use the `update_agent_before_reply` to register the updates run when the agent is selected, but before they reply.\n", + "When initialising a swarm agent use the `update_agent_state_before_reply` parameter to register updates that run after the agent is selected, but before they reply.\n", "\n", - "The `update_agent_before_reply` takes a list of any combination of the following (executing them in the provided order):\n", + "`update_agent_state_before_reply` takes a list of any combination of the following (executing them in the provided order):\n", "\n", "- `UPDATE_SYSTEM_MESSAGE` provides a simple way to update the agent's system message via an f-string that substitutes the values of context variables, or a Callable that returns a string\n", "- Callable with two parameters of type `ConversableAgent` for the agent and `List[Dict[str Any]]` for the messages, and does not return a value\n", @@ -198,7 +198,7 @@ "customer_service = SwarmAgent(\n", " name=\"CustomerServiceRep\",\n", " system_message=\"You are a customer service representative.\",\n", - " update_agent_before_reply=[\n", + " update_agent_state_before_reply=[\n", " UPDATE_SYSTEM_MESSAGE(\"You are a customer service representative. Quote passport number '{passport_number}'\"),\n", " UPDATE_SYSTEM_MESSAGE(create_system_prompt_function),\n", " my_callable_state_update_function]\n",