Skip to content

mirascope.tools.system._docker_operation

DockerOperationToolKitConfig

Bases: _ConfigurableToolConfig

Configuration for DockerOperationToolKit toolkit

DockerOperation

Bases: ConfigurableTool[DockerOperationToolKitConfig], ABC

Base class for Docker operations.

DockerContainer

Bases: BaseModel

DockerOperationToolKit

Bases: ConfigurableToolKit[DockerOperationToolKitConfig]

ToolKit for executing Python code and shell commands in a Docker container.

ExecutePython

Bases: DockerOperation

Tool for executing Python code in a Docker container.

call

call() -> str

Executes Python code in a Docker container.

docker_image: {self.config.docker_image} allow_network: {self.config.allow_network}

Returns:

Name Type Description
str str

Output of the code execution

Source code in mirascope/tools/system/_docker_operation.py
def call(self) -> str:
    """Executes Python code in a Docker container.

    docker_image: {self.__config__.docker_image}
    allow_network: {self.__config__.allow_network}

    Returns:
        str: Output of the code execution
    """
    try:
        contents = {
            "main.py": self.code,
        }
        if self.requirements:
            if not self._get_config().allow_network:
                return "Error: Network access is disabled. Cannot install requirements via pip."
            contents["requirements.txt"] = "\n".join(self.requirements)
        stream = self._create_tar_stream(contents)
        self._docker_container.container.put_archive("/", stream)
        if self.requirements:
            exit_code, output = self._docker_container.container.exec_run(
                cmd=["pip", "install", "-r", "/requirements.txt"],
            )
            if exit_code != 0:  # pragma: no cover
                return (
                    f"Error installing requirements: {output.decode('utf-8')}"
                )
        exit_code, output = self._docker_container.container.exec_run(
            cmd=["python", "/main.py"],
        )
        return output.decode("utf-8")

    except Exception as e:
        return f"Error executing code: {str(e)}"

ExecuteShell

Bases: DockerOperation

Tool for executing shell commands in a Docker container.

call

call() -> str

Executes shell commands in a Docker container.

docker_image: {self.config.docker_image} allow_network: {self.config.allow_network}

Returns:

Name Type Description
str str

Output of the command execution

Source code in mirascope/tools/system/_docker_operation.py
def call(self) -> str:
    """Executes shell commands in a Docker container.

    docker_image: {self.__config__.docker_image}
    allow_network: {self.__config__.allow_network}

    Returns:
        str: Output of the command execution
    """
    try:
        exec_result = self._docker_container.container.exec_run(
            cmd=["sh", "-c", self.command],
        )
        return exec_result.output.decode("utf-8")
    except Exception as e:  # pragma: no cover
        return f"Error executing command: {str(e)}"

create_tools

create_tools() -> list[type[BaseTool]]

The method to create the tools.

Source code in mirascope/tools/system/_docker_operation.py
def create_tools(self) -> list[type[BaseTool]]:
    """The method to create the tools."""

    tools = super().create_tools()
    for tool in tools:
        if issubclass(tool, DockerOperation):
            tool._docker_container = self.docker_container
    return tools