Skip to content

Commit 8c167dc

Browse files
authored
docs: explain rollout_id caching (#8746)
1 parent 9e5967d commit 8c167dc

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

docs/docs/cheatsheet.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ This page will contain snippets for frequent usage patterns.
88

99
## DSPy Programs
1010

11+
### Forcing fresh LM outputs
12+
13+
DSPy caches LM calls. Provide a unique ``rollout_id`` to bypass an existing
14+
cache entry while still caching the new result:
15+
16+
```python
17+
predict = dspy.Predict("question -> answer")
18+
predict(question="1+1", config={"rollout_id": 1})
19+
```
20+
1121
### dspy.Signature
1222

1323
```python

docs/docs/learn/programming/language_models.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,34 @@ gpt_4o_mini = dspy.LM('openai/gpt-4o-mini', temperature=0.9, max_tokens=3000, st
167167
By default LMs in DSPy are cached. If you repeat the same call, you will get the same outputs. But you can turn off caching by setting `cache=False`.
168168

169169
If you want to keep caching enabled but force a new request (for example, to obtain diverse outputs),
170-
pass a unique `rollout_id` in your call. Different values ensure a different cache entry while
171-
still caching future calls with the same inputs and `rollout_id`.
170+
pass a unique `rollout_id` in your call. DSPy hashes both the inputs and the `rollout_id` when
171+
looking up a cache entry, so different values force a new LM request while
172+
still caching future calls with the same inputs and `rollout_id`. The ID is also recorded in
173+
`lm.history`, which makes it easy to track or compare different rollouts during experiments.
172174

173175
```python linenums="1"
174176
lm("Say this is a test!", rollout_id=1)
175177
```
176178

179+
You can pass these LM kwargs directly to DSPy modules as well. Supplying them at
180+
initialization sets the defaults for every call:
181+
182+
```python linenums="1"
183+
predict = dspy.Predict("question -> answer", rollout_id=1)
184+
```
185+
186+
To override them for a single invocation, provide a ``config`` dictionary when
187+
calling the module:
188+
189+
```python linenums="1"
190+
predict = dspy.Predict("question -> answer")
191+
predict(question="What is 1 + 52?", config={"rollout_id": 5})
192+
```
193+
194+
In both cases, ``rollout_id`` is forwarded to the underlying LM, affects
195+
its caching behavior, and is stored alongside each response so you can
196+
replay or analyze specific rollouts later.
197+
177198

178199
## Inspecting output and usage metadata.
179200

dspy/predict/predict.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717

1818

1919
class Predict(Module, Parameter):
20+
"""Basic DSPy module that maps inputs to outputs using a language model.
21+
22+
Args:
23+
signature: The input/output signature describing the task.
24+
callbacks: Optional list of callbacks for instrumentation.
25+
**config: Default keyword arguments forwarded to the underlying
26+
language model. These values can be overridden for a single
27+
invocation by passing a ``config`` dictionary when calling the
28+
module. For example::
29+
30+
predict = dspy.Predict("q -> a", rollout_id=1)
31+
predict(q="What is 1 + 52?", config={"rollout_id": 2})
32+
"""
33+
2034
def __init__(self, signature: str | type[Signature], callbacks: list[BaseCallback] | None = None, **config):
2135
super().__init__(callbacks=callbacks)
2236
self.stage = random.randbytes(8).hex()
@@ -99,7 +113,7 @@ def _forward_preprocess(self, **kwargs):
99113
assert "new_signature" not in kwargs, "new_signature is no longer a valid keyword argument."
100114
signature = ensure_signature(kwargs.pop("signature", self.signature))
101115
demos = kwargs.pop("demos", self.demos)
102-
config = dict(**self.config, **kwargs.pop("config", {}))
116+
config = {**self.config, **kwargs.pop("config", {})}
103117

104118
# Get the right LM to use.
105119
lm = kwargs.pop("lm", self.lm) or settings.lm

0 commit comments

Comments
 (0)