Skip to content

Commit

Permalink
feat: add javascript and c++ examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vndee committed Jul 7, 2024
1 parent 9100023 commit 829ecf8
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 7 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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.
Expand Down
85 changes: 84 additions & 1 deletion examples/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
""",
)
print(output)

output = session.run(
"""
#include <iostream>
#include <vector>
int main() {
std::vector<int> 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 <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> 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()
3 changes: 2 additions & 1 deletion llm_sandbox/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("__")
]
7 changes: 6 additions & 1 deletion llm_sandbox/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion llm_sandbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 829ecf8

Please sign in to comment.