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

feat(sandbox): Add Jupyter Kernel for Interactive Python Interpreter for Sandbox #1215

Merged
merged 11 commits into from
Apr 19, 2024

Conversation

xingyaoww
Copy link
Collaborator

This PR adds Python code execution capability from CodeAct based on IPython to augment the bash command line interface for current agents.

Eventual expected outcome (see video below): Agents can automatically solve user's requests by writing and executing Python code (interactively) and running bash commands (based on our existing sandbox). This interactive Python interpreter supports displaying figures/charts in markdown format via base64, which we can later integrate into the front-end.

codeact-demo.mp4

Demo of the expected capability - work-in-progress.

Right now, it only supports SSHBox, and I have tested and confirmed it is working in ubuntu with both RUN_AS_DEVIN='True' and RUN_AS_DEVIN='False'.

You can test it locally by first building the image via ./containers/build.sh sandbox (comment out the build architecture line if error), then run python3 opendevin/sandbox/ssh_box.py. Any command you type that starts with py: will be forwarded to the Python Interpreter for execution.

image

@xingyaoww xingyaoww requested a review from rbren April 18, 2024 17:40
@@ -206,6 +211,28 @@ def execute(self, cmd: str) -> Tuple[int, str]:
exit_code = int(exit_code.lstrip('echo $?').strip())
return exit_code, command_output

def setup_jupyter(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of putting this into SSHBox, could we have a JupyterBox that extends SSHBox?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am also debating about this: Ideally, the Python execution capability should be parallel to bash execution so that every agent can resort to one of them when needed. In that case, I think it probably makes sense for us to extend the existing SSHBox to have a separate Python execution capability via execute_python as it won't interfere with the existing actions going through the SSHBox and other existing agents can also have python execution natively supported easily without specifying a different SSHJupyterBox?

Or we can just do the sub-class and set the SSHJupyterBox to the default just to make the code look better :) - anyway, I can create a sub-class first, and then we can decide whether we want this Python execution capability to be a default behavior.

@@ -371,6 +402,12 @@ def close(self):
ssh_box.kill_background(bg_cmd.id)
logger.info('Background process killed')
continue
if user_input.startswith('py:'):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should have a JupyterAgent or something to demonstrate this functionality?

Copy link
Collaborator Author

@xingyaoww xingyaoww Apr 19, 2024

Choose a reason for hiding this comment

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

For this PR, I'm just trying to get the sandbox component ready :) I'll submit follow-up PRs to update CodeActAgent using the sandbox with instructions on how to use the fine-tuned open-source models (CodeAgentAgent-Mistral-7b - it can run on a laptop!).

@rbren
Copy link
Collaborator

rbren commented Apr 18, 2024

This is very cool! The demo is neat

Comment on lines +51 to +52
self.base_url = f'http://{url_suffix}'
self.base_ws_url = f'ws://{url_suffix}'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.base_url = f'http://{url_suffix}'
self.base_ws_url = f'ws://{url_suffix}'
self.base_url = f'http://{url_suffix}'
self.base_ws_url = f'ws://{url_suffix}'
self.ws = None

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My original implement was like this but mypy was not super happy about it: it assume self.ws is None and does not have methods like 'send_messages' etc, hence causing issue for its typing system :(

Copy link
Collaborator

@yufansong yufansong Apr 19, 2024

Choose a reason for hiding this comment

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

Fine, let's keep the original code.

Comment on lines +74 to +75
if not hasattr(self, 'ws') or not self.ws:
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if not hasattr(self, 'ws') or not self.ws:
return
if not self.ws:
return

)

async def _connect(self):
if hasattr(self, 'ws') and self.ws:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if hasattr(self, 'ws') and self.ws:
if self.ws:

RUN pip install jupyterlab notebook jupyter_kernel_gateway
# Add common data science utils
RUN pip install transformers[torch]
RUN pip install torch --index-url https://download.pytorch.org/whl/cpu
Copy link
Collaborator

@li-boxuan li-boxuan Apr 19, 2024

Choose a reason for hiding this comment

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

Do you expect agent would run torch to solve user's problems? An example would be cool.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes! I did try to use CodeActAgent-Mistral 7B and let it write Pytorch code for some simple tasks (e.g., regression), and it did actually solve the problem by 80% - I suspect by switching towards a stronger base model (e.g., 70B), the agent will be able to tackle such problems, so i'd like to keep these as in the container.

alternatively in the future if we are concerned about the size of the container, we can make these optional and/or allow user to use their customized executor image.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't really have a concrete qualitative example at hand using PyTorch, the closest example I have is having the agent/LLM using SKLearn for machine learning tasks: https://twitter.com/xingyaow_/status/1754556862949994917

Copy link
Collaborator

Choose a reason for hiding this comment

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

that's cool!

Copy link
Collaborator

@yufansong yufansong left a comment

Choose a reason for hiding this comment

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

LGTM

@xingyaoww xingyaoww changed the title (feat) Add Jupyter Kernel for Interactive Python Interpreter for Sandbox feat(sandbox): Add Jupyter Kernel for Interactive Python Interpreter for Sandbox Apr 19, 2024
@xingyaoww
Copy link
Collaborator Author

@rbren, I will merge this so that I can get the new sandbox image built for further development -- but feel free to revert if you think something is wrong! ;)

@xingyaoww xingyaoww merged commit 492feec into All-Hands-AI:main Apr 19, 2024
Umpire2018 pushed a commit to Umpire2018/OpenDevin that referenced this pull request Apr 19, 2024
…for Sandbox (All-Hands-AI#1215)

* add initial version of py interpreter

* fix bug

* fix async issue

* remove debugging print statement

* initialize kernel & update printing

* fix port mapping

* uncomment debug lines

* fix poetry lock

* make jupyter py interpreter into a subclass
xingyaoww added a commit to xingyaoww/OpenHands that referenced this pull request Apr 19, 2024
rbren pushed a commit that referenced this pull request Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants