Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
olgavrou committed Jan 5, 2024
1 parent b082678 commit dad9854
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
pip install -e. pytest mock
- name: Set AUTOGEN_USE_DOCKER based on OS
run: |
if ($env:matrix_os -eq "macos-latest" -or $env:matrix_os -eq "windows-latest") {
if ($env:matrix_os -eq "macos-latest" -or $env:matrix_os -eq "windows-2019") {
"AUTOGEN_USE_DOCKER=False" | Out-File -Append -FilePath $env:GITHUB_ENV
} # Add other conditions or an else clause as needed
shell: pwsh # Specify pwsh as the shell for this step
Expand Down
48 changes: 6 additions & 42 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
DEFAULT_MODEL,
UNKNOWN,
content_str,
check_use_docker,
decide_use_docker,
execute_code,
extract_code,
infer_lang,
is_docker_running,
in_docker_container,
)

from ..function_utils import get_function_schema, load_basemodels_if_needed, serialize_to_str
from .agent import Agent
from .._pydantic import model_dump
Expand All @@ -38,45 +39,6 @@ def colored(x, *args, **kwargs):
F = TypeVar("F", bound=Callable[..., Any])


def check_and_decide_use_docker(code_execution_config: Dict) -> bool:
if "use_docker" in code_execution_config:
use_docker = code_execution_config["use_docker"]
else:
env_var_use_docker = os.environ.get("AUTOGEN_USE_DOCKER", "True")

truthy_values = {"1", "true", "yes", "t"}
falsy_values = {"0", "false", "no", "f"}

# Convert the value to lowercase for case-insensitive comparison
env_var_use_docker_lower = env_var_use_docker.lower()

# Determine the boolean value based on the environment variable
if env_var_use_docker_lower in truthy_values:
use_docker = True
elif env_var_use_docker_lower in falsy_values:
use_docker = False
elif env_var_use_docker_lower == "none": # Special case for 'None' as a string
use_docker = None
else:
# Raise an error for any unrecognized value
raise ValueError(
f'Invalid value for AUTOGEN_USE_DOCKER: {env_var_use_docker}. Please set AUTOGEN_USE_DOCKER to "1/True/yes", "0/False/no", or "None".'
)

if use_docker is not None:
inside_docker = in_docker_container()
docker_installed_and_running = is_docker_running()
if use_docker and not inside_docker and not docker_installed_and_running:
raise RuntimeError(
'Docker is not running, please make sure docker is running (advised approach for code execution) or set "use_docker":False.'
)
if not use_docker:
logger.warning(
'use_docker was set to False. Any code execution will be run natively but we strongly advise to set "use_docker":True. Set "use_docker":None to silence this message.'
)
return use_docker


class ConversableAgent(Agent):
"""(In preview) A class for generic conversable agents which can be configured as assistant or user proxy.
Expand Down Expand Up @@ -180,7 +142,9 @@ def __init__(
)

if isinstance(self._code_execution_config, Dict):
use_docker = check_and_decide_use_docker(self._code_execution_config)
use_docker = self._code_execution_config.get("use_docker", None)
use_docker = decide_use_docker(use_docker)
check_use_docker(use_docker)
self._code_execution_config["use_docker"] = use_docker
print(f"Code execution is set to use docker: {use_docker}")

Expand Down
55 changes: 46 additions & 9 deletions autogen/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
except ImportError:
docker = None

SENTINEL = object()
DEFAULT_MODEL = "gpt-4"
FAST_MODEL = "gpt-3.5-turbo"
# Regular expression for finding a code block
Expand Down Expand Up @@ -254,12 +255,51 @@ def in_docker_container():
return os.path.exists("/.dockerenv")


def decide_use_docker(use_docker) -> bool:
if use_docker is None:
env_var_use_docker = os.environ.get("AUTOGEN_USE_DOCKER", "True")

truthy_values = {"1", "true", "yes", "t"}
falsy_values = {"0", "false", "no", "f"}

# Convert the value to lowercase for case-insensitive comparison
env_var_use_docker_lower = env_var_use_docker.lower()

# Determine the boolean value based on the environment variable
if env_var_use_docker_lower in truthy_values:
use_docker = True
elif env_var_use_docker_lower in falsy_values:
use_docker = False

Check warning on line 272 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L272

Added line #L272 was not covered by tests
elif env_var_use_docker_lower == "none": # Special case for 'None' as a string
use_docker = None

Check warning on line 274 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L274

Added line #L274 was not covered by tests
else:
# Raise an error for any unrecognized value
raise ValueError(

Check warning on line 277 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L277

Added line #L277 was not covered by tests
f'Invalid value for AUTOGEN_USE_DOCKER: {env_var_use_docker}. Please set AUTOGEN_USE_DOCKER to "1/True/yes", "0/False/no", or "None".'
)
return use_docker


def check_use_docker(use_docker) -> None:
if use_docker is not None:
inside_docker = in_docker_container()
docker_installed_and_running = is_docker_running()
if use_docker and not inside_docker and not docker_installed_and_running:
raise RuntimeError(

Check warning on line 288 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L288

Added line #L288 was not covered by tests
'Docker is not running, please make sure docker is running (advised approach for code execution) or set "use_docker":False.'
)
if not use_docker:
logger.warning(
'use_docker was set to False. Any code execution will be run natively but we strongly advise to set "use_docker":True. Set "use_docker":None to silence this message.'
)


def execute_code(
code: Optional[str] = None,
timeout: Optional[int] = None,
filename: Optional[str] = None,
work_dir: Optional[str] = None,
use_docker: Union[List[str], str, bool] = True,
use_docker: Union[List[str], str, bool] = SENTINEL,
lang: Optional[str] = "python",
) -> Tuple[int, str, str]:
"""Execute code in a docker container.
Expand Down Expand Up @@ -302,16 +342,14 @@ def execute_code(
logger.error(error_msg)
raise AssertionError(error_msg)

# # Warn if use_docker was set to False
# # In this case the current behavior is to fall back to run natively, but this behavior
# # is subject to change.
running_inside_docker = in_docker_container()
docker_running = is_docker_running()

if use_docker is not None and not use_docker and not running_inside_docker:
logger.warning(
"execute_code was called with use_docker set to False. Code will be run natively but we strongly advise to set use_docker=True. Set use_docker=None to silence this message."
)
print(f"BEFORE check_and_decide_use_docker: {use_docker}")
if use_docker is SENTINEL:
use_docker = decide_use_docker(use_docker=None)

Check warning on line 350 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L350

Added line #L350 was not covered by tests
print(f"AFTER check_and_decide_use_docker: {use_docker}")
check_use_docker(use_docker)

timeout = timeout or DEFAULT_TIMEOUT
original_filename = filename
Expand Down Expand Up @@ -377,7 +415,6 @@ def execute_code(

# create a docker client
if use_docker and not docker_running:
print("******** docker not running raising runtime error ********")
raise RuntimeError(

Check warning on line 418 in autogen/code_utils.py

View check run for this annotation

Codecov / codecov/patch

autogen/code_utils.py#L418

Added line #L418 was not covered by tests
"Docker package is missing or docker is not running. Please make sure docker is running or set use_docker=False."
)
Expand Down

0 comments on commit dad9854

Please sign in to comment.