diff --git a/README.md b/README.md index 33889e7..6513e64 100644 --- a/README.md +++ b/README.md @@ -54,15 +54,28 @@ with SandboxSession(image="python:3.9.19-bullseye", keep_template=True, lang="py # Run some Python code in the sandbox result = session.run("print('Hello, World!')") print(result) + +# With custom Dockerfile +with SandboxSession(dockerfile="Dockerfile", keep_template=True, lang="python") as session: + # Run some Python code in the sandbox + result = session.run("print('Hello, World!')") + print(result) + +# Or default image +with SandboxSession(lang="python", keep_template=True) as session: + # Run some Python code in the sandbox + result = session.run("print('Hello, World!')") + print(result) ``` + LLM Sandbox also supports copying files between the host and the sandbox: ```python from llm_sandbox.session import SandboxSession # Create a new sandbox session -with SandboxSession(image="python:3.9.19-bullseye", keep_template=True, lang="python") as session: +with SandboxSession(lang="python", keep_template=True) as session: # Copy a file from the host to the sandbox session.copy_to_runtime("test.py", "/sandbox/test.py") @@ -110,8 +123,8 @@ SandboxSession( We welcome contributions to improve LLM Sandbox! Since I am a Python developer, I am not familiar with other languages. If you are interested in adding better support for other languages, please feel free to submit a pull request. Here is a list of things you can do to contribute: -- [ ] Add support for Java. -- [ ] Add support for JavaScript. +- [ ] Add Java maven support. +- [x] Add support for JavaScript. - [ ] Add support for C++. - [ ] Add support for Go. - [ ] Add support for Ruby. diff --git a/examples/code_runner.py b/examples/code_runner.py index 9165d02..dd31429 100644 --- a/examples/code_runner.py +++ b/examples/code_runner.py @@ -18,5 +18,88 @@ def run_python_code(): session.copy_to_runtime("README.md", "/sandbox/data.csv") +def run_java_code(): + with SandboxSession(lang="java", keep_template=True, verbose=True) as session: + output = session.run( + """ + public class Main { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } + } + """, + ) + print(output) + + +def run_javascript_code(): + with SandboxSession(lang="javascript", keep_template=True, verbose=True) as session: + output = session.run("console.log('Hello, World!')") + print(output) + + output = session.run( + """ + const axios = require('axios'); + axios.get('https://jsonplaceholder.typicode.com/posts/1') + .then(response => console.log(response.data)); + """, + libraries=["axios"], + ) + print(output) + + +def run_cpp_code(): + with SandboxSession(lang="cpp", keep_template=True, verbose=True) as session: + output = session.run( + """ + #include + int main() { + std::cout << "Hello, World!" << std::endl; + return 0; + } + """, + ) + print(output) + + output = session.run( + """ + #include + #include + int main() { + std::vector v = {1, 2, 3, 4, 5}; + for (int i : v) { + std::cout << i << " "; + } + std::cout << std::endl; + return 0; + } + """, + ) + print(output) + + # run with libraries + output = session.run( + """ + #include + #include + #include + int main() { + std::vector v = {1, 2, 3, 4, 5}; + std::reverse(v.begin(), v.end()); + for (int i : v) { + std::cout << i << " "; + } + std::cout << std::endl; + return 0; + } + """, + libraries=["libstdc++"], + ) + print(output) + + if __name__ == "__main__": - run_python_code() + # run_python_code() + # run_java_code() + # run_javascript_code() + run_cpp_code() \ No newline at end of file diff --git a/llm_sandbox/const.py b/llm_sandbox/const.py index 44e01c1..c29abfe 100644 --- a/llm_sandbox/const.py +++ b/llm_sandbox/const.py @@ -15,12 +15,13 @@ class SupportedLanguage: class DefaultImage: PYTHON = "python:3.9.19-bullseye" JAVA = "openjdk:11.0.12-jdk-bullseye" - JAVASCRIPT = "node:16.6.1-bullseye" + JAVASCRIPT = "node:22-bullseye" CPP = "gcc:11.2.0-bullseye" GO = "golang:1.17.0-bullseye" RUBY = "ruby:3.0.2-bullseye" +NotSupportedLibraryInstallation = ["JAVA"] SupportedLanguageValues = [ v for k, v in SupportedLanguage.__dict__.items() if not k.startswith("__") ] diff --git a/llm_sandbox/session.py b/llm_sandbox/session.py index a014e23..6d54a57 100644 --- a/llm_sandbox/session.py +++ b/llm_sandbox/session.py @@ -12,7 +12,7 @@ get_code_file_extension, get_code_execution_command, ) -from llm_sandbox.const import SupportedLanguage, SupportedLanguageValues, DefaultImage +from llm_sandbox.const import SupportedLanguage, SupportedLanguageValues, DefaultImage, NotSupportedLibraryInstallation class SandboxSession: @@ -128,6 +128,11 @@ def run(self, code: str, libraries: Optional[List] = None): ) if libraries: + if self.lang.upper() in NotSupportedLibraryInstallation: + raise ValueError( + f"Library installation has not been supported for {self.lang} yet!" + ) + command = get_libraries_installation_command(self.lang, libraries) self.execute_command(command) diff --git a/llm_sandbox/utils.py b/llm_sandbox/utils.py index 2b3b573..e18c162 100644 --- a/llm_sandbox/utils.py +++ b/llm_sandbox/utils.py @@ -36,7 +36,7 @@ def get_libraries_installation_command( elif lang == SupportedLanguage.JAVA: return f"mvn install:install-file -Dfile={' '.join(libraries)}" elif lang == SupportedLanguage.JAVASCRIPT: - return f"npm install {' '.join(libraries)}" + return f"yarn add {' '.join(libraries)}" elif lang == SupportedLanguage.CPP: return f"apt-get install {' '.join(libraries)}" elif lang == SupportedLanguage.GO: