-
Notifications
You must be signed in to change notification settings - Fork 774
FEAT: add llama_cpp json schema output #4282
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
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
757af7c
FEAT: add llama_cpp json schema output
OliverBryant 0501936
modify llama_cpp_python to xllamacpp
OliverBryant 0c0eead
add a test python
OliverBryant 288c99a
add new xllamacpp unit test
OliverBryant 1dbc12d
test CI thread error
OliverBryant 464ca84
test CI thread error
OliverBryant 4739b39
test CI thread error
OliverBryant 7ad1c71
test CI thread error
OliverBryant de7c64e
test CI thread error
OliverBryant 1762822
test CI thread error
OliverBryant 6314f43
test CI thread error
OliverBryant e3f4f0f
test CI thread error
OliverBryant 3bd495a
test CI thread error
OliverBryant 3cae81e
add real test for llama cpp json schema
OliverBryant 7cb457b
add real test for llama cpp json schema
OliverBryant fb244c5
fix test
OliverBryant 3642ea0
fix test
OliverBryant 268b227
fix ci error
OliverBryant 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
103 changes: 103 additions & 0 deletions
103
xinference/model/llm/llama_cpp/tests/test_structured.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,103 @@ | ||
| import sys | ||
| from types import SimpleNamespace | ||
|
|
||
|
|
||
| def test_apply_response_format_sets_grammar(monkeypatch): | ||
| from xinference.model.llm.llama_cpp.core import _apply_response_format | ||
|
|
||
| fake_xllamacpp = SimpleNamespace(json_schema_to_grammar=lambda schema: "GRAMMAR") | ||
| monkeypatch.setitem(sys.modules, "xllamacpp", fake_xllamacpp) | ||
|
|
||
| cfg = { | ||
| "response_format": { | ||
| "type": "json_schema", | ||
| "json_schema": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": {"a": {"type": "string"}}, | ||
| "required": ["a"], | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| _apply_response_format(cfg) | ||
|
|
||
| assert "response_format" not in cfg | ||
| assert cfg["json_schema"]["required"] == ["a"] | ||
| assert cfg["grammar"] == "GRAMMAR" | ||
|
|
||
|
|
||
| def test_apply_response_format_handles_conversion_failure(monkeypatch): | ||
| from xinference.model.llm.llama_cpp.core import _apply_response_format | ||
qinxuye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def _raise(_): | ||
| raise ValueError("bad schema") | ||
|
|
||
| fake_xllamacpp = SimpleNamespace(json_schema_to_grammar=_raise) | ||
| monkeypatch.setitem(sys.modules, "xllamacpp", fake_xllamacpp) | ||
|
|
||
| cfg = { | ||
| "response_format": { | ||
| "type": "json_schema", | ||
| "json_schema": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": {"b": {"type": "string"}}, | ||
| "required": ["b"], | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| _apply_response_format(cfg) | ||
|
|
||
| assert "response_format" not in cfg | ||
| assert cfg["json_schema"]["required"] == ["b"] | ||
| assert "grammar" not in cfg | ||
|
|
||
|
|
||
| def test_apply_response_format_ignores_non_schema(monkeypatch): | ||
| from xinference.model.llm.llama_cpp.core import _apply_response_format | ||
qinxuye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| cfg = {"response_format": {"type": "json_object"}} | ||
| _apply_response_format(cfg) | ||
| assert "grammar" not in cfg | ||
| assert "json_schema" not in cfg | ||
|
|
||
|
|
||
| def test_apply_response_format_uses_real_xllamacpp_if_available(): | ||
| import importlib.util | ||
qinxuye marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import pytest | ||
|
|
||
| if importlib.util.find_spec("xllamacpp") is None: | ||
| pytest.skip("xllamacpp not installed") | ||
|
|
||
| import importlib | ||
|
|
||
| xllamacpp = importlib.import_module("xllamacpp") | ||
| if not hasattr(xllamacpp, "json_schema_to_grammar"): | ||
| pytest.skip("xllamacpp does not expose json_schema_to_grammar") | ||
|
|
||
| from xinference.model.llm.llama_cpp.core import _apply_response_format | ||
|
|
||
| cfg = { | ||
| "response_format": { | ||
| "type": "json_schema", | ||
| "json_schema": { | ||
| "schema": { | ||
| "type": "object", | ||
| "properties": {"c": {"type": "integer"}}, | ||
| "required": ["c"], | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| _apply_response_format(cfg) | ||
|
|
||
| assert "response_format" not in cfg | ||
| # Real xllamacpp should attach grammar alongside json_schema | ||
| assert "json_schema" in cfg | ||
| assert "grammar" in cfg and cfg["grammar"] | ||
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.
Uh oh!
There was an error while loading. Please reload this page.