Skip to content

Commit

Permalink
Refactor code runner script and remove commented out code
Browse files Browse the repository at this point in the history
  • Loading branch information
vndee committed Jan 20, 2025
1 parent 9661baf commit 247b304
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
8 changes: 4 additions & 4 deletions examples/code_runner_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def run_go_code():

if __name__ == "__main__":
run_python_code()
run_java_code()
run_javascript_code()
run_cpp_code()
run_go_code()
# run_java_code()
# run_javascript_code()
# run_cpp_code()
# run_go_code()
5 changes: 0 additions & 5 deletions llm_sandbox/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ def run(self, code: str, libraries: Optional[List] = None) -> ExecutionResult:
self._log(f"Error during execution: {str(e)}", level='error')
raise

@abstractmethod
def _execute_code(self, code: str, libraries: Optional[List] = None) -> ConsoleOutput:
"""Execute code in the sandbox environment."""
raise NotImplementedError

@abstractmethod
def copy_to_runtime(self, src: str, dest: str):
"""Copy file to sandbox runtime."""
Expand Down
4 changes: 3 additions & 1 deletion llm_sandbox/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(
verbose: bool = False,
mounts: Optional[list[Mount]] = None,
container_configs: Optional[dict] = None,
*args,
**kwargs
):
"""
Create a new sandbox session
Expand Down Expand Up @@ -280,4 +282,4 @@ def execute_command(
if self.verbose:
print(chunk_str, end="")

return ConsoleOutput(output)
return ConsoleOutput(output, exit_code)
8 changes: 6 additions & 2 deletions llm_sandbox/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def create_session(self, **kwargs) -> SandboxDockerSession:
'mem_limit': str(resource_limits.max_memory_bytes),
'network_disabled': kwargs.pop('network_disabled', False)
}


if kwargs.get('container_configs', {}):
kwargs['container_configs'] = kwargs.get('container_configs', {}) | container_configs
else:
kwargs['container_configs'] = container_configs

return SandboxDockerSession(
client=self.client,
container_configs=container_configs,
**kwargs
)

Expand Down
10 changes: 5 additions & 5 deletions llm_sandbox/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from kubernetes import client as k8s_client, config
from kubernetes.stream import stream
from llm_sandbox.base import Session, ConsoleOutput, KubernetesConsoleOutput
from llm_sandbox.base import Session, ConsoleOutput
from llm_sandbox.utils import (
get_libraries_installation_command,
get_code_file_extension,
Expand Down Expand Up @@ -154,7 +154,7 @@ def run(self, code: str, libraries: Optional[List] = None) -> ConsoleOutput:
self.copy_to_runtime(code_file, code_dest_file)
commands = get_code_execution_command(self.lang, code_dest_file)

output = KubernetesConsoleOutput(0, "")
output = ConsoleOutput(exit_code=0, text="")
for command in commands:
if self.lang == SupportedLanguage.GO:
output = self.execute_command(command, workdir="/example")
Expand All @@ -164,7 +164,7 @@ def run(self, code: str, libraries: Optional[List] = None) -> ConsoleOutput:
if output.exit_code != 0:
break

return ConsoleOutput(output.text)
return ConsoleOutput(output.text, output.exit_code)

def copy_to_runtime(self, src: str, dest: str):
if not self.container:
Expand Down Expand Up @@ -248,7 +248,7 @@ def copy_from_runtime(self, src: str, dest: str):

def execute_command(
self, command: str, workdir: Optional[str] = None
) -> KubernetesConsoleOutput:
) -> ConsoleOutput:
if not self.container:
raise RuntimeError(
"Session is not open. Please call open() method before executing commands."
Expand Down Expand Up @@ -292,4 +292,4 @@ def execute_command(
print(chunk, end="")

exit_code = resp.returncode
return KubernetesConsoleOutput(exit_code, output)
return ConsoleOutput(output, exit_code)

0 comments on commit 247b304

Please sign in to comment.