Skip to content

Commit 4232345

Browse files
committed
new docs
1 parent 67eecb9 commit 4232345

File tree

8 files changed

+424
-53
lines changed

8 files changed

+424
-53
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
21+
22+
.env
23+
*.log
24+
*.ipynb
Lines changed: 84 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 2
33
---
44

55
# Language Models
66

7+
The most powerful features in DSPy revolve around algorithmically optimizing the prompts (or weights) of LMs, especially when you're building programs that use the LMs within a pipeline.
78

8-
## Remote LMs.
9-
10-
These models are managed services. You just need to sign up and obtain an API key.
9+
Let's first make sure you can set up your language model. DSPy support clients for many remote and local LMs.
1110

12-
1. `dspy.OpenAI` for GPT-3.5 and GPT-4.
11+
## Setting up the LM client.
1312

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

16-
3. `dspy.Anyscale` for hosted Llama2 models.
15+
For example, to use OpenAI language models, you can do it as follows.
1716

18-
### Local LMs.
17+
```python
18+
gpt3_turbo = dspy.OpenAI(model='gpt-3.5-turbo-1106', max_tokens=300)
19+
dspy.configure(lm=gpt3_turbo)
20+
```
1921

20-
You need to host these models on your own GPU(s). Below, we include pointers for how to do that.
22+
['Hello! How can I assist you today?']
2123

22-
1. `dspy.HFClientTGI`: for HuggingFace models through the Text Generation Inference (TGI) system. [Tutorial: How do I install and launch the TGI server?](/api/hosting_language_models_locally/TGI)
24+
## Directly calling the LM.
2325

24-
2. `dspy.HFClientVLLM`: for HuggingFace models through vLLM. [Tutorial: How do I install and launch the vLLM server?](/api/hosting_language_models_locally/vLLM)
26+
You can simply call the LM with a string to give it a raw prompt, i.e. a string.
2527

26-
3. `dspy.HFModel` (experimental)
28+
```python
29+
gpt3_turbo("hello! this is a raw prompt to GPT-3.5")
30+
```
2731

28-
4. `dspy.Ollama` (experimental)
32+
This is almost never the recommended way to interact with LMs in DSPy, but it is allowed.
2933

30-
5. `dspy.ChatModuleClient` (experimental): [How do I install and use MLC?](/api/hosting_language_models_locally/MLC)
34+
## Using the LM with DSPy signatures.
3135

32-
If there are other clients you want added, let us know!
36+
You can also use the LM via DSPy [signatures] and [modules], which we discuss in more depth in the remaining guides.
3337

34-
## Setting up the LM client.
35-
36-
You can just call the constructor that connects to the LM. Then, use
37-
`dspy.configure` to declare this as the default LM.
38-
39-
For example, for OpenAI, you can do it as follows.
38+
```python
39+
# Define a module (ChainOfThought) and assign it a signature (return an answer, given a question).
40+
qa = dspy.ChainOfThought('question -> answer')
4041

41-
``` python
42-
gpt3_turbo = dspy.OpenAI(model='gpt-3.5-turbo-1106', max_tokens=300)
43-
gpt4_turbo = dspy.OpenAI(model='gpt-4-1106-preview', max_tokens=300)
42+
# Run with the default LM configured with `dspy.configure` above.
43+
response = qa(question="How many floors are in the castle David Gregory inherited?")
44+
print(response.answer)
45+
```
4446

45-
# cohere = dspy.Cohere(...)
46-
# anyscale = dspy.Anyscale(...)
47-
# tgi_llama2 = dspy.HFClientTGI(model="meta-llama/Llama-2-7b-hf", port=8080, url="http://localhost")
47+
The castle David Gregory inherited has 7 floors.
4848

49-
dspy.configure(lm=gpt3_turbo)
50-
```
5149

5250
## Using multiple LMs at once.
5351

@@ -57,19 +55,22 @@ Instead of changing the default LM, you can just change it inside a block of cod
5755

5856
**Tip:** Using `dspy.configure` and `dspy.context` is thread-safe!
5957

60-
``` python
61-
qa = dspy.ChainOfThought('question -> answer')
62-
58+
```python
59+
# Run with the default LM configured above, i.e. GPT-3.5
6360
response = qa(question="How many floors are in the castle David Gregory inherited?")
64-
print(response.answer)
61+
print('GPT-3.5:', response.answer)
62+
63+
gpt4_turbo = dspy.OpenAI(model='gpt-4-1106-preview', max_tokens=300)
6564

65+
# Run with GPT-4 instead
6666
with dspy.context(lm=gpt4_turbo):
6767
response = qa(question="How many floors are in the castle David Gregory inherited?")
68-
print(response.answer)
68+
print('GPT-4-turbo:', response.answer)
6969
```
7070

71-
The castle David Gregory inherited has 7 floors.
72-
The number of floors in the castle David Gregory inherited cannot be determined with the information provided.
71+
GPT-3.5: The castle David Gregory inherited has 7 floors.
72+
GPT-4-turbo: The number of floors in the castle David Gregory inherited cannot be determined with the information provided.
73+
7374

7475
## Tips and Tricks.
7576

@@ -80,7 +81,7 @@ will get new outputs.)
8081
To generate 5 outputs, you can use `n=5` in the module constructor, or
8182
pass `config=dict(n=5)` when invoking the module.
8283

83-
``` python
84+
```python
8485
qa = dspy.ChainOfThought('question -> answer', n=5)
8586

8687
response = qa(question="How many floors are in the castle David Gregory inherited?")
@@ -99,14 +100,53 @@ return the same value! That\'s by design.
99100
To loop and generate one output at a time with the same input, bypass
100101
the cache by making sure each request is (slightly) unique, as below.
101102

102-
``` python
103+
```python
103104
for idx in range(5):
104105
response = qa(question="How many floors are in the castle David Gregory inherited?", config=dict(temperature=0.7+0.0001*idx))
105-
print(response.answer)
106+
print(f'{idx+1}.', response.answer)
106107
```
107108

108-
The specific number of floors in David Gregory's inherited castle is not provided here, so further research would be needed to determine the answer.
109-
It is not possible to determine the exact number of floors in the castle David Gregory inherited without specific information about the castle's layout and history.
110-
The castle David Gregory inherited has 5 floors.
111-
We need more information to determine the number of floors in the castle David Gregory inherited.
112-
The castle David Gregory inherited has a total of 6 floors.
109+
1. The specific number of floors in David Gregory's inherited castle is not provided here, so further research would be needed to determine the answer.
110+
2. It is not possible to determine the exact number of floors in the castle David Gregory inherited without specific information about the castle's layout and history.
111+
3. The castle David Gregory inherited has 5 floors.
112+
4. We need more information to determine the number of floors in the castle David Gregory inherited.
113+
5. The castle David Gregory inherited has a total of 6 floors.
114+
115+
116+
## Remote LMs.
117+
118+
These models are managed services. You just need to sign up and obtain an API key.
119+
120+
1. `dspy.OpenAI` for GPT-3.5 and GPT-4.
121+
122+
2. `dspy.Cohere`
123+
124+
3. `dspy.Anyscale` for hosted Llama2 models.
125+
126+
127+
### Local LMs.
128+
129+
You need to host these models on your own GPU(s). Below, we include pointers for how to do that.
130+
131+
1. `dspy.HFClientTGI`: for HuggingFace models through the Text Generation Inference (TGI) system. [Tutorial: How do I install and launch the TGI server?](/api/hosting_language_models_locally/TGI)
132+
133+
2. `dspy.HFClientVLLM`: for HuggingFace models through vLLM. [Tutorial: How do I install and launch the vLLM server?](/api/hosting_language_models_locally/vLLM)
134+
135+
3. `dspy.HFModel` (experimental)
136+
137+
4. `dspy.Ollama` (experimental)
138+
139+
5. `dspy.ChatModuleClient` (experimental): [How do I install and use MLC?](/api/hosting_language_models_locally/MLC)
140+
141+
If there are other clients you want added, let us know!
142+
143+
144+
<!-- TODO: Usage examples for these all.
145+
146+
```python
147+
148+
# cohere = dspy.Cohere(...)
149+
# anyscale = dspy.Anyscale(...)
150+
# tgi_llama2 = dspy.HFClientTGI(model="meta-llama/Llama-2-7b-hf", port=8080, url="http://localhost")
151+
152+
``` -->
File renamed without changes.

docs/building-blocks/2-modules.md renamed to docs/building-blocks/3-modules.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ True
9696

9797
The others are very similar. They mainly change the internal behavior with which your signature is implemented!
9898

99-
1. **`dspy.Predict`**:
99+
1. **`dspy.Predict`**: Basic predictor. Does not modify the signature. Handles the key forms of learning (i.e., storing the instructions and demonstrations and updates to the LM).
100100

101-
2. **`dspy.ChainOfThought`**:
101+
2. **`dspy.ChainOfThought`**: Teaches the LM to think step-by-step before committing to the signature's response.
102102

103-
3. **`dspy.ProgramOfThought`**:
103+
3. **`dspy.ProgramOfThought`**: Teaches the LM to output code, whose execution results will dictate the response.
104104

105-
4. **`dspy.ReAct`**:
105+
4. **`dspy.ReAct`**: An agent that can use tools to implement the given signature.
106106

107-
5. **`dspy.MultiChainComparison`**:
107+
5. **`dspy.MultiChainComparison`**: Can compare multiple outputs from `ChainOfThought` to produce a final prediction.
108108

109109

110110
We also have some function-style modules:
111111

112-
6. **`dspy.majority`**:
112+
6. **`dspy.majority`**: Can do basic voting to return the most popular response from a set of predictions.
113113

114114

115115
More example soon!

docs/building-blocks/4-data.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# Data
6+
7+
DSPy is a machine learning framework, so working in it involves training sets, development sets, and test sets.
8+
9+
For each example in your data, we distinguish typically between three types of values: the inputs, the intermediate labels, and the final label. You can use DSPy effectively without any intermediate or final labels, but you will need at least a few example inputs.
10+
11+
## How much data do I need and how do I collect data for my task?
12+
13+
Concretely, you can use DSPy optimizers usefully with as few as 10 example inputs, but having 50-100 examples (or even better, 300-500 examples) goes a long way.
14+
15+
How can you get examples like these? If your task is extremely unusual, please invest in preparing ~10 examples by hand. Often times, depending on your metric below, you just need inputs and not labels, so it's not that hard.
16+
17+
However, chances are that your task is not actually that unique. You can almost always find somewhat adjacent datasets on, say, HuggingFace datasets or other forms of data that you can leverage here.
18+
19+
If there's data whose licenses are permissive enough, we suggest you use them. Otherwise, you can also start using/deploying/demoing your system and collect some initial data that way.
20+
21+
## DSPy `Example` objects
22+
23+
The core data type for data in DSPy is `Example`. You will use **Examples** to represent items in your training set and test set.
24+
25+
DSPy **Examples** are similar to Python `dict`s but have a few useful utilities. Your DSPy modules will return values of the type `Prediction`, which is a special sub-class of `Example`.
26+
27+
When you use DSPy, you will do a lot of evaluation and optimization runs. Your individual datapoints will be of type `Example`:
28+
29+
```python
30+
qa_pair = dspy.Example(question="This is a question?", answer="This is an answer.")
31+
32+
print(qa_pair)
33+
print(qa_pair.question)
34+
print(qa_pair.answer)
35+
```
36+
**Output:**
37+
```text
38+
Example({'question': 'This is a question?', 'answer': 'This is an answer.'}) (input_keys=None)
39+
This is a question?
40+
This is an answer.
41+
```
42+
43+
Examples can have any field keys and any value types, though usually values are strings.
44+
45+
```text
46+
object = Example(field1=value1, field2=value2, field3=value3, ...)
47+
```
48+
49+
You can now express your training set for example as:
50+
51+
```python
52+
trainset = [dspy.Example(report="LONG REPORT 1", summary="short summary 1"), ...]
53+
```
54+
55+
56+
### Specifying Input Keys
57+
58+
In traditional ML, there are separated "inputs" and "labels".
59+
60+
In DSPy, the `Example` objects have a `with_inputs()` method, which can mark specific fields as inputs. (The rest are just metadata or labels.)
61+
62+
```python
63+
# Single Input.
64+
print(qa_pair.with_inputs("question"))
65+
66+
# Multiple Inputs; be careful about marking your labels as inputs unless you mean it.
67+
print(qa_pair.with_inputs("question", "answer"))
68+
```
69+
70+
Values can be accessed using the `.`(dot) operator. You can access the value of key `name` in defined object `Example(name="John Doe", job="sleep")` through `object.name`.
71+
72+
To access or exclude certain keys, use `inputs()` and `labels()` methods to return new Example objects containing only input or non-input keys, respectively.
73+
74+
```python
75+
article_summary = dspy.Example(article= "This is an article.", summary= "This is a summary.").with_inputs("article")
76+
77+
input_key_only = article_summary.inputs()
78+
non_input_key_only = article_summary.labels()
79+
80+
print("Example object with Input fields only:", input_key_only)
81+
print("Example object with Non-Input fields only:", non_input_key_only))
82+
```
83+
84+
**Output**
85+
```
86+
Example object with Input fields only: Example({'article': 'This is an article.'}) (input_keys=None)
87+
Example object with Non-Input fields only: Example({'summary': 'This is a summary.'}) (input_keys=None)
88+
```

0 commit comments

Comments
 (0)