Skip to content

Commit

Permalink
add warning if duplicate function is registered (#2159)
Browse files Browse the repository at this point in the history
* add warning if duplicate function is registereed

* check _function_map and llm_config

* check function_map and llm_config

* use register_function and  llm_config

* cleanups

* cleanups

* warning test

* warning test

* more test coverage

* use a fake config

* formatting

* formatting

---------

Co-authored-by: Jason <jtoy@grids.local>
Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
  • Loading branch information
3 people authored May 22, 2024
1 parent b698a98 commit 4b5f599
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
9 changes: 8 additions & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,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:
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}

Expand Down Expand Up @@ -2442,6 +2444,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]
Expand Down Expand Up @@ -2481,7 +2486,9 @@ 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 "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"] = [
tool
for tool in self.llm_config["tools"]
Expand Down
59 changes: 59 additions & 0 deletions test/agentchat/test_conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -1414,4 +1472,5 @@ def test_http_client():
# test_process_before_send()
# test_message_func()
test_summary()
test_adding_duplicate_function_warning()
# test_function_registration_e2e_sync()

0 comments on commit 4b5f599

Please sign in to comment.