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] Support new parameter - EBNF in xgrammar #2526

Merged
merged 6 commits into from
Dec 26, 2024

Conversation

adarshxs
Copy link
Contributor

Motivation

XGrammar enables efficient structured generation. XGrammar backend already support enforcement of EBNF (Extended Backus-Naur Form) grammars.
adds feature requested in: #2376

Modifications

  • added a new parameter called ebnf in sampling_params.py and treat it similar to regex and JSON.
  • Implemented the test_ebnf_generate method to validate multiple valid output patterns based on a complex EBNF grammar.

Checklist

  • Format your code according to the Contributor Guide.
  • Add unit tests as outlined in the Contributor Guide.
  • Update documentation as needed, including docstrings or example tutorials.

TODO: update docs

@zhyncs
Copy link
Member

zhyncs commented Dec 20, 2024

cc @Ubospica @DarkSharpness

Copy link
Contributor

@merrymercy merrymercy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Can you also support it with the OpenAI API?

response = client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "You are a helpful AI assistant"},
{"role": "user", "content": "Introduce the capital of France."},
],
temperature=0,
max_tokens=128,
extra_body={"regex": regex},
)
text = response.choices[0].message.content

test/srt/test_ebnf_constrained.py Outdated Show resolved Hide resolved
@adarshxs
Copy link
Contributor Author

@merrymercy done

Copy link
Contributor

@Ubospica Ubospica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It overall looks great for me. Thanks!

python/sglang/srt/managers/scheduler.py Show resolved Hide resolved
@adarshxs adarshxs requested a review from merrymercy December 26, 2024 12:35
@merrymercy
Copy link
Contributor

Thanks for the contribution! It is merged @adarshxs

@merrymercy merrymercy merged commit acb3407 into sgl-project:main Dec 26, 2024
15 checks passed
@adarshxs
Copy link
Contributor Author

Thank you!

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 this pull request may close these issues.

4 participants