-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[https://nvbugs/5361178][fix]: Json schema support in trtllm-serve using xgrammar #6197
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
Open
mayani-nv
wants to merge
7
commits into
NVIDIA:main
Choose a base branch
from
mayani-nv:json_schmea_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−3
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
834821d
Update openai_protocol.py
mayani-nv 52f833a
Update openai_server.py
mayani-nv 3009662
Update openai_server.py
mayani-nv 02e62f2
Update openai_server.py
mayani-nv 005cbbb
Merge branch 'NVIDIA:main' into json_schmea_support
mayani-nv c709b28
Update openai_server.py
mayani-nv 93ba7d5
Create _test_openai_json_schema.py
mayani-nv 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import os | ||
import tempfile | ||
|
||
import openai | ||
import pytest | ||
import yaml | ||
from pydantic import BaseModel, Field | ||
|
||
from ..test_llm import get_model_path | ||
from .openai_server import RemoteOpenAIServer | ||
|
||
pytestmark = pytest.mark.threadleak(enabled=False) | ||
|
||
|
||
@pytest.fixture(scope="module", ids=["TinyLlama-1.1B-Chat"]) | ||
def model_name(): | ||
return "llama-3.1-model/Llama-3.1-8B-Instruct" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def temp_extra_llm_api_options_file(request): | ||
temp_dir = tempfile.gettempdir() | ||
temp_file_path = os.path.join(temp_dir, "extra_llm_api_options.yaml") | ||
try: | ||
extra_llm_api_options_dict = {"guided_decoding_backend": "xgrammar"} | ||
|
||
with open(temp_file_path, 'w') as f: | ||
yaml.dump(extra_llm_api_options_dict, f) | ||
|
||
yield temp_file_path | ||
finally: | ||
if os.path.exists(temp_file_path): | ||
os.remove(temp_file_path) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def server(model_name: str, temp_extra_llm_api_options_file: str): | ||
model_path = get_model_path(model_name) | ||
args = [ | ||
"--backend", "pytorch", "--extra_llm_api_options", | ||
temp_extra_llm_api_options_file | ||
] | ||
with RemoteOpenAIServer(model_path, args) as remote_server: | ||
yield remote_server | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def client(server: RemoteOpenAIServer): | ||
return server.get_client() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def async_client(server: RemoteOpenAIServer): | ||
return server.get_async_client() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def capital_info_model(): | ||
|
||
class CapitalInfo(BaseModel): | ||
name: str = Field(..., | ||
pattern=r"^\w+$", | ||
description="The name of the capital city") | ||
population: int = Field(..., | ||
description="The population of the capital city") | ||
|
||
return CapitalInfo | ||
|
||
|
||
def test_chat_json_schema(client: openai.OpenAI, model_name: str, | ||
capital_info_model): | ||
|
||
CapitalInfo = capital_info_model | ||
messages = [{ | ||
"role": | ||
"user", | ||
"content": | ||
"Please generate the information of the capital of France in the JSON format. ", | ||
}, ] | ||
|
||
chat_completion = client.chat.completions.create( | ||
model=model_name, | ||
messages=messages, | ||
response_format={ | ||
"type": "json_schema", | ||
"json_schema": CapitalInfo.model_json_schema(), | ||
}, | ||
temperature=0.7, | ||
max_completion_tokens=100, | ||
) | ||
|
||
assert chat_completion.id is not None | ||
assert len(chat_completion.choices) == 1 | ||
message = chat_completion.choices[0].message | ||
assert message.content is not None | ||
assert message.role == "assistant" | ||
|
||
capital_info = CapitalInfo.model_validate_json(message.content) | ||
|
||
assert isinstance(capital_info, CapitalInfo) | ||
assert capital_info.name == "Paris" | ||
assert isinstance(capital_info.population, int) | ||
assert capital_info.population > 0 |
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.
please add a simple test case for json_schema. can be based on the test for structural_tag.
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.
Done, please review.