Skip to content

Commit 7fae436

Browse files
lapp0Andrew Lapp
and
Andrew Lapp
authored
Use Better Models, Better Prompts in Docs (#619)
Some of the results in the docs are difficult to reproduce. Prompt engineering and better model recommendations are applied here to alleviate that. fixes #586 - Update docs: never recommend `mistralai/Mistral-7B-v0.1`, always prefer `WizardLM/WizardMath-7B-V1.1` for math above kindergarten level and `mistralai/Mistral-7B-Instruct-v0.2` for everything else. - Smoke tested all `README.md` and `quickstart.md` examples. Replaced model with `mistralai/Mistral-7B-Instruct-v0.2`, generation quality was equal or better in all cases. - fixes #612 (see #612 (comment)) - don't force `device="cuda"` in any `outlines.models.transformers` - fix arithmetic grammar to not have recursion issue (fixes #580) --------- Co-authored-by: Andrew Lapp <[email protected]>
1 parent 1626cea commit 7fae436

File tree

2 files changed

+60
-73
lines changed

2 files changed

+60
-73
lines changed

README.md

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ You can reduce the completion to a choice between multiple possibilities:
5353
``` python
5454
import outlines
5555

56-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
56+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
5757

5858
prompt = """You are a sentiment-labelling assistant.
5959
Is the following review positive or negative?
@@ -73,15 +73,18 @@ You can instruct the model to only return integers or floats:
7373
``` python
7474
import outlines
7575

76-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
76+
model = outlines.models.transformers("WizardLM/WizardMath-7B-V1.1")
7777

78-
prompt = "1+1="
78+
prompt = "<s>result of 9 + 9 = 18</s><s>result of 1 + 2 = "
7979
answer = outlines.generate.format(model, int)(prompt)
80+
print(answer)
81+
# 3
8082

8183
prompt = "sqrt(2)="
82-
8384
generator = outlines.generate.format(model, float)
84-
answer = generator(prompt)
85+
answer = generator(prompt, max_tokens=10)
86+
print(answer)
87+
# 1.41421356
8588
```
8689

8790
### Efficient regex-guided generation
@@ -93,7 +96,7 @@ hood:
9396
``` python
9497
import outlines
9598

96-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
99+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
97100

98101
prompt = "What is the IP address of the Google DNS servers? "
99102

@@ -156,7 +159,7 @@ class Character(BaseModel):
156159
strength: int
157160

158161

159-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1", device="cuda")
162+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
160163

161164
# Construct guided sequence generator
162165
generator = outlines.generate.json(model, Character, max_tokens=100)
@@ -165,25 +168,15 @@ generator = outlines.generate.json(model, Character, max_tokens=100)
165168
rng = torch.Generator(device="cuda")
166169
rng.manual_seed(789001)
167170

168-
sequence = generator("Give me a character description", rng=rng)
169-
print(sequence)
170-
# {
171-
# "name": "clerame",
172-
# "age": 7,
173-
# "armor": "plate",
174-
# "weapon": "mace",
175-
# "strength": 4171
176-
# }
177-
178-
sequence = generator("Give me an interesting character description", rng=rng)
179-
print(sequence)
180-
# {
181-
# "name": "piggyback",
182-
# "age": 23,
183-
# "armor": "chainmail",
184-
# "weapon": "sword",
185-
# "strength": 0
186-
# }
171+
character = generator("Give me a character description", rng=rng)
172+
173+
print(repr(character))
174+
# Character(name='Anderson', age=28, armor=<Armor.chainmail: 'chainmail'>, weapon=<Weapon.sword: 'sword'>, strength=8)
175+
176+
character = generator("Give me an interesting character description", rng=rng)
177+
178+
print(repr(character))
179+
# Character(name='Vivian Thr', age=44, armor=<Armor.plate: 'plate'>, weapon=<Weapon.crossbow: 'crossbow'>, strength=125)
187180
```
188181

189182
The method works with union types, optional types, arrays, nested schemas, etc. Some field constraints are [not supported yet](https://github.com/outlines-dev/outlines/issues/215), but everything else should work.
@@ -232,9 +225,9 @@ schema = '''{
232225
}
233226
}'''
234227

235-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1", device="cuda")
228+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
236229
generator = outlines.generate.json(model, schema)
237-
sequence = generator("Give me a character description")
230+
character = generator("Give me a character description")
238231
```
239232

240233
### Using context-free grammars to guide generation
@@ -245,34 +238,25 @@ Formal grammars rule the world, and Outlines makes them rule LLMs too. You can p
245238
import outlines
246239

247240
arithmetic_grammar = """
248-
?start: sum
241+
?start: expression
249242
250-
?sum: product
251-
| sum "+" product -> add
252-
| sum "-" product -> sub
243+
?expression: term (("+" | "-") term)*
253244
254-
?product: atom
255-
| product "*" atom -> mul
256-
| product "/" atom -> div
245+
?term: factor (("*" | "/") factor)*
257246
258-
?atom: NUMBER -> number
259-
| "-" atom -> neg
260-
| "(" sum ")"
247+
?factor: NUMBER
248+
| "-" factor
249+
| "(" expression ")"
261250
262251
%import common.NUMBER
263-
%import common.WS_INLINE
264-
265-
%ignore WS_INLINE
266252
"""
267253

268-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1", device="cuda")
254+
model = outlines.models.transformers("WizardLM/WizardMath-7B-V1.1")
269255
generator = outlines.generate.cfg(model, arithmetic_grammar)
270-
sequence = generator("Write a formula that returns 5 using only additions and subtractions.")
271-
272-
# It looks like Mistral is not very good at arithmetics :)
256+
sequence = generator("Alice had 4 apples and Bob ate 2. Write an expression for Alice's apples:")
273257

274258
print(sequence)
275-
# 1+3-2-4+5-7+8-6+9-6+4-2+3+5-1+1
259+
# (8-2)
276260
```
277261

278262
This was a very simple grammar, and you can use `outlines.generate.cfg` to generate syntactically valid Python, SQL, and much more than this. Any kind of structured text, really. All you have to do is search for "X EBNF grammar" on the web, and take a look at the [Outlines Grammars repository](https://github.com/outlines-dev/grammars).
@@ -288,9 +272,9 @@ import outlines
288272
def add(a: int, b: int):
289273
return a + b
290274

291-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
275+
model = outlines.models.transformers("WizardLM/WizardMath-7B-V1.1")
292276
generator = outlines.generate.json(model, add)
293-
result = generator("Return two integers named a and b respectively. a is odd and b even.")
277+
result = generator("Return json with two integers named a and b respectively. a is odd and b even.")
294278

295279
print(add(**result))
296280
# 3
@@ -329,7 +313,7 @@ def labelling(to_label, examples):
329313
{{ to_label }} //
330314
"""
331315

332-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
316+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
333317
prompt = labelling("Just awesome", examples)
334318
answer = outlines.generate.text(model)(prompt, max_tokens=100)
335319
```

docs/quickstart.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The first step when writing a program with Outlines is to initialize a model. We
1515
```python
1616
import outlines
1717

18-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
18+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
1919
```
2020

2121
### Text generator
@@ -27,7 +27,7 @@ Once the model is initialized you can build a text generator. This generator can
2727
```python
2828
import outlines
2929

30-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
30+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
3131
generator = outlines.generate.text(model, max_tokens=100)
3232

3333
result = generator("What's 2+2?")
@@ -43,7 +43,7 @@ Once the model is initialized you can build a text generator. This generator can
4343
```python
4444
import outlines
4545

46-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
46+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
4747
generator = outlines.generate.text(model, max_tokens=100)
4848

4949
stream = generator.stream("What's 2+2?")
@@ -64,7 +64,7 @@ Outlines allows you to do multi-label classification by guiding the model so it
6464
```python
6565
import outlines
6666

67-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
67+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
6868
generator = outlines.generate.choice(model, ["Blue", "Red", "Yellow"])
6969

7070
color = generator("What is the closest color to Indigo? ")
@@ -96,7 +96,7 @@ Outlines can guide models so that they output valid JSON **100%** of the time. Y
9696
armor: Armor
9797
strength: conint(gt=1, lt=100)
9898

99-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
99+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
100100
generator = outlines.generate.json(model, Character)
101101

102102
character = generator(
@@ -131,7 +131,7 @@ Outlines can guide models so that they output valid JSON **100%** of the time. Y
131131
"type": "object"
132132
}"""
133133

134-
model = outlines.models.transformers("mistralai/Mistral-7B-v0.1")
134+
model = outlines.models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
135135
generator = outlines.generate.json(model, schema)
136136
character = generator(
137137
"Generate a new character for my awesome game: "
@@ -175,12 +175,12 @@ arithmetic_grammar = """
175175
%ignore WS_INLINE
176176
"""
177177

178-
model = models.transformers("mistralai/Mistral-7B-v0.1")
178+
model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
179179
generator = generate.cfg(model, arithmetic_grammar, max_tokens=100)
180180

181-
result = generator("Write a series of operations on integers that return the number 5")
181+
result = generator("Question: How can you write 5*5 using addition?\nAnswer:")
182182
print(result)
183-
# 4*5*3*2*1/6*4*3*2*1/2*1*1*1/4*1*1*1/2*1*1*1/2*1*1/2*1*1*5*1/2*2*1*1/2*1*1*6*1*1/2*1*1*1*1*2*1*1*1*1
183+
# 5+5+5+5+5
184184
```
185185

186186

@@ -189,12 +189,12 @@ EBNF grammars can be cumbersome to write. This is why Outlines provides grammar
189189
```python
190190
from outlines import models, generate, grammars
191191

192-
model = models.transformers("mistralai/Mistral-7B-v0.1")
192+
model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
193193
generator = generate.cfg(model, grammars.arithmetic, max_tokens=100)
194194

195-
result = generator("Write a series of operations on integers that return the number 5 ")
195+
result = generator("Question: How can you write 5*5 using addition?\nAnswer:")
196196
print(result)
197-
# 100-2-75+50-18+27-501.
197+
# 5+5+5+5+5
198198
```
199199

200200
The available grammars are listed [here](https://github.com/outlines-dev/outlines/tree/main/outlines/grammars).
@@ -207,14 +207,14 @@ Slightly simpler, but no less useful, Outlines can generate text that is in the
207207
```python
208208
from outlines import models, generate
209209

210-
model = models.transformers("mistralai/Mistral-7B-v0.1")
210+
model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
211211

212212
regex_str = r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)"
213213
generator = generate.regex(model, regex_str)
214214

215-
result = generator("What is the IP address of Google's DNS sever?")
215+
result = generator("What is the IP address of localhost?\nIP: ")
216216
print(result)
217-
# 0.0.0.0
217+
# 127.0.0.100
218218
```
219219

220220
### Generate a given Python type
@@ -224,7 +224,7 @@ We provide a shortcut to regex-guided generation for simple use cases. Pass a Py
224224
```python
225225
from outlines import models, generate
226226

227-
model = models.transformers("mistralai/Mistral-7B-v0.1")
227+
model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2")
228228
generator = generate.format(model, int)
229229

230230
result = generator("What is 2+2?")
@@ -245,15 +245,15 @@ python -m outlines.serve.serve
245245
This will by default start a server at `http://127.0.0.1:8000` (check what the console says, though) with the OPT-125M model. If you want to specify another model:
246246

247247
```python
248-
python -m outlines.serve.serve --model="mistralai/Mistral-7B-v0.1"
248+
python -m outlines.serve.serve --model="mistralai/Mistral-7B-Instruct-v0.2"
249249
```
250250

251251
You can then query the model in shell by passing a prompt and a [JSON Schema][jsonschema]{:target="_blank"} specification for the structure of the output:
252252

253253
```bash
254-
curl http://0.0.0.1:8000 \
254+
curl http://127.0.0.1:8000/generate \
255255
-d '{
256-
"prompt": "What is the capital of France?",
256+
"prompt": "Question: What is a language model? Answer:",
257257
"schema": {"type": "string"}
258258
}'
259259
```
@@ -329,25 +329,28 @@ Once you are done experimenting with a prompt and an output structure, it is use
329329

330330
@outlines.prompt
331331
def tell_a_joke(topic):
332-
"""Tell me a joke about {{ topic }}."
332+
"""Tell me a joke about {{ topic }}."""
333333

334334
class Joke(BaseModel):
335335
setup: str
336336
punchline: str
337337

338-
fn = outlines.Function(
338+
generate_joke = outlines.Function(
339339
tell_a_joke,
340340
Joke,
341-
"mistralai/Mistral-7B-v0.1"
341+
"mistralai/Mistral-7B-Instruct-v0.2"
342342
)
343343
```
344344

345345
=== "Call a function"
346346

347347
```python
348-
from .function import fn as joke
348+
from .function import generate_joke
349349

350-
response = joke("baseball")
350+
response = generate_joke("baseball")
351+
352+
# haha
353+
# Joke(setup='Why was the baseball in a bad mood?', punchline='Because it got hit around a lot.')
351354
```
352355

353356
=== "Call a function stored on GitHub"

0 commit comments

Comments
 (0)