main_use_pool.py
1 # Copyright 2025 Alibaba Group Holding Ltd. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 import asyncio 16 import os 17 from datetime import timedelta 18 19 from code_interpreter import CodeInterpreter, SupportedLanguage 20 from opensandbox import Sandbox 21 from opensandbox.config import ConnectionConfig 22 23 24 async def main() -> None: 25 domain = os.getenv("SANDBOX_DOMAIN", "localhost:8080") 26 api_key = os.getenv("SANDBOX_API_KEY") 27 image = os.getenv( 28 "SANDBOX_IMAGE", 29 "sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2", 30 ) 31 32 config = ConnectionConfig( 33 domain=domain, 34 api_key=api_key, 35 request_timeout=timedelta(seconds=60), 36 ) 37 38 sandbox = await Sandbox.create( 39 image, 40 connection_config=config, 41 extensions={"poolRef":"pool-sample"}, 42 entrypoint=["/opt/opensandbox/code-interpreter.sh"], 43 env={ 44 "TEST_ENV": "test", 45 }, 46 ) 47 48 async with sandbox: 49 interpreter = await CodeInterpreter.create(sandbox=sandbox) 50 51 # Verify environment variable is set 52 print("\n=== Verify Environment Variable ===") 53 env_check = await interpreter.codes.run( 54 "import os\n" 55 "test_env = os.getenv('TEST_ENV', 'NOT_SET')\n" 56 "print(f'TEST_ENV value: {test_env}')\n" 57 "test_env", 58 language=SupportedLanguage.PYTHON, 59 ) 60 for msg in env_check.logs.stdout: 61 print(f"[ENV Check] {msg.text}") 62 if env_check.result: 63 for res in env_check.result: 64 print(f"[ENV Result] {res.text}") 65 66 # Java example: print to stdout and return the final result line. 67 java_exec = await interpreter.codes.run( 68 "System.out.println(\"Hello from Java!\");\n" 69 "int result = 2 + 3;\n" 70 "System.out.println(\"2 + 3 = \" + result);\n" 71 "result", 72 language=SupportedLanguage.JAVA, 73 ) 74 print("\n=== Java example ===") 75 for msg in java_exec.logs.stdout: 76 print(f"[Java stdout] {msg.text}") 77 if java_exec.result: 78 for res in java_exec.result: 79 print(f"[Java result] {res.text}") 80 if java_exec.error: 81 print(f"[Java error] {java_exec.error.name}: {java_exec.error.value}") 82 83 # Go example: print logs and demonstrate a main function structure. 84 go_exec = await interpreter.codes.run( 85 "package main\n" 86 "import \"fmt\"\n" 87 "func main() {\n" 88 " fmt.Println(\"Hello from Go!\")\n" 89 " sum := 3 + 4\n" 90 " fmt.Println(\"3 + 4 =\", sum)\n" 91 "}", 92 language=SupportedLanguage.GO, 93 ) 94 print("\n=== Go example ===") 95 for msg in go_exec.logs.stdout: 96 print(f"[Go stdout] {msg.text}") 97 if go_exec.error: 98 print(f"[Go error] {go_exec.error.name}: {go_exec.error.value}") 99 100 # TypeScript example: use typing and sum an array. 101 ts_exec = await interpreter.codes.run( 102 "console.log('Hello from TypeScript!');\n" 103 "const nums: number[] = [1, 2, 3];\n" 104 "console.log('sum =', nums.reduce((a, b) => a + b, 0));", 105 language=SupportedLanguage.TYPESCRIPT, 106 ) 107 print("\n=== TypeScript example ===") 108 for msg in ts_exec.logs.stdout: 109 print(f"[TypeScript stdout] {msg.text}") 110 if ts_exec.error: 111 print(f"[TypeScript error] {ts_exec.error.name}: {ts_exec.error.value}") 112 113 await sandbox.kill() 114 115 116 if __name__ == "__main__": 117 asyncio.run(main())