Skip to content

Commit

Permalink
feat: add golang support
Browse files Browse the repository at this point in the history
  • Loading branch information
vndee committed Jul 8, 2024
1 parent 492e982 commit 6d639be
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Here is a list of things you can do to contribute:
- [ ] Add Java maven support.
- [x] Add support for JavaScript.
- [x] Add support for C++.
- [ ] Add support for Go.
- [x] Add support for Go.
- [ ] Add support for Ruby.
- [ ] Add remote Docker host support.
- [ ] Add remote Kubernetes cluster support.
Expand Down
27 changes: 19 additions & 8 deletions examples/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run_cpp_code():


def run_go_code():
with SandboxSession(lang="go", keep_template=False, verbose=True) as session:
with SandboxSession(lang="go", keep_template=True, verbose=True) as session:
output = session.run(
"""
package main
Expand All @@ -117,20 +117,31 @@ def run_go_code():
package main
import (
"fmt"
"github.com/gorilla/mux"
"github.com/spyzhov/ajson"
)
func main() {
fmt.Println("Hello, World!")
json := []byte(`...`)
root, _ := ajson.Unmarshal(json)
nodes, _ := root.JSONPath("$..price")
for _, node := range nodes {
node.SetNumeric(node.MustNumeric() * 1.25)
node.Parent().AppendObject("currency", ajson.StringNode("", "EUR"))
}
result, _ := ajson.Marshal(root)
fmt.Printf("%s", result)
}
""",
libraries=["github.com/gorilla/mux"],
libraries=["github.com/spyzhov/ajson"],
)
print(output)


if __name__ == "__main__":
run_python_code()
run_java_code()
run_javascript_code()
run_cpp_code()
# run_go_code()
# run_python_code()
# run_java_code()
# run_javascript_code()
# run_cpp_code()
run_go_code()
34 changes: 24 additions & 10 deletions llm_sandbox/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,36 @@ def run(self, code: str, libraries: Optional[List] = None):
)

if self.lang == SupportedLanguage.GO:
init_cmd = "sh -c 'cd /sandbox && go mod init example.com/sandbox && go mod tidy'"
self.execute_command(init_cmd)
self.execute_command("mkdir -p /example")
self.execute_command("go mod init example", worKdir="/example")
self.execute_command("go mod tidy", worKdir="/example")

for lib in libraries:
install_cmd = f"sh -c 'cd /sandbox && go get {lib}'"
self.execute_command(install_cmd)
command = get_libraries_installation_command(self.lang, lib)
self.execute_command(command, worKdir="/example")
else:
for lib in libraries:
command = get_libraries_installation_command(self.lang, lib)
self.execute_command(command)

code_file = f"/tmp/code.{get_code_file_extension(self.lang)}"
if self.lang == SupportedLanguage.GO:
code_dest_file = "/example/code.go"
else:
code_dest_file = code_file

with open(code_file, "w") as f:
f.write(code)

self.copy_to_runtime(code_file, code_file)
self.copy_to_runtime(code_file, code_dest_file)

output = ""
commands = get_code_execution_command(self.lang, code_file)
commands = get_code_execution_command(self.lang, code_dest_file)
for command in commands:
output = self.execute_command(command)
if self.lang == SupportedLanguage.GO:
output = self.execute_command(command, worKdir="/example")
else:
output = self.execute_command(command)

return output

Expand Down Expand Up @@ -204,7 +213,7 @@ def copy_to_runtime(self, src: str, dest: str):
tarstream.seek(0)
self.container.put_archive(os.path.dirname(dest), tarstream)

def execute_command(self, command: Optional[str]):
def execute_command(self, command: Optional[str], worKdir: Optional[str] = None):
if not command:
raise ValueError("Command cannot be empty")

Expand All @@ -216,9 +225,14 @@ def execute_command(self, command: Optional[str]):
if self.verbose:
print(f"Executing command: {command}")

_, exec_log = self.container.exec_run(command, stream=True, tty=True)
output = ""
if worKdir:
_, exec_log = self.container.exec_run(
command, stream=True, tty=True, workdir=worKdir
)
else:
_, exec_log = self.container.exec_run(command, stream=True, tty=True)

output = ""
if self.verbose:
print("Output:", end=" ")

Expand Down
10 changes: 4 additions & 6 deletions llm_sandbox/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import docker
import docker.errors
from typing import List, Optional
from typing import Optional

from docker import DockerClient
from llm_sandbox.const import SupportedLanguage
Expand All @@ -22,13 +22,11 @@ def image_exists(client: DockerClient, image: str) -> bool:
raise e


def get_libraries_installation_command(
lang: str, library: str
) -> Optional[str]:
def get_libraries_installation_command(lang: str, library: str) -> Optional[str]:
"""
Get the command to install libraries for the given language
:param lang: Programming language
:param libraries: List of libraries
:param library: List of libraries
:return: Installation command
"""
if lang == SupportedLanguage.PYTHON:
Expand All @@ -40,7 +38,7 @@ def get_libraries_installation_command(
elif lang == SupportedLanguage.CPP:
return f"apt-get install {library}"
elif lang == SupportedLanguage.GO:
return f"go get {library}"
return f"go get -u {library}"
elif lang == SupportedLanguage.RUBY:
return f"gem install {library}"
else:
Expand Down

0 comments on commit 6d639be

Please sign in to comment.