-
Notifications
You must be signed in to change notification settings - Fork 264
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
Add E2B Sandboxed Code Execution Integration #1397
Open
YukiHinana
wants to merge
2
commits into
georgia-tech-db:staging
Choose a base branch
from
YukiHinana:staging
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Executing Code Generated by OpenAI Chat Completion Model | ||
======================================================== | ||
|
||
This section provides an overview on how to use the E2B Sandbox and EvaDB to execute code snippets generated by ChatGPT / Chat Completion Model. | ||
|
||
1. Getting a cursor and Create the E2BExec Function | ||
|
||
.. code-block:: python | ||
|
||
import evadb | ||
cursor = evadb.connect().cursor() | ||
cursor.query("DROP FUNCTION IF EXISTS E2BExec").execute() | ||
cursor.query(f"CREATE FUNCTION IF NOT EXISTS E2BExec IMPL '<path>/e2b_exec.py'").execute() | ||
|
||
Assume output of OpenAI Chat Completion model has been stored in a table named `CodeToExecute` | ||
under the column `codejson` | ||
|
||
The input that this function expects should look like | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"function_call":{ | ||
"name":"exec_code", | ||
"arguments":"{\n \"code\": \"def fibonacci(n):\\n fibonacci_sequence = [0, 1]\\n while len(fibonacci_sequence) < n:\\n fibonacci_sequence.append(fibonacci_sequence[-1] + fibonacci_sequence[-2])\\n return fibonacci_sequence\\n\\nprint(fibonacci(100))\"\n}" | ||
} | ||
} | ||
|
||
2. Using the following snippet it will execute each column and return stderr and stdout | ||
|
||
.. code-block:: python | ||
|
||
result = cursor.query("SELECT E2BExec(codejson) FROM CodeToExecute").execute() | ||
|
||
.. note:: | ||
Remember to set your E2B API key as `E2B_API_KEY` environment variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
|
||
import pandas as pd | ||
from evadb.catalog.catalog_type import NdArrayType | ||
from evadb.functions.abstract.abstract_function import AbstractFunction, InputType | ||
from evadb.functions.decorators.decorators import setup, forward | ||
from evadb.functions.decorators.io_descriptors.data_types import PandasDataframe | ||
from e2b import Sandbox | ||
|
||
|
||
class E2BExec(AbstractFunction): | ||
""" | ||
Takes input as a string like: | ||
{ | ||
"function_call":{ | ||
"name":"exec_code", | ||
"arguments":"{\n \"code\": \"def fibonacci(n):\\n fibonacci_sequence = [0, 1]\\n while len(fibonacci_sequence) < n:\\n fibonacci_sequence.append(fibonacci_sequence[-1] + fibonacci_sequence[-2])\\n return fibonacci_sequence\\n\\nprint(fibonacci(100))\"\n}" | ||
} | ||
} | ||
This is the output from OpenAI Chat Completion model. | ||
""" | ||
@setup(cacheable=False, function_type="code_execution", batchable=True) | ||
def setup(self, *args, **kwargs) -> None: | ||
pass | ||
|
||
@forward( | ||
input_signatures=[ | ||
PandasDataframe( | ||
columns=["data"], | ||
column_types=[NdArrayType.STR], | ||
column_shapes=[(1,)], | ||
) | ||
], | ||
output_signatures=[ | ||
PandasDataframe( | ||
columns=["stdout", "stderr"], | ||
column_types=[NdArrayType.STR, ], | ||
column_shapes=[(1,), (1, )], | ||
) | ||
], | ||
) | ||
def forward(self, frames: InputType) -> InputType: | ||
self.sandbox = Sandbox() | ||
output = [] | ||
stderrs = [] | ||
for code in frames['codejson']: | ||
code_obj = json.loads(code) | ||
code_text = json.loads(code_obj['function_call']['arguments'])['code'] | ||
self.sandbox.filesystem.write('/home/user/test.py', code_text) | ||
proc = self.sandbox.process.start('python /home/user/test.py') | ||
out = proc.wait() | ||
output.append(out.stdout) | ||
stderrs.append(out.stderr) | ||
|
||
return_df = pd.DataFrame({"stdout": output, "stderr": stderrs}) | ||
self.sandbox.close() | ||
return return_df | ||
|
||
@property | ||
def name(self) -> str: | ||
return 'E2BExec' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard coded, but since it is what is executed in the VM/sandbox. this is probably fine for now.