From 6018fe5872a650a906813167e6a33b5adf449247 Mon Sep 17 00:00:00 2001 From: Eduardo Chiarotti Date: Thu, 27 Jun 2024 02:25:39 -0300 Subject: [PATCH] feat: add CodeInterpreterTool to run when enable code execution (#804) * feat: add CodeInterpreterTool to run when enable code execution is allowed on agent * feat: change to allow_code_execution * feat: add readme for CodeInterpreterTool --- docs/tools/CodeInterpreterTool.md | 29 +++++++++++++++++++++++++++++ src/crewai/agent.py | 9 +++++++++ 2 files changed, 38 insertions(+) create mode 100644 docs/tools/CodeInterpreterTool.md diff --git a/docs/tools/CodeInterpreterTool.md b/docs/tools/CodeInterpreterTool.md new file mode 100644 index 000000000..e66a82e39 --- /dev/null +++ b/docs/tools/CodeInterpreterTool.md @@ -0,0 +1,29 @@ +# CodeInterpreterTool + +## Description +This tool is used to give the Agent the ability to run code (Python3) from the code generated by the Agent itself. The code is executed in a sandboxed environment, so it is safe to run any code. + +It is incredible useful since it allows the Agent to generate code, run it in the same environment, get the result and use it to make decisions. + +## Requirements + +- Docker + +## Installation +Install the crewai_tools package +```shell +pip install 'crewai[tools]' +``` + +## Example + +Remember that when using this tool, the code must be generated by the Agent itself. The code must be a Python3 code. And it will take some time for the first time to run because it needs to build the Docker image. + +```python +from crewai_tools import CodeInterpreterTool + +Agent( + ... + tools=[CodeInterpreterTool()], +) +``` diff --git a/src/crewai/agent.py b/src/crewai/agent.py index e4912db50..40524bbd9 100644 --- a/src/crewai/agent.py +++ b/src/crewai/agent.py @@ -133,6 +133,9 @@ class Agent(BaseModel): response_template: Optional[str] = Field( default=None, description="Response format for the agent." ) + allow_code_execution: Optional[bool] = Field( + default=False, description="Enable code execution for the agent." + ) _original_role: str | None = None _original_goal: str | None = None @@ -409,6 +412,12 @@ class Agent(BaseModel): tools_list.append(tool.to_langchain()) else: tools_list.append(tool) + + if self.allow_code_execution: + from crewai_tools.code_interpreter_tool import CodeInterpreterTool + + tools_list.append(CodeInterpreterTool) + except ModuleNotFoundError: for tool in tools: tools_list.append(tool)