Commit 6a01260
authored
[Feature] Provide a new function to convert xml style function calling to grammars. (#387)
This PR provides a new function to convert a schema to a xml-style tool
calling format grammar.
Consider a schema:
```
schema = {
"type": "object",
"properties": {"name": {"type": "string"}, "age": {"type": "integer"}},
"required": ["name", "age"],
}
```
It describes the parameters of a function, which has `name: str`, `age:
integer` as its parameters. Moreover, we want to constrain the llm's
output, then we can use this function:
```
from xgrammar.testing import _qwen_xml_tool_calling_to_ebnf
ebnf_grammar = _qwen_xml_tool_calling_to_ebnf(schema)
```
Then we can get an ebnf grammar(a string), which can force the llm to
output:`<parameter=name>...</parameter><parameter=age>...</parameter>`.
Moreover, if the properties belong to the root schema, then the string
will be in xml-style, and the parameters will be xml-style too. However,
if the properties belong to a nested object, then it will be in json
style again.
Let's consider such a schema:
```
schema = {
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {"street": {"type": "string"}, "city": {"type": "string"}},
"required": ["street", "city"],
}
},
"required": ["address"],
}
```
Here is a valid output:
```
<parameter=address>{"street": "Main St", "city": "New York"}</parameter>
```
---------
Signed-off-by: Yuchuan <[email protected]>1 parent ba63066 commit 6a01260
File tree
7 files changed
+1134
-313
lines changed- cpp
- nanobind
- python/xgrammar
- tests/python
7 files changed
+1134
-313
lines changed
0 commit comments