Skip to content

Regular expressions

Outlines can guarantee that the text generated by the LLM will be valid to a regular expression:

from outlines import models, generate

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

generator = generate.regex(

prompt = "What is the IP address of the Google DNS servers? "
answer = generator(prompt, max_tokens=30)

# What is the IP address of the Google DNS servers?

If you find yourself using generate.regex to restrict the answers' type you can take a look at type-structured generation instead.


generate.regex computes an index that helps Outlines guide generation. This can take some time, but only needs to be done once. If you want to generate several times using the same regular expression make sure that you only call generate.regex once.