AutoGPT icon indicating copy to clipboard operation
AutoGPT copied to clipboard

feat(agent): Persistent execute_code session

Open MKdir98 opened this issue 1 year ago • 15 comments
trafficstars

  • Add notebook libs
  • Add two tests for the execute_code part
  • Add a Python kernel to each agent
  • Change the execute_code command part to persist the state of the codes

Background

As we talked about in Persistent Python session / Jupyter Notebook integration , we need to change the execute_code command to save the state of each executed code. We attach a Python kernel and a notebook to each agent in initializing the agent part. After that now we can use that kernel to run Python code with that kernel and this kernel will save the session until the agent gets destroyed. The error part may be a little dirty and I think maybe we can find something better instead of kernel.execute. You can check tests for execute_code to see scenarios we can support right now.

Changes 🏗️

In the execute_code command instead of creating a temp file and running that file, we run the code directly by python kernel.

PR Quality Scorecard ✨

  • [x] Have you used the PR description template?   +2 pts
  • [x] Is your pull request atomic, focusing on a single change?   +5 pts
  • [x] Have you linked the GitHub issue(s) that this PR addresses?   +5 pts
  • [x] Have you documented your changes clearly and comprehensively?   +5 pts
  • [x] Have you changed or added a feature?   -4 pts
    • [ ] Have you added/updated corresponding documentation?   +4 pts
    • [x] Have you added/updated corresponding integration tests?   +5 pts
  • [ ] Have you changed the behavior of AutoGPT?   -5 pts
    • [ ] Have you also run agbenchmark to verify that these changes do not regress performance?   +10 pts

MKdir98 avatar Apr 08 '24 04:04 MKdir98

This PR exceeds the recommended size of 1000 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Apr 08 '24 04:04 github-actions[bot]

Deploy Preview for auto-gpt-docs canceled.

Name Link
Latest commit 02897f408cc050706fd7621280802c771b468ad8
Latest deploy log https://app.netlify.com/sites/auto-gpt-docs/deploys/6638e4b505296d00086db219

netlify[bot] avatar Apr 08 '24 04:04 netlify[bot]

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.

github-actions[bot] avatar Apr 22 '24 17:04 github-actions[bot]

@CodiumAI-Agent /review

Swiftyos avatar Apr 23 '24 14:04 Swiftyos

PR Review

(Review updated until commit https://github.com/Significant-Gravitas/AutoGPT/commit/60cdfd71ac689d77957906127cd490d84667d961)

⏱️ Estimated effort to review [1-5]

4, due to the complexity of integrating Jupyter kernels and the need to ensure that the implementation does not introduce any security vulnerabilities or logical errors in handling kernel sessions and outputs.

🧪 Relevant tests

Yes

🔍 Possible issues

Possible Bug: The error handling in execute_python_code might not correctly format exceptions. The CommandExecutionError is raised with *e.args which might not correctly pass the exception details if e has more than one argument.

Resource Management: The kernel sessions started with start_new_kernel are not explicitly terminated or cleaned up, which could lead to resource leaks if kernels are not properly managed.

🔒 Security concerns

No

Code feedback:
relevant fileautogpts/autogpt/autogpt/agents/base.py
suggestion      

Consider managing the lifecycle of the Python kernel to prevent resource leaks. You should ensure that kernels are properly shut down when they are no longer needed or when the agent is destroyed. This can be done by implementing a cleanup method in the BaseAgentSettings class that calls self.python_kernel.shutdown(). [important]

relevant lineself.python_kernel = start_new_kernel(

relevant fileautogpts/autogpt/autogpt/commands/execute_code.py
suggestion      

Improve the exception handling in execute_python_code to ensure that all parts of the exception are correctly formatted and passed to CommandExecutionError. Instead of using *e.args, you might want to directly pass the string representation of the exception or format it to include more context. [important]

relevant lineraise CommandExecutionError(*e.args)

relevant fileautogpts/autogpt/autogpt/commands/execute_code.py
suggestion      

To enhance code readability and maintainability, consider abstracting the logic that handles the output and errors from the kernel into separate methods. This can help in isolating the functionality and making the code easier to manage and test. [medium]

relevant lineagent.python_kernel.execute(code)

relevant fileautogpts/autogpt/autogpt/agents/base.py
suggestion      

Add error handling for the kernel start-up process in the __init__ method of BaseAgentSettings. This should manage scenarios where the kernel fails to start, possibly due to configuration issues or system limitations. [important]

relevant lineself.python_kernel = start_new_kernel(


✨ Review tool usage guide:

Overview: The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

  • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
/review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
[pr_reviewer]
some_config1=...
some_config2=...

See the review usage page for a comprehensive guide on using this tool.

CodiumAI-Agent avatar Apr 23 '24 14:04 CodiumAI-Agent

Conflicts have been resolved! 🎉 A maintainer will review the pull request shortly.

github-actions[bot] avatar May 02 '24 12:05 github-actions[bot]

@CodiumAI-Agent /review

MKdir98 avatar May 02 '24 12:05 MKdir98

Persistent review updated to latest commit https://github.com/Significant-Gravitas/AutoGPT/commit/60cdfd71ac689d77957906127cd490d84667d961

CodiumAI-Agent avatar May 02 '24 12:05 CodiumAI-Agent

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 44.81%. Comparing base (9bac6f4) to head (02897f4). Report is 345 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7078      +/-   ##
==========================================
+ Coverage   44.65%   44.81%   +0.16%     
==========================================
  Files         133      133              
  Lines        6306     6321      +15     
  Branches      822      824       +2     
==========================================
+ Hits         2816     2833      +17     
+ Misses       3379     3377       -2     
  Partials      111      111              
Flag Coverage Δ
Linux 44.73% <100.00%> (+0.16%) :arrow_up:
Windows 42.72% <34.78%> (-0.01%) :arrow_down:
autogpt-agent 44.78% <100.00%> (+0.16%) :arrow_up:
macOS 43.94% <34.78%> (-0.01%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar May 02 '24 14:05 codecov[bot]

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.

github-actions[bot] avatar May 04 '24 18:05 github-actions[bot]

Conflicts have been resolved! 🎉 A maintainer will review the pull request shortly.

github-actions[bot] avatar May 06 '24 14:05 github-actions[bot]

This looks ok to me overall and;

OK. I'll test it with agent protocol

MKdir98 avatar May 08 '24 12:05 MKdir98

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.

github-actions[bot] avatar May 14 '24 14:05 github-actions[bot]

@MKdir98 Do you intend to finish this PR off or shall we close it now

Swiftyos avatar Aug 07 '24 08:08 Swiftyos

@MKdir98 Do you intend to finish this PR off or shall we close it now

I'm really sorry but I can't afford time to complete this one. Sorry for getting your time and energy.

MKdir98 avatar Aug 07 '24 09:08 MKdir98

@MKdir98 another excellent contribution that sadly got missed at the time. Likewise this suffers from the same issue - things have changed so much I don't see a path for this to be merged.

We'd love for you to still help out if your interested and can let you know areas that we are actively looking for contributors

Swiftyos avatar Aug 21 '24 10:08 Swiftyos