guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
18.91k stars 1.04k forks source link

Regex Doesn't support specified number of characters {n} #530

Open msharp9 opened 10 months ago

msharp9 commented 10 months ago

The bug Regex doesn't seem to support basic regex syntax. {n}

To Reproduce Give a full working code snippet that can be pasted into a notebook cell or python file. Make sure to include the LLM load step so we know which model you are using.

# put your code snippet here
from guidance import models, gen

# Load a HuggingFace Transformers model
lm = models.Transformers("gpt2")

lm = lm + 'Generate a phone number: ' + gen(regex='\d{3}-\d{3}-\d{4}')
print(lm)
#  Generate a phone number: 1{3}-8{3}-8{4}

System info (please complete the following information):

slundberg commented 10 months ago

This is a limitation of the pyformlang library we are using right now to convert from regex patterns into guidance grammars. We don't plan to keep this dependency in the long run, so I will leave this issue open to flag that {n} support should be in the next iteration :)

Aunsiels commented 7 months ago

Hi! I added {m} and {n,m} in the latest version of Pyformlang. Do not hesitate to open issues directly on Pyformlang in the future :)