-
-
Notifications
You must be signed in to change notification settings - Fork 11k
[V1] Add structural_tag support using xgrammar
#17085
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
Merged
+270
−15
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e438c9b
[V1] Add `structural_tag` support using xgrammar
russellb 86069e3
openai: define AnyResponseFormat to avoid duplication
russellb baa933c
Expand assertion to include the type for StructuralTagResponseFormat
russellb 5fe716d
Simplify code to count how many guided methods are set
russellb af097b7
Add test case for structural_tag with xgrammar
russellb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
examples/online_serving/openai_chat_completion_structured_outputs_structural_tag.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from openai import OpenAI | ||
|
|
||
| # This example demonstrates the `structural_tag` response format. | ||
| # It can be used to specify a structured output format that occurs between | ||
| # specific tags in the response. This example shows how it could be used | ||
| # to enforce the format of a tool call response, but it could be used for | ||
| # any structured output within a subset of the response. | ||
|
|
||
|
|
||
| def main(): | ||
| client = OpenAI( | ||
| base_url="http://localhost:8000/v1", | ||
| api_key="-", | ||
| ) | ||
|
|
||
| messages = [{ | ||
| "role": | ||
| "user", | ||
| "content": | ||
| """ | ||
| You have access to the following function to retrieve the weather in a city: | ||
|
|
||
| { | ||
| "name": "get_weather", | ||
| "parameters": { | ||
| "city": { | ||
| "param_type": "string", | ||
| "description": "The city to get the weather for", | ||
| "required": True | ||
| } | ||
| } | ||
| } | ||
|
|
||
| If a you choose to call a function ONLY reply in the following format: | ||
| <{start_tag}={function_name}>{parameters}{end_tag} | ||
| where | ||
|
|
||
| start_tag => `<function` | ||
| parameters => a JSON dict with the function argument name as key and function | ||
| argument value as value. | ||
| end_tag => `</function>` | ||
|
|
||
| Here is an example, | ||
| <function=example_function_name>{"example_name": "example_value"}</function> | ||
|
|
||
| Reminder: | ||
| - Function calls MUST follow the specified format | ||
| - Required parameters MUST be specified | ||
| - Only call one function at a time | ||
| - Put the entire function call reply on one line | ||
| - Always add your sources when using search results to answer the user query | ||
|
|
||
| You are a helpful assistant. | ||
|
|
||
| Given the previous instructions, what is the weather in New York City, Boston, | ||
| and San Francisco? | ||
| """ | ||
| }] | ||
|
|
||
| response = client.chat.completions.create( | ||
| model="meta-llama/Llama-3.1-8B-Instruct", | ||
| messages=messages, | ||
| response_format={ | ||
| "type": | ||
| "structural_tag", | ||
| "structures": [{ | ||
| "begin": "<function=get_weather>", | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "city": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| }, | ||
| "end": "</function>" | ||
| }], | ||
| "triggers": ["<function="] | ||
| }) | ||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then you don't have to use json here :)