Skip to content

Commit 35a58e1

Browse files
Merge branch 'main' into rpgoldman-readme-patch-1
2 parents dbbcac8 + a8a325b commit 35a58e1

File tree

13 files changed

+64
-27
lines changed

13 files changed

+64
-27
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
136136

137137
**Some other examples (not exhaustive, feel free to add more via PR):**
138138

139+
140+
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/tasks)
141+
- [Sophisticated Extreme Multi-Class Classification, IReRa, by Karel D’Oosterlinck](https://github.com/KarelDO/xmc.dspy)
142+
- [Haize Lab's Red Teaming with DSPy](https://blog.haizelabs.com/posts/dspy/) and see [their DSPy code](https://github.com/haizelabs/dspy-redteam)
139143
- Applying DSPy Assertions
140144
- [Long-form Answer Generation with Citations, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/longformqa/longformqa_assertions.ipynb)
141145
- [Generating Answer Choices for Quiz Questions, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/quiz/quiz_assertions.ipynb)
@@ -144,7 +148,6 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
144148
- [AI feedback, or writing LM-based metrics in DSPy](https://github.com/stanfordnlp/dspy/blob/main/examples/tweets/tweet_metric.py)
145149
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/README.md)
146150
- [Indian Languages NLI with gains due to compiling by Saiful Haq](https://github.com/saifulhaq95/DSPy-Indic/blob/main/indicxlni.ipynb)
147-
- [Sophisticated Extreme Multi-Class Classification, IReRa, by Karel D’Oosterlinck](https://github.com/KarelDO/xmc.dspy)
148151
- [DSPy on BIG-Bench Hard Example, by Chris Levy](https://drchrislevy.github.io/posts/dspy/dspy.html)
149152
- [Using Ollama with DSPy for Mistral (quantized) by @jrknox1977](https://gist.github.com/jrknox1977/78c17e492b5a75ee5bbaf9673aee4641)
150153
- [Using DSPy, "The Unreasonable Effectiveness of Eccentric Automatic Prompts" (paper) by VMware's Rick Battle & Teja Gollapudi](https://arxiv.org/abs/2402.10949), and [interview at TheRegister](https://www.theregister.com/2024/02/22/prompt_engineering_ai_models/)
@@ -153,7 +156,8 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
153156
- [Using DSPy to train Gpt 3.5 on HumanEval by Thomas Ahle](https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb)
154157
- [Building a chess playing agent using DSPy by Franck SN](https://medium.com/thoughts-on-machine-learning/building-a-chess-playing-agent-using-dspy-9b87c868f71e)
155158

156-
TODO: Add links to the state-of-the-art results on Theory of Mind (ToM) by Plastic Labs, the results by Haize Labs for Red Teaming with DSPy, and the DSPy pipeline from Replit.
159+
160+
TODO: Add links to the state-of-the-art results by the University of Toronto on Clinical NLP, on Theory of Mind (ToM) by Plastic Labs, and the DSPy pipeline from Replit.
157161

158162
There are also recent cool examples at [Weaviate's DSPy cookbook](https://github.com/weaviate/recipes/tree/main/integrations/dspy) by Connor Shorten. [See tutorial on YouTube](https://www.youtube.com/watch?v=CEuUG4Umfxs).
159163

docs/docs/building-blocks/1-language_models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Let's first make sure you can set up your language model. DSPy support clients f
1010

1111
## Setting up the LM client.
1212

13-
You can just call the constructor that connects to the LM. Then, use `dspy.configure` to declare this as the dexfault LM.
13+
You can just call the constructor that connects to the LM. Then, use `dspy.configure` to declare this as the default LM.
1414

1515
For example, to use OpenAI language models, you can do it as follows.
1616

docs/docs/building-blocks/3-modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar_position: 3
66

77
A **DSPy module** is a building block for programs that use LMs.
88

9-
- Each built-in module abstracts a **prompting technique** (like chain of thought or ReAct). Crucially, they are generalized to handle any [DSPy Signature].
9+
- Each built-in module abstracts a **prompting technique** (like chain of thought or ReAct). Crucially, they are generalized to handle any [DSPy Signature](https://dspy-docs.vercel.app/docs/building-blocks/signatures).
1010

1111
- A DSPy module has **learnable parameters** (i.e., the little pieces comprising the prompt and the LM weights) and can be invoked (called) to process inputs and return outputs.
1212

@@ -17,7 +17,7 @@ A **DSPy module** is a building block for programs that use LMs.
1717

1818
Let's start with the most fundamental module, `dspy.Predict`. Internally, all other DSPy modules are just built using `dspy.Predict`.
1919

20-
We'll assume you are already at least a little familiar with [DSPy signatures], which are declarative specs for defining the behavior of any module we use in DSPy.
20+
We'll assume you are already at least a little familiar with [DSPy signatures](https://dspy-docs.vercel.app/docs/building-blocks/signatures), which are declarative specs for defining the behavior of any module we use in DSPy.
2121

2222
To use a module, we first **declare** it by giving it a signature. Then we **call** the module with the input arguments, and extract the output fields!
2323

docs/docs/building-blocks/7-assertions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Specifically, when a constraint is not met:
3030
- Past Output: your model's past output that did not pass the validation_fn
3131
- Instruction: your user-defined feedback message on what went wrong and what possibly to fix
3232

33-
If the error continues past the `max_backtracking_attempts`, then `dspy.Assert` will halt the pipeline execution, altering you with an `dspy.AssertionError`. This ensures your program doesn't continue executing with “bad” LM behavior and immediately highlights sample failure outputs for user assessment.
33+
If the error continues past the `max_backtracking_attempts`, then `dspy.Assert` will halt the pipeline execution, alerting you with an `dspy.AssertionError`. This ensures your program doesn't continue executing with “bad” LM behavior and immediately highlights sample failure outputs for user assessment.
3434

3535
- **dspy.Suggest vs. dspy.Assert**: `dspy.Suggest` on the other hand offers a softer approach. It maintains the same retry backtracking as `dspy.Assert` but instead serves as a gentle nudger. If the model outputs cannot pass the model constraints after the `max_backtracking_attempts`, `dspy.Suggest` will log the persistent failure and continue execution of the program on the rest of the data. This ensures the LM pipeline works in a "best-effort" manner without halting execution.
3636

docs/docs/quick-start/minimal-example.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ We make use of the [GSM8K dataset](https://huggingface.co/datasets/gsm8k) and th
1212

1313
## Setup
1414

15-
Before we delve into the example, let's ensure our environment is properly configured. We'll start by importing the necessary modules and configuring our language model:
15+
Before we jump into the example, let's ensure our environment is properly configured. We'll start by importing the necessary modules and configuring our language model:
1616

1717
```python
1818
import dspy
@@ -33,7 +33,7 @@ Let's take a look at what `gsm8k_trainset` and `gsm8k_devset` are:
3333
print(gsm8k_trainset)
3434
```
3535

36-
The `gsm8k_trainset` and `gsm8k_devset` datasets contain a list of Examples with each example having `question` and `answer` field. We'll use these datasets to train and evaluate our model.
36+
The `gsm8k_trainset` and `gsm8k_devset` datasets contain a list of Examples with each example having `question` and `answer` field.
3737

3838
## Define the Module
3939

@@ -51,7 +51,7 @@ class CoT(dspy.Module):
5151

5252
## Compile and Evaluate the Model
5353

54-
With our simple program in place, let's move on to optimizing it using the [`BootstrapFewShot`](/api/optimizers/BootstrapFewShot) teleprompter:
54+
With our simple program in place, let's move on to compiling it with the [`BootstrapFewShot`](/api/optimizers/BootstrapFewShot) teleprompter:
5555

5656
```python
5757
from dspy.teleprompt import BootstrapFewShot
@@ -61,9 +61,11 @@ config = dict(max_bootstrapped_demos=4, max_labeled_demos=4)
6161

6262
# Optimize! Use the `gsm8k_metric` here. In general, the metric is going to tell the optimizer how well it's doing.
6363
teleprompter = BootstrapFewShot(metric=gsm8k_metric, **config)
64-
optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset, valset=gsm8k_devset)
64+
optimized_cot = teleprompter.compile(CoT(), trainset=gsm8k_trainset)
6565
```
6666

67+
Note that BootstrapFewShot is not an optimizing teleprompter, i.e. it simple creates and validates examples for steps of the pipeline (in this case, the chain-of-thought reasoning) but does not optimize the metric. Other teleprompters like `BootstrapFewShotWithRandomSearch` and `MIPRO` will apply direct optimization.
68+
6769
## Evaluate
6870

6971
Now that we have a compiled (optimized) DSPy program, let's move to evaluating its performance on the dev dataset.

docs/docs/tutorials/other_tutorial.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ sidebar_position: 99999
2424
- [DSPy webinar with MLOps Learners](https://www.youtube.com/watch?v=im7bCLW2aM4), a bit longer with Q&A.
2525
- Hands-on Overviews of DSPy by the community: [DSPy Explained! by Connor Shorten](https://www.youtube.com/watch?v=41EfOY0Ldkc), [DSPy explained by code_your_own_ai](https://www.youtube.com/watch?v=ycfnKPxBMck), [DSPy Crash Course by AI Bites](https://youtu.be/5-zgASQKkKQ?si=3gnmVouT5_rpk_nu)
2626
- Interviews: [Weaviate Podcast in-person](https://www.youtube.com/watch?v=CDung1LnLbY), and you can find 6-7 other remote podcasts on YouTube from a few different perspectives/audiences.
27-
- **Tracing in DSPy** with Arize Phoenix: [Tutorial for tracing your prompts and the steps of your DSPy programs](https://colab.research.google.com/github/Arize-ai/phoenix/blob/main/tutorials/tracing/dspy_tracing_tutorial.ipynb)
27+
- **Tracing in DSPy** with Arize Phoenix: [Tutorial for tracing your prompts and the steps of your DSPy programs](https://colab.research.google.com/github/Arize-ai/phoenix/blob/main/tutorials/tracing/dspy_tracing_tutorial.ipynb)
28+
- **Tracing & Optimization Tracking in DSPy** with Parea AI: [Tutorial on tracing & evaluating a DSPy RAG program](https://docs.parea.ai/tutorials/dspy-rag-trace-evaluate/tutorial)

dspy/primitives/module.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,48 @@
44

55
import ujson
66

7+
# NOTE: Note: It's important (temporary decision) to maintain named_parameters that's different in behavior from
8+
# named_sub_modules for the time being.
79

810
class BaseModule:
911
def __init__(self):
1012
pass
1113

1214
def named_parameters(self):
13-
"""Unlike PyTorch, handles lists of parameters too."""
15+
"""
16+
Unlike PyTorch, handles (non-recursive) lists of parameters too.
17+
"""
18+
19+
import dspy
1420
from dspy.predict.parameter import Parameter
1521

16-
# Remove the 'self.' prefix from the names
17-
return [(name[5:], param) for name, param in self.named_sub_modules(Parameter)]
22+
visited = set()
23+
named_parameters = []
24+
25+
def add_parameter(param_name, param_value):
26+
if isinstance(param_value, Parameter) and id(param_value) not in visited:
27+
visited.add(id(param_value))
28+
named_parameters.append((param_name, param_value))
29+
30+
for name, value in self.__dict__.items():
31+
if isinstance(value, Parameter):
32+
add_parameter(name, value)
33+
34+
elif isinstance(value, dspy.Module):
35+
# When a sub-module is pre-compiled, keep it frozen.
36+
if not getattr(value, "_compiled", False):
37+
for sub_name, param in value.named_parameters():
38+
add_parameter(f"{name}.{sub_name}", param)
39+
40+
elif isinstance(value, (list, tuple)):
41+
for idx, item in enumerate(value):
42+
add_parameter(f"{name}[{idx}]", item)
43+
44+
elif isinstance(value, dict):
45+
for key, item in value.items():
46+
add_parameter(f"{name}['{key}']", item)
47+
48+
return named_parameters
1849

1950
def named_sub_modules(self, type_=None, skip_compiled=False) -> Generator[tuple[str, "BaseModule"], None, None]:
2051
"""Find all sub-modules in the module, as well as their names.

dspy/teleprompt/bootstrap.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ def __init__(
5252
self.error_count = 0
5353
self.error_lock = threading.Lock()
5454

55-
def compile(self, student, *, teacher=None, trainset, valset=None):
55+
def compile(self, student, *, teacher=None, trainset):
5656
self.trainset = trainset
57-
self.valset = valset
5857

5958
self._prepare_student_and_teacher(student, teacher)
6059
self._prepare_predictor_mappings()
@@ -133,7 +132,7 @@ def _bootstrap(self, *, max_bootstraps=None):
133132
self.validation = [x for idx, x in enumerate(self.trainset) if idx not in bootstrapped]
134133
random.Random(0).shuffle(self.validation)
135134

136-
self.validation = self.valset or self.validation
135+
self.validation = self.validation
137136

138137
# NOTE: Can't yet use evaluate because we need to trace *per example*
139138
# evaluate = Evaluate(program=self.teacher, metric=self.metric, num_threads=12)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "dspy-ai"
7-
version = "2.4.3"
7+
version = "2.4.9"
88
description = "DSPy"
99
readme = "README.md"
1010
authors = [{ name = "Omar Khattab", email = "okhattab@stanford.edu" }]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name="dspy-ai",
13-
version="2.4.3",
13+
version="2.4.9",
1414
description="DSPy",
1515
long_description=long_description,
1616
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)