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

feature: constrained grammars #354

Closed
mudler opened this issue May 23, 2023 · 4 comments · Fixed by #726
Closed

feature: constrained grammars #354

mudler opened this issue May 23, 2023 · 4 comments · Fixed by #726

Comments

@mudler
Copy link
Owner

mudler commented May 23, 2023

Is your feature request related to a problem? Please describe.
Output of valid JSON, YAML, or alikes from a model is challenging. Models hallucinates, prompts needs to be fine-tuned, and there is no guarantee it spits valid output format.

Describe the solution you'd like
A way to constrain the output with a grammar. Ideally, would be great to have here as an endpoint this https://twitter.com/GrantSlatton/status/1660348210605596672 - grantslatton/llama.cpp@007e26a .

Describe alternatives you've considered
N/A

Additional context

@kcarnold
Copy link

Related existing solution https://github.com/microsoft/guidance/

@lee-b
Copy link

lee-b commented Jul 2, 2023

See also: https://github.com/1rgs/jsonformer

I think there are probably two ways to accomplish this:

  1. At the prompt/output level, generating all of the syntax between values as a prompt, then prompting for only the value, and cutting off the response when a syntactically incorrect token is generated, and so on. Pro: should work with any model / implementation. Con: while it should never generate invalid json, it probably generates empty json that might need to be thrown away at a higher-level json schema checker.

  2. at the sampler level, with knowledge of the desired syntax, so adjusting the likelihood of generating a particular token. Benefit: should work much more reliably, with true syntax awareness. Con: is probably limited to one NN architecture/SDK, like pytorch.

Both approaches have pros and cons, and both require significant effort in parsing syntax.

JSONFormer appears to be the latter, better approach, but also refers to other implementations that seem to do the former.

@mudler
Copy link
Owner Author

mudler commented Jul 2, 2023

I can share some updates very soon - I got it working with the llama.cpp PR which adds constrained grammars ( I don't have the link now). I could successfully implement OpenAI functions. Will follow up shortly with my experiments

@mudler
Copy link
Owner Author

mudler commented Jul 2, 2023

When llama.cpp is going to merge this PR: ggerganov/llama.cpp#1773 constrained grammar output will be unblocked and quite easy to implement. I've hacked my way today something, will cleanup and push in a branch soon.

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

Successfully merging a pull request may close this issue.

3 participants