microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
32.52k stars 4.74k forks source link

[Bug]: Autogen Studio: [Errno 2] No such file or directory: 'python', where only `python3` and `pip3` are available #3055

Open robinmanuelthiel opened 4 months ago

robinmanuelthiel commented 4 months ago

Describe the bug

When running Autogen Studio, on macOS, where usually Python is only available as python3 locally in the command line, Function Calling does not work, as when an Agent is trying to execute Python code, the following error occurs: FileNotFoundError: [Errno 2] No such file or directory: 'python'

This make sense, as python is not available in the $PATH of this machine, only python3. Same for pip and pip3.

Steps to reproduce

  1. Run Autogen Studio on a Mac autogenstudio ui --port 8081
  2. Click on Workflows -> Default Workflow
  3. Click on Test Workflow and choose the Stock Price example
  4. Watch the Logs

Model Used

gpt-4-1106-preview

Expected Behavior

Code should be executed or path to python and pip should be configurable.

Screenshots and logs

2024-07-01 15:16:35.243 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **
user_proxy (to default_assistant):

Plot a chart of NVDA and TESLA stock price for 2023. Save the result to a file named nvda_tesla.png

--------------------------------------------------------------------------------
2024-07-01 15:16:35.244 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf
2024-07-01 15:16:47.488 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **
default_assistant (to user_proxy):

2024-07-01 15:16:47.489 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf
To plot a chart of NVDA (NVIDIA Corporation) and TESLA stock prices for 2023, we will need to perform the following steps:

1. Collect stock price data for NVDA and TESLA for the year 2023.
2. Plot the data on a chart.
3. Save the chart to a file named `nvda_tesla.png`.

For step 1, we can use a financial data provider like Yahoo Finance. We can access this data using the `yfinance` Python package. If `yfinance` is not installed, we will need to install it using pip.

For step 2 and 3, we will use the `matplotlib` library to plot the chart and save it to a file. If `matplotlib` is not installed, we will also need to install it using pip.

Let's start by installing the required packages using shell script:

```sh
# Install the required Python packages
pip install yfinance --quiet
pip install matplotlib --quiet
```

After installing the necessary packages, we will write a Python script to fetch the stock data, plot the chart, and save it to a file. Please execute the shell script above to install the required packages before running the Python script that I will provide next.

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK (inferred language is sh)...
user_proxy (to default_assistant):
2024-07-01 15:16:47.522 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **

2024-07-01 15:16:47.522 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf
exitcode: 127 (execution failed)
Code output: /Users/robinmanuelthiel/.autogenstudio/files/user/198fb9b77fb10d10bae092eea5789295/None/20240701_15-16-34/Install the required Python packages: line 2: pip: command not found
/Users/robinmanuelthiel/.autogenstudio/files/user/198fb9b77fb10d10bae092eea5789295/None/20240701_15-16-34/Install the required Python packages: line 3: pip: command not found

--------------------------------------------------------------------------------
default_assistant (to user_proxy):
2024-07-01 15:16:54.581 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **

It seems that the `pip` command is not found in your environment, which could indicate that Python is not installed or `pip` is not in your PATH. To resolve this, we can try using `python -m pip` instead, which should work if Python is installed and accessible.

Let's try installing the required packages using the `python -m pip` command:

```sh
# Install the required Python packages using python -m pip
python -m pip install yfinance --quiet
python -m pip install matplotlib --quiet
```

Please run the above shell script to install the necessary Python packages. If Python is not installed or if you encounter any further issues, please ensure that Python and pip are correctly installed on your system.
2024-07-01 15:16:54.582 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK (inferred language is sh)...
user_proxy (to default_assistant):
2024-07-01 15:16:54.617 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **

2024-07-01 15:16:54.617 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf
exitcode: 127 (execution failed)
Code output: /Users/robinmanuelthiel/.autogenstudio/files/user/198fb9b77fb10d10bae092eea5789295/None/20240701_15-16-34/Install the required Python packages using python -m pip: line 2: python: command not found
/Users/robinmanuelthiel/.autogenstudio/files/user/198fb9b77fb10d10bae092eea5789295/None/20240701_15-16-34/Install the required Python packages using python -m pip: line 3: python: command not found

--------------------------------------------------------------------------------
2024-07-01 15:17:00.044 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **
default_assistant (to user_proxy):
2024-07-01 15:17:00.044 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf

It appears that the `python` command is not recognized in your environment. This could mean that Python is not installed, or it might be installed under a different name such as `python3`. Let's try using `python3 -m pip` to install the packages:

```sh
# Install the required Python packages using python3 -m pip
python3 -m pip install yfinance --quiet
python3 -m pip install matplotlib --quiet
```

Please run the above shell script to attempt the installation of the necessary Python packages. If this still does not work, it may be necessary to verify that Python is correctly installed on your system and accessible from the command line.

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK (inferred language is sh)...
user_proxy (to default_assistant):
2024-07-01 15:17:03.460 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **

2024-07-01 15:17:03.460 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf
exitcode: 1 (execution failed)
Code output: error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: 

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

[notice] A new release of pip is available: 24.0 -> 24.1.1
[notice] To update, run: python3.12 -m pip install --upgrade pip
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a Python library that isn't in Homebrew,
    use a virtual environment:

    python3 -m venv path/to/venv
    source path/to/venv/bin/activate
    python3 -m pip install xyz

    If you wish to install a Python application that isn't in Homebrew,
    it may be easiest to use 'pipx install xyz', which will manage a
    virtual environment for you. You can install pipx with

    brew install pipx

    You may restore the old behavior of pip by passing
    the '--break-system-packages' flag to pip, or by adding
    'break-system-packages = true' to your pip.conf file. The latter
    will permanently disable this error.

    If you disable this error, we STRONGLY recommend that you additionally
    pass the '--user' flag to pip, or set 'user = true' in your pip.conf
    file. Failure to do this can result in a broken Homebrew installation.

    Read more about this behavior here: 

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

[notice] A new release of pip is available: 24.0 -> 24.1.1
[notice] To update, run: python3.12 -m pip install --upgrade pip

--------------------------------------------------------------------------------
default_assistant (to user_proxy):
2024-07-01 15:17:24.982 | INFO     | autogenstudio.web.app:message_handler:38 - ** Processing Agent Message on Queue: Active Connections: ['6c348afa-edfa-494b-ae5c-2ec8e62ddacf'] **
2024-07-01 15:17:24.982 | INFO     | autogenstudio.web.app:message_handler:45 - Sending message to connection_id: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf. Connection ID: 6c348afa-edfa-494b-ae5c-2ec8e62ddacf

The error message indicates that the environment is externally managed and suggests using Homebrew to install system-wide Python packages or using a virtual environment for Python libraries that are not available in Homebrew.

Since we cannot proceed with the installation of the required packages in the current environment, I will provide you with a Python script that assumes the necessary packages (`yfinance` and `matplotlib`) are already installed. You can run this script in an environment where you have the required permissions to install packages or where the packages are already available.

Here is the Python script that fetches the stock data for NVDA and TESLA for the year 2023, plots the chart, and saves it to a file named `nvda_tesla.png`:

```python
# filename: plot_stocks.py
import yfinance as yf
import matplotlib.pyplot as plt

# Define the ticker symbols for NVIDIA and Tesla
nvda_ticker = 'NVDA'
tesla_ticker = 'TSLA'

# Define the start and end dates for the year 2023
start_date = '2023-01-01'
end_date = '2023-12-31'

# Fetch the historical data for both stocks
nvda_data = yf.download(nvda_ticker, start=start_date, end=end_date)
tesla_data = yf.download(tesla_ticker, start=start_date, end=end_date)

# Plot the closing prices of both stocks
plt.figure(figsize=(14, 7))
plt.plot(nvda_data['Close'], label='NVDA')
plt.plot(tesla_data['Close'], label='TESLA')

# Add title and labels to the plot
plt.title('NVDA and TESLA Stock Prices for 2023')
plt.xlabel('Date')
plt.ylabel('Price (USD)')

# Add a legend to the plot
plt.legend()

# Save the plot to a file
plt.savefig('nvda_tesla.png')
print('The chart has been saved as nvda_tesla.png')

# Show the plot
plt.show()
```

Please save the above script to a file named `plot_stocks.py` and run it in an environment where the necessary Python packages are installed. If you are unable to run this script due to the environment restrictions, you may need to set up a proper Python environment with the required permissions first.

--------------------------------------------------------------------------------

>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...
Traceback (most recent call last):
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/web/app.py", line 453, in run_session_workflow
    agent_response: Message = managers["chat"].chat(
                              ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/chatmanager.py", line 79, in chat
    result_message: Message = workflow_manager.run(message=f"{message_text}", clear_history=False, history=history)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 400, in run
    self._run_workflow(message=message, history=history,
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 104, in _run_workflow
    self.sender.initiate_chat(
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 1018, in initiate_chat
    self.send(msg2send, recipient, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 820, in receive
    self.send(reply, sender, silent=silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 655, in send
    recipient.receive(message, self, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogenstudio/workflowmanager.py", line 679, in receive
    super().receive(message, sender, request_reply, silent)
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 818, in receive
    reply = self.generate_reply(messages=self.chat_messages[sender], sender=sender)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 1972, in generate_reply
    final, reply = reply_func(self, messages=messages, sender=sender, config=reply_func_tuple["config"])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/agentchat/conversable_agent.py", line 1470, in _generate_code_execution_reply_using_executor
    code_result = self._code_executor.execute_code_blocks(code_blocks)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/coding/local_commandline_code_executor.py", line 246, in execute_code_blocks
    return self._execute_code_dont_check_setup(code_blocks)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/robinmanuelthiel/.local/pipx/venvs/autogenstudio/lib/python3.12/site-packages/autogen/coding/local_commandline_code_executor.py", line 304, in _execute_code_dont_check_setup
    result = subprocess.run(
             ^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/subprocess.py", line 1955, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python'
robinmanuelthiel commented 4 months ago

When running autogenstudio ui --port 8081 in a venv, where python and pip (without the 3-suffix) are available, it works.

victordibia commented 3 months ago

Thanks for sharing your solution.