A programming framework for agentic AI
add warning if duplicate function is registered (#2159)
jtoy4b5f5996a2d8
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.pyindex 89b2dd943..c3394a96b 100644--- a/autogen/agentchat/conversable_agent.py+++ b/autogen/agentchat/conversable_agent.py@@ -2406,6 +2406,8 @@ class ConversableAgent(LLMAgent):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)+ if name in self._function_map:+ warnings.warn(f"Function '{name}' is being overridden.", UserWarning)self._function_map.update(function_map)self._function_map = {k: v for k, v in self._function_map.items() if v is not None}@@ -2442,6 +2444,9 @@ class ConversableAgent(LLMAgent):self._assert_valid_name(func_sig["name"])if "functions" in self.llm_config.keys():+ if any(func["name"] == func_sig["name"] for func in self.llm_config["functions"]):+ warnings.warn(f"Function '{func_sig['name']}' is being overridden.", UserWarning)+self.llm_config["functions"] = [func for func in self.llm_config["functions"] if func.get("name") != func_sig["name"]] + [func_sig]@@ -2481,7 +2486,9 @@ class ConversableAgent(LLMAgent):f"The tool signature must be of the type dict. Received tool signature type {type(tool_sig)}")self._assert_valid_name(tool_sig["function"]["name"])- if "tools" in self.llm_config.keys():+ if "tools" in self.llm_config:+ if any(tool["function"]["name"] == tool_sig["function"]["name"] for tool in self.llm_config["tools"]):+ warnings.warn(f"Function '{tool_sig['function']['name']}' is being overridden.", UserWarning)self.llm_config["tools"] = [toolfor tool in self.llm_config["tools"]diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.pyindex b81a897b4..3c2e79beb 100755--- a/test/agentchat/test_conversable_agent.py+++ b/test/agentchat/test_conversable_agent.py@@ -1403,6 +1403,64 @@ def test_http_client():)+def test_adding_duplicate_function_warning():++ config_base = [{"base_url": "http://0.0.0.0:8000", "api_key": "NULL"}]++ agent = autogen.ConversableAgent(+ "jtoy",+ llm_config={"config_list": config_base},+ )++ def sample_function():+ pass++ agent.register_function(+ function_map={+ "sample_function": sample_function,+ }+ )+ agent.update_function_signature(+ {+ "name": "foo",+ },+ is_remove=False,+ )+ agent.update_tool_signature(+ {+ "type": "function",+ "function": {+ "name": "yo",+ },+ },+ is_remove=False,+ )++ with pytest.warns(UserWarning, match="Function 'sample_function' is being overridden."):+ agent.register_function(+ function_map={+ "sample_function": sample_function,+ }+ )+ with pytest.warns(UserWarning, match="Function 'foo' is being overridden."):+ agent.update_function_signature(+ {+ "name": "foo",+ },+ is_remove=False,+ )+ with pytest.warns(UserWarning, match="Function 'yo' is being overridden."):+ agent.update_tool_signature(+ {+ "type": "function",+ "function": {+ "name": "yo",+ },+ },+ is_remove=False,+ )++if __name__ == "__main__":# test_trigger()# test_context()@@ -1414,4 +1472,5 @@ if __name__ == "__main__":# test_process_before_send()# test_message_func()test_summary()+ test_adding_duplicate_function_warning()# test_function_registration_e2e_sync()