Regular expression constraints

While you can specify any constraint you want with raw guidance functions/grammars, it is often useful to enforce very simple patterns on our gen() calls. To do this you can specify regular expression formats to which generated text must adhere. Internally these expressions are converted to native guidance grammars (set of guidance functions).

Example

In the following example we know that we want to get number for the generated chapter variable, but GPT2 does not know that, and instead makes up something else.

Invalid output without a regex guide

[1]:
from guidance import models, gen

gpt2 = models.Transformers("gpt2")

gpt2 + f"""\
Tweak this proverb to apply to model instructions instead.

Where there is no guidance, a people falls,
but in an abundance of counselors there is safety.
- Proverbs 11:14

UPDATED
Where there is no guidance{gen('rewrite', stop="- ")}
- GPT {gen('chapter', max_tokens=10)}:{gen('verse', max_tokens=10)}"""
[1]:
Tweak this proverb to apply to model instructions instead.

Where there is no guidance, a people falls,
but in an abundance of counselors there is safety.
- Proverbs 11:14

UPDATED
Where there is no guidance, a people falls,but in an abundance of counselors there is safety.


- GPT - The Bible says that the Lord will guide you:

"And he will guide you,

Valid output with a regex guide

[2]:
gpt2 + f"""\
Tweak this proverb to apply to model instructions instead.

Where there is no guidance, a people falls,
but in an abundance of counselors there is safety.
- Proverbs 11:14

UPDATED
Where there is no guidance{gen('rewrite', stop="- ")}
- GPT {gen('chapter', regex="[0-9]+")}:{gen('verse', regex="[0-9]+")}"""
[2]:
Tweak this proverb to apply to model instructions instead.

Where there is no guidance, a people falls,
but in an abundance of counselors there is safety.
- Proverbs 11:14

UPDATED
Where there is no guidance, a people falls,but in an abundance of counselors there is safety.


- GPT 1:1

Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!