"vllm/vscode:/vscode.git/clone" did not exist on "fdcc405346565c733c35501c76c1c811f0608857"
structured_outputs.py 3.39 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
"""
4
5
6
7
This file demonstrates the example usage of structured outputs
in vLLM. It shows how to apply different constraints such as choice,
regex, json schema, and grammar to produce structured and formatted
results based on specific prompts.
8
"""
9

10
11
12
13
14
from enum import Enum

from pydantic import BaseModel

from vllm import LLM, SamplingParams
15
from vllm.sampling_params import StructuredOutputsParams
16

17
18
MAX_TOKENS = 50

19
20
21
22
23
24
25
# Structured outputs by Choice (list of possible options)
structured_outputs_params_choice = StructuredOutputsParams(
    choice=["Positive", "Negative"]
)
sampling_params_choice = SamplingParams(
    structured_outputs=structured_outputs_params_choice
)
26
prompt_choice = "Classify this sentiment: vLLM is wonderful!"
27

28
29
# Structured outputs by Regex
structured_outputs_params_regex = StructuredOutputsParams(regex=r"\w+@\w+\.com\n")
30
sampling_params_regex = SamplingParams(
31
    structured_outputs=structured_outputs_params_regex,
32
33
    stop=["\n"],
    max_tokens=MAX_TOKENS,
34
)
35
36
37
prompt_regex = (
    "Generate an email address for Alan Turing, who works in Enigma."
    "End in .com and new line. Example result:"
38
39
    "alan.turing@enigma.com\n"
)
40
41


42
# Structured outputs by JSON using Pydantic schema
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class CarType(str, Enum):
    sedan = "sedan"
    suv = "SUV"
    truck = "Truck"
    coupe = "Coupe"


class CarDescription(BaseModel):
    brand: str
    model: str
    car_type: CarType


json_schema = CarDescription.model_json_schema()
57
structured_outputs_params_json = StructuredOutputsParams(json=json_schema)
58
sampling_params_json = SamplingParams(
59
    structured_outputs=structured_outputs_params_json, max_tokens=MAX_TOKENS
60
)
61
prompt_json = (
62
    "Generate a JSON with the brand, model and car_type of "
63
64
    "the most iconic car from the 90's"
)
65

66
# Structured outputs by Grammar
67
simplified_sql_grammar = """
68
69
70
71
72
73
root ::= select_statement
select_statement ::= "SELECT " column " from " table " where " condition
column ::= "col_1 " | "col_2 "
table ::= "table_1 " | "table_2 "
condition ::= column "= " number
number ::= "1 " | "2 "
74
"""
75
76
77
structured_outputs_params_grammar = StructuredOutputsParams(
    grammar=simplified_sql_grammar
)
78
sampling_params_grammar = SamplingParams(
79
    structured_outputs=structured_outputs_params_grammar,
80
81
    max_tokens=MAX_TOKENS,
)
82
prompt_grammar = (
83
    "Generate an SQL query to show the 'username' and 'email' from the 'users' table."
84
)
85
86
87
88
89
90
91


def format_output(title: str, output: str):
    print(f"{'-' * 50}\n{title}: {output}\n{'-' * 50}")


def generate_output(prompt: str, sampling_params: SamplingParams, llm: LLM):
92
    outputs = llm.generate(prompt, sampling_params=sampling_params)
93
94
95
96
97
98
99
    return outputs[0].outputs[0].text


def main():
    llm = LLM(model="Qwen/Qwen2.5-3B-Instruct", max_model_len=100)

    choice_output = generate_output(prompt_choice, sampling_params_choice, llm)
100
    format_output("Structured outputs by Choice", choice_output)
101
102

    regex_output = generate_output(prompt_regex, sampling_params_regex, llm)
103
    format_output("Structured outputs by Regex", regex_output)
104
105

    json_output = generate_output(prompt_json, sampling_params_json, llm)
106
    format_output("Structured outputs by JSON", json_output)
107

108
    grammar_output = generate_output(prompt_grammar, sampling_params_grammar, llm)
109
    format_output("Structured outputs by Grammar", grammar_output)
110
111
112
113


if __name__ == "__main__":
    main()