Unverified Commit db2f8d91 authored by Reid's avatar Reid Committed by GitHub
Browse files

[V1] Update structured output (#16812)


Signed-off-by: default avatarreidliu41 <reid201711@gmail.com>
Co-authored-by: default avatarreidliu41 <reid201711@gmail.com>
parent 6167c0e5
...@@ -150,17 +150,17 @@ def sample_requests(tokenizer: PreTrainedTokenizerBase, ...@@ -150,17 +150,17 @@ def sample_requests(tokenizer: PreTrainedTokenizerBase,
elif args.dataset == "grammar": elif args.dataset == "grammar":
schema = """ schema = """
?start: select_statement root ::= select_statement
?select_statement: "SELECT " column_list " FROM " table_name select_statement ::= "SELECT " column " from " table " where " condition
?column_list: column_name ("," column_name)* column ::= "col_1 " | "col_2 "
?table_name: identifier table ::= "table_1 " | "table_2 "
?column_name: identifier condition ::= column "= " number
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/ number ::= "1 " | "2 "
""" """
prompt = "Generate an SQL query to show the 'username' \ prompt = "Generate an SQL query to show the 'username' \
and 'email' from the 'users' table." and 'email' from the 'users' table."
......
...@@ -50,7 +50,7 @@ completion = client.chat.completions.create( ...@@ -50,7 +50,7 @@ completion = client.chat.completions.create(
"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", "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",
} }
], ],
extra_body={"guided_regex": "\w+@\w+\.com\n", "stop": ["\n"]}, extra_body={"guided_regex": r"\w+@\w+\.com\n", "stop": ["\n"]},
) )
print(completion.choices[0].message.content) print(completion.choices[0].message.content)
``` ```
...@@ -105,17 +105,17 @@ It works by using a context free EBNF grammar, which for example we can use to d ...@@ -105,17 +105,17 @@ It works by using a context free EBNF grammar, which for example we can use to d
```python ```python
simplified_sql_grammar = """ simplified_sql_grammar = """
?start: select_statement root ::= select_statement
?select_statement: "SELECT " column_list " FROM " table_name select_statement ::= "SELECT " column " from " table " where " condition
?column_list: column_name ("," column_name)* column ::= "col_1 " | "col_2 "
?table_name: identifier table ::= "table_1 " | "table_2 "
?column_name: identifier condition ::= column "= " number
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/ number ::= "1 " | "2 "
""" """
completion = client.chat.completions.create( completion = client.chat.completions.create(
......
...@@ -33,7 +33,7 @@ completion = client.chat.completions.create( ...@@ -33,7 +33,7 @@ completion = client.chat.completions.create(
"content": prompt, "content": prompt,
}], }],
extra_body={ extra_body={
"guided_regex": "\w+@\w+\.com\n", "guided_regex": r"\w+@\w+\.com\n",
"stop": ["\n"] "stop": ["\n"]
}, },
) )
...@@ -70,17 +70,17 @@ print(completion.choices[0].message.content) ...@@ -70,17 +70,17 @@ print(completion.choices[0].message.content)
# Guided decoding by Grammar # Guided decoding by Grammar
simplified_sql_grammar = """ simplified_sql_grammar = """
?start: select_statement root ::= select_statement
?select_statement: "SELECT " column_list " FROM " table_name select_statement ::= "SELECT " column " from " table " where " condition
?column_list: column_name ("," column_name)* column ::= "col_1 " | "col_2 "
?table_name: identifier table ::= "table_1 " | "table_2 "
?column_name: identifier condition ::= column "= " number
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/ number ::= "1 " | "2 "
""" """
prompt = ("Generate an SQL query to show the 'username' and 'email'" prompt = ("Generate an SQL query to show the 'username' and 'email'"
...@@ -110,7 +110,7 @@ try: ...@@ -110,7 +110,7 @@ try:
"content": prompt, "content": prompt,
}], }],
extra_body={ extra_body={
"guided_regex": "\w+@\w+\.com\n", "guided_regex": r"\w+@\w+\.com\n",
"stop": ["\n"], "stop": ["\n"],
"guided_decoding_backend": "xgrammar:no-fallback" "guided_decoding_backend": "xgrammar:no-fallback"
}, },
......
...@@ -101,17 +101,17 @@ print("content: ", completion.choices[0].message.content) ...@@ -101,17 +101,17 @@ print("content: ", completion.choices[0].message.content)
# Guided decoding by Grammar # Guided decoding by Grammar
simplified_sql_grammar = """ simplified_sql_grammar = """
?start: select_statement root ::= select_statement
?select_statement: "SELECT " column_list " FROM " table_name select_statement ::= "SELECT " column " from " table " where " condition
?column_list: column_name ("," column_name)* column ::= "col_1 " | "col_2 "
?table_name: identifier table ::= "table_1 " | "table_2 "
?column_name: identifier condition ::= column "= " number
?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/ number ::= "1 " | "2 "
""" """
# This may be very slow https://github.com/vllm-project/vllm/issues/12122 # This may be very slow https://github.com/vllm-project/vllm/issues/12122
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment