Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gen-like arguments to substring #439

Closed
SimJeg opened this issue Nov 16, 2023 · 1 comment
Closed

Add gen-like arguments to substring #439

SimJeg opened this issue Nov 16, 2023 · 1 comment

Comments

@SimJeg
Copy link

SimJeg commented Nov 16, 2023

Hello,

Following this issue (thanks @marcotcr for the quick reply !), would it be possible to add more gen arguments in the substring component ?

For instance name to save the result of the generation, regex to allow only substrings that respect a given pattern,max_tokens to limit the generation, stop to stop it etc.

This substring is really great because it limits hallucinations, thanks for it :)

Thanks,
Simon

@marcotcr
Copy link
Collaborator

Hi there. We really need to document the features in the grammar library, but in the meantime he is an example where we:

  1. Save the result of a generation in a variable, using capture, even when something is optional
  2. Set max_tokens only on part of the grammar, using token_limit
from guidance import capture, substring, select, regex, token_limit
# Either substring or a number with at most one token
grammar = select(
        [capture(substring('This is very cool.'), name='substring'),
         token_limit(capture(regex('\d+'), name='number'), 1)])
lm = llama2 + 'Hi there, here is a sentence "' + (grammar + '"')
lm = llama2 + 'Hi there, here is a sentence "' + (grammar + '"')
lm['substring']

Hi there, here is a sentence "This is very cool."

lm = llama2 + 'Hi there, here is a number: ' + grammar
lm['number']

Hi there, here is a number: 123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants