Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type and default value of the code_execution_config parameter in UserProxyAgent #1996

Merged
merged 7 commits into from
Mar 13, 2024
6 changes: 6 additions & 0 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def __init__(
description (str): a short description of the agent. This description is used by other agents
(e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message)
"""
# we change code_execution_config below and we have to make sure we don't change the input
# in case of UserProxyAgent, without this we could even change the default value {}
code_execution_config = (
code_execution_config.copy() if hasattr(code_execution_config, "copy") else code_execution_config
)

self._name = name
# a dictionary of conversations, default value is list
self._oai_messages = defaultdict(list)
Expand Down
2 changes: 1 addition & 1 deletion autogen/agentchat/user_proxy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
max_consecutive_auto_reply: Optional[int] = None,
human_input_mode: Literal["ALWAYS", "TERMINATE", "NEVER"] = "ALWAYS",
function_map: Optional[Dict[str, Callable]] = None,
code_execution_config: Optional[Union[Dict, Literal[False]]] = None,
code_execution_config: Union[Dict, Literal[False]] = {},
default_auto_reply: Optional[Union[str, Dict, None]] = "",
llm_config: Optional[Union[Dict, Literal[False]]] = False,
system_message: Optional[Union[str, List]] = "",
Expand Down
45 changes: 15 additions & 30 deletions test/agentchat/test_agent_setup_with_use_docker_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
skip = False or skip_openai


def get_current_autogen_env_var():
return os.environ.get("AUTOGEN_USE_DOCKER", None)


def restore_autogen_env_var(current_env_value):
if current_env_value is None:
del os.environ["AUTOGEN_USE_DOCKER"]
else:
os.environ["AUTOGEN_USE_DOCKER"] = current_env_value


def docker_running():
return is_docker_running() or in_docker_container()

Expand Down Expand Up @@ -54,32 +43,36 @@ def test_agent_setup_with_use_docker_false():


@pytest.mark.skipif(skip, reason="openai not installed")
def test_agent_setup_with_env_variable_false_and_docker_running():
current_env_value = get_current_autogen_env_var()
def test_agent_setup_with_env_variable_false_and_docker_running(monkeypatch):
monkeypatch.setenv("AUTOGEN_USE_DOCKER", "False")

os.environ["AUTOGEN_USE_DOCKER"] = "False"
user_proxy = UserProxyAgent(
name="test_agent",
human_input_mode="NEVER",
)

assert user_proxy._code_execution_config["use_docker"] is False

restore_autogen_env_var(current_env_value)


@pytest.mark.skipif(skip or (not docker_running()), reason="openai not installed OR docker not running")
def test_agent_setup_with_default_and_docker_running():
def test_agent_setup_with_default_and_docker_running(monkeypatch):
monkeypatch.delenv("AUTOGEN_USE_DOCKER", raising=False)

assert os.getenv("AUTOGEN_USE_DOCKER") is None

user_proxy = UserProxyAgent(
name="test_agent",
human_input_mode="NEVER",
)

assert os.getenv("AUTOGEN_USE_DOCKER") is None

assert user_proxy._code_execution_config["use_docker"] is True


@pytest.mark.skipif(skip or (docker_running()), reason="openai not installed OR docker running")
def test_raises_error_agent_setup_with_default_and_docker_not_running():
def test_raises_error_agent_setup_with_default_and_docker_not_running(monkeypatch):
monkeypatch.delenv("AUTOGEN_USE_DOCKER", raising=False)
with pytest.raises(RuntimeError):
UserProxyAgent(
name="test_agent",
Expand All @@ -88,31 +81,23 @@ def test_raises_error_agent_setup_with_default_and_docker_not_running():


@pytest.mark.skipif(skip or (docker_running()), reason="openai not installed OR docker running")
def test_raises_error_agent_setup_with_env_variable_true_and_docker_not_running():
current_env_value = get_current_autogen_env_var()

os.environ["AUTOGEN_USE_DOCKER"] = "True"
def test_raises_error_agent_setup_with_env_variable_true_and_docker_not_running(monkeypatch):
monkeypatch.setenv("AUTOGEN_USE_DOCKER", "True")

with pytest.raises(RuntimeError):
UserProxyAgent(
name="test_agent",
human_input_mode="NEVER",
)

restore_autogen_env_var(current_env_value)


@pytest.mark.skipif(skip or (not docker_running()), reason="openai not installed OR docker not running")
def test_agent_setup_with_env_variable_true_and_docker_running():
current_env_value = get_current_autogen_env_var()

os.environ["AUTOGEN_USE_DOCKER"] = "True"
def test_agent_setup_with_env_variable_true_and_docker_running(monkeypatch):
monkeypatch.setenv("AUTOGEN_USE_DOCKER", "True")

user_proxy = UserProxyAgent(
name="test_agent",
human_input_mode="NEVER",
)

assert user_proxy._code_execution_config["use_docker"] is True

restore_autogen_env_var(current_env_value)
Loading