Skip to content

Commit d009e67

Browse files
committed
Update README: use n= OpenAI examples for Speculate and BestOfN
1 parent f24f5a8 commit d009e67

1 file changed

Lines changed: 18 additions & 27 deletions

File tree

README.md

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Requires Python >= 3.10. No external dependencies.
2424
### Docker
2525

2626
The Docker image ships with [BranchFS](https://github.com/multikernel/branchfs)
27-
built in no need to install FUSE, compile Rust, or configure any filesystem
27+
built in -- no need to install FUSE, compile Rust, or configure any filesystem
2828
yourself. Just pull the image and go:
2929

3030
```bash
@@ -88,20 +88,28 @@ committed; the rest are aborted.
8888
Use when you have several plausible approaches and care about latency more
8989
than optimality: bug fixes where any passing patch is good enough, tool
9090
selection where multiple tools could work, or prompt variants where you
91-
just need one that doesn't error out.
91+
just need one that doesn't error out. Pairs naturally with the ``n=``
92+
parameter in OpenAI's Chat Completions API to race N variations in
93+
parallel.
9294

9395
```python
9496
from branching import Workspace, Speculate
97+
import openai
9598

96-
def try_fix_a(path: Path) -> bool:
97-
apply_patch(path / "a.patch")
98-
return run_tests(path)
99+
client = openai.OpenAI()
100+
resp = client.chat.completions.create(
101+
model="gpt-4o", n=5,
102+
messages=[{"role": "user", "content": prompt}],
103+
)
99104

100-
def try_fix_b(path: Path) -> bool:
101-
apply_patch(path / "b.patch")
102-
return run_tests(path)
105+
def make_candidate(code: str):
106+
def candidate(path: Path) -> bool:
107+
(path / "fix.py").write_text(code)
108+
return run_tests(path)
109+
return candidate
103110

104-
outcome = Speculate([try_fix_a, try_fix_b], first_wins=True, timeout=60)(ws)
111+
candidates = [make_candidate(c.message.content) for c in resp.choices]
112+
outcome = Speculate(candidates, first_wins=True, timeout=60)(ws)
105113

106114
if outcome.committed:
107115
print(f"Fix {outcome.winner.branch_index} succeeded!")
@@ -122,23 +130,6 @@ Candidates can return ``bool`` or ``(bool, float)``. Scoring is flexible:
122130
pass pre-computed ``scores`` (e.g. from logprobs), provide an ``evaluate``
123131
callback for post-execution scoring, or let candidates score themselves.
124132

125-
```python
126-
from branching import BestOfN
127-
128-
def make_candidate(code: str):
129-
def candidate(path: Path) -> tuple[bool, float]:
130-
(path / "solution.py").write_text(code)
131-
passed = run_tests(path)
132-
return passed, evaluate_quality(path) if passed else 0.0
133-
return candidate
134-
135-
candidates = [make_candidate(c) for c in generate_solutions(n=5)]
136-
outcome = BestOfN(candidates)(ws)
137-
```
138-
139-
**Logprobs workflow** — score candidates externally using model confidence,
140-
then let BestOfN pick the highest-scoring one that passes:
141-
142133
```python
143134
from branching import BestOfN
144135
import openai
@@ -155,7 +146,7 @@ logprob_scores = [
155146
for c in resp.choices
156147
]
157148

158-
# Candidates just apply code and test return bare bool
149+
# Candidates just apply code and test -- return bare bool
159150
candidates = [make_test(c.message.content) for c in resp.choices]
160151

161152
# BestOfN picks the highest-logprob passing candidate

0 commit comments

Comments
 (0)