結構化輸出內容可讓模型一律按照特定結構定義生成輸出內容。舉例來說,模型可能會收到回應結構定義,確保回應產生有效的 JSON。Vertex AI Model as a Service (MaaS) 提供的所有開放原始碼模型都支援結構化輸出。
如要進一步瞭解結構化輸出功能的概念,請參閱「結構化輸出簡介」。
使用結構化輸出內容
下列用途是設定回應結構定義,確保模型輸出內容為具有下列屬性的 JSON 物件:名稱、日期和參與者。Python 程式碼會使用 OpenAI SDK 和 Pydantic 物件產生 JSON 結構定義。
from pydantic import BaseModel
from openai import OpenAI
client = OpenAI()
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
completion = client.beta.chat.completions.parse(
model="MODEL_NAME",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
],
response_format=CalendarEvent,
)
print(completion.choices[0].message.parsed)
模型輸出內容會遵循下列 JSON 結構定義:
{ "name": STRING, "date": STRING, "participants": [STRING] }
如果提示是「Alice 和 Bob 星期五要去參加科學展」,模型可能會產生以下回覆:
{
"name": "science fair",
"date": "Friday",
"participants": [
"Alice",
"Bob"
]
}
詳細範例
以下程式碼是遞迴結構定義的範例。UI
類別包含 children
清單,也可以是 UI
類別。
from pydantic import BaseModel
from openai import OpenAI
from enum import Enum
from typing import List
client = OpenAI()
class UIType(str, Enum):
div = "div"
button = "button"
header = "header"
section = "section"
field = "field"
form = "form"
class Attribute(BaseModel):
name: str
value: str
class UI(BaseModel):
type: UIType
label: str
children: List["UI"]
attributes: List[Attribute]
UI.model_rebuild() # This is required to enable recursive types
class Response(BaseModel):
ui: UI
completion = client.beta.chat.completions.parse(
model="MODEL_NAME",
messages=[
{"role": "system", "content": "You are a UI generator AI. Convert the user input into a UI."},
{"role": "user", "content": "Make a User Profile Form"}
],
response_format=Response,
)
print(completion.choices[0].message.parsed)
模型輸出內容會遵循前一個程式碼片段中指定的 Pydantic 物件結構定義。在本例中,模型可能會產生下列 UI 表單:
Form
Input
Name
Email
Age
回應可能如下所示:
ui = UI(
type=UIType.div,
label='Form',
children=[
UI(
type=UIType.div,
label='Input',
children=[],
attributes=[
Attribute(name='label', value='Name')
]
),
UI(
type=UIType.div,
label='Input',
children=[],
attributes=[
Attribute(name='label', value='Email')
]
),
UI(
type=UIType.div,
label='Input',
children=[],
attributes=[
Attribute(name='label', value='Age')
]
)
],
attributes=[
Attribute(name='name', value='John Doe'),
Attribute(name='email', value='john.doe@example.com'),
Attribute(name='age', value='30')
]
)