开放模型的结构化输出

结构化输出使模型能够生成始终遵循特定架构的输出。例如,可以为模型提供响应架构,以确保响应生成有效的 JSON。Vertex AI 模型即服务 (MaaS) 上提供的所有开放模型都支持结构化输出。

如需详细了解结构化输出功能的相关概念,请参阅结构化输出简介

使用结构化输出

以下用例设置了一个响应架构,该架构可确保模型输出是一个具有以下属性的 JSON 对象:name、date 和 participants。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 and Bob are going to a science fair on Friday”,模型可能会生成以下回答:

{
  "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 对象的架构。在此示例中,模型可以生成以下界面表单:

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')
    ]
)