Skip to content

A programming framework for agentic AI

Support functions removing in ConversableAgent (#1786)

Shaokun Zhangb8727893f7ac
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py
index 2434d7325..d23551343 100644
--- a/autogen/agentchat/conversable_agent.py
+++ b/autogen/agentchat/conversable_agent.py
@@ -2129,15 +2129,18 @@ class ConversableAgent(LLMAgent):
self._process_carryover(context)
return context["message"]
- def register_function(self, function_map: Dict[str, Callable]):
+ def register_function(self, function_map: Dict[str, Union[Callable, None]]):
"""Register functions to the agent.
Args:
- function_map: a dictionary mapping function names to functions.
+ function_map: a dictionary mapping function names to functions. if function_map[name] is None, the function will be removed from the function_map.
"""
- for name in function_map.keys():
+ for name, func in function_map.items():
self._assert_valid_name(name)
+ if func is None and name not in self._function_map.keys():
+ warnings.warn(f"The function {name} to remove doesn't exist", name)
self._function_map.update(function_map)
+ self._function_map = {k: v for k, v in self._function_map.items() if v is not None}
def update_function_signature(self, func_sig: Union[str, Dict], is_remove: None):
"""update a function_signature in the LLM configuration for function_call.
diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py
index 2a5eaf5f5..df6a06a79 100644
--- a/test/agentchat/test_conversable_agent.py
+++ b/test/agentchat/test_conversable_agent.py
@@ -559,6 +559,16 @@ def test_update_function_signature_and_register_functions() -> None:
assert agent.function_map["python"] == exec_python
assert agent.function_map["sh"] == exec_sh
+ # remove the functions
+ agent.register_function(
+ function_map={
+ "python": None,
+ }
+ )
+
+ assert set(agent.function_map.keys()) == {"sh"}
+ assert agent.function_map["sh"] == exec_sh
+
def test__wrap_function_sync():
CurrencySymbol = Literal["USD", "EUR"]