From c072c4aee67208ce4b39b3f6ab30139302879dae Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sat, 16 Nov 2024 17:01:31 -0500 Subject: [PATCH 1/9] update --- autogen/agentchat/conversable_agent.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index d2f8763281..840da79204 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -659,6 +659,9 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id: if message.get("role") in ["function", "tool"]: oai_message["role"] = message.get("role") + if "tool_responses" in oai_message: + for tool_response in oai_message["tool_responses"]: + tool_response["content"] = str(tool_response["content"]) elif "override_role" in message: # If we have a direction to override the role then set the # role accordingly. Used to customise the role for the @@ -791,15 +794,16 @@ async def a_send( "Message can't be converted into a valid ChatCompletion message. Either content or function_call must be provided." ) - def _print_received_message(self, message: Union[Dict, str], sender: Agent): + def _print_received_message(self, message: Union[Dict, str], sender: Agent, skip_head: bool = False): iostream = IOStream.get_default() # print the message received - iostream.print(colored(sender.name, "yellow"), "(to", f"{self.name}):\n", flush=True) + if not skip_head: + iostream.print(colored(sender.name, "yellow"), "(to", f"{self.name}):\n", flush=True) message = self._message_to_dict(message) if message.get("tool_responses"): # Handle tool multi-call responses for tool_response in message["tool_responses"]: - self._print_received_message(tool_response, sender) + self._print_received_message(tool_response, sender, skip_head=True) if message.get("role") == "tool": return # If role is tool, then content is just a concatenation of all tool_responses @@ -2288,7 +2292,7 @@ def _format_json_str(jstr): result.append(char) return "".join(result) - def execute_function(self, func_call, verbose: bool = False) -> Tuple[bool, Dict[str, str]]: + def execute_function(self, func_call, verbose: bool = False) -> Tuple[bool, Dict[str, Any]]: """Execute a function call and return the result. Override this function to modify the way to execute function and tool calls. @@ -2342,7 +2346,7 @@ def execute_function(self, func_call, verbose: bool = False) -> Tuple[bool, Dict return is_exec_success, { "name": func_name, "role": "function", - "content": str(content), + "content": content, } async def a_execute_function(self, func_call): @@ -2397,7 +2401,7 @@ async def a_execute_function(self, func_call): return is_exec_success, { "name": func_name, "role": "function", - "content": str(content), + "content": content, } def generate_init_message(self, message: Union[Dict, str, None], **kwargs) -> Union[str, Dict]: From 366332e98c74f3b8886667c6200665b8c9947205 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 11:53:24 -0500 Subject: [PATCH 2/9] update --- autogen/agentchat/groupchat.py | 2 ++ test/agentchat/test_conversable_agent.py | 2 +- test/agentchat/test_function_call.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index 4e9e107f92..fcd5f842b6 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -294,6 +294,8 @@ def append(self, message: Dict, speaker: Agent): # if the role is tool, it is OK to modify the name if message["role"] != "function": message["name"] = speaker.name + if not isinstance(message["content"], str) and isinstance(message["content"], list): + message["content"] = str(message["content"]) message["content"] = content_str(message["content"]) self.messages.append(message) diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index 8790c84db1..62bdf07c41 100755 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -464,7 +464,7 @@ def add_num(num_to_be_added): # when sender is None, messages is provided assert ( - dummy_agent_2.generate_reply(messages=messages, sender=None)["content"] == "15" + dummy_agent_2.generate_reply(messages=messages, sender=None)["content"] == 15 ), "generate_reply not working when sender is None" # when sender is provided, messages is None diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index 525a24cc17..21db979b17 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -116,7 +116,7 @@ def add_num(num_to_be_added): # correct execution correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} - assert user.execute_function(func_call=correct_args)[1]["content"] == "15" + assert user.execute_function(func_call=correct_args)[1]["content"] == 15 # function name called is wrong or doesn't exist wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'} From 15b565a23e02d4379ca9c3f2ec48748fc87c56a7 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:50:23 -0500 Subject: [PATCH 3/9] update --- autogen/agentchat/groupchat.py | 2 +- test/agentchat/test_conversable_agent.py | 2 +- test/agentchat/test_function_call.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/autogen/agentchat/groupchat.py b/autogen/agentchat/groupchat.py index fcd5f842b6..0e14bf35f8 100644 --- a/autogen/agentchat/groupchat.py +++ b/autogen/agentchat/groupchat.py @@ -294,7 +294,7 @@ def append(self, message: Dict, speaker: Agent): # if the role is tool, it is OK to modify the name if message["role"] != "function": message["name"] = speaker.name - if not isinstance(message["content"], str) and isinstance(message["content"], list): + if not isinstance(message["content"], str) and not isinstance(message["content"], list): message["content"] = str(message["content"]) message["content"] = content_str(message["content"]) self.messages.append(message) diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index 62bdf07c41..a2477ce2bf 100755 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -471,7 +471,7 @@ def add_num(num_to_be_added): dummy_agent_1 = ConversableAgent(name="dummy_agent_1", llm_config=False, human_input_mode="ALWAYS") dummy_agent_2._oai_messages[dummy_agent_1] = messages assert ( - dummy_agent_2.generate_reply(messages=None, sender=dummy_agent_1)["content"] == "15" + dummy_agent_2.generate_reply(messages=None, sender=dummy_agent_1)["content"] == 15 ), "generate_reply not working when messages is None" dummy_agent_2.register_reply(["str", None], ConversableAgent.generate_oai_reply) diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index 21db979b17..e2c77aea8f 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -144,8 +144,8 @@ def add(self, num_to_be_added): user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add}) func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} - assert user.execute_function(func_call=func_call)[1]["content"] == "15" - assert user.execute_function(func_call=func_call)[1]["content"] == "20" + assert user.execute_function(func_call=func_call)[1]["content"] == 15 + assert user.execute_function(func_call=func_call)[1]["content"] == 20 # 3. test calling a function with no arguments def get_number(): From 6024f2973b012f2e442944fd4d4dbcca5c4cfa75 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 17:44:08 -0500 Subject: [PATCH 4/9] update --- test/agentchat/test_function_call.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index e2c77aea8f..b89b65ca72 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -153,7 +153,7 @@ def get_number(): user = UserProxyAgent("user", function_map={"get_number": get_number}) func_call = {"name": "get_number", "arguments": "{}"} - assert user.execute_function(func_call)[1]["content"] == "42" + assert user.execute_function(func_call)[1]["content"] == 42 # 4. test with a non-existent function user = UserProxyAgent(name="test", function_map={}) @@ -185,9 +185,9 @@ async def add_num(num_to_be_added): correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} # Asset coroutine doesn't match. - assert user.execute_function(func_call=correct_args)[1]["content"] != "15" + assert user.execute_function(func_call=correct_args)[1]["content"] != 15 # Asset awaited coroutine does match. - assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == "15" + assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == 15 # function name called is wrong or doesn't exist wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'} From e3d119e44b32d60b02c1eb12a422aa5cdb53d9d2 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 17:56:50 -0500 Subject: [PATCH 5/9] update --- test/agentchat/test_function_call.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index b89b65ca72..b813895908 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -110,13 +110,13 @@ def test_execute_function(): # 1. test calling a simple function def add_num(num_to_be_added): given_num = 10 - return num_to_be_added + given_num + return str(num_to_be_added + given_num) user = UserProxyAgent(name="test", function_map={"add_num": add_num}) # correct execution correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} - assert user.execute_function(func_call=correct_args)[1]["content"] == 15 + assert user.execute_function(func_call=correct_args)[1]["content"] == "15" # function name called is wrong or doesn't exist wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'} @@ -144,16 +144,16 @@ def add(self, num_to_be_added): user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add}) func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} - assert user.execute_function(func_call=func_call)[1]["content"] == 15 - assert user.execute_function(func_call=func_call)[1]["content"] == 20 + assert user.execute_function(func_call=func_call)[1]["content"] == "15" + assert user.execute_function(func_call=func_call)[1]["content"] == "20" # 3. test calling a function with no arguments def get_number(): - return 42 + return str(42) user = UserProxyAgent("user", function_map={"get_number": get_number}) func_call = {"name": "get_number", "arguments": "{}"} - assert user.execute_function(func_call)[1]["content"] == 42 + assert user.execute_function(func_call)[1]["content"] == "42" # 4. test with a non-existent function user = UserProxyAgent(name="test", function_map={}) @@ -179,15 +179,15 @@ async def test_a_execute_function(): async def add_num(num_to_be_added): given_num = 10 time.sleep(1) - return num_to_be_added + given_num + return str(num_to_be_added + given_num) user = UserProxyAgent(name="test", function_map={"add_num": add_num}) correct_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} # Asset coroutine doesn't match. - assert user.execute_function(func_call=correct_args)[1]["content"] != 15 + assert user.execute_function(func_call=correct_args)[1]["content"] != "15" # Asset awaited coroutine does match. - assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == 15 + assert (await user.a_execute_function(func_call=correct_args))[1]["content"] == "15" # function name called is wrong or doesn't exist wrong_func_name = {"name": "subtract_num", "arguments": '{ "num_to_be_added": 5 }'} @@ -214,8 +214,7 @@ def __init__(self, given_num): def add(self, num_to_be_added): self.given_num = num_to_be_added + self.given_num - return self.given_num - + return str(self.given_num) user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add}) func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "15" @@ -223,7 +222,7 @@ def add(self, num_to_be_added): # 3. test calling a function with no arguments def get_number(): - return 42 + return str(42) user = UserProxyAgent("user", function_map={"get_number": get_number}) func_call = {"name": "get_number", "arguments": "{}"} From b5d14531e43a49485b1c0e90cc26007c5c57865a Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:04:56 -0500 Subject: [PATCH 6/9] udpate --- test/agentchat/test_function_call.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index b813895908..5ca82d3ff5 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -140,7 +140,7 @@ def __init__(self, given_num): def add(self, num_to_be_added): self.given_num = num_to_be_added + self.given_num - return self.given_num + return str(self.given_num) user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add}) func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} From 01d933f0a7822b7a2092a68a43c036d6437c6907 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:22:48 -0500 Subject: [PATCH 7/9] udpate code format --- test/agentchat/test_function_call.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py index 5ca82d3ff5..5dfcd839f3 100755 --- a/test/agentchat/test_function_call.py +++ b/test/agentchat/test_function_call.py @@ -215,6 +215,7 @@ def __init__(self, given_num): def add(self, num_to_be_added): self.given_num = num_to_be_added + self.given_num return str(self.given_num) + user = UserProxyAgent(name="test", function_map={"add_num": AddNum(given_num=10).add}) func_call = {"name": "add_num", "arguments": '{ "num_to_be_added": 5 }'} assert (await user.a_execute_function(func_call=func_call))[1]["content"] == "15" From d679c7ee785d65837a328bc74a676b78f9504429 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:22:52 -0500 Subject: [PATCH 8/9] update --- test/agentchat/test_conversable_agent.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index a2477ce2bf..12daee5abc 100755 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -455,7 +455,7 @@ def test_conversable_agent(): def test_generate_reply(): def add_num(num_to_be_added): given_num = 10 - return num_to_be_added + given_num + return str(num_to_be_added + given_num) dummy_agent_2 = ConversableAgent( name="user_proxy", llm_config=False, human_input_mode="TERMINATE", function_map={"add_num": add_num} @@ -1115,7 +1115,7 @@ class Function: def get_random_number(self): self.call_count += 1 - return random.randint(0, 100) + return str(random.randint(0, 100)) config_list = autogen.config_list_from_json( OAI_CONFIG_LIST, @@ -1171,7 +1171,7 @@ class Function: def get_random_number(self): self.call_count += 1 - return random.randint(0, 100) + return str(random.randint(0, 100)) config_list = autogen.config_list_from_json( OAI_CONFIG_LIST, file_location=KEY_LOC, filter_dict={"tags": ["gpt-3.5-turbo"]} From d1d6f1c427f208ab46fd2818babb8b37fb0df320 Mon Sep 17 00:00:00 2001 From: Yiran Wu <32823396+kevin666aa@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:28:08 -0500 Subject: [PATCH 9/9] update --- test/agentchat/test_conversable_agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py index 12daee5abc..81e0036dc0 100755 --- a/test/agentchat/test_conversable_agent.py +++ b/test/agentchat/test_conversable_agent.py @@ -455,7 +455,7 @@ def test_conversable_agent(): def test_generate_reply(): def add_num(num_to_be_added): given_num = 10 - return str(num_to_be_added + given_num) + return num_to_be_added + given_num dummy_agent_2 = ConversableAgent( name="user_proxy", llm_config=False, human_input_mode="TERMINATE", function_map={"add_num": add_num}