Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python interpreter refactor to sandbox #1956

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions dspy/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dspy.primitives.module import BaseModule
from dspy.primitives.prediction import Prediction, Completions
from dspy.primitives.program import Program, Module
from dspy.primitives.python_interpreter import PythonInterpreter, TextPrompt, CodePrompt
from dspy.primitives.python_interpreter import PythonInterpreter


__all__ = [
Expand All @@ -15,6 +15,4 @@
"Program",
"Module",
"PythonInterpreter",
"TextPrompt",
"CodePrompt",
]
Loading
Loading