crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] CodeInterpreterTool Error while fetching server API version: (2, 'CreateFile', 'The system cannot find the file specified.')

Open hliuci opened this issue 1 year ago • 6 comments

Description

Hi, I am using Crew AI to set up an agent to convert JSON data to an Excel file and save it in the current directory. I used CodeInterpreterTool for the setup. When I run the agent, it showed (the same error exists even with unsafe mode):

I encountered an error while trying to use the tool. This was the error: Error while fetching server API version: (2, 'CreateFile', 'The system cannot find the file specified.').
Tool Code Interpreter accepts these inputs: Code Interpreter(code: 'string', libraries_used: 'array') - Interprets Python3 code strings with a final print statement. code: 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', libraries_used: 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4'

And the final answer is:

Below is the Python script that would accomplish the task of converting the JSON data to an Excel file, given a functioning Python environment with pandas installed:

python
import pandas as pd
...

This script first flattens the JSON data into a list of dictionaries, each representing a row in the final dataframe. Then it uses pandas to convert this list into a dataframe. Finally, it writes this dataframe to an Excel file named 'output.xlsx' in the current working directory. To execute this script, one would need a Python interpreter and the pandas library installed on their system.

It seems that the codes were not executed to generate an Excel file.

Steps to Reproduce

The error occured when running the codes below.

Expected behavior

An Excel file is generated and saved in the current directory.

Screenshots/Code snippets

from crewai import Agent, Task, Crew
from crewai_tools import CodeInterpreterTool

code_interpreter_tool = CodeInterpreterTool(libraries_used = ['pandas', 'openpyxl', 'json', 'ast', 'os'])

coding_agent = Agent(
    role="Python software developer",
    goal="Analyze the input data {input} and do file conversion using Python",
    backstory="You are an experienced software developer with strong Python skills.",
    allow_code_execution=True,
    verbose=True,
    allow_delegation=False,
    llm="azure/gpt-4-1106-preview",
    tools = [code_interpreter_tool]
)

json_to_excel_task = Task(
    description="Write Python codes to convert the input JSON data {input} to Excel file and save to the current directory",
    expected_output="A saved Excel file with the data from the input JSON data {input}",
    agent=coding_agent
)

analysis_crew = Crew(
    agents=[coding_agent],
    tasks=[json_to_excel_task],
    verbose=True
)

nested_json = '''{
    "Kitchen": {
        "temperature": 30,
        "adjacent_room": {
            "Toilet": {
                "temperature": 24,
                "shared_wall_area": 13.48
            },
            "Living_Rm1": {
                "temperature": 27,
                "shared_wall_area": 21.6
            }
        }
    },
    "Toilet": {
        "temperature": 24,
        "adjacent_room": {
            "Kitchen": {
                "temperature": 30,
                "shared_wall_area": 42.6
            },
            "Living_Rm1": {
                "temperature": 27,
                "shared_wall_area": 21.6
            }
        }
    }
}'''

result = analysis_crew.kickoff(inputs={"input": nested_json})
print(result)

Operating System

Windows 11

Python Version

3.10

crewAI Version

0.70.1

crewAI Tools Version

0.12.1

Virtual Environment

Venv

Evidence

image

Possible Solution

None

Additional context

NA

hliuci avatar Nov 04 '24 09:11 hliuci

I don't know if it's related but I can't make the CodeInterpreterTool work either. That said, I don't want to hijack the OP, just maybe complete the debugging info.

For simplicity, I have everything in a single python file:

import os
from crewai import Agent, Task, Crew, Process, LLM
from crewai_tools import SerperDevTool, SeleniumScrapingTool, ScrapeWebsiteTool, FileReadTool, FileWriterTool, CodeInterpreterTool, ScrapeWebsiteTool

os.environ["SERPER_API_KEY"] = "XXXXX"

llm_researcher = LLM(model="ollama_chat/qwen2.5:32b-instruct-q4_K_M_11k", base_url="http://localhost:11434", temperature=0.05)
llm_journalist = LLM(model="ollama_chat/qwen2.5:32b-instruct-q4_K_M_11k", base_url="http://localhost:11434", temperature=0.05)

researcher = Agent(
  role='Web Researcher',
  goal='Find any information on the web.',
  backstory="""You work at a leading web monitoring company.
  You have a knack for dissecting complex data and presenting it.""",
  verbose=True,
  allow_delegation=False,
  llm=llm_researcher,
  tools=[SerperDevTool(), SeleniumScrapingTool(), FileWriterTool(), FileReadTool()]
)

coder = Agent(
  role='Coder',
  goal='Solve coding problems and automate tasks.',
  backstory="""You are an experienced coder working at a leading BigTech company.
  You have a knack for writing efficient code to solve complex data problems.""",
  verbose=True,
  allow_delegation=False,
  llm=llm_researcher,
  tools=[CodeInterpreterTool(), FileWriterTool(), FileReadTool()]
)


journalist = Agent(
  role='Journalist',
  goal="""Summarize any received text, regardless of what type it is.
  Go in depth, detail everything.
    Organize your summary using headings and subheadings, paragraphs, text styles, lists, quotes, or tables as needed to make the content easy to read.
    Adapt the vocabulary so that it is accessible to anyone.""",
    backstory="""You are a renowned rigorous journalist known for insightful and engaging articles. You excel at transforming complex concepts into compelling narratives.""",
  verbose=True,
  allow_delegation=False,
  llm=llm_journalist,
  tools=[FileWriterTool(), FileReadTool()]
)

task1 = Task(
  description="Get the current date/time by executing a minimal python script.",
  expected_output="""The current date.""",
  agent=coder
)

task2 = Task(
  description="Keeping in mind the current date obtained from task1, scrape the content from the second to most recent article on xxxx.",
  expected_output="""The raw text of the article.""",
  output_file="""article.txt""",
  agent=researcher
)

task3 = Task(
  description="""Provide a detailed summary of the article contained in article.md.""",
  expected_output="""A summary (formatted in Markdown), along with the sources links which it could eventually contain.""",
  output_file="""summary.md""",
  agent=journalist
)

crew = Crew(
  agents=[researcher, journalist],
  tasks=[task1, task2, task3],
  verbose=True,
  #process = Process.hierarchical,
  #manager_llm = LLM(model="ollama_chat/qwen2.5:32b-instruct-q4_K_M_11k, base_url="http://localhost:11434", temperature=0.05),
  process = Process.sequential
)
result = crew.kickoff()

print("######################")
print(result)

And here is the error I keep on getting:

# Agent: Coder
## Task: Get the current date/time by executing a minimal python script.
 

I encountered an error while trying to use the tool. This was the error: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')).
 Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter
Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}
Tool Description: Interprets Python3 code strings with a final print statement.



# Agent: Coder
## Using tool: Code Interpreter
## Tool Input: 
"{\"code\": \"from datetime import datetime\\nprint(datetime.now())\", \"libraries_used\": []}"
## Tool Output: 

I encountered an error while trying to use the tool. This was the error: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')).
 Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter
Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}
Tool Description: Interprets Python3 code strings with a final print statement..
Moving on then. I MUST either use a tool (use one at time) OR give my best final answer not both at the same time. To Use the following format:

Thought: you should always think about what to do
Action: the action to take, should be one of [Code Interpreter, File Writer Tool, Read a file's content]
Action Input: the input to the action, dictionary enclosed in curly braces
Observation: the result of the action
... (this Thought/Action/Action Input/Result can repeat N times)
Thought: I now can give a great answer
Final Answer: Your final answer must be the great and the most complete as possible, it must be outcome described

 
 

I encountered an error while trying to use the tool. This was the error: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')).
 Tool Code Interpreter accepts these inputs: Tool Name: Code Interpreter
Tool Arguments: {'code': {'description': 'Python3 code used to be interpreted in the Docker container. ALWAYS PRINT the final result and the output of the code', 'type': 'str'}, 'libraries_used': {'description': 'List of libraries used in the code with proper installing names separated by commas. Example: numpy,pandas,beautifulsoup4', 'type': 'list[str]'}}
Tool Description: Interprets Python3 code strings with a final print statement.

I also tried qwen2.5-coder:32b-instruct-q4_K_M without any success. The other tools are working as intended.

blakkd avatar Nov 13 '24 23:11 blakkd

I tried with LLM(model="groq/llama-3.1-70b-versatile", temperature=0.05) but same result

blakkd avatar Nov 15 '24 22:11 blakkd

I also need this solution. any one please help to resolve this.

paarttipaabhalaji avatar Nov 22 '24 12:11 paarttipaabhalaji

I am also getting RuntimeError: Docker is not installed. Please install Docker to use code execution with agent: Coder when set "unsafe"

Expected script behavior: Local endpoint self-host example run with code execution (without Docker):
import os
from crewai import Agent, Task, Crew
from crewai_tools import CodeInterpreterTool

os.environ['OPENAI_API_BASE'] = 'http://127.0.0.1:5000/v1'
os.environ['OPENAI_API_KEY'] = 'sk-1111'

def main():
    agent = Agent(
        role="Coder",
        goal="Execute simple Python code",
        backstory="Python expert",
        tools=[CodeInterpreterTool()],
        code_execution_mode="unsafe",  # Try direct execution
        allow_code_execution=True      # Enable code execution
    )

    task = Task(
        description="Print: x = 2 + 2",
        expected_output="Simple calculation result",
        agent=agent
    )

    crew = Crew(
        agents=[agent],
        tasks=[task],
        verbose=True
    )

    result = crew.kickoff()
    print("\nResult:", result.raw)

if __name__ == "__main__":
    main()
(venv) C:\>no_docker_agent_CodeInterpreter.py
Traceback (most recent call last):
  File "C:\no_docker_agent_CodeInterpreter.py", line 34, in <module>
    main()
  File "C:\no_docker_agent_CodeInterpreter.py", line 9, in main
    agent = Agent(
  File "C:\venv\lib\site-packages\pydantic\main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
  File "C:\venv\lib\site-packages\crewai\agent.py", line 242, in post_init_setup
    self._validate_docker_installation()
  File "C:\venv\lib\site-packages\crewai\agent.py", line 525, in _validate_docker_installation
    raise RuntimeError(
RuntimeError: Docker is not installed. Please install Docker to use code execution with agent: Coder

Modify from docs/how-to/coding-agents.mdx

Without Docker we should set:code_execution_mode="unsafe", allow_code_execution=True as explained by the docs
  • docs/tools/codeinterpretertool.mdx states that it requires Docker: https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/docs/tools/codeinterpretertool.mdx?plain=1#L16-L18

  • But code_execution_mode="unsafe" should allow Direct execution (no Docker) https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/docs/concepts/agents.mdx?plain=1#L233-L236 https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/docs/concepts/agents.mdx?plain=1#L45 https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/src/crewai/agent.py#L125-L127

Along allow_code_execution=True:

https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/docs/concepts/agents.mdx?plain=1#L293-L296 https://github.com/crewAIInc/crewAI/blob/c7c0647dd225d655d928ec9cc96c6b933ad77e0e/docs/concepts/agents.mdx?plain=1#L42

V0.86.0 of crewai - Without allow_code_execution=True we get : I encountered an error while trying to use the tool. This was the error: Error while fetching server API version: (2, 'CreateFile', 'The system cannot find the file specified.').

As a workaround, I used a FileWriterTool and custom ShellTool combo.
import os
from crewai import Agent, Task, Crew
from crewai.tasks.conditional_task import ConditionalTask
from pydantic import BaseModel, Field
from typing import Type
from crewai.tools import BaseTool
from crewai_tools import FileWriterTool

os.environ['OPENAI_API_BASE'] = 'http://127.0.0.1:5000/v1' # ooba API + Qwen2.5-Coder-32B
os.environ['OPENAI_API_KEY'] = 'sk-1111'

class ShellInput(BaseModel):
    command: str = Field(description="Windows command to execute")

class ShellTool(BaseTool): # Custom CMD tool: replaces `DirectoryReadTool` (`dir`) and `FileReadTool` (`more`). Only built-in `FileWriterTool` used to avoids `echo`'s limitations`/n` inconvenient.
    name: str = "shell_executor"
    description: str = "Execute Windows commands. Avoid interactive/GUI commands" # Avoid `type`, `notepad`
    args_schema: Type[BaseModel] = ShellInput
    
    def _run(self, command: str) -> str:
        try:
            import subprocess
            result = subprocess.run(command, shell=True, capture_output=True, text=True)
            return f"Exit Code: {result.returncode}\nOutput:\n{result.stdout}\nErrors:\n{result.stderr}"
        except Exception as e:
            return f"Error: {str(e)}"

def main():
    creator = Agent(
        role="Creator",
        goal="Create digital content",
        backstory="Expert in content creation with access to system commands and file operations",
        tools=[ShellTool(), FileWriterTool()],
        verbose=True
    )

    initial_task = Task(
        description="Create a snake game",  # Could be "Write a book chapter", etc.
        expected_output="Created content",
        agent=creator
    )

    def check_issues(output):
        return "Error:" in output.raw or "Traceback" in output.raw

    fix_task = ConditionalTask(
        description="Fix any issues found",
        expected_output="Fixed content",
        agent=creator,
        condition=check_issues, # ConditionalTask for shell error
        context=[initial_task]
    )

    crew = Crew(
        agents=[creator],
        tasks=[initial_task, fix_task],
        verbose=True,
        planning=True
    )

    try:
        result = crew.kickoff()
        print("\nFinal Output:")
        print("=" * 50)
        print(result.raw)
    except Exception as e:
        print(f"Error: {str(e)}")

if __name__ == "__main__":
    main()
  • Single, autonomous, general-purpose agent (no hierarchy). Agent selects ShellTool or FileWriterTool based on context.
  • I’ve considered SeleniumScrapingTool (JS support), but it only works with URL inputs, not search queries. (I’ve got a try to perplexity.ai-like agent tool; Though more complex, awaiting an official tool.)

Inspired by CLI aider and open-interpreter

Katehuuh avatar Dec 06 '24 16:12 Katehuuh

In case it helps:

I just tried again my same crewai script without touching anything: the issue is fixed on 0.86.0. I didn't set allow_code_execution=True and neither code_execution_mode="unsafe"

(crewai) user@user-PC:~/workspaces/crewai$ /home/user/miniforge3/envs/crewai/bin/python /home/user/workspaces/crewai/TEMPLATES/duo_simple.py
# Agent: Coder
## Task: Get the current date/time by executing a minimal python script.


# Agent: Coder
## Thought: I will use the Code Interpreter tool to execute a Python script that retrieves the current date and time using the datetime module. I'll ensure to print out the result.
## Using tool: Code Interpreter
## Tool Input: 
"{\"code\": \"from datetime import datetime\\nprint(datetime.now())\", \"libraries_used\": []}"
## Tool Output: 
2024-12-07 11:01:03.152320



# Agent: Coder
## Final Answer: 
2024-12-07 11:01:03.152320

I don't know from which release it's been fixed out but given that my case is solved, the issue seems specific to windows.

PS: congrat and thanks to the commiter

blakkd avatar Dec 07 '24 03:12 blakkd

For me, the issue originated from the docker client library.

The docker client could not find my docker daemon, and hence connect to my running docker engine.

The solution was to set the DOCKER_HOST environment variable explicitly to my docker.sock.

You can find the docker.sock file using docker context ls. Then

DOCKER_HOST=unix:<path to docker.sock file>

To test:

from crewai_tools import CodeInterpreterTool

tool = CodeInterpreterTool(user_dockerfile_path='path to dockerfile where code is executed')
r = tool.run()

Hope this helps.

HammamWahab avatar Dec 14 '24 16:12 HammamWahab

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Jan 17 '25 12:01 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Jan 23 '25 12:01 github-actions[bot]