Skip to content

Commit 03967a3

Browse files
reidliu41reidliu41
authored andcommitted
[V1] Update structured output (vllm-project#16812)
Signed-off-by: reidliu41 <reid201711@gmail.com> Co-authored-by: reidliu41 <reid201711@gmail.com>
1 parent 7901156 commit 03967a3

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

benchmarks/benchmark_serving_structured_output.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ def get_schema(index: int):
150150

151151
elif args.dataset == "grammar":
152152
schema = """
153-
?start: select_statement
153+
root ::= select_statement
154154
155-
?select_statement: "SELECT " column_list " FROM " table_name
155+
select_statement ::= "SELECT " column " from " table " where " condition
156156
157-
?column_list: column_name ("," column_name)*
157+
column ::= "col_1 " | "col_2 "
158158
159-
?table_name: identifier
159+
table ::= "table_1 " | "table_2 "
160160
161-
?column_name: identifier
161+
condition ::= column "= " number
162162
163-
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
163+
number ::= "1 " | "2 "
164164
"""
165165
prompt = "Generate an SQL query to show the 'username' \
166166
and 'email' from the 'users' table."

docs/source/features/structured_outputs.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ completion = client.chat.completions.create(
5050
"content": "Generate an example email address for Alan Turing, who works in Enigma. End in .com and new line. Example result: alan.turing@enigma.com\n",
5151
}
5252
],
53-
extra_body={"guided_regex": "\w+@\w+\.com\n", "stop": ["\n"]},
53+
extra_body={"guided_regex": r"\w+@\w+\.com\n", "stop": ["\n"]},
5454
)
5555
print(completion.choices[0].message.content)
5656
```
@@ -105,17 +105,17 @@ It works by using a context free EBNF grammar, which for example we can use to d
105105

106106
```python
107107
simplified_sql_grammar = """
108-
?start: select_statement
108+
root ::= select_statement
109109
110-
?select_statement: "SELECT " column_list " FROM " table_name
110+
select_statement ::= "SELECT " column " from " table " where " condition
111111
112-
?column_list: column_name ("," column_name)*
112+
column ::= "col_1 " | "col_2 "
113113
114-
?table_name: identifier
114+
table ::= "table_1 " | "table_2 "
115115
116-
?column_name: identifier
116+
condition ::= column "= " number
117117
118-
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
118+
number ::= "1 " | "2 "
119119
"""
120120

121121
completion = client.chat.completions.create(

examples/online_serving/openai_chat_completion_structured_outputs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"content": prompt,
3434
}],
3535
extra_body={
36-
"guided_regex": "\w+@\w+\.com\n",
36+
"guided_regex": r"\w+@\w+\.com\n",
3737
"stop": ["\n"]
3838
},
3939
)
@@ -70,17 +70,17 @@ class CarDescription(BaseModel):
7070

7171
# Guided decoding by Grammar
7272
simplified_sql_grammar = """
73-
?start: select_statement
73+
root ::= select_statement
7474
75-
?select_statement: "SELECT " column_list " FROM " table_name
75+
select_statement ::= "SELECT " column " from " table " where " condition
7676
77-
?column_list: column_name ("," column_name)*
77+
column ::= "col_1 " | "col_2 "
7878
79-
?table_name: identifier
79+
table ::= "table_1 " | "table_2 "
8080
81-
?column_name: identifier
81+
condition ::= column "= " number
8282
83-
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
83+
number ::= "1 " | "2 "
8484
"""
8585

8686
prompt = ("Generate an SQL query to show the 'username' and 'email'"
@@ -110,7 +110,7 @@ class CarDescription(BaseModel):
110110
"content": prompt,
111111
}],
112112
extra_body={
113-
"guided_regex": "\w+@\w+\.com\n",
113+
"guided_regex": r"\w+@\w+\.com\n",
114114
"stop": ["\n"],
115115
"guided_decoding_backend": "xgrammar:no-fallback"
116116
},

examples/online_serving/openai_chat_completion_structured_outputs_with_reasoning.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ class CarDescription(BaseModel):
101101

102102
# Guided decoding by Grammar
103103
simplified_sql_grammar = """
104-
?start: select_statement
104+
root ::= select_statement
105105
106-
?select_statement: "SELECT " column_list " FROM " table_name
106+
select_statement ::= "SELECT " column " from " table " where " condition
107107
108-
?column_list: column_name ("," column_name)*
108+
column ::= "col_1 " | "col_2 "
109109
110-
?table_name: identifier
110+
table ::= "table_1 " | "table_2 "
111111
112-
?column_name: identifier
112+
condition ::= column "= " number
113113
114-
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
114+
number ::= "1 " | "2 "
115115
"""
116116

117117
# This may be very slow https://github.com/vllm-project/vllm/issues/12122

0 commit comments

Comments
 (0)