不要分类,不要产生幻觉!
Don't classify, hallucinate!

原始链接: https://softwaredoug.com/blog/2026/08/10/hypothetical-classifications

将大语言模型(LLM)的输出限制在大型固定词汇表(如 Wayfair WANDS 数据集)中,既具有挑战性且成本高昂,还会受到 Token 上下文窗口的限制。虽然使用 Pydantic 字面量(literals)实现结构化输出可行,但在大规模应用中效率低下。 一种更有效且经济的模式是:使用更小型、“更笨”的 LLM 为给定的搜索查询生成“幻觉”或合理的分类。与其强迫模型从庞大且僵化的模式(schema)中进行选择,不如引导它建议自然语言类别。 当模型生成候选分类后,你可以通过向量相似度将其映射到官方分类体系中。通过预先计算真实类别的向量嵌入(embeddings),只需进行简单的点积计算,即可将 LLM 的输出匹配到数据集中最接近的有效项。这种方法无需在每次提示(prompt)中都传入庞大的模式,在保持准确性的同时,显著降低了成本并提高了灵活性。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 不要进行分类或产生幻觉! (softwaredoug.com) 12 分,由 softwaredoug 在 1 小时前发布 | 隐藏 | 过往 | 收藏 | 讨论 | 帮助 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Using LLMs to classify products, search queries, etc is by now boring. Yet it can still be difficult to constrains the LLM’s output to the legal vocabulary of brands, colors, categories, etc your system allows.

In the Wayfair WANDS e-commerce dataset, for example, you want to classify a query like “wood coffee table” into its most appropriate category. Of which there are hundreds:

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

The classic way to implement this would be with structured outputs. You tell your provide it must constrain its outputs to a list of legal values. In Pydantic, you create a giant literal of legal output values:

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

This works. But there’s a way to do this a lot cheaper with small / dumb models at scale. Not to mention, there’s an upper limit you can send

Luckily, there’s an easy pattern that makes LLM classification pretty seamless.

Just ask a dumb LLM to invent plausible, fake classifications for your query:

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

Now we’re not sending the list of legal classifications. We’re instead, asking the LLM to make stuff up:

response = client.responses.parse(
    model="gpt-5.4-mini",
    input=hallucination_prompt,
    text_format=list[str],
)

It’ll then make up some BS that doesn’t actually exist in your real taxonomy like:

Furniture / Living Room / Tables / Coffee

Well that’s not very helpful.

Actually it’s extremely helpful. You can now resolve that into the real vocabulary.

It’s very cheap to build an in-memory set of embeddings of the REAL classifications. As I’ve done in this notebook and this utility.

In the notebook, I compute a MiniLM embedding of every real Wayfair classification. I compute the embedding of the fake, hypothetical embedding from the LLM. I then dot product the fake embedding into the real ones to find the most similar. Producing:

Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables

You can give these hallucination tasks to dumb / cheap LLMs. And you don’t need to ship the schema over to the LLM every time.

Join me for Vectors Week, a series of events about vector retrieval, hybrid search, and building your own vector database.

联系我们 contact @ memedata.com