オープンモデルの構造化出力

構造化出力を使用すると、モデルは常に特定のスキーマに準拠する出力を生成できます。たとえば、レスポンスが有効な 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] }

「アリスとボブは金曜日に科学博覧会に行く」というプロンプトが提供された場合、モデルは次のようなレスポンスを生成する可能性があります。

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