Skip to content

Commit efd6976

Browse files
python interpreter refactor to sandbox (#1956)
* python interpreter refactor to sandbox * add deno to tests workflow
1 parent d6be9e3 commit efd6976

File tree

6 files changed

+175
-796
lines changed

6 files changed

+175
-796
lines changed

.github/workflows/run_tests.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ jobs:
4141
python-version: ["3.9"]
4242
steps:
4343
- uses: actions/checkout@v4
44+
- name: Install Deno
45+
run: |
46+
curl -fsSL https://deno.land/install.sh | sh
47+
echo "Deno installed"
48+
49+
- name: Add Deno to PATH
50+
run: echo "${HOME}/.deno/bin" >> $GITHUB_PATH
51+
52+
- name: Verify Deno installation
53+
run: deno --version
54+
4455
- name: Load cached Poetry installation
4556
id: cached-poetry
4657
uses: actions/cache@v3

docs/docs/deep-dive/modules/program-of-thought.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Program of Thought is instantiated based on a user-defined DSPy Signature, which
1919
import dsp
2020
import dspy
2121
from ..primitives.program import Module
22-
from ..primitives.python_interpreter import CodePrompt, PythonInterpreter
22+
from ..primitives.python_interpreter import PythonInterpreter
2323
import re
2424

2525
class ProgramOfThought(Module):
@@ -58,7 +58,7 @@ Executes the last stored generated code and outputs the final answer, with the s
5858
- **Code Parsing:**
5959
Program of Thought internally processes each code generation as a string and filters out extraneous bits to ensure the code block conforms to executable Python syntax. If the code is empty or does not match these guidelines, the parser returns an error string, signaling the PoT process for regeneration.
6060
- **Code Execution:**
61-
Program of Thought relies on a Python interpreter adapted by CAMEL-AI to execute code generated by LLMs. The final code generation is formatted as a CodePrompt instance and executed by the PythonInterpreter. This adaptation is present in [DSPy primitives](https://github.com/stanfordnlp/dspy/blob/main/dspy/primitives/python_interpreter.py).
61+
Program of Thought relies on a sandboxed environment Python interpreter using Deno and Pyodide adapted by [this tutorial](https://til.simonwillison.net/deno/pyodide-sandbox). This adaptation is present in [DSPy primitives](https://github.com/stanfordnlp/dspy/blob/main/dspy/primitives/python_interpreter.py).
6262

6363
## Tying It All Together
6464
Using ProgramOfThought mirrors the simplicity of the base `Predict` and `ChainOfThought` modules. Here is an example call:

dspy/predict/program_of_thought.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
from dspy.signatures.signature import ensure_signature
55

66
from ..primitives.program import Module
7-
from ..primitives.python_interpreter import CodePrompt, PythonInterpreter
7+
from ..primitives.python_interpreter import PythonInterpreter
88

99

1010
class ProgramOfThought(Module):
11-
def __init__(self, signature, max_iters=3, import_white_list=None):
11+
def __init__(self, signature, max_iters=3):
1212
super().__init__()
1313
self.signature = signature = ensure_signature(signature)
1414
self.max_iters = max_iters
15-
self.import_white_list = import_white_list
1615

1716
self.input_fields = signature.input_fields
1817
self.output_fields = signature.output_fields
@@ -152,10 +151,9 @@ def parse_code(self, code_data):
152151
def execute_code(self, code):
153152
if not code:
154153
return code, None, "Error: Empty code before execution."
155-
code_prompt = CodePrompt(code, code_type="python")
156-
interpreter = PythonInterpreter(action_space={"print": print}, import_white_list=self.import_white_list)
154+
interpreter = PythonInterpreter()
157155
try:
158-
output = str(code_prompt.execute(interpreter=interpreter)[0])
156+
output = str(interpreter.execute(code))
159157
return code, output, None
160158
except Exception as e:
161159
return code, None, str(e)

0 commit comments

Comments
 (0)