Skip to content

A programming framework for agentic AI

fix test_human_input (#2063)

Chi Wang6b47a966dd4d
diff --git a/test/agentchat/test_async_chats.py b/test/agentchat/test_async_chats.py
index 95a54f4cf..883becc43 100755
--- a/test/agentchat/test_async_chats.py
+++ b/test/agentchat/test_async_chats.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3 -m pytest
import pytest
+import asyncio
import sys
import os
import autogen
@@ -30,7 +31,7 @@ async def test_async_chats():
financial_assistant_1 = AssistantAgent(
name="Financial_assistant_1",
llm_config={"config_list": config_list},
- system_message="You are a knowledgeable AI Assistant.",
+ system_message="You are a knowledgeable AI Assistant. Reply TERMINATE when everything is done.",
)
financial_assistant_2 = AssistantAgent(
name="Financial_assistant_2",
@@ -60,7 +61,7 @@ async def test_async_chats():
)
def my_summary_method(recipient, sender, summary_args):
- return recipient.chat_messages[sender][-1].get("content", "")
+ return recipient.chat_messages[sender][1].get("content", "")
chat_res = await user.a_initiate_chats(
[
@@ -70,6 +71,7 @@ async def test_async_chats():
"message": financial_tasks[0],
"silent": False,
"summary_method": my_summary_method,
+ "max_turns": 1,
},
{
"chat_id": 2,
@@ -87,6 +89,7 @@ async def test_async_chats():
"message": financial_tasks[2],
"summary_method": "last_msg",
"clear_history": False,
+ "max_turns": 1,
},
{
"chat_id": 4,
@@ -95,6 +98,7 @@ async def test_async_chats():
"message": writing_tasks[0],
"carryover": "I want to include a figure or a table of data in the blogpost.",
"summary_method": "last_msg",
+ "max_turns": 2,
},
]
)
@@ -116,4 +120,4 @@ async def test_async_chats():
if __name__ == "__main__":
- test_async_chats()
+ asyncio.run(test_async_chats())
diff --git a/test/agentchat/test_chats.py b/test/agentchat/test_chats.py
index 4aa0ce76d..153e5cd3b 100755
--- a/test/agentchat/test_chats.py
+++ b/test/agentchat/test_chats.py
@@ -14,6 +14,15 @@ from autogen import initiate_chats
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from conftest import skip_openai # noqa: E402
+config_list = (
+ []
+ if skip_openai
+ else autogen.config_list_from_json(
+ OAI_CONFIG_LIST,
+ file_location=KEY_LOC,
+ )
+)
+
def test_chat_messages_for_summary():
assistant = UserProxyAgent(name="assistant", human_input_mode="NEVER", code_execution_config={"use_docker": False})
@@ -38,10 +47,6 @@ def test_chat_messages_for_summary():
@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_chats_group():
- config_list = autogen.config_list_from_json(
- OAI_CONFIG_LIST,
- file_location=KEY_LOC,
- )
financial_tasks = [
"""What are the full names of NVDA and TESLA.""",
"""Give lucky numbers for them.""",
@@ -85,9 +90,9 @@ def test_chats_group():
llm_config={"config_list": config_list},
)
- groupchat_1 = GroupChat(agents=[user_proxy, financial_assistant, critic], messages=[], max_round=50)
+ groupchat_1 = GroupChat(agents=[user_proxy, financial_assistant, critic], messages=[], max_round=3)
- groupchat_2 = GroupChat(agents=[user_proxy, writer, critic], messages=[], max_round=50)
+ groupchat_2 = GroupChat(agents=[user_proxy, writer, critic], messages=[], max_round=3)
manager_1 = GroupChatManager(
groupchat=groupchat_1,
@@ -134,9 +139,8 @@ def test_chats_group():
"recipient": manager_1,
"message": financial_tasks[1],
"summary_method": "reflection_with_llm",
- "max_turns": 1,
},
- {"recipient": manager_2, "message": writing_tasks[0], "max_turns": 1},
+ {"recipient": manager_2, "message": writing_tasks[0]},
]
)
@@ -161,11 +165,6 @@ def test_chats():
self.call_count += 1
return random.randint(0, 100)
- config_list = autogen.config_list_from_json(
- OAI_CONFIG_LIST,
- file_location=KEY_LOC,
- )
-
def luck_number_message(sender, recipient, context):
final_msg = {}
final_msg["content"] = "Give lucky numbers for them."
@@ -215,7 +214,7 @@ def test_chats():
)
def my_summary_method(recipient, sender, summary_args):
- return recipient.chat_messages[sender][0].get("content", "")
+ return recipient.chat_messages[sender][1].get("content", "")
# chat_res_play = user.initiate_chat(
# player,
@@ -233,6 +232,7 @@ def test_chats():
"message": financial_tasks[0],
"silent": False,
"summary_method": my_summary_method,
+ "max_turns": 1,
},
{
"recipient": financial_assistant_2,
@@ -284,11 +284,6 @@ def test_chats():
@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_chats_general():
- config_list = autogen.config_list_from_json(
- OAI_CONFIG_LIST,
- file_location=KEY_LOC,
- )
-
financial_tasks = [
"""What are the full names of NVDA and TESLA.""",
"""Give lucky numbers for them.""",
@@ -341,7 +336,7 @@ def test_chats_general():
)
def my_summary_method(recipient, sender, summary_args):
- return recipient.chat_messages[sender][0].get("content", "")
+ return recipient.chat_messages[sender][1].get("content", "")
chat_res = initiate_chats(
[
@@ -351,6 +346,7 @@ def test_chats_general():
"message": financial_tasks[0],
"silent": False,
"summary_method": my_summary_method,
+ "max_turns": 1,
},
{
"sender": user_2,
@@ -366,6 +362,7 @@ def test_chats_general():
"message": financial_tasks[2],
"summary_method": "last_msg",
"clear_history": False,
+ "max_turns": 1,
},
{
"sender": user,
@@ -373,6 +370,7 @@ def test_chats_general():
"message": writing_tasks[0],
"carryover": "I want to include a figure or a table of data in the blogpost.",
"summary_method": "last_msg",
+ "max_turns": 2,
},
]
)
@@ -390,11 +388,6 @@ def test_chats_general():
@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_chats_exceptions():
- config_list = autogen.config_list_from_json(
- OAI_CONFIG_LIST,
- file_location=KEY_LOC,
- )
-
financial_tasks = [
"""What are the full names of NVDA and TESLA.""",
"""Give lucky numbers for them.""",
@@ -442,12 +435,14 @@ def test_chats_exceptions():
"message": financial_tasks[0],
"silent": False,
"summary_method": "last_msg",
+ "max_turns": 1,
},
{
"recipient": financial_assistant_2,
"message": financial_tasks[2],
"summary_method": "llm",
"clear_history": False,
+ "max_turns": 1,
},
]
)
@@ -462,12 +457,14 @@ def test_chats_exceptions():
"message": financial_tasks[0],
"silent": False,
"summary_method": "last_msg",
+ "max_turns": 1,
},
{
"recipient": user_2,
"message": financial_tasks[2],
"clear_history": False,
"summary_method": "reflection_with_llm",
+ "max_turns": 1,
},
]
)
@@ -475,11 +472,6 @@ def test_chats_exceptions():
@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_chats_w_func():
- config_list = autogen.config_list_from_json(
- OAI_CONFIG_LIST,
- file_location=KEY_LOC,
- )
-
llm_config = {
"config_list": config_list,
"timeout": 120,
@@ -536,9 +528,6 @@ def test_chats_w_func():
@pytest.mark.skipif(skip_openai, reason="requested to skip openai tests")
def test_udf_message_in_chats():
- import autogen
-
- config_list = autogen.config_list_from_json(env_or_file="OAI_CONFIG_LIST")
llm_config = {"config_list": config_list}
research_task = """
@@ -601,6 +590,7 @@ def test_udf_message_in_chats():
"message": research_task,
"clear_history": True,
"silent": False,
+ "max_turns": 2,
},
{
"sender": user_proxy_auto,
@@ -617,8 +607,8 @@ def test_udf_message_in_chats():
if __name__ == "__main__":
- test_chats()
- # test_chats_general()
+ # test_chats()
+ test_chats_general()
# test_chats_exceptions()
# test_chats_group()
# test_chats_w_func()
diff --git a/test/agentchat/test_conversable_agent.py b/test/agentchat/test_conversable_agent.py
index 5c5676dd1..6d9c1ac32 100755
--- a/test/agentchat/test_conversable_agent.py
+++ b/test/agentchat/test_conversable_agent.py
@@ -985,11 +985,11 @@ def test_function_registration_e2e_sync() -> None:
# With 'await', the async function is executed and the current function is paused until the awaited function returns a result.
user_proxy.initiate_chat( # noqa: F704
coder,
- message="Create a timer for 2 seconds and then a stopwatch for 3 seconds.",
+ message="Create a timer for 1 second and then a stopwatch for 2 seconds.",
)
- timer_mock.assert_called_once_with(num_seconds="2")
- stopwatch_mock.assert_called_once_with(num_seconds="3")
+ timer_mock.assert_called_once_with(num_seconds="1")
+ stopwatch_mock.assert_called_once_with(num_seconds="2")
@pytest.mark.skipif(
@@ -1063,11 +1063,11 @@ async def test_function_registration_e2e_async() -> None:
# With 'await', the async function is executed and the current function is paused until the awaited function returns a result.
await user_proxy.a_initiate_chat( # noqa: F704
coder,
- message="Create a timer for 4 seconds and then a stopwatch for 5 seconds.",
+ message="Create a timer for 1 second and then a stopwatch for 2 seconds.",
)
- timer_mock.assert_called_once_with(num_seconds="4")
- stopwatch_mock.assert_called_once_with(num_seconds="5")
+ timer_mock.assert_called_once_with(num_seconds="1")
+ stopwatch_mock.assert_called_once_with(num_seconds="2")
@pytest.mark.skipif(skip_openai, reason=REASON)
diff --git a/test/agentchat/test_human_input.py b/test/agentchat/test_human_input.py
index 7668c9f67..52686232b 100755
--- a/test/agentchat/test_human_input.py
+++ b/test/agentchat/test_human_input.py
@@ -8,17 +8,10 @@ import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
-from conftest import skip_openai # noqa: E402
+from conftest import skip_openai as skip # noqa: E402
-try:
- from openai import OpenAI
-except ImportError:
- skip = True
-else:
- skip = False or skip_openai
-
[email protected](skip, reason="openai not installed OR requested to skip")
[email protected](skip, reason="requested to skip")
def test_get_human_input():
config_list = autogen.config_list_from_json(OAI_CONFIG_LIST, KEY_LOC)
@@ -34,8 +27,6 @@ def test_get_human_input():
# Use MagicMock to create a mock get_human_input function
user_proxy.get_human_input = MagicMock(return_value="This is a test")
- user_proxy.register_reply([autogen.Agent, None], autogen.ConversableAgent.a_check_termination_and_human_reply)
-
res = user_proxy.initiate_chat(assistant, clear_history=True, message="Hello.")
print("Result summary:", res.summary)
print("Human input:", res.human_input)