Skip to content

Can guidance generate list of jsons? #402

Answered by marcotcr
CSWellesSun asked this question in Q&A
Discussion options

You must be logged in to vote

I will push something general for json at some point, but here is a version for your simple use case:

from guidance import select, zero_or_more, any_char, one_or_more, commit_point, regex

@guidance(stateless=True)
def simple_json(lm):
    lm += ('{"name": ' + one_or_more(any_char()) + commit_point('"') +
            ', "age": ' + regex('\d+') + commit_point('}') + ',\n')
    return lm

@guidance(stateless=True)
def simple_list_of_jsons(lm):
    lm += '[\n' + one_or_more(simple_json()) + ']'
    return lm

The commit_points are just ways of stopping one_or_more(any_char()) once we hit ", or \d+ ondce we hit '}'.
Notice that this does not constrain the LM, i.e. it can generate jsons forever…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@CSWellesSun
Comment options

Comment options

You must be logged in to vote
1 reply
@motaatmo
Comment options

Answer selected by CSWellesSun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants