Skip to content

Commit bfdf533

Browse files
authored
Merge branch 'main' into fastembed-support
2 parents f47a304 + d7dace6 commit bfdf533

File tree

28 files changed

+1125
-602
lines changed

28 files changed

+1125
-602
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ The DSPy documentation is divided into **tutorials** (step-by-step illustration
105105
- 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.
106106
- **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)
107107
- [DSPy: Not Your Average Prompt Engineering](https://jina.ai/news/dspy-not-your-average-prompt-engineering), why it's crucial for future prompt engineering, and yet why it is challenging for prompt engineers to learn.
108+
- **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)
108109
109110
### B) Guides
110111
@@ -136,24 +137,28 @@ You can find other examples tweeted by [@lateinteraction](https://twitter.com/la
136137

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

140+
141+
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/tasks)
142+
- [Sophisticated Extreme Multi-Class Classification, IReRa, by Karel D’Oosterlinck](https://github.com/KarelDO/xmc.dspy)
143+
- [Haize Lab's Red Teaming with DSPy](https://blog.haizelabs.com/posts/dspy/) and see [their DSPy code](https://github.com/haizelabs/dspy-redteam)
139144
- Applying DSPy Assertions
140145
- [Long-form Answer Generation with Citations, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/longformqa/longformqa_assertions.ipynb)
141146
- [Generating Answer Choices for Quiz Questions, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/quiz/quiz_assertions.ipynb)
142147
- [Generating Tweets for QA, by Arnav Singhvi](https://colab.research.google.com/github/stanfordnlp/dspy/blob/main/examples/tweets/tweets_assertions.ipynb)
143148
- [Compiling LCEL runnables from LangChain in DSPy](https://github.com/stanfordnlp/dspy/blob/main/examples/tweets/compiling_langchain.ipynb)
144149
- [AI feedback, or writing LM-based metrics in DSPy](https://github.com/stanfordnlp/dspy/blob/main/examples/tweets/tweet_metric.py)
145-
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/tasks)
150+
- [DSPy Optimizers Benchmark on a bunch of different tasks, by Michael Ryan](https://github.com/stanfordnlp/dspy/tree/main/testing/README.md)
146151
- [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)
148152
- [DSPy on BIG-Bench Hard Example, by Chris Levy](https://drchrislevy.github.io/posts/dspy/dspy.html)
149153
- [Using Ollama with DSPy for Mistral (quantized) by @jrknox1977](https://gist.github.com/jrknox1977/78c17e492b5a75ee5bbaf9673aee4641)
150-
- [Using DSPy, "The Unreasonable Effectiveness of Eccentric Automatic Prompts" (paper) by VMware's Rick Battle & Teja Gollapudi, and interview at TheRegister](https://www.theregister.com/2024/02/22/prompt_engineering_ai_models/)
154+
- [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/)
151155
- [Optimizing Performance of Open Source LM for Text-to-SQL using DSPy and vLLM, by Juan Ovalle](https://github.com/jjovalle99/DSPy-Text2SQL)
152156
- Typed DSPy (contributed by [@normal-computing](https://github.com/normal-computing))
153157
- [Using DSPy to train Gpt 3.5 on HumanEval by Thomas Ahle](https://github.com/stanfordnlp/dspy/blob/main/examples/functional/functional.ipynb)
154158
- [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)
155159

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.
160+
161+
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.
157162

158163
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).
159164

docs/api/language_model_clients/AzureOpenAI.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ lm = dspy.AzureOpenAI(api_base='...', api_version='2023-12-01-preview', model='g
1414

1515
The constructor initializes the base class `LM` and verifies the provided arguments like the `api_provider`, `api_key`, and `api_base` to set up OpenAI request retrieval through Azure. The `kwargs` attribute is initialized with default values for relevant text generation parameters needed for communicating with the GPT API, such as `temperature`, `max_tokens`, `top_p`, `frequency_penalty`, `presence_penalty`, and `n`.
1616

17+
Azure requires that the deployment id of the Azure deployment to be also provided using the argument `deployment_id`.
18+
1719
```python
1820
class AzureOpenAI(LM):
1921
def __init__(
@@ -53,4 +55,4 @@ After generation, the completions are post-processed based on the `model_type` p
5355
- `**kwargs`: Additional keyword arguments for completion request.
5456

5557
**Returns:**
56-
- `List[Dict[str, Any]]`: List of completion choices.
58+
- `List[Dict[str, Any]]`: List of completion choices.

docs/api/modules/ChainOfThought.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@ class ChainOfThought(Predict):
1313

1414
self.activated = activated
1515

16-
signature = self.signature
17-
*keys, last_key = signature.kwargs.keys()
18-
19-
DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
20-
desc="${produce the " + last_key + "}. We ...")
21-
22-
rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE
23-
24-
extended_kwargs = {key: signature.kwargs[key] for key in keys}
25-
extended_kwargs.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
26-
27-
self.extended_signature = dsp.Template(signature.instructions, **extended_kwargs)
16+
signature = ensure_signature(self.signature)
17+
*_keys, last_key = signature.output_fields.keys()
18+
19+
rationale_type = rationale_type or dspy.OutputField(
20+
prefix="Reasoning: Let's think step by step in order to",
21+
desc="${produce the " + last_key + "}. We ...",
22+
)
23+
24+
self.extended_signature = signature.prepend("rationale", rationale_type, type_=str)
2825
```
2926

3027
**Parameters:**
3128
- `signature` (_Any_): Signature of predictive model.
32-
- `rationale_type` (_dsp.Type_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
29+
- `rationale_type` (_dspy.OutputField_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
3330
- `activated` (_bool_, _optional_): Flag for activated chain of thought processing. Defaults to `True`.
3431
- `**config` (_dict_): Additional configuration parameters for model.
3532

@@ -64,3 +61,15 @@ pred = generate_answer(question=question)
6461
print(f"Question: {question}")
6562
print(f"Predicted Answer: {pred.answer}")
6663
```
64+
65+
The following example shows how to specify your custom rationale. Here `answer` corresponds to the last key to produce, it may be different in your case.
66+
67+
```python
68+
#define a custom rationale
69+
rationale_type = dspy.OutputField(
70+
prefix="Reasoning: Let's think step by step in order to",
71+
desc="${produce the answer}. We ...",
72+
)
73+
#Pass signature to ChainOfThought module
74+
generate_answer = dspy.ChainOfThought(BasicQA, rationale_type=rationale_type)
75+
```

docs/api/modules/ChainOfThoughtWithHint.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,23 @@ The constructor initializes the `ChainOfThoughtWithHint` class and sets up its a
88
class ChainOfThoughtWithHint(Predict):
99
def __init__(self, signature, rationale_type=None, activated=True, **config):
1010
super().__init__(signature, **config)
11-
1211
self.activated = activated
13-
1412
signature = self.signature
15-
*keys, last_key = signature.kwargs.keys()
16-
17-
DEFAULT_HINT_TYPE = dsp.Type(prefix="Hint:", desc="${hint}")
18-
19-
DEFAULT_RATIONALE_TYPE = dsp.Type(prefix="Reasoning: Let's think step by step in order to",
20-
desc="${produce the " + last_key + "}. We ...")
2113

22-
rationale_type = rationale_type or DEFAULT_RATIONALE_TYPE
23-
24-
extended_kwargs1 = {key: signature.kwargs[key] for key in keys}
25-
extended_kwargs1.update({'rationale': rationale_type, last_key: signature.kwargs[last_key]})
14+
*keys, last_key = signature.fields.keys()
15+
rationale_type = rationale_type or dspy.OutputField(
16+
prefix="Reasoning: Let's think step by step in order to",
17+
desc="${produce the " + last_key + "}. We ...",
18+
)
19+
self.extended_signature1 = self.signature.insert(-2, "rationale", rationale_type, type_=str)
2620

27-
extended_kwargs2 = {key: signature.kwargs[key] for key in keys}
28-
extended_kwargs2.update({'hint': DEFAULT_HINT_TYPE, 'rationale': rationale_type, last_key: signature.kwargs[last_key]})
29-
30-
self.extended_signature1 = dsp.Template(signature.instructions, **extended_kwargs1)
31-
self.extended_signature2 = dsp.Template(signature.instructions, **extended_kwargs2)
21+
DEFAULT_HINT_TYPE = dspy.OutputField()
22+
self.extended_signature2 = self.extended_signature1.insert(-2, "hint", DEFAULT_HINT_TYPE, type_=str)
3223
```
3324

3425
**Parameters:**
3526
- `signature` (_Any_): Signature of predictive model.
36-
- `rationale_type` (_dsp.Type_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
27+
- `rationale_type` (_dspy.OutputField_, _optional_): Rationale type for reasoning steps. Defaults to `None`.
3728
- `activated` (_bool_, _optional_): Flag for activated chain of thought processing. Defaults to `True`.
3829
- `**config` (_dict_): Additional configuration parameters for model.
3930

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/building-blocks/8-typed_predictors.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ prediction = predictor(input=doc_query_pair)
6767
Let's see the output and its type.
6868

6969
```python
70-
answer = prediction.answer
71-
confidence_score = prediction.confidence
70+
answer = prediction.output.answer
71+
confidence_score = prediction.output.confidence
7272

7373
print(f"Prediction: {prediction}\n\n")
7474
print(f"Answer: {answer}, Answer Type: {type(answer)}")

docs/docs/cheatsheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class FactJudge(dspy.Signature):
233233
context = dspy.InputField(desc="Context for the prediciton")
234234
question = dspy.InputField(desc="Question to be answered")
235235
answer = dspy.InputField(desc="Answer for the question")
236-
factually_correct = dspy.OutputField(desc="Is the answer factually correct based on the context?", prefix="Facual[Yes/No]:")
236+
factually_correct = dspy.OutputField(desc="Is the answer factually correct based on the context?", prefix="Factual[Yes/No]:")
237237

238238
judge = dspy.ChainOfThought(FactJudge)
239239

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.

0 commit comments

Comments
 (0)