Skip to content

Commit

Permalink
python interpreter refactor to sandbox (#1956)
Browse files Browse the repository at this point in the history
* python interpreter refactor to sandbox

* add deno to tests workflow
  • Loading branch information
arnavsinghvi11 authored Dec 26, 2024
1 parent d6be9e3 commit efd6976
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 796 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ jobs:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v4
- name: Install Deno
run: |
curl -fsSL https://deno.land/install.sh | sh
echo "Deno installed"
- name: Add Deno to PATH
run: echo "${HOME}/.deno/bin" >> $GITHUB_PATH

- name: Verify Deno installation
run: deno --version

- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/deep-dive/modules/program-of-thought.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Program of Thought is instantiated based on a user-defined DSPy Signature, which
import dsp
import dspy
from ..primitives.program import Module
from ..primitives.python_interpreter import CodePrompt, PythonInterpreter
from ..primitives.python_interpreter import PythonInterpreter
import re

class ProgramOfThought(Module):
Expand Down Expand Up @@ -58,7 +58,7 @@ Executes the last stored generated code and outputs the final answer, with the s
- **Code Parsing:**
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.
- **Code Execution:**
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).
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).

## Tying It All Together
Using ProgramOfThought mirrors the simplicity of the base `Predict` and `ChainOfThought` modules. Here is an example call:
Expand Down
10 changes: 4 additions & 6 deletions dspy/predict/program_of_thought.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
from dspy.signatures.signature import ensure_signature

from ..primitives.program import Module
from ..primitives.python_interpreter import CodePrompt, PythonInterpreter
from ..primitives.python_interpreter import PythonInterpreter


class ProgramOfThought(Module):
def __init__(self, signature, max_iters=3, import_white_list=None):
def __init__(self, signature, max_iters=3):
super().__init__()
self.signature = signature = ensure_signature(signature)
self.max_iters = max_iters
self.import_white_list = import_white_list

self.input_fields = signature.input_fields
self.output_fields = signature.output_fields
Expand Down Expand Up @@ -152,10 +151,9 @@ def parse_code(self, code_data):
def execute_code(self, code):
if not code:
return code, None, "Error: Empty code before execution."
code_prompt = CodePrompt(code, code_type="python")
interpreter = PythonInterpreter(action_space={"print": print}, import_white_list=self.import_white_list)
interpreter = PythonInterpreter()
try:
output = str(code_prompt.execute(interpreter=interpreter)[0])
output = str(interpreter.execute(code))
return code, output, None
except Exception as e:
return code, None, str(e)
Expand Down
Loading

0 comments on commit efd6976

Please sign in to comment.