diff --git a/llm_sandbox/docker.py b/llm_sandbox/docker.py index 2b1b972..0f0704b 100644 --- a/llm_sandbox/docker.py +++ b/llm_sandbox/docker.py @@ -2,6 +2,7 @@ import os import docker import tarfile +import tempfile from typing import List, Optional, Union from docker.models.images import Image @@ -163,27 +164,28 @@ def run(self, code: str, libraries: Optional[List] = None) -> ConsoleOutput: for library in libraries: command = get_libraries_installation_command(self.lang, library) _ = self.execute_command(command) + with tempfile.TemporaryDirectory() as directory_name: - 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 + code_file = os.path.join(directory_name,f"code.{get_code_file_extension(self.lang)}") + if self.lang == SupportedLanguage.GO: + code_dest_file = "/example/code.go" + else: + code_dest_file = f"/tmp/code.{get_code_file_extension(self.lang)}"# code_file - with open(code_file, "w") as f: - f.write(code) + with open(code_file, "w") as f: + f.write(code) - self.copy_to_runtime(code_file, code_dest_file) + self.copy_to_runtime(code_file, code_dest_file) - output = ConsoleOutput("") - commands = get_code_execution_command(self.lang, code_dest_file) - for command in commands: - if self.lang == SupportedLanguage.GO: - output = self.execute_command(command, workdir="/example") - else: - output = self.execute_command(command) + output = ConsoleOutput("") + commands = get_code_execution_command(self.lang, code_dest_file) + for command in commands: + if self.lang == SupportedLanguage.GO: + output = self.execute_command(command, workdir="/example") + else: + output = self.execute_command(command) - return output + return output def copy_from_runtime(self, src: str, dest: str): if not self.container: diff --git a/pyproject.toml b/pyproject.toml index 3d59411..81b86d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "llm-sandbox" -version = "0.1.1" +version = "0.1.1-alpha" description = "Lightweight and portable LLM sandbox runtime (code interpreter) Python library" authors = ["Duy Huynh "] license = "MIT"