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

Fix issues with file reading and writing with Python code #4567

Merged

Conversation

erik-megarad
Copy link
Contributor

Background

Several times I've had Auto-GPT fail to recognize "does not exist" error for files, and it will attempt other fixes such as analyzing the code (for a file that does not exist). I've also had issues with python code not being executed in the working directory, which means that code written by Auto-GPT that reads and writes files will usually not work (since they use relative paths).

These issues are related:
#4439
#4413
#4366
#4360
#3583

Changes

  • Change the error messages for file not found errors so that they're easier to recognize as such
  • Set cwd when executing python code so that relative paths work as expected

Documentation

  • In-code comments

Test Plan

  • Wrote a new test for reading a file that does not exist (was previously untested)
  • Wrote a new test for executing a python file that does not exist (was previously untested)

PR Quality Checklist

  • My pull request is atomic and focuses on a single change.
  • I have thoroughly tested my changes with multiple different prompts.
  • I have considered potential risks and mitigations for my changes.
  • I have documented my changes clearly and comprehensively.
  • I have not snuck in any "extra" small tweaks changes.
  • I have run the following commands against my code to ensure it passes our linters:
    black .
    isort .
    mypy
    autoflake --remove-all-unused-imports --recursive --ignore-init-module-imports autogpt tests --in-place

@vercel
Copy link

vercel bot commented Jun 4, 2023

Deployment failed with the following error:

Resource is limited - try again in 11 hours (more than 100, code: "api-deployments-free-per-day").

@github-actions github-actions bot added the size/m label Jun 4, 2023
@erik-megarad
Copy link
Contributor Author

Example of the new error being recognized:

WEATHERGPT THOUGHTS:  I have found two reliable sources of weather data for Paris, Texas on October 10th, 1991. I will cross-reference the data from both sources to ensure accuracy and reliability.
REASONING:  Cross-referencing multiple sources of weather data will help ensure the accuracy and reliability of the information.
PLAN:
-  Retrieve the weather data from both sources.
-  Cross-reference the data to ensure accuracy and reliability.
-  Present the weather information in a clear and concise manner.
CRITICISM:  I need to be more efficient in cross-referencing multiple sources of weather data.
SPEAK:  I have found two reliable sources of weather data for Lilburn, Georgia on October 12th, 1991. Please wait a moment while I cross-reference the data to ensure accuracy and reliability.
NEXT ACTION:  COMMAND = execute_python_file ARGUMENTS = {'filename': '/app/autogpt/auto_gpt_workspace/cross_reference_weather_data.py'}
  Enter 'y' to authorise command, 'y -N' to run N continuous commands, 's' to run self-feedback commands, 'n' to exit program, or enter feedback for WeatherGPT...
  Asking user via keyboard...
Input:y
-=-=-=-=-=-=-= COMMAND AUTHORISED BY USER -=-=-=-=-=-=-=
  Executing python file '/app/autogpt/auto_gpt_workspace/cross_reference_weather_data.py' in working directory '/app/autogpt/auto_gpt_workspace'
SYSTEM:  Command execute_python_file returned: python: can't open file '/app/autogpt/auto_gpt_workspace/cross_reference_weather_data.py': [Errno 2] No such file or directory
WEATHERGPT THOUGHTS:  I need to create a Python script to cross-reference the weather data from both sources. I will create a new file and write the necessary code to cross-reference the data.
REASONING:  Creating a Python script will allow me to automate the process of cross-referencing the weather data from both sources.
PLAN:
-  Create a new Python file.
-  Write the necessary code to cross-reference the weather data from both sources.
-  Save the file.
CRITICISM:  I need to ensure that the code I write is efficient and accurate.
SPEAK:  I need to create a Python script to cross-reference the weather data from both sources. Please wait a moment while I create the necessary file and write the code.

@erik-megarad erik-megarad force-pushed the fix/python_directory branch 5 times, most recently from 3cd4517 to ba858ad Compare June 4, 2023 04:53
@vercel
Copy link

vercel bot commented Jun 4, 2023

Deployment failed with the following error:

Resource is limited - try again in 8 hours (more than 100, code: "api-deployments-free-per-day").

@vercel
Copy link

vercel bot commented Jun 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) Jun 6, 2023 7:28pm

@github-actions github-actions bot added size/l and removed size/m labels Jun 4, 2023
@codecov
Copy link

codecov bot commented Jun 4, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.14 🎉

Comparison is base (20a4922) 69.57% compared to head (743fa25) 69.71%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4567      +/-   ##
==========================================
+ Coverage   69.57%   69.71%   +0.14%     
==========================================
  Files          72       72              
  Lines        3556     3560       +4     
  Branches      569      569              
==========================================
+ Hits         2474     2482       +8     
+ Misses        892      889       -3     
+ Partials      190      189       -1     
Impacted Files Coverage Δ
autogpt/commands/execute_code.py 65.55% <100.00%> (+1.60%) ⬆️
autogpt/commands/file_operations_utils.py 92.85% <100.00%> (+2.04%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Boostrix
Copy link
Contributor

Boostrix commented Jun 6, 2023

Several times I've had Auto-GPT fail to recognize "does not exist" error for files,

that's true, I ended up adding commands to check if a directory/file exists and made this available so that the LLM can use it.
I also changed the prompt to specifically state "do not try to read or execute files that do not exist", and I told it "do not overwrite existing files"
So, now it needs to check first if a file exists before reading/executing it.

it will attempt other fixes such as analyzing the code (for a file that does not exist).

analyze_code seems to be called for all sorts of stuff where it wants to "analyze" a problem (not just code).
But if you prime it to check first if a file/folder exists before accessing it, that basically solves the problem. Especially in combination with a file_exists command:

+@command("file_exists", "Check if File or Directory Exists", '"path": "<path>"')
+def file_exists(path: str) -> int:
+    """Check if a file or directory exists.
+
+    Args:
+        path (str): The path of the file or directory to check.
+
+    Returns:
+        int: 1 if the file or directory exists, 0 otherwise.
+    """
+    full_path = os.path.join(CFG.workspace_path, path)
+    if os.path.exists(full_path):
+        return 1
+    else:
+        return 0

That is one of the reasons why I suggested elsewhere that we should be able to chain commands together, as in:

file_exists foo.py && read_file foo.py

note that this would make it possible to run these chains locally, without having to go through the LLM at all: #4157

@erik-megarad erik-megarad force-pushed the fix/python_directory branch 2 times, most recently from 154f955 to 98b97ce Compare June 6, 2023 15:30
Copy link
Member

@Pwuts Pwuts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up!

autogpt/commands/execute_code.py Show resolved Hide resolved
autogpt/commands/execute_code.py Outdated Show resolved Hide resolved
autogpt/commands/file_operations_utils.py Outdated Show resolved Hide resolved
@vercel
Copy link

vercel bot commented Jun 6, 2023

Deployment failed with the following error:

Resource is limited - try again in 43 minutes (more than 100, code: "api-deployments-free-per-day").

@vercel
Copy link

vercel bot commented Jun 7, 2023

Deployment failed with the following error:

Resource is limited - try again in 29 minutes (more than 100, code: "api-deployments-free-per-day").

@vercel
Copy link

vercel bot commented Jun 7, 2023

Deployment failed with the following error:

Resource is limited - try again in 22 minutes (more than 100, code: "api-deployments-free-per-day").

@vercel
Copy link

vercel bot commented Jun 7, 2023

Deployment failed with the following error:

Resource is limited - try again in 20 minutes (more than 100, code: "api-deployments-free-per-day").

@vercel
Copy link

vercel bot commented Jun 7, 2023

Deployment failed with the following error:

Resource is limited - try again in 11 minutes (more than 100, code: "api-deployments-free-per-day").

@Pwuts Pwuts merged commit fdc6e12 into Significant-Gravitas:master Jun 7, 2023
9 of 10 checks passed
@maxfield-allison
Copy link

Thank you for your service!

@Pwuts Pwuts added this to the v0.4.1 Release milestone Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

4 participants