dspy-side-questions 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dspy_side_questions-0.1.0/.gitignore +33 -0
- dspy_side_questions-0.1.0/LICENSE +21 -0
- dspy_side_questions-0.1.0/PKG-INFO +346 -0
- dspy_side_questions-0.1.0/README.md +317 -0
- dspy_side_questions-0.1.0/pyproject.toml +48 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/__init__.py +17 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/adapter.py +95 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/backend.py +98 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/combiner.py +176 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/decisions.py +142 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/fitting.py +190 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/optimizer.py +298 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/program.py +173 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/questions.py +93 -0
- dspy_side_questions-0.1.0/src/dspy_side_questions/reflection.py +231 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
.mypy_cache/
|
|
7
|
+
.ruff_cache/
|
|
8
|
+
.ipynb_checkpoints/
|
|
9
|
+
|
|
10
|
+
# Environments and secrets
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
.env
|
|
14
|
+
.env.*
|
|
15
|
+
|
|
16
|
+
# Third-party datasets (kept locally, too large to version)
|
|
17
|
+
experiments/data/
|
|
18
|
+
|
|
19
|
+
# Editors and OS
|
|
20
|
+
.DS_Store
|
|
21
|
+
.idea/
|
|
22
|
+
.vscode/
|
|
23
|
+
*.swp
|
|
24
|
+
|
|
25
|
+
# Build output
|
|
26
|
+
build/
|
|
27
|
+
dist/
|
|
28
|
+
|
|
29
|
+
# Run logs from the experiment scripts
|
|
30
|
+
experiments/**/logs/
|
|
31
|
+
|
|
32
|
+
# Private research journal
|
|
33
|
+
NOTES.md
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Drew Breunig
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dspy-side-questions
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A DSPy optimizer that adds learned yes/no side questions to decision-model programs.
|
|
5
|
+
Project-URL: Homepage, https://github.com/cmpnd-ai/dspy-side-questions
|
|
6
|
+
Project-URL: Source, https://github.com/cmpnd-ai/dspy-side-questions
|
|
7
|
+
Project-URL: Issues, https://github.com/cmpnd-ai/dspy-side-questions/issues
|
|
8
|
+
Author-email: Drew Breunig <hello@cmpnd.ai>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: classification,dspy,gepa,optimizer,prompt optimization
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: dspy>=3.5.0
|
|
22
|
+
Requires-Dist: gepa
|
|
23
|
+
Requires-Dist: numpy
|
|
24
|
+
Requires-Dist: pydantic>=2
|
|
25
|
+
Requires-Dist: scikit-learn>=1.8
|
|
26
|
+
Provides-Extra: test
|
|
27
|
+
Requires-Dist: pytest; extra == 'test'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# `SideQuestions`: A Prototype DSPy Optimizer for System One Programs
|
|
31
|
+
|
|
32
|
+
`dspy-side-questions` is a DSPy optimizer for programs that call decision models, like Jev. It writes extra yes/no questions about each input and asks them in the same call as a program's original questions. For each output, a small regression model fitted on your metric combines the answers into the final decision.
|
|
33
|
+
|
|
34
|
+
For example, if our question is:
|
|
35
|
+
|
|
36
|
+
- Was this hotel review written by someone who did not actually stay at the hotel?
|
|
37
|
+
|
|
38
|
+
We can improve our performance by asking the following questions as well:
|
|
39
|
+
|
|
40
|
+
- Does the review identify a specific booking channel or rewards program used to arrange the stay?
|
|
41
|
+
- Does the review tell a connected sequence of events, one incident leading to the next?
|
|
42
|
+
- Is the review mainly broad judgments about quality, comfort, cleanliness, service, or location?
|
|
43
|
+
- Does the review compare this experience with the author's own previous hotel experiences?
|
|
44
|
+
|
|
45
|
+
In our benchmarks, this raised accuracy by up to 28 points, at about twice the cost per call and 1.0 to 1.2 times the latency.
|
|
46
|
+
|
|
47
|
+
<picture>
|
|
48
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/cmpnd-ai/dspy-side-questions/main/assets/hotel-reviews-dark.svg">
|
|
49
|
+
<img src="https://raw.githubusercontent.com/cmpnd-ai/dspy-side-questions/main/assets/hotel-reviews-light.svg" width="100%" alt="Bar chart of test accuracy on deceptive hotel reviews: Jev 57.3% at 0.06 s and $0.026 per 1,000 reviews, Jev with SideQuestions 79.5% at 0.07 s and $0.059, GPT-6-Luna 72.0% at 1.74 s and $0.136.">
|
|
50
|
+
</picture>
|
|
51
|
+
|
|
52
|
+
## How SideQuestions Works
|
|
53
|
+
|
|
54
|
+
Decision models, also called System One models, are fast, cheap models that return structured answers to decisions as probabilities. They read an input once and answer typed questions about it: yes/no (`Noul`), one of several options (`Choice`), a level on an ordered scale (`Score`), and more.
|
|
55
|
+
|
|
56
|
+
One way decision models achieve their speed is by abstaining from reasoning. A reasoning LLM can work through a question from several angles before it answers, searching for approaches before landing on a final response. Decision models answer each question in a single pass, so they struggle when an answer benefits from combining several clues.
|
|
57
|
+
|
|
58
|
+
Decision models also bill differently. A request has one [state](https://docs.typesafe.ai/concepts/state.md), the task description and the input, plus any number of questions. The model reads the state once and evaluates each question against it [independently](https://docs.typesafe.ai/cookbooks/parallel_questions.md) of the other questions. For example, Jev [charges per input token](https://docs.typesafe.ai/models.md), so the state is paid for once per request, and each added question costs only its own tokens. In [our measurements](https://github.com/cmpnd-ai/dspy-side-questions/blob/main/experiments/opspam/results/user/cost.json) from [`experiments/jev_cost.py`](https://github.com/cmpnd-ai/dspy-side-questions/blob/main/experiments/jev_cost.py), each added question costs about 70 to 80 input tokens, so a call with 11 questions costs about twice as much as a call with one.
|
|
59
|
+
|
|
60
|
+
The new questions deliver new information, but we need to apply that information to our original questions. To do this, `SideQuestions` builds a small regression model to combine the answers. Its inputs are the original answer and each new answer, a probability from 0.0 to 1.0. `SideQuestions` fits the regression against your metric, with a weight for each answer. For each training example, the metric scores every possible answer to the original question, and the regression learns to favor the answers that score well. An L1 penalty sets the weight of an unhelpful question to exactly zero, and cross-validation on the training examples picks the penalty's strength.
|
|
61
|
+
|
|
62
|
+
The optimized program is a `SideQuestionsPredict` module, whose `forward` method does the combining. `forward` sends the original questions and the side questions to the decision model in one request, then multiplies the answers by their weights, adds the results, and converts the sum back into a probability or a level. The output field's threshold, choice weights or score cuts then turn that number into a decision, so the optimized program returns the same types as the original.
|
|
63
|
+
|
|
64
|
+
## SideQuestions in Action
|
|
65
|
+
|
|
66
|
+
Let's consider a program that tries to detect fake hotel reviews:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import dspy
|
|
70
|
+
from dspy.experimental import Choice
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Review(dspy.Signature):
|
|
74
|
+
"""Decide whether an online hotel review is a genuine guest review or a fake one."""
|
|
75
|
+
|
|
76
|
+
review: str = dspy.InputField(desc="A review of a Chicago hotel.")
|
|
77
|
+
verdict: Choice[("Deceptive", "Fabricated by someone who did not stay at the hotel."),
|
|
78
|
+
("Truthful", "Written by a real guest about an actual stay.")] = dspy.OutputField(
|
|
79
|
+
desc="Was this hotel review written by someone who did not actually stay at the hotel?")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
program = dspy.Predict(Review)
|
|
83
|
+
program.set_lm(dspy.LM("typesafe:jev-latest"))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
When run, the above will ask a decision model one question, “Was this hotel review written by someone who did not actually stay at the hotel?”
|
|
87
|
+
|
|
88
|
+
A decision model reads the review once and answers with a probability, and on its own it gets 57% of reviews right. Fake reviews leave clues that a decision model can spot, but a decision model might not connect those clues to its verdict.
|
|
89
|
+
|
|
90
|
+
The optimizer shows a reflection LLM the program's mistakes and asks it to write yes/no questions about those clues:
|
|
91
|
+
|
|
92
|
+
- Is the review mainly broad judgments about quality, comfort, cleanliness, service or location?
|
|
93
|
+
- Does the review name the booking channel or rewards program used for the stay?
|
|
94
|
+
- Does the review tell a connected sequence of events, one incident leading to the next?
|
|
95
|
+
|
|
96
|
+
Jev answers these side questions in the same request as the original question, so the review's tokens are billed once. A regression model fitted on your metric learns how much each answer should move the verdict. GEPA tries several lists of side questions and keeps the best one. On this benchmark, the optimized program gets 80% of reviews right.
|
|
97
|
+
|
|
98
|
+
A program with several outputs shares one list of side questions, and each output gets its own regression model. The regression's L1 penalty gives unhelpful questions zero weight, and the returned program stops asking them.
|
|
99
|
+
|
|
100
|
+
## Install
|
|
101
|
+
|
|
102
|
+
The package needs Python 3.11 or later and DSPy 3.5.
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pip install dspy-side-questions
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
The decision LM and the reflection LM each need their provider's key in the environment, such as `TYPESAFE_API_KEY` for Jev.
|
|
109
|
+
|
|
110
|
+
To work on the package or run the benchmarks, install it from a clone. `requirements.txt` installs the package in editable mode and adds the libraries the experiment scripts use.
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
python -m venv .venv
|
|
114
|
+
.venv/bin/pip install -r requirements.txt
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Usage
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
import dspy
|
|
121
|
+
from dspy.experimental import Noul
|
|
122
|
+
|
|
123
|
+
from dspy_side_questions import SideQuestions
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class Phish(dspy.Signature):
|
|
127
|
+
"""Classify an email."""
|
|
128
|
+
|
|
129
|
+
email: str = dspy.InputField(desc="The full email, headers and body.")
|
|
130
|
+
is_phishing: Noul[(True, "A phishing attempt"), (False, "Legitimate")] = dspy.OutputField(
|
|
131
|
+
desc="Is this email phishing?")
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def accuracy(example, pred, trace=None):
|
|
135
|
+
return float(pred.is_phishing.value == example.is_phishing)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
student = dspy.Predict(Phish)
|
|
139
|
+
student.set_lm(dspy.LM("typesafe:jev-latest"))
|
|
140
|
+
|
|
141
|
+
optimizer = SideQuestions(accuracy, reflection_lm=dspy.LM("openai/gpt-6-luna"))
|
|
142
|
+
program = optimizer.compile(student, trainset=train, valset=val)
|
|
143
|
+
|
|
144
|
+
program(email="...").is_phishing # a Noul, like the student's output
|
|
145
|
+
print(optimizer.report)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
A signature with several decision outputs works the same way. The metric scores the whole prediction:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from dspy.experimental import Choice, Score
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class Ticket(dspy.Signature):
|
|
155
|
+
"""Triage a support ticket."""
|
|
156
|
+
|
|
157
|
+
text: str = dspy.InputField()
|
|
158
|
+
urgent: Noul = dspy.OutputField(desc="Is the ticket urgent?")
|
|
159
|
+
team: Choice[("billing", "Billing"), ("tech", "Technical"), ("sales", "Sales")] = dspy.OutputField(
|
|
160
|
+
desc="Which team should handle it?")
|
|
161
|
+
anger: Score[("none", "low", "high", "extreme")] = dspy.OutputField(desc="How angry is the customer?")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def triage_score(example, pred, trace=None):
|
|
165
|
+
return (float(pred.urgent.value == example.urgent) + float(pred.team.value == example.team)
|
|
166
|
+
+ 1 - abs(pred.anger.level - example.anger) / 3) / 3
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
student = dspy.Predict(Ticket)
|
|
170
|
+
student.set_lm(dspy.LM("typesafe:jev-latest"))
|
|
171
|
+
|
|
172
|
+
optimizer = SideQuestions(triage_score, reflection_lm=dspy.LM("openai/gpt-6-luna"))
|
|
173
|
+
program = optimizer.compile(student, trainset=train, valset=val)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Requirements on the student
|
|
177
|
+
|
|
178
|
+
- The student is a `dspy.Predict`, or a module that holds exactly one `Predict`.
|
|
179
|
+
- Every output field is typed as `Noul`, `Choice` or `Score`. The signature may have several.
|
|
180
|
+
- The `Predict` has a decision LM, set with `set_lm` or `dspy.configure`.
|
|
181
|
+
- The `Predict`'s input fields are read from each example.
|
|
182
|
+
- The metric scores the student's output, like any DSPy metric. Examples need gold labels only if the metric reads them.
|
|
183
|
+
|
|
184
|
+
### What `compile` returns
|
|
185
|
+
|
|
186
|
+
`compile` holds half the valset out of the search (`check_fraction=0.5`). After the search, each output picks one of three options on that held-out check set:
|
|
187
|
+
|
|
188
|
+
1. `student`: the student's answer, unchanged;
|
|
189
|
+
2. `reanchor_only`: the student's answer reanchored, which means recalibrated on the trainset by a combiner with no side questions;
|
|
190
|
+
3. `side_questions`: a combiner over the answer and the best candidate's side questions.
|
|
191
|
+
|
|
192
|
+
`compile` picks one output at a time and switches an output away from the student's answer only when the switch raises the metric. On ties it keeps the simpler option. `compile` returns a copy of the student. Unless every output keeps the student's answer, the copy's `Predict` is replaced by a `SideQuestionsPredict` module that asks only the side questions its chosen combiners use. A wrapper module keeps its own `forward`.
|
|
193
|
+
|
|
194
|
+
### Reading the report
|
|
195
|
+
|
|
196
|
+
`optimizer.report` explains what `compile` chose. This report comes from the two-output hotel-review run under Results:
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
{
|
|
200
|
+
"chosen": {"verdict": "side_questions", "positive": "student"},
|
|
201
|
+
"check": {"examples": 40, "student": 0.7625, "reanchor_only": 0.8125,
|
|
202
|
+
"side_questions": 0.85, "selected": 0.85, "metric": "mean of metric"},
|
|
203
|
+
"questions": [{"name": "spatial_observations", "instructions": "Does `review` describe a concrete spatial relationship, ...?", ...}, ...],
|
|
204
|
+
"weights": {"verdict": [0.77, 0.05, 1.52, ...]},
|
|
205
|
+
"accepted_candidates": 1,
|
|
206
|
+
"val_scores": [0.85, 0.9375],
|
|
207
|
+
"decision_calls": 480,
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
| Key | Meaning |
|
|
212
|
+
| --- | --- |
|
|
213
|
+
| `chosen` | The option each output uses in the returned program. |
|
|
214
|
+
| `check` | Scores on the held-out check set. `student`, `reanchor_only` and `side_questions` give every output the same option, and `side_questions` falls back to the student's answer for outputs without side-question weights. `selected` scores the mix in `chosen`, which is the returned program. Compare `selected` with `student` to see the improvement. On a small check set (`examples`), a few points can be noise. |
|
|
215
|
+
| `questions` | The side questions the returned program asks. Questions no output uses are dropped. |
|
|
216
|
+
| `weights` | For each output that chose side questions, its combiner's weight on each question, in the order of `questions`. A larger absolute weight moves the decision more. A weight of 0 means another output uses that question. |
|
|
217
|
+
| `accepted_candidates` | How many question lists the search accepted. |
|
|
218
|
+
| `val_scores` | Each accepted list's score on the search half of the valset, starting with the empty list. The search selected on these scores, so they run high. |
|
|
219
|
+
| `decision_calls` | Requests `compile` sent to the decision LM. |
|
|
220
|
+
|
|
221
|
+
### Saving and loading
|
|
222
|
+
|
|
223
|
+
`SideQuestionsPredict` keeps the student's signature and output types. It applies each field's threshold, Choice weights or Score cuts from `Predict.fields`. `program.save(path)` writes JSON. To restore it, build the student again, then call `with_side_questions(student).load(path)`. `with_side_questions` swaps the student's `Predict` for an empty `SideQuestionsPredict`, which `load` fills in.
|
|
224
|
+
|
|
225
|
+
### Main options
|
|
226
|
+
|
|
227
|
+
| Argument | Default | Meaning |
|
|
228
|
+
| --- | --- | --- |
|
|
229
|
+
| `metric` | required | Per-example DSPy metric over the program's output. It returns a number, or a `dspy.Prediction` with `score` and `feedback`. GEPA scores every candidate with it, the combiners are fitted against it, and its feedback goes to the reflection LM. |
|
|
230
|
+
| `reflection_lm` | `None` | The LM that writes side questions. Required unless `instruction_proposer` is given. |
|
|
231
|
+
| `instruction_proposer` | `None` | A function that replaces the default proposer, as in GEPA. |
|
|
232
|
+
| `dataset_metric` | `None` | `dataset_metric(examples, predictions)`. It replaces the mean of `metric` in the final comparison, for metrics such as Spearman, F1 or QWK. |
|
|
233
|
+
| `max_questions` | `None` | Side questions per candidate. `None` means 10 for one output and 15 for several. |
|
|
234
|
+
| `max_proposals` | 3 | GEPA iterations, each with at most one reflection call. |
|
|
235
|
+
| `reflection_minibatch_size` | 60 | Training examples the reflection LM sees per proposal. |
|
|
236
|
+
| `check_fraction` | 0.5 | Share of the valset held out for the final comparison. |
|
|
237
|
+
| `valset` (on `compile`) | `None` | Examples for the search and the final comparison. Without one, `compile` holds out a third of the trainset. |
|
|
238
|
+
| `checkset` (on `compile`) | `None` | An explicit check set. Pass one when examples come in groups that must not straddle the split. |
|
|
239
|
+
| `C` | `None` | Combiner penalty. `None` picks it for each output and candidate by cross-validation on the trainset. |
|
|
240
|
+
| `class_weight` | `None` | `"balanced"` reweights rare labels for Noul and Choice outputs. |
|
|
241
|
+
| `n_folds` | 5 | Cross-fitting folds for scoring trainset examples. |
|
|
242
|
+
| `num_threads` | 8 | Concurrent metric calls. |
|
|
243
|
+
|
|
244
|
+
## How it works
|
|
245
|
+
|
|
246
|
+
- **One call per input.** The optimizer adds each side question to the student's signature as a Noul output field, so every original question and side question goes to the decision LM in one request.
|
|
247
|
+
- **Cached answers.** `Prober` caches answers per (input, question), so a new candidate pays only for its new questions. System One models such as Jev answer each question independently of the others in the request, so the cache is exact. A generative LLM writes all its answers in one response, so its answers can shift with the other questions, and the cache is an approximation during the search.
|
|
248
|
+
- **One combiner per output.** Each output gets its own `Combiner` over its original answer and every side question, so outputs can rely on different questions. `Combiner` fits an L1 logistic regression for Noul outputs, a multinomial L1 logistic regression for Choice outputs, and an L1 linear regression on the expected level for Score outputs. Score outputs also fit their cut points. L1 drives useless questions to zero weight, and `compile` drops them so production calls do not pay for them.
|
|
249
|
+
- **Fitting against the metric.** For each output and training example, the metric scores every possible answer: both values of a Noul, each Choice option, each Score level. The program's other outputs stay at the student's answers. Each answer that beats the example's worst answer becomes a fit row, weighted by its margin. With a 0/1 metric over gold labels, this is an ordinary fit on the labels. The scores are computed once and cached, so an LM-judge metric costs one call per possible answer per training example.
|
|
250
|
+
- **Held-out search signal.** Each output's combiner scores trainset examples with cross-fitted predictions, and the metric combines the outputs into one score per example. GEPA's minibatch scores and the failures shown to the reflection LM are therefore held-out errors.
|
|
251
|
+
- **Reflection prompt.** `llm_proposer`, the default instruction proposer, frames the question list as twenty questions written in advance. It asks for broad questions that apply to most inputs and split them along dimensions that matter for the original questions. With several outputs, the reflection LM sees every original question and writes one list that serves all of them. To replace it, pass `instruction_proposer=` to `SideQuestions`, as with GEPA. `default_proposer_instructions()` returns the prompt for building variants.
|
|
252
|
+
|
|
253
|
+
## Results
|
|
254
|
+
|
|
255
|
+
Each benchmark below follows one protocol: one train/val/test split, one `compile` call with default settings, and a test-set comparison of the student and the returned program. `openai/gpt-6.1-sol` is the reflection LM. Brackets give 95% bootstrap intervals.
|
|
256
|
+
|
|
257
|
+
### Across benchmarks, on Jev
|
|
258
|
+
|
|
259
|
+
`jev-latest` is the decision LM. The chart compares each result with DSPy's `ReAnchor`, which recalibrates the student's answer without adding questions.
|
|
260
|
+
|
|
261
|
+
<picture>
|
|
262
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/cmpnd-ai/dspy-side-questions/main/assets/benchmarks-dark.svg">
|
|
263
|
+
<img src="https://raw.githubusercontent.com/cmpnd-ai/dspy-side-questions/main/assets/benchmarks-light.svg" width="100%" alt="Bar chart of seven benchmarks on Jev, each with Jev, Jev with ReAnchor and Jev with SideQuestions, and each arm's score, gain, questions per call, latency and cost. SideQuestions scores highest on six of the seven.">
|
|
264
|
+
</picture>
|
|
265
|
+
|
|
266
|
+
| Benchmark | Output | Returned | Student → program on test |
|
|
267
|
+
| --- | --- | --- | --- |
|
|
268
|
+
| Deceptive hotel reviews | Choice | 10 side questions | accuracy 0.573 → 0.795, +0.222 [+0.173, +0.269] |
|
|
269
|
+
| Beemo, raw model output | Choice | 10 side questions | accuracy 0.502 → 0.782, +0.280 [+0.227, +0.330] |
|
|
270
|
+
| Beemo, expert-edited output | Choice | 5 side questions | accuracy 0.502 → 0.627, +0.125 [+0.065, +0.178] |
|
|
271
|
+
| Ghostbuster essays | Choice | 4 side questions | accuracy 0.512 → 1.000, +0.488 [+0.452, +0.525] |
|
|
272
|
+
| LegalBench learned_hands_consumer | Choice | 8 side questions | accuracy 0.725 → 0.912, +0.186 [+0.078, +0.294] |
|
|
273
|
+
| CIRCL vulnerability severity | Score | Reanchor only | QWK 0.407 → 0.496, +0.090 [+0.027, +0.150] |
|
|
274
|
+
| SummEval consistency | Score | Reanchor only | Spearman 0.524 → 0.633, +0.109 [+0.037, +0.183] |
|
|
275
|
+
|
|
276
|
+
Side questions help most when the decision model ranks the inputs poorly. When it ranks them well and only its cutoff is wrong, reanchoring alone fixes the cutoff without extra questions. On SummEval, reanchoring raises Spearman mostly by moving high predictions to the top of the scale, where they tie.
|
|
277
|
+
|
|
278
|
+
### Across decision models, on deceptive hotel reviews
|
|
279
|
+
|
|
280
|
+
These runs repeat the hotel-review benchmark with three other decision models through the LunaRoute gateway.
|
|
281
|
+
|
|
282
|
+
| Decision model | Type | Student → program on test |
|
|
283
|
+
| --- | --- | --- |
|
|
284
|
+
| jev-latest | System One | accuracy 0.573 → 0.795, +0.222 [+0.173, +0.269] |
|
|
285
|
+
| clef-flash | System One | accuracy 0.502 → 0.759, +0.258 [+0.208, +0.309] |
|
|
286
|
+
| djev | System One | accuracy 0.502 → 0.730, +0.228 [+0.186, +0.272] |
|
|
287
|
+
| pplx-decider-v1.3 | System One | accuracy 0.566 → 0.836, +0.270 [+0.225, +0.319] |
|
|
288
|
+
|
|
289
|
+
The side questions also transfer between models. The questions found on Jev, with the combiner refitted to each model's answers, score 0.750 on clef-flash, 0.745 on djev and 0.823 on pplx-decider. Each of those is within 1.5 points of a fresh `compile` on the same model.
|
|
290
|
+
|
|
291
|
+
### Several outputs
|
|
292
|
+
|
|
293
|
+
These runs use a small slice of each benchmark (120 train, 80 val, 120 test) and two proposals, with `jev-latest` as the decision LM. The metric is the mean over outputs of a per-output score: 1 if correct for Choice and Noul, and 1 minus the level error over the scale's length for Score.
|
|
294
|
+
|
|
295
|
+
| Benchmark | Outputs | Student → program on test | Choices |
|
|
296
|
+
| --- | --- | --- | --- |
|
|
297
|
+
| Deceptive hotel reviews and polarity | 1 Choice, 1 Noul | 0.754 → 0.912 | verdict: side questions; polarity: student |
|
|
298
|
+
| CVSS v3.1 base metrics | 8 Choice | 0.769 → 0.901 | 5 side questions, 3 reanchor only |
|
|
299
|
+
| Measuring Hate Speech items | 3 Score | 0.807 → 0.879 | 2 side questions, 1 reanchor only |
|
|
300
|
+
|
|
301
|
+
Each output makes its own choice. In the CVSS run, confidentiality rose from 0.517 to 0.858 with side questions, and scope rose from 0.767 to 0.975 with reanchoring alone. The polarity output was already at 0.967 and kept the student's answer.
|
|
302
|
+
|
|
303
|
+
On the full CVSS split (400 train, 200 val, 210 test), one shared list of 15 side questions came within one point of eight separate single-output compiles, at 0.31 times their cost per CVE. Over three searches, its mean accuracy across the eight metrics was 0.910 to 0.913, against 0.917 for the separate compiles. The largest gap on any single metric was 2.4 to 5.2 points, usually on availability. The results are in [`experiments/cvss/results/shared/`](https://github.com/cmpnd-ai/dspy-side-questions/tree/main/experiments/cvss/results/shared/).
|
|
304
|
+
|
|
305
|
+
### Cost
|
|
306
|
+
|
|
307
|
+
Measured on Jev, a call with 11 questions costs about twice as much as a one-question call and takes 1.0 to 1.2 times as long. Each added question adds about 70 to 80 input tokens and 20 output tokens.
|
|
308
|
+
|
|
309
|
+
`BRIEF.md` records the original design and the facts it rests on.
|
|
310
|
+
|
|
311
|
+
## Repository layout
|
|
312
|
+
|
|
313
|
+
| Path | Contents |
|
|
314
|
+
| --- | --- |
|
|
315
|
+
| `src/dspy_side_questions/` | The package: the optimizer (`SideQuestions`), the module (`SideQuestionsPredict`) and their parts. |
|
|
316
|
+
| `pyproject.toml` | Package metadata and dependencies. |
|
|
317
|
+
| `experiments/<benchmark>/run_user.py` | One benchmark run through `SideQuestions.compile`. |
|
|
318
|
+
| `experiments/<benchmark>/results/` | Saved reports, programs and cost measurements. |
|
|
319
|
+
| `experiments/students/run.py` | The hotel-review benchmark on other decision models. |
|
|
320
|
+
| `experiments/multi_output/smoke.py` | Small checks on programs with several outputs. |
|
|
321
|
+
| `tests/` | Offline tests with a fake decision backend. |
|
|
322
|
+
| `experiments/jev_cost.py` | Per-call cost and latency of the student against the returned program. |
|
|
323
|
+
| `experiments/download_data.py` | Downloads the datasets. `experiments/README.md` lists the sources. |
|
|
324
|
+
| `experiments/data/` | Local copies of the datasets. Git ignores this folder. |
|
|
325
|
+
| `BRIEF.md` | Design background. |
|
|
326
|
+
|
|
327
|
+
## Running the benchmarks
|
|
328
|
+
|
|
329
|
+
The benchmark scripts need the editable install from `requirements.txt`. They read `OPENAI_API_KEY` and `TYPESAFE_API_KEY` from the environment. A `.env` file in the project root also works, and variables already set in the shell take precedence. Run the scripts from the project root.
|
|
330
|
+
|
|
331
|
+
`experiments/download_data.py` downloads every dataset into `experiments/data/`. [`experiments/README.md`](https://github.com/cmpnd-ai/dspy-side-questions/blob/main/experiments/README.md) lists the sources and their licenses.
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
.venv/bin/python experiments/download_data.py # optional: dataset names
|
|
335
|
+
.venv/bin/python experiments/opspam/run_user.py
|
|
336
|
+
.venv/bin/python experiments/beemo/run_user.py model # or expert_edited
|
|
337
|
+
.venv/bin/python experiments/ghostbuster/run_user.py
|
|
338
|
+
.venv/bin/python experiments/legalbench/run_user.py learned_hands_consumer
|
|
339
|
+
.venv/bin/python experiments/circl/run_user.py
|
|
340
|
+
.venv/bin/python experiments/cvss/run_user.py # optional: metric names
|
|
341
|
+
.venv/bin/python experiments/summeval/run_user.py
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
`experiments/multi_output/smoke.py <cvss|reviews|hate>` runs the several-output checks. `experiments/students/run.py <djev|clef-flash|pplx-decider|deepseek>` runs the decision-model comparison and also reads `LUNAROUTE_API_KEY`.
|
|
345
|
+
|
|
346
|
+
Each script makes live decision-LM and reflection-LM calls and overwrites the files in its `results/user/` folder.
|