Gli output strutturati consentono a un modello di generare output che rispettano sempre uno schema specifico. Ad esempio, a un modello può essere fornito uno schema di risposta per garantire che la risposta produca un JSON valido. Tutti i modelli aperti disponibili su Vertex AI Model as a Service (MaaS) supportano output strutturati.
Per maggiori informazioni concettuali sulla funzionalità di output strutturato, consulta la pagina Introduzione all'output strutturato.
Utilizzare output strutturati
Il seguente caso d'uso imposta uno schema di risposta che garantisce che l'output del modello sia un oggetto JSON con le seguenti proprietà: nome, data e partecipanti. Il codice Python utilizza l'SDK OpenAI e gli oggetti Pydantic per generare lo schema 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)
L'output del modello rispetterà il seguente schema JSON:
{ "name": STRING, "date": STRING, "participants": [STRING] }
Se viene fornito il prompt "Alice e Bob andranno a una fiera della scienza venerdì", il modello potrebbe produrre la seguente risposta:
{
"name": "science fair",
"date": "Friday",
"participants": [
"Alice",
"Bob"
]
}
Esempio dettagliato
Il seguente codice è un esempio di schema ricorsivo. La classe UI
contiene
un elenco di children
, che possono appartenere anche alla classe 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)
L'output del modello rispetterà lo schema dell'oggetto Pydantic specificato nello snippet precedente. In questo esempio, il modello potrebbe generare il seguente modulo UI:
Form
Input
Name
Email
Age
Una risposta potrebbe essere simile alla seguente:
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')
]
)