不要分类,要幻觉!
摘要
本文介绍了一种使用LLM对电子商务查询进行分类的技术,其做法是让模型“幻觉”出假设性分类,再利用嵌入将其映射到真实分类体系,这比受限的结构化输出更便宜、也更简单。
暂无内容
查看缓存全文
缓存时间: 2026/08/14 12:27
# 别分类,直接幻觉!
来源:https://softwaredoug.com/blog/2026/08/10/hypothetical-classifications
使用LLM对产品、搜索查询等进行分类现在已经很常见了。但仍然很难将LLM的输出限制在你的系统所允许的品牌、颜色、类别等合法词汇范围内。
例如,在Wayfair WANDS电子商务数据集中,你可能想把像“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
``
这种方法可行。但还有一种方法可以用小型/笨模型在规模上更便宜地做到这一点。更不用说,你可以发送的内容有一个上限(https://developers.openai.com/api/docs/guides/structured-outputs)
幸运的是,有一种简单的模式可以让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
``
嗯,这没什么用。
实际上这非常有用。你现在可以将其解析为真实的词汇表。
构建真实分类的内存嵌入集合非常便宜。正如我在此笔记本(https://colab.research.google.com/drive/1ljk72SBRuqWIijuEusCnDbhG1WAfZFcC#scrollTo=RZ-hEr-CSr9T)和此实用程序(https://github.com/softwaredoug/cheat-at-search/blob/main/cheat_at_search/enrich/vocabulary.py)中所做的那样。
在笔记本中,我为每个真实的Wayfair分类计算MiniLM嵌入。我计算LLM生成的虚假假设嵌入,然后将其与真实嵌入做点积,找到最相似的。结果如下:
``
Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
``
你可以将这些幻觉任务交给笨/便宜的LLM。而且你不需要每次都把模式发送给LLM。
加入我的Vectors Week吧,这是一个关于向量检索、混合搜索和构建自己的向量数据库的系列活动。
相似文章
不要分类,要幻觉!
文章介绍了一种方法:LLMs为博客内容生成假设标签,并利用向量嵌入技术与现有标签进行匹配,从而提升分类效果。
LLMs为何在结构化知识上产生幻觉:对线性化表示推理的机制分析
本文对LLMs在推理线性化结构化知识时产生幻觉的原因进行了机制分析,发现幻觉源于系统的内部动态,例如对捷径线索的关注以及前馈层中语义基础的失败,而非随机噪声。
如何解决幻觉问题(使用RLCD)
文章解释了如何通过使用概率分布和校准来量化不确定性,而不是依赖不可靠的置信分数,以解决大型语言模型中的幻觉问题。
LLMs中的幻觉:基于生命周期的成因、检测、缓解与预防综述
这篇综述论文提出了一种基于生命周期的框架,用于理解LLMs中的幻觉现象,涵盖数据、训练和推理阶段的成因、检测、缓解与预防。
LLM分类是特征工程
本文讨论了直接使用LLMs作为分类器的局限性,并提出将LLMs输出作为特征应用于传统机器学习模型(如逻辑回归),以实现更好的校准、可解释性和性能。