分類するな、幻覚を見せろ!
Don't classify, hallucinate! (softwaredoug.com)
要約
LLMを使った製品分類は、固定された正解リストに制約されるとコストが高く、柔軟性に欠ける場合があります。この記事では、LLMに架空の分類を「幻覚」として生成させ、それを実際の分類リストとの類似度で解決する、より安価で効率的な手法を提案しています。このアプローチは、小規模なLLMでも利用可能で、スキーマを毎回送信する必要がないという利点があります。
全文翻訳
分類するな。幻覚を見ろ!
LLMを使って製品や検索クエリなどを分類するのは、今や陳腐な手法です。しかし、LLMの出力を、システムが許可するブランド、色、カテゴリなどの法的な語彙に制約することは、依然として難しい場合があります。
例えば、WayfairのWANDS eコマースデータセットでは、“wood coffee table”(木製コーヒーテーブル)のようなクエリを、数百ある最も適切なカテゴリのいずれかに分類したいとします。
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],
)
すると、実際の分類には存在しないような、でっち上げられた(BS)情報が生成されます。
Furniture / Living Room / Tables / Coffee
これはまったく役に立たないように思えます。
実際には、これは非常に役立ちます。この架空の分類を、実際の語彙に解決できるようになります。
実際の分類のインメモリセットの埋め込み(embeddings)を構築することは非常に安価です。私はこのノートブックとこのユーティリティで行いました。
ノートブックでは、すべての実際のWayfair分類のMiniLM埋め込みを計算します。LLMからの偽の、仮説的な埋め込みの埋め込みを計算します。次に、偽の埋め込みを実際の埋め込みにドット積して、最も類似したものを見つけます。これにより、以下が生成されます。
Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
これらの幻覚タスクは、ダウングレードされた安価なLLMに実行させることができます。そして、スキーマを毎回LLMに送信する必要はありません。
AI-Powered Searchの著者と共に最新のAI検索スキルを学びましょう。Doug TurnbullとTrey Graingerが、人間とエージェントのためのモダンな検索(retrieval)を探求します。Mavenでコースを探す。