Skip to content

Commit df0ad50

Browse files
Minor update to DSPy integration (#1588)
Description ----------- This PR introduces two minor updates: - We want the users to use the `wandb.init` as context manager pattern, - The `WandbDSPyCallback` can take in the `run` object or it defaults to the global `wandb.run`. This minor update is part of this PR: wandb/wandb#10327 and should be merged once the said PR is merged. <!-- preview-links-comment --> 📄 **[View preview links for changed pages](#1588 (comment) --------- Co-authored-by: John Mulhausen <john.mulhausen@wandb.com>
1 parent 77db61c commit df0ad50

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

  • content/en/guides/integrations

content/en/guides/integrations/dspy.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,42 @@ from wandb.integration.dspy import WandbDSPyCallback
9191
9292
# Initialize W&B (importing weave is sufficient; no explicit weave.init needed)
9393
project_name = "dspy-optimization"
94-
wandb.init(project=project_name)
95-
96-
# Add W&B callback to DSPy
97-
dspy.settings.callbacks.append(WandbDSPyCallback())
98-
99-
# Configure language models
100-
teacher_lm = dspy.LM('openai/gpt-4o', max_tokens=2000, cache=True)
101-
student_lm = dspy.LM('openai/gpt-4o-mini', max_tokens=2000)
102-
dspy.configure(lm=student_lm)
103-
104-
# Load dataset and define program
105-
dataset = MATH(subset='algebra')
106-
program = dspy.ChainOfThought("question -> answer")
107-
108-
# Configure and run optimizer
109-
optimizer = dspy.MIPROv2(
110-
metric=dataset.metric,
111-
auto="light",
112-
num_threads=24,
113-
teacher_settings=dict(lm=teacher_lm),
114-
prompt_model=student_lm
115-
)
116-
117-
optimized_program = optimizer.compile(
118-
program,
119-
trainset=dataset.train,
120-
max_bootstrapped_demos=2,
121-
max_labeled_demos=2
122-
)
94+
with wandb.init(project=project_name) as run:
95+
# Add W&B callback to DSPy
96+
dspy.settings.callbacks.append(
97+
WandbDSPyCallback(run=run)
98+
)
99+
100+
# Configure language models
101+
teacher_lm = dspy.LM('openai/gpt-4o', max_tokens=2000, cache=True)
102+
student_lm = dspy.LM('openai/gpt-4o-mini', max_tokens=2000)
103+
dspy.configure(lm=student_lm)
104+
105+
# Load dataset and define program
106+
dataset = MATH(subset='algebra')
107+
program = dspy.ChainOfThought("question -> answer")
108+
109+
# Configure and run optimizer
110+
optimizer = dspy.MIPROv2(
111+
metric=dataset.metric,
112+
auto="light",
113+
num_threads=24,
114+
teacher_settings=dict(lm=teacher_lm),
115+
prompt_model=student_lm
116+
)
117+
118+
optimized_program = optimizer.compile(
119+
program,
120+
trainset=dataset.train,
121+
max_bootstrapped_demos=2,
122+
max_labeled_demos=2
123+
)
123124
```
124125

125126
After running this code, you receive both a W&B Run URL and a Weave URL. W&B displays evaluation metrics over time, along with Tables that show the evolution of program signatures. The run's **Overview** tab includes links to Weave traces for detailed inspection.
126127
128+
If a `run` object is not passed to `WandbDSPyCallback`, the global `run` object is used.
129+
127130
{{< img src="/images/integrations/dspy_run_page.png" alt="DSPy optimization run in W&B" >}}
128131
129132
For comprehensive details about Weave tracing, evaluation, and optimization with DSPy, see the [Weave DSPy integration guide](https://weave-docs.wandb.ai/guides/integrations/dspy).

0 commit comments

Comments
 (0)