OpenAI Chat Completion Structured Outputs

OpenAI Chat Completion Structured Outputs#

Source vllm-project/vllm.

 1from enum import Enum
 2
 3from openai import OpenAI
 4from pydantic import BaseModel
 5
 6client = OpenAI(
 7    base_url="http://localhost:8000/v1",
 8    api_key="-",
 9)
10
11# Guided decoding by Choice (list of possible options)
12completion = client.chat.completions.create(
13    model="Qwen/Qwen2.5-3B-Instruct",
14    messages=[{
15        "role": "user",
16        "content": "Classify this sentiment: vLLM is wonderful!"
17    }],
18    extra_body={"guided_choice": ["positive", "negative"]},
19)
20print(completion.choices[0].message.content)
21
22# Guided decoding by Regex
23prompt = ("Generate an email address for Alan Turing, who works in Enigma."
24          "End in .com and new line. Example result:"
25          "[email protected]\n")
26
27completion = client.chat.completions.create(
28    model="Qwen/Qwen2.5-3B-Instruct",
29    messages=[{
30        "role": "user",
31        "content": prompt,
32    }],
33    extra_body={
34        "guided_regex": "\w+@\w+\.com\n",
35        "stop": ["\n"]
36    },
37)
38print(completion.choices[0].message.content)
39
40
41# Guided decoding by JSON using Pydantic schema
42class CarType(str, Enum):
43    sedan = "sedan"
44    suv = "SUV"
45    truck = "Truck"
46    coupe = "Coupe"
47
48
49class CarDescription(BaseModel):
50    brand: str
51    model: str
52    car_type: CarType
53
54
55json_schema = CarDescription.model_json_schema()
56
57prompt = ("Generate a JSON with the brand, model and car_type of"
58          "the most iconic car from the 90's")
59completion = client.chat.completions.create(
60    model="Qwen/Qwen2.5-3B-Instruct",
61    messages=[{
62        "role": "user",
63        "content": prompt,
64    }],
65    extra_body={"guided_json": json_schema},
66)
67print(completion.choices[0].message.content)
68
69# Guided decoding by Grammar
70simplified_sql_grammar = """
71    ?start: select_statement
72
73    ?select_statement: "SELECT " column_list " FROM " table_name
74
75    ?column_list: column_name ("," column_name)*
76
77    ?table_name: identifier
78
79    ?column_name: identifier
80
81    ?identifier: /[a-zA-Z_][a-zA-Z0-9_]*/
82"""
83
84prompt = ("Generate an SQL query to show the 'username' and 'email'"
85          "from the 'users' table.")
86completion = client.chat.completions.create(
87    model="Qwen/Qwen2.5-3B-Instruct",
88    messages=[{
89        "role": "user",
90        "content": prompt,
91    }],
92    extra_body={"guided_grammar": simplified_sql_grammar},
93)
94print(completion.choices[0].message.content)