[Docs] Add EBNF to sampling params docs (#2609)
This commit is contained in:
committed by
GitHub
parent
8ee9a8501a
commit
fd34f2da35
@@ -220,14 +220,21 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Structured decoding (JSON, Regex)\n",
|
||||
"You can define a JSON schema or regular expression to constrain the model's output. The model output will be guaranteed to follow the given constraints and this depends on the grammar backend.\n",
|
||||
"## Structured Outputs (JSON, Regex, EBNF)\n",
|
||||
"You can specify a JSON schema, Regular Expression or [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) to constrain the model output. The model output will be guaranteed to follow the given constraints. \n",
|
||||
"\n",
|
||||
"SGlang has two backends: [Outlines](https://github.com/dottxt-ai/outlines) (default) and [XGrammar](https://blog.mlc.ai/2024/11/22/achieving-efficient-flexible-portable-structured-generation-with-xgrammar). Xgrammar accelerates JSON decoding performance but does not support regular expressions. To use Xgrammar, add the `--grammar-backend xgrammar` when launching the server:\n",
|
||||
"SGLang supports two grammar backends:\n",
|
||||
"\n",
|
||||
"- [Outlines](https://github.com/dottxt-ai/outlines) (default): Supports JSON schema and Regular Expression constraints.\n",
|
||||
"- [XGrammar](https://github.com/mlc-ai/xgrammar): Supports JSON schema and EBNF constraints.\n",
|
||||
" - XGrammar currently uses the [GGML BNF format](https://github.com/ggerganov/llama.cpp/blob/master/grammars/README.md)\n",
|
||||
"\n",
|
||||
"> 🔔 Only one constraint parameter (`json_schema`, `regex`, or `ebnf`) can be specified at a time.\n",
|
||||
"\n",
|
||||
"Initialise xgrammar backend using `--grammar-backend xgrammar` flag\n",
|
||||
"```bash\n",
|
||||
"python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-8B-Instruct \\\n",
|
||||
"--port 30000 --host 0.0.0.0 --grammar-backend xgrammar\n",
|
||||
"--port 30000 --host 0.0.0.0 --grammar-backend [xgrammar|outlines] # xgrammar or outlines (default: outlines)\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"### JSON"
|
||||
@@ -275,7 +282,7 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Regular expression"
|
||||
"### Regular expression (use default \"outlines\" backend)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -297,6 +304,46 @@
|
||||
"print_highlight(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### EBNF (use \"xgrammar\" backend)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# terminate the existing server(that's using default outlines backend) for this demo\n",
|
||||
"terminate_process(server_process)\n",
|
||||
"\n",
|
||||
"# start new server with xgrammar backend\n",
|
||||
"server_process = execute_shell_command(\n",
|
||||
" \"python -m sglang.launch_server --model-path meta-llama/Meta-Llama-3.1-8B-Instruct --port 30000 --host 0.0.0.0 --grammar-backend xgrammar\"\n",
|
||||
")\n",
|
||||
"wait_for_server(\"http://localhost:30000\")\n",
|
||||
"\n",
|
||||
"# EBNF example\n",
|
||||
"ebnf_grammar = r\"\"\"\n",
|
||||
" root ::= \"Hello\" | \"Hi\" | \"Hey\"\n",
|
||||
" \"\"\"\n",
|
||||
"response = client.chat.completions.create(\n",
|
||||
" model=\"meta-llama/Meta-Llama-3.1-8B-Instruct\",\n",
|
||||
" messages=[\n",
|
||||
" {\"role\": \"system\", \"content\": \"You are a helpful EBNF test bot.\"},\n",
|
||||
" {\"role\": \"user\", \"content\": \"Say a greeting.\"},\n",
|
||||
" ],\n",
|
||||
" temperature=0,\n",
|
||||
" max_tokens=32,\n",
|
||||
" extra_body={\"ebnf\": ebnf_grammar},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print_highlight(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
||||
Reference in New Issue
Block a user