Repository navigation
Expand file tree
/
Copy pathtest_program_of_thought.py
More file actions
273 lines (216 loc) · 9.25 KB
/
Copy pathtest_program_of_thought.py
File metadata and controls
273 lines (216 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import threading
from unittest.mock import Mock, patch
import pytest
import dspy
from dspy import ProgramOfThought, Signature
from dspy.evaluate.metrics import answer_exact_match
from dspy.primitives.code_interpreter import CodeInterpreterError, FinalOutput
from dspy.primitives.python_interpreter import PythonInterpreter
from dspy.utils import DummyLM
from tests.mock_interpreter import MockInterpreter, MockInterpreterFactory
class BasicQA(Signature):
question = dspy.InputField()
answer = dspy.OutputField(desc="often between 1 and 5 words")
class StaticPredictor:
def __init__(self, **fields):
self.fields = fields
def __call__(self, **kwargs):
return dspy.Prediction(**self.fields)
class RecordingPythonInterpreterFactory:
def __init__(self, parties: int):
self.instances = []
self._lock = threading.Lock()
self._barrier = threading.Barrier(parties)
def __call__(self):
interpreter = PythonInterpreter()
with self._lock:
self.instances.append(interpreter)
self._barrier.wait(timeout=30)
return interpreter
def test_pot_warns_that_rlm_is_preferred():
with pytest.warns(
DeprecationWarning,
match=r"ProgramOfThought is deprecated and will be removed in 3\.5\. RLM is the preferred replacement\.",
):
ProgramOfThought(BasicQA)
@pytest.mark.deno
def test_pot_code_generation(pooled_interpreter):
lm = DummyLM(
[
{
"reasoning": "Reason_A",
"generated_code": "```python\nresult = 1+1\nSUBMIT({'answer': result})\n```",
},
{"reasoning": "Reason_B", "answer": "2"},
]
)
dspy.configure(lm=lm)
pot = ProgramOfThought(BasicQA)
res = pot(pooled_interpreter, question="What is 1+1?")
assert res.answer == "2"
# This test ensures the old finetuned saved models still work
@pytest.mark.deno
def test_old_style_pot(pooled_interpreter):
lm = DummyLM(
[
{"reasoning": "Reason_A", "generated_code": "```python\nresult = 1+1\n```"},
{"reasoning": "Reason_B", "answer": "2"},
]
)
dspy.configure(lm=lm)
pot = ProgramOfThought(BasicQA)
res = pot(pooled_interpreter, question="What is 1+1?")
assert res.answer == "2"
class ExtremumFinder(Signature):
input_list = dspy.InputField()
maximum = dspy.OutputField(desc="The maximum of the given numbers")
minimum = dspy.OutputField(desc="The minimum of the given numbers")
@pytest.mark.deno
def test_pot_support_multiple_fields(pooled_interpreter):
lm = DummyLM(
[
{
"reasoning": "Reason_A",
"generated_code": "```python\nmaximum = 6\nminimum = 2\nSUBMIT({'maximum': maximum, 'minimum': minimum})\n```",
},
{"reasoning": "Reason_B", "maximum": "6", "minimum": "2"},
]
)
dspy.configure(lm=lm)
pot = ProgramOfThought(ExtremumFinder)
res = pot(pooled_interpreter, input_list="2, 3, 5, 6")
assert res.maximum == "6"
assert res.minimum == "2"
@pytest.mark.deno
def test_pot_code_generation_with_one_error(pooled_interpreter):
lm = DummyLM(
[
{
"reasoning": "Reason_A",
"generated_code": "```python\nresult = 1+0/0\nSUBMIT({'answer': result})\n```",
},
{
"reasoning": "Reason_B",
"generated_code": "```python\nresult = 1+1\nSUBMIT({'answer': result})\n```",
},
{"reasoning": "Reason_C", "answer": "2"},
]
)
dspy.configure(lm=lm)
pot = ProgramOfThought(BasicQA)
res = pot(pooled_interpreter, question="What is 1+1?")
assert res.answer == "2"
@pytest.mark.deno
def test_pot_evaluate_creates_one_interpreter_per_example():
factory = RecordingPythonInterpreterFactory(parties=4)
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
pot.code_generate = StaticPredictor(generated_code="SUBMIT({'answer': 2})")
pot.generate_output = StaticPredictor(answer="2")
devset = [
dspy.Example(question=f"What is 1+1? ({index})", answer="2").with_inputs("question") for index in range(4)
]
result = dspy.Evaluate(
devset=devset,
metric=answer_exact_match,
num_threads=4,
display_progress=False,
)(pot)
assert result.score == 100.0
assert len(factory.instances) == 4
assert len({id(interpreter) for interpreter in factory.instances}) == 4
assert all(interpreter.deno_process is None for interpreter in factory.instances)
def test_pot_factory_creates_fresh_interpreter_per_sequential_call():
factory = MockInterpreterFactory(responses=[FinalOutput({"answer": "2"})])
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
pot.code_generate = StaticPredictor(generated_code="SUBMIT({'answer': 2})")
pot.generate_output = StaticPredictor(answer="2")
first = pot(question="What is 1+1?")
second = pot(question="What is 1+1 again?")
assert first.answer == second.answer == "2"
assert len(factory.instances) == 2
assert factory.instances[0] is not factory.instances[1]
for interpreter in factory.instances:
with pytest.raises(CodeInterpreterError, match="shutdown"):
interpreter.execute("print('closed')")
def test_pot_allows_interpreter_as_signature_input():
factory = MockInterpreterFactory(responses=[FinalOutput({"answer": "CPython"})])
pot = ProgramOfThought("interpreter -> answer", interpreter_factory=factory)
pot.code_generate = Mock(return_value=dspy.Prediction(generated_code="SUBMIT({'answer': interpreter})"))
pot.generate_output = StaticPredictor(answer="CPython")
result = pot(interpreter="CPython")
assert result.answer == "CPython"
pot.code_generate.assert_called_once_with(interpreter="CPython")
def test_pot_rejects_keyword_interpreter_override():
factory = MockInterpreterFactory()
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
with pytest.raises(TypeError, match="first positional argument"):
pot(question="What is 1+1?", interpreter=MockInterpreter())
assert factory.instances == []
def test_pot_rejects_removed_constructor_interpreter_keyword():
with pytest.raises(TypeError, match="unexpected keyword argument 'interpreter'"):
ProgramOfThought(BasicQA, interpreter=MockInterpreter())
def test_pot_does_not_shutdown_caller_owned_interpreter():
factory = MockInterpreterFactory()
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
pot.code_generate = StaticPredictor(generated_code="SUBMIT({'answer': 2})")
pot.generate_output = StaticPredictor(answer="2")
interpreter = MockInterpreter(responses=[FinalOutput({"answer": "2"})])
try:
result = pot(interpreter, question="What is 1+1?")
assert result.answer == "2"
assert factory.instances == []
assert interpreter.execute("print('still open')") == ""
finally:
interpreter.shutdown()
def test_pot_shuts_down_factory_interpreter_when_execution_raises():
factory = MockInterpreterFactory(responses=[ValueError("unexpected interpreter failure")])
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
pot.code_generate = StaticPredictor(generated_code="raise ValueError")
with pytest.raises(ValueError, match="unexpected interpreter failure"):
pot(question="What is 1+1?")
assert len(factory.instances) == 1
with pytest.raises(CodeInterpreterError, match="shutdown"):
factory.instances[0].execute("print('closed')")
def test_pot_propagates_terminal_interpreter_failure_and_shuts_down():
factory = MockInterpreterFactory(responses=[CodeInterpreterError("protocol corrupt")])
pot = ProgramOfThought(BasicQA, interpreter_factory=factory)
pot.code_generate = StaticPredictor(generated_code="print('test')")
with pytest.raises(CodeInterpreterError, match="protocol corrupt"):
pot(question="What is 1+1?")
assert len(factory.instances) == 1
with pytest.raises(CodeInterpreterError, match="shutdown"):
factory.instances[0].execute("print('closed')")
@pytest.mark.deno
def test_pot_code_generation_persistent_errors():
max_iters = 3
lm = DummyLM(
[
{
"reasoning": "Reason_A",
"generated_code": "```python\nresult = 1+0/0\nSUBMIT({'answer': result})\n```",
},
]
* max_iters
)
dspy.configure(lm=lm)
pot = ProgramOfThought(BasicQA, max_iters=max_iters)
with pytest.raises(RuntimeError, match=r"Max hops reached. Failed to run ProgramOfThought: ZeroDivisionError:"):
pot(question="What is 1+1?")
def test_pot_code_parse_error():
max_iters = 3
lm = DummyLM(
[
{"reasoning": "Reason_A", "generated_code": "```python\ninvalid=python=code\n```"},
]
* max_iters
)
dspy.configure(lm=lm)
pot = ProgramOfThought(BasicQA, max_iters=max_iters)
with (
patch("dspy.predict.program_of_thought.ProgramOfThought._execute_code") as mock_execute_code,
pytest.raises(
RuntimeError, match=r"Max hops reached. Failed to run ProgramOfThought: Error: Code format is not correct."
),
):
pot(question="What is 1+1?")
mock_execute_code.assert_not_called()