Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

1

Omar Khattab6854c99b16ed
diff --git a/docs/building-blocks/1-signatures.md b/docs/building-blocks/1-signatures.md
index 1bd34418b..3b00e46a6 100644
--- a/docs/building-blocks/1-signatures.md
+++ b/docs/building-blocks/1-signatures.md
@@ -2,9 +2,9 @@
sidebar_position: 2
---
-# [1] DSPy Signatures
+# 1️⃣ Signatures
-## 1) What is a DSPy Signature?
+## What is a DSPy Signature?
When we assign tasks to LMs in DSPy, we specify the behavior we need as a Signature.
@@ -18,7 +18,7 @@ You're probably familiar with function signatures, which specify the input and o
- The field names matter in DSPy Signatures. You express semantic roles in plain English: a `question` is different from an `answer`, a `sql_query` is different from `python_code`.
-## 2) Why should I use a DSPy Signature?
+## Why should I use a DSPy Signature?
**tl;dr** For modular and clean code, in which LM calls can be optimized into high-quality prompts (or automatic finetunes).
@@ -27,7 +27,7 @@ You're probably familiar with function signatures, which specify the input and o
Writing signatures is far more modular, adaptive, and reproducible than hacking at prompts or finetunes. The DSPy compiler will figure out how to build a highly-optimized prompt for your LM (or finetune your small LM) for your signature, on your data, and within your pipeline. In many cases, we found that compiling leads to better prompts than humans write. Not because DSPy optimizers are more creative than humans, but simply because they can try more things and tune the metrics directly.
-## 3) **Inline** DSPy Signatures
+## **Inline** DSPy Signatures
Signatures can be defined as a short string, with argument names that define semantic roles for inputs/outputs.
@@ -90,7 +90,7 @@ print("Rationale:", response.rationale)
Rationale: produce the summary. We need to highlight the key points about Lee's performance for West Ham, his loan spells in League One, and his new contract with Barnsley. We also need to mention that his contract length has not been disclosed.
```
-## 4) **Class-based** DSPy Signatures
+## **Class-based** DSPy Signatures
For some advanced tasks, you need more verbose signatures. This is typically to:
@@ -155,7 +155,7 @@ Prediction(
```
-## 5) Building modules & compiling them
+## Using signatures to build modules & compiling them
While signatures are covenient for prototyping with structured inputs/outputs, that's not the main reason to use them!
diff --git a/docs/building-blocks/2-modules.ipynb b/docs/building-blocks/2-modules.ipynb
deleted file mode 100644
index 89418ed8d..000000000
--- a/docs/building-blocks/2-modules.ipynb
+++ /dev/null
@@ -1,45 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 1,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "1"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "py39_dec_2023",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.9.18"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}
diff --git a/docs/building-blocks/2-modules.md b/docs/building-blocks/2-modules.md
new file mode 100644
index 000000000..65c7ed035
--- /dev/null
+++ b/docs/building-blocks/2-modules.md
@@ -0,0 +1,127 @@
+---
+sidebar_position: 2
+---
+
+# 2️⃣ Modules
+
+
+## What is a DSPy Module?
+
+A **DSPy module** is a building block for programs that use LMs.
+
+- Each built-in module abstracts a **prompting technique** (like chain of thought or ReAct). Crucially, they are generalized to handle any [DSPy Signature]().
+
+- 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.
+
+- Multiple modules can be composed into bigger modules (programs). DSPy modules are inspired directly by NN modules in PyTorch, but applied to LM programs.
+
+
+### How do I use a built-in module, like `dspy.Predict` or `dspy.ChainOfThought`?
+
+Let's start with the most fundamental module, `dspy.Predict`. Internally, all other DSPy modules are just built using `dspy.Predict`.
+
+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.
+
+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!
+
+```python
+sentence = "it's a charming and often affecting journey." # example from the SST-2 dataset.
+
+# 1) Declare with a signature.
+classify = dspy.Predict('sentence -> sentiment')
+
+# 2) Call with input argument(s).
+response = classify(sentence=sentence)
+
+# 3) Access the output.
+print(response.sentiment)
+```
+**Output:**
+```text
+Positive
+```
+
+When we declare a module, we can pass configuration keys to it.
+
+Below, we'll pass `n=5` to request five completions. We can also pass `temperature` or `max_len`, etc.
+
+Let's use `dspy.ChainOfThought`. In many cases, simply swapping `dspy.ChainOfThought` in place of `dspy.Predict` improves quality.
+
+```python
+question = "What's something great about the ColBERT retrieval model?"
+
+# 1) Declare with a signature, and pass some config.
+classify = dspy.ChainOfThought('question -> answer', n=5)
+
+# 2) Call with input argument.
+response = classify(question=question)
+
+# 3) Access the outputs.
+response.completions.answer
+```
+**Output:**
+```text
+['One great thing about the ColBERT retrieval model is its superior efficiency and effectiveness compared to other models.',
+ 'Its ability to efficiently retrieve relevant information from large document collections.',
+ 'One great thing about the ColBERT retrieval model is its superior performance compared to other models and its efficient use of pre-trained language models.',
+ 'One great thing about the ColBERT retrieval model is its superior efficiency and accuracy compared to other models.',
+ 'One great thing about the ColBERT retrieval model is its ability to incorporate user feedback and support complex queries.']
+```
+
+Let's dicuss the output object here.
+
+The `dspy.ChainOfThought` module will generally inject a `rationale` before the output field(s) of your signature.
+
+Let's inspect the (first) rationale and answer!
+
+```python
+print(f"Rationale: {response.rationale}")
+print(f"Answer: {response.answer}")
+```
+**Output:**
+```text
+Rationale: produce the answer. We can consider the fact that ColBERT has shown to outperform other state-of-the-art retrieval models in terms of efficiency and effectiveness. It uses contextualized embeddings and performs document retrieval in a way that is both accurate and scalable.
+Answer: One great thing about the ColBERT retrieval model is its superior efficiency and effectiveness compared to other models.
+```
+
+This is accessible whether we request one or many completions.
+
+We can also access the different completions as a list of `Prediction`s or as several lists, one for each field.
+
+```python
+response.completions[3].rationale == response.completions.rationale[3]
+```
+**Output:**
+True
+
+
+## What other DSPy modules are there? How can I use them?
+
+The others are very similar. They mainly change the internal behavior with which your signature is implemented!
+
+1. **`dspy.Predict`**:
+
+2. **`dspy.ChainOfThought`**:
+
+3. **`dspy.ProgramOfThought`**:
+
+4. **`dspy.ReAct`**:
+
+5. **`dspy.MultiChainComparison`**:
+
+
+We also have some function-style modules:
+
+6. **`dspy.majority`**:
+
+
+More example soon!
+
+
+## How do I compose multiple modules into a bigger program?
+
+DSPy is just Python code that uses modules in any control flow you like. (There's some magic internally at `compile` time to trace your LM calls.)
+
+What this means is that, you can just call the modules freely. No weird abstractions for chaining calls.
+
+This is basically PyTorch's design approach for define-by-run / dynamic computation graphs. Refer to the intro tutorials for examples.