Merge pull request #70 from joaomdmoura/fix/code-interpreter-description

fix: fix type hinting, add container name and handle exception and returned old description
This commit is contained in:
João Moura
2024-07-04 12:59:29 -07:00
committed by GitHub

View File

@@ -1,6 +1,5 @@
import os
import importlib.util import importlib.util
import textwrap import os
from typing import List, Optional, Type from typing import List, Optional, Type
import docker import docker
@@ -10,15 +9,18 @@ from pydantic.v1 import BaseModel, Field
class CodeInterpreterSchema(BaseModel): class CodeInterpreterSchema(BaseModel):
"""Input for CodeInterpreterTool.""" """Input for CodeInterpreterTool."""
code: str = Field( code: str = Field(
..., ...,
description="Mandatory string of python3 code used to be interpreted with a final print statement.", description="Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code",
) )
dependencies_used_in_code: List[str] = Field(
libraries_used: List[str] = Field(
..., ...,
description="Mandatory list of libraries used in the code with proper installing names.", description="List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4",
) )
class CodeInterpreterTool(BaseTool): class CodeInterpreterTool(BaseTool):
name: str = "Code Interpreter" name: str = "Code Interpreter"
description: str = "Interprets Python3 code strings with a final print statement." description: str = "Interprets Python3 code strings with a final print statement."
@@ -27,7 +29,7 @@ class CodeInterpreterTool(BaseTool):
@staticmethod @staticmethod
def _get_installed_package_path(): def _get_installed_package_path():
spec = importlib.util.find_spec('crewai_tools') spec = importlib.util.find_spec("crewai_tools")
return os.path.dirname(spec.origin) return os.path.dirname(spec.origin)
def _verify_docker_image(self) -> None: def _verify_docker_image(self) -> None:
@@ -36,11 +38,13 @@ class CodeInterpreterTool(BaseTool):
""" """
image_tag = "code-interpreter:latest" image_tag = "code-interpreter:latest"
client = docker.from_env() client = docker.from_env()
try: try:
client.images.get(image_tag) client.images.get(image_tag)
except:
except docker.errors.ImageNotFound:
package_path = self._get_installed_package_path() package_path = self._get_installed_package_path()
dockerfile_path = os.path.join(package_path, 'tools/code_interpreter_tool') dockerfile_path = os.path.join(package_path, "tools/code_interpreter_tool")
if not os.path.exists(dockerfile_path): if not os.path.exists(dockerfile_path):
raise FileNotFoundError(f"Dockerfile not found in {dockerfile_path}") raise FileNotFoundError(f"Dockerfile not found in {dockerfile_path}")
@@ -52,7 +56,7 @@ class CodeInterpreterTool(BaseTool):
def _run(self, **kwargs) -> str: def _run(self, **kwargs) -> str:
code = kwargs.get("code", self.code) code = kwargs.get("code", self.code)
libraries_used = kwargs.get("dependencies_used_in_code", []) libraries_used = kwargs.get("libraries_used", [])
return self.run_code_in_docker(code, libraries_used) return self.run_code_in_docker(code, libraries_used)
def _install_libraries( def _install_libraries(
@@ -67,10 +71,14 @@ class CodeInterpreterTool(BaseTool):
def _init_docker_container(self) -> docker.models.containers.Container: def _init_docker_container(self) -> docker.models.containers.Container:
client = docker.from_env() client = docker.from_env()
return client.containers.run( return client.containers.run(
"code-interpreter", detach=True, tty=True, working_dir="/workspace" "code-interpreter",
detach=True,
tty=True,
working_dir="/workspace",
name="code-interpreter",
) )
def run_code_in_docker(self, code: str, libraries_used: str) -> str: def run_code_in_docker(self, code: str, libraries_used: List[str]) -> str:
self._verify_docker_image() self._verify_docker_image()
container = self._init_docker_container() container = self._init_docker_container()
self._install_libraries(container, libraries_used) self._install_libraries(container, libraries_used)