@@ -21,19 +21,19 @@ A Rust-powered secure sandbox for multi-language code execution, leveraging WebA
2121** Install Lyric via pip:**
2222
2323``` bash
24- pip install " lyric-py>=0.1.3 "
24+ pip install " lyric-py>=0.1.4-rc0 "
2525```
2626
2727** Install default Python webassembly worker:**
2828
2929``` bash
30- pip install " lyric-py-worker>=0.1.3 "
30+ pip install " lyric-py-worker>=0.1.4-rc0 "
3131```
3232
3333** Install default JavaScript webassembly worker:**
3434
3535``` bash
36- pip install " lyric-js-worker>=0.1.3 "
36+ pip install " lyric-js-worker>=0.1.4-rc0 "
3737```
3838
3939### Basic Usage
@@ -42,6 +42,17 @@ pip install "lyric-js-worker>=0.1.3"
4242import asyncio
4343from lyric import DefaultLyricDriver
4444
45+ python_code = """
46+ def add(a, b):
47+ return a + b
48+ result = add(1, 2)
49+ print(result)
50+ """
51+
52+ js_code = """
53+ console.log('Hello from JavaScript!');
54+ """
55+
4556async def main ():
4657 lcd = DefaultLyricDriver(host = " localhost" , log_level = " ERROR" )
4758 lcd.start()
@@ -50,21 +61,10 @@ async def main():
5061 await lcd.lyric.load_default_workers()
5162
5263 # Execute Python code
53- python_code = """
54- def add(a, b):
55- return a + b
56- result = add(1, 2)
57- print(result)
58- """
59-
6064 py_res = await lcd.exec(python_code, " python" )
6165 print (py_res)
6266
6367 # Execute JavaScript code
64- js_code = """
65- console.log('Hello from JavaScript!');
66- """
67-
6868 js_res = await lcd.exec(js_code, " javascript" )
6969 print (js_res)
7070
@@ -81,13 +81,7 @@ import asyncio
8181import json
8282from lyric import DefaultLyricDriver
8383
84- async def main ():
85- lcd = DefaultLyricDriver(host = " localhost" , log_level = " ERROR" )
86- lcd.start()
87-
88- # Load workers(default: Python, JavaScript)
89- await lcd.lyric.load_default_workers()
90- py_func = """
84+ py_func = """
9185def message_handler(message_dict):
9286 user_message = message_dict.get("user_message")
9387 ai_message = message_dict.get("ai_message")
@@ -99,18 +93,8 @@ def message_handler(message_dict):
9993 "handler_language": "python",
10094 }
10195"""
102- input_data = {
103- " user_message" : " Hello from user" ,
104- " ai_message" : " Hello from AI" ,
105- }
106- input_bytes = json.dumps(input_data).encode(" utf-8" )
107- py_res = await lcd.exec1(py_func, input_bytes, " message_handler" , lang = " python" )
108- # Get the result of the function execution
109- result_dict = py_res.output
110- print (" Python result:" , result_dict)
111- print (f " Full output: { py_res} " )
11296
113- js_func = """
97+ js_func = """
11498function message_handler(message_dict) {
11599 return {
116100 user: message_dict.user_message,
@@ -121,6 +105,25 @@ function message_handler(message_dict) {
121105 };
122106}
123107"""
108+ async def main ():
109+ lcd = DefaultLyricDriver(host = " localhost" , log_level = " ERROR" )
110+ lcd.start()
111+
112+ # Load workers(default: Python, JavaScript)
113+ await lcd.lyric.load_default_workers()
114+
115+ input_data = {
116+ " user_message" : " Hello from user" ,
117+ " ai_message" : " Hello from AI" ,
118+ }
119+ input_bytes = json.dumps(input_data).encode(" utf-8" )
120+
121+ py_res = await lcd.exec1(py_func, input_bytes, " message_handler" , lang = " python" )
122+ # Get the result of the function execution
123+ result_dict = py_res.output
124+ print (" Python result:" , result_dict)
125+ print (f " Full output: { py_res} " )
126+
124127 js_res = await lcd.exec1(js_func, input_bytes, " message_handler" , lang = " javascript" )
125128 # Get the result of the function execution
126129 result_dict = js_res.output
@@ -133,6 +136,59 @@ function message_handler(message_dict) {
133136asyncio.run(main())
134137```
135138
139+ ## Advanced Usage
140+
141+ ### Execution With Specific Resources
142+
143+ ``` python
144+ import asyncio
145+ from lyric import DefaultLyricDriver, PyTaskResourceConfig, PyTaskFsConfig, PyTaskMemoryConfig
146+
147+ lcd = DefaultLyricDriver(host = " localhost" , log_level = " ERROR" )
148+ lcd.start()
149+
150+ python_code = """
151+ import os
152+
153+ # List the files in the root directory
154+ root = os.listdir('/tmp/')
155+ print("Files in the root directory:", root)
156+
157+ # Create a new file in the home directory
158+ with open('/home/new_file.txt', 'w') as f:
159+ f.write('Hello, World!')
160+ """
161+
162+ async def main ():
163+ # Load workers(default: Python, JavaScript)
164+ await lcd.lyric.load_default_workers()
165+
166+ dir_read, dir_write = 1 , 2
167+ file_read, file_write = 3 , 4
168+ resources = PyTaskResourceConfig(
169+ fs = PyTaskFsConfig(
170+ preopens = [
171+ # Mount current directory in host to "/tmp" in the sandbox with read permission
172+ (" ." , " /tmp" , dir_read, file_read),
173+ # Mount "/tmp" in host to "/home" in the sandbox with read and write permission
174+ (" /tmp" , " /home" , dir_read | dir_write, file_read | file_write),
175+ ]
176+ ),
177+ memory = PyTaskMemoryConfig(
178+ # Set the memory limit to 30MB
179+ memory_limit = 30 * 1024 * 1024 # 30MB in bytes
180+ )
181+ )
182+
183+ py_res = await lcd.exec(python_code, " python" , resources = resources)
184+ assert py_res.exit_code == 0 , " Python code should exit with 0"
185+
186+ # Stop the driver
187+ lcd.stop()
188+
189+ asyncio.run(main())
190+ ```
191+
136192## Examples
137193
138194- [ Notebook-Qick Start] ( examples/notebook/lyric_quick_start.ipynb ) : A Jupyter notebook demonstrating how to use Lyric to execute Python and JavaScript code.
0 commit comments