Repository: microsoft/autogen
Pull request: add warning if duplicate function is registered
<!-- Thank you for your contribution! Please review https://microsoft.github.io/autogen/docs/Contribute before opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. -->

## Why are these changes needed?

This changes gives a warning if you register a function for execution with the same name

## Related issue number

Closes #1607 

## Checks

- [x] I've included any doc changes needed for https://microsoft.github.io/autogen/. See https://microsoft.github.io/autogen/docs/Contribute#documentation to build and test documentation locally.
- [x] I've added tests (if relevant) corresponding to the changes introduced in this PR.
- [x] I've made sure all auto checks have passed.


Problem statement:
[Feature Request]: Display warning when overwriting tool
### Is your feature request related to a problem? Please describe.

Currently overriding a tool is as simple as

```python
@user_proxy.register_for_execution(name="my-first-tool")
def do_something():
     pass

@user_proxy.register_for_execution(name="my-first-tool")
def do_something_else():
     pass
```

The problem is that it might happen that a user copies the annotation from the first tool without changing the name. In that case, does the first tool function get overridden by the second definition. This might be completely fine in some cases.

But it might also happen that this results in unexpected behaviors. It might make sense to display a warning when the user overrides an existing tool name.

### Describe the solution you'd like

Create a warning in the console when the user overrides an existing tool function

### Additional context

_No response_

Hints from issue discussion:
You are welcome to submit a pr to fix this

Commit message: cleanups

Patch to review:
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py
index eb9d1c9cbcb8..24d3ad19d053 100644
--- a/autogen/agentchat/conversable_agent.py
+++ b/autogen/agentchat/conversable_agent.py
@@ -2312,6 +2312,8 @@ def register_function(self, function_map: Dict[str, Union[Callable, None]]):
             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.keys():
+                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}
 
@@ -2348,6 +2350,9 @@ def update_function_signature(self, func_sig: Union[str, Dict], is_remove: None)
 
             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]
@@ -2387,6 +2392,8 @@ def update_tool_signature(self, tool_sig: Union[str, Dict], is_remove: None):
                     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 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)
             if "tools" in self.llm_config.keys():
                 self.llm_config["tools"] = [
                     tool


Code review comments:
diff_hunk: @@ -2312,6 +2312,8 @@ def register_function(self, function_map: Dict[str, Union[Callable, None]]):
             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.keys():
path: autogen/agentchat/conversable_agent.py
text: @user1:
```suggestion
            if name in self._function_map:
```

diff_hunk: @@ -2387,6 +2392,8 @@ def update_tool_signature(self, tool_sig: Union[str, Dict], is_remove: None):
                     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 any(tool["function"]["name"] == tool_sig["function"]["name"] for tool in self.llm_config["tools"]):
path: autogen/agentchat/conversable_agent.py
text: @user1:
Move this after line 2397 to make sure there is a `tools` field.

diff_hunk: @@ -2387,6 +2392,8 @@ def update_tool_signature(self, tool_sig: Union[str, Dict], is_remove: None):
                     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 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)
             if "tools" in self.llm_config.keys():
path: autogen/agentchat/conversable_agent.py
text: @user1:
```suggestion
            if "tools" in self.llm_config:
```