from dart_math.exec import *Code Execution
Execute code in text efficiently and safely.
Execution Code Cells as in Notebooks
from dart_math.exec import *
exec_cells(
[
"print('Hey, Jude')",
"print('Don\\'t make it bad')",
"print('Take a sad song and make it better')",
]
) # Only return the stdout and stderr of the last cell('Take a sad song and make it better', '')
exec_cells
exec_cells (cells:list[str])
Execute the code cells like a notebook and return the stdout and stderr of the last cell. Modified from - https://github.com/Kipok/NeMo-Skills/blob/6a909ec0974340b02a1083dce90e79bea30ecb60/nemo_skills/code_execution/sandbox.py#L168-L233 - https://github.com/deepseek-ai/DeepSeek-Math/blob/b8b0f8ce093d80bf8e9a641e44142f06d092c305/evaluation/infer/run_tool_integrated_eval.py#L163-L180
| Type | Details | |
|---|---|---|
| cells | list | The code cells to execute. |
| Returns | str |
Unified Language & Code Context Configuration
code_exec_cfg = CodeExecCfg.load_from_id_or_path("python")
code_exec_cfg.__dict__{'input_begin': '```python',
'input_end': '```',
'output_code_prefix': 'print(',
'output_begin': '```output',
'output_end': '```',
'timeout': 5,
'max_n_calls': None,
'trunc_len': None,
'elipsis': '...'}
code_exec_cfg<dart_math.exec.CodeExecCfg>
EG_LANG_CODE_CONTEXT = """
```python
print('Hey, Jude')
```
```output
Hey, Jude
```
Don't make it bad
```python
print('Take a sad song and make it better')
```
"""code_exec_cfg.no_cells_todo(EG_LANG_CODE_CONTEXT)0
code_exec_cfg.no_cells_todo(
EG_LANG_CODE_CONTEXT + "```output\nTake a sad song and make it better```"
)1
code_exec_cfg.extract_cells(EG_LANG_CODE_CONTEXT)["print('Hey, Jude')", "print('Take a sad song and make it better')"]
code_exec_cfg.wrap_output("Take a sad song and make it better")
# Usually appended with some newlines'```output\nTake a sad song and make it better\n```'
CodeExecCfg
CodeExecCfg (input_begin:str='```python', input_end:str='```', output_code_prefix:str='print(', output_begin:str='```output', output_end:str='```', timeout:int=5, max_n_workers:int=4, max_n_calls:int=None, trunc_len:tuple[int,int]=None, elipsis:str='...')
Configuration for code execution.
| Type | Default | Details | |
|---|---|---|---|
| input_begin | str | python | | | input_end | str | |
|
| output_code_prefix | str | print( | Prefix of code that will be executed to display the output. |
| output_begin | str | output | | | output_end | str | |
|
| timeout | int | 5 | Timeout in seconds for code execution. |
| max_n_workers | int | 4 | The maximum number of CPU core workers to execute the code with multi-processing. |
| max_n_calls | int | None | The maximum number of calls to the code execution function. This could be large because there is token length limit already. None / Non-positive values mean no limit. |
| trunc_len | tuple | None | The maximum lengths to truncate the output into the beginning and end.None / double non-positive values like (0, 0) mean no truncation. |
| elipsis | str | … | The elipsis to use when truncating the output. |
CodeExecCfg.load_from_id_or_path
CodeExecCfg.load_from_id_or_path (tool_config:str='python')
Load the configuration from the ID or path.
| Type | Default | Details | |
|---|---|---|---|
| tool_config | str | python | ID / Path to file of the code executeion configuration. |
| Returns | CodeExecCfg | The code execution configuration object. |
CodeExecCfg.no_cells_todo
CodeExecCfg.no_cells_todo (context:str)
Judge if there are no code cells to execute.
| Type | Details | |
|---|---|---|
| context | str | The whole context containing all the code cells. |
| Returns | int | 0: Normal 1: No code cells to execute 2: Output cells are more than input cells |
test_eq(
code_exec_cfg.no_cells_todo(EG_LANG_CODE_CONTEXT), False
) # 2 code cells but only 1 executed to output
test_eq(
code_exec_cfg.no_cells_todo(
EG_LANG_CODE_CONTEXT + "```output\nTake a sad song and make it better```"
),
True,
) # All the code cells have been executedCodeExecCfg.extract_cells
CodeExecCfg.extract_cells (text:str)
Extract code cells from the text.
| Type | Details | |
|---|---|---|
| text | str | The text to extract code cells from. |
| Returns | list | The extracted code cells. |
test_eq(
code_exec_cfg.extract_cells(EG_LANG_CODE_CONTEXT),
[
"print('Hey, Jude')",
# "print('Don\\'t make it bad')",
"print('Take a sad song and make it better')",
],
)CodeExecCfg.wrap_output
CodeExecCfg.wrap_output (output:str)
Return f"{self.output_begin}\n{output}\n{self.output_end}"
test_eq(
code_exec_cfg.wrap_output("Take a sad song and make it better"),
"```output\nTake a sad song and make it better\n```",
)