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

Refactor CodeInterpreterTool to Support Pluggable Container Managers and Add Podman Support #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tavernari
Copy link
Contributor

Note

All these changes were made by o1-preview model from OpenAI.
https://chatgpt.com/share/66e8e625-942c-800d-aec3-ea363441fd5c

Overview

This PR refactors the CodeInterpreterTool to make it more flexible and extensible by:

  • Extracting container management logic into separate classes (DockerContainer and PodmanContainer) that implement a common ContainerManager interface.
  • Allowing users to specify the container strategy (Docker, Podman, or custom) when instantiating CodeInterpreterTool.
  • Updating the README to explain how to use the new container strategy mechanism.
  • Adjusting import statements to match the package's directory structure.

These changes enable the CodeInterpreterTool to work with different container technologies and allow users to implement custom container management strategies if needed.

Changes

1. Introduced ContainerManager Interface

Created an abstract base class ContainerManager that defines the interface for container managers:

# container_manager.py
from abc import ABC, abstractmethod
from typing import List

class ContainerManager(ABC):
    @abstractmethod
    def verify_image(self):
        pass

    @abstractmethod
    def init_container(self):
        pass

    @abstractmethod
    def install_libraries(self, libraries: List[str]):
        pass

    @abstractmethod
    def run_code(self, code: str) -> str:
        pass

    @abstractmethod
    def cleanup(self):
        pass

2. Extracted Docker Logic into DockerContainer

Moved all Docker-related operations into a separate class DockerContainer that implements ContainerManager:

# docker_container.py
import os
from typing import List, Optional
import docker
from .container_manager import ContainerManager

class DockerContainer(ContainerManager):
    # Implementation details...

3. Added PodmanContainer for Podman Support

Implemented a PodmanContainer class to support Podman, following the same interface:

# podman_container.py
import os
from typing import List, Optional
from podman import PodmanClient
from podman.errors import NotFound, ImageNotFound
from .container_manager import ContainerManager

class PodmanContainer(ContainerManager):
    # Implementation details...

4. Updated CodeInterpreterTool to Use ContainerManager

Modified CodeInterpreterTool to accept a container_manager parameter, allowing users to specify which container strategy to use:

# code_interpreter_tool.py
from typing import Union, Type
from .container_manager import ContainerManager
from .docker_container import DockerContainer  # Default container manager

class CodeInterpreterTool(BaseTool):
    # ...

    def __init__(
        self,
        container_manager: Union[Type[ContainerManager], ContainerManager] = DockerContainer,
        **kwargs
    ):
        # Implementation details...

5. Updated README

Revised the README to explain how to use the new container strategy mechanism, including examples for using Docker, Podman, and custom container managers.

6. Adjusted Import Statements

Modified import statements to reflect the correct package structure and directory hierarchy, ensuring that modules are imported correctly within the crewai_tools package.

Benefits

  • Flexibility: Users can now choose between Docker and Podman or implement their own container managers.
  • Extensibility: By using the ContainerManager interface, it's easy to add support for other container technologies in the future.
  • Separation of Concerns: Extracting container logic into separate classes improves code organization and maintainability.
  • Customizability: Users can implement custom container management logic to suit their specific needs.

Usage Examples

Using Docker (Default)

from crewai_tools import CodeInterpreterTool

Agent(
    ...
    tools=[CodeInterpreterTool()],
)

Using Podman

from crewai_tools import CodeInterpreterTool
from crewai_tools.tools.code_interpreter_tool.podman_container import PodmanContainer

Agent(
    ...
    tools=[CodeInterpreterTool(container_manager=PodmanContainer)],
)

Using a Custom Container Manager

from crewai_tools.tools.code_interpreter_tool.container_manager import ContainerManager

class CustomContainerManager(ContainerManager):
    # Implement required methods...

Agent(
    ...
    tools=[CodeInterpreterTool(container_manager=CustomContainerManager)],
)

Testing and Compatibility

  • Tested the CodeInterpreterTool with both Docker and Podman container managers.
  • Ensured that existing functionality remains unchanged when using the default Docker container manager.
  • Verified that custom container managers can be integrated without issues.

Notes

  • Dependencies:
    • For Docker support, ensure the docker Python library is installed (pip install docker).
    • For Podman support, ensure the podman Python library is installed (pip install podman).
  • Directory Structure:
    • Adjusted import statements based on the directory structure and __init__.py files.
    • All container manager classes are located in crewai_tools/tools/code_interpreter_tool/.

Conclusion

This PR enhances the CodeInterpreterTool by making it more flexible and extensible. Users can now choose their preferred container technology or implement custom container managers, improving adaptability to different environments and use cases.

Implemented the strategy pattern to allow selection between Docker and Podman for container management in `CodeInterpreterTool`. Updated the README to reflect these changes, including:

- Enhanced usage instructions for specifying container strategies (Docker or Podman).
- Added examples demonstrating how to select Podman as the container manager.
- Provided guidance on creating custom container managers using the strategy pattern.
- Improved the overall documentation for clarity and extensibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants