AI・機械学習
分類するな、幻覚を見せろ!
Don't classify, hallucinate! (softwaredoug.com)
要約
LLMを使った製品分類は、固定された正解リストに制約されるとコストが高く、柔軟性に欠ける場合があります。この記事では、LLMに架空の分類を「幻覚」として生成させ、それを実際の分類リストとの類似度で解決する、より安価で効率的な手法を提案しています。このアプローチは、小規模なLLMでも利用可能で、スキーマを毎回送信する必要がないという利点があります。
全文翻訳
LLMを使って製品や検索クエリなどを分類することは、今では退屈な作業です。しかし、システムが許可するブランド、色、カテゴリなどの法的な語彙にLLMの出力を制約することは、依然として難しい場合があります。例えば、WayfairのWANDS eコマースデータセットでは、「木製のコーヒーテーブル」のようなクエリを最も適切なカテゴリに分類したいとします。そこには数百ものカテゴリがあります。
Furniture / Office Furniture / Desks Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables Furniture / Living Room Furniture / Coffee Tables & End Tables / End & Side Tables Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows Furniture / Bedroom Furniture / Dressers & Chests
これを実装する古典的な方法は、構造化された出力を使用することです。プロバイダーに、法的な値のリストにその出力を制約する必要があることを伝えます。Pydanticでは、法的な出力値の巨大なリテラルを作成します。
from typing import Literal
from pydantic import BaseModel, Field
FullyQualifiedClassifications = Literal[
'Furniture / Bedroom Furniture / Beds & Headboards / Beds',
'Furniture / Living Room Furniture / Chairs & Seating / Accent Chairs',
'Rugs / Area Rugs',
... # times 500
]
class QueryClassification(BaseModel):
"""
Structured representation of a search query for furniture e-commerce.
Inherits keywords from the base Query model and adds category and sub-category.
"""
classifications: list[FullyQualifiedClassifications] = Field(
description="A possible classification for the product."
)
response = client.responses.parse(
model="gpt-5.4-mini",
input="Classify the query: brown coffee table",
text_format=QueryClassification,
)
print(response.output_parsed.message)
# Outputs: Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
これは機能します。しかし、もっと安価に、小規模で単純なモデルを大規模に使う方法があります。言うまでもなく、送信できる量には上限があります。
幸いなことに、LLMによる分類を非常にシームレスにする簡単なパターンがあります。単純なLLMに、クエリのもっともらしい偽の分類を考案させるだけです。
hallucination_prompt = f"""
Your task is to create novel, never seen before, furniture, home goods, or hardware classification that best fit a search query. Product classifications might look like:
Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows
Furniture / Bedroom Furniture / Dressers & Chests
Kitchen & Tabletop / Kitchen Organization / Food Storage & Canisters
School Furniture and Supplies / School Furniture / School Chairs & Seating / Stackable Chairs
Baby & Kids / Toddler & Kids Bedroom Furniture / Kids Beds
Here's the query to generate classifications for: brown coffee table
"""
ここでは、法的な分類リストを送信していません。代わりに、LLMに何かを作り出させるのです。
response = client.responses.parse(
model="gpt-5.4-mini",
input=hallucination_prompt,
text_format=list[str],
)
すると、実際の分類体系には存在しないような、くだらないものを生成するでしょう。
Furniture / Living Room / Tables / Coffee
それはあまり役に立たないように思えます。実際には、それは非常に役立ちます。あなたはそれを実際の語彙に解決できます。実際の分類のインメモリセットを構築するのは非常に安価です。このノートブックやこのユーティリティで行ったように。ノートブックでは、すべての実際のWayfair分類のMiniLM埋め込みを計算します。LLMからの偽の、仮説的な埋め込みの埋め込みを計算します。次に、偽の埋め込みを実際の埋め込みにドット積して、最も類似するものを見つけます。これにより、次のような結果が得られます。
Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
これらの幻覚タスクは、単純で安価なLLMに与えることができます。そして、毎回スキーマをLLMに送信する必要はありません。
今後のイベント:ベクターウィーク ベクター検索、ハイブリッド検索、独自のベクターデータベースの構築に関する一連のイベントに参加してください。
Doug Turnbull
Dougからのその他の記事
Twitter | LinkedIn | Newsletter | Bsky