-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Serving] Support tool function calls under strict format constraints #3190
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
Irfnfnkemed
wants to merge
22
commits into
mlc-ai:main
Choose a base branch
from
Irfnfnkemed:tool_call
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.
Conversation
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
- upgrade xgrammar calling to latest API
…mmar - ensure the tool function will be called in expected format using xgrammar - modify RequestResponseFormat: add structural tag according to the tools when building response format - the tool function calling is now constrained by format: <function=function_name>parameters</function> - tools call list will be parsed according to the calling format when processing the response - also expose the Structural Tag api of xgrammar to RequestResponseFormat
- Expose Structural-Tag api, which can be used to standarlize function calling format - Add test script for Structural-Tag (passed on Llama-2-7b-chat-hf-q0f16-MLC and Llama-3-8B-Instruct-q4f16_1-MLC)
- Add "tool_call_format" attribute in EngineConfig, which determines the tool calls format - Add "strict" attribute in ChatFunction, which is aligned to OpenAI format - Set system prompt according to tool_call_format - Set structural tag to ensure strict func calls - Parse output to json-style func calls - TODO: Now only supports format <function=NAME>{PARA}</function>
- rename the 'default' format mode to 'xml'
Ubospica
approved these changes
Apr 15, 2025
@@ -975,13 +977,25 @@ class EngineImpl : public Engine { | |||
* is not JSON, return std::nullopt. */ | |||
std::optional<xgrammar::CompiledGrammar> GetGrammarFromResponseFormat( | |||
const ResponseFormat& response_format) { | |||
if (response_format.type != "json_object") { | |||
// TODO: add other grammar type | |||
if (response_format.type == "text") { |
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.
align with openai api
@@ -86,12 +86,34 @@ class ModelResponse(BaseModel): | |||
|
|||
|
|||
class RequestResponseFormat(BaseModel): | |||
type: Literal["text", "json_object"] = "text" | |||
json_schema: Optional[str] = Field(default=None, alias="schema") | |||
type: Literal["text", "json_object", "structural_tag"] = "text" |
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.
@@ -81,6 +81,8 @@ class Conversation(BaseModel): | |||
function_string: str = "" | |||
# whether using function calling or not, helps check for output message format in API call | |||
use_function_calling: bool = False | |||
# Tool function call format mode | |||
tool_call_format: str = "default" |
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.
Suggested change
tool_call_format: str = "default" | |
_tool_call_format: str = "json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR supports tool function calls under strict format constraints. Specifically, it uses structural tag to constrain the calling format.
It made following changes:
In most cases, the "json" and "xml" mode can meet the requirements. For some models specialized in Python code call output, "python" mode can be used, which means output will be parsed in python ast. For a few special cases, users can use the API of structural tags to customize their own function call format.