家具 / 办公家具 / 书桌 家具 / 客厅家具 / 咖啡桌与边几 / 咖啡桌 家具 / 客厅家具 / 咖啡桌与边几 / 边桌与角几 装饰与抱枕 / 装饰抱枕与毯子 / 抱枕 家具 / 卧室家具 / 梳妆台与五斗柜
经典实现方式会使用结构化输出。你告诉提供方必须将其输出限制为合法值列表。在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’, …
共500个
]
class QueryClassification(BaseModel): """ 面向家具电商的搜索查询结构化表示。 继承基础Query模型的关键词,并添加类别和子类别。 """ classifications: list[FullyQualifiedClassifications] = Field( description=“产品可能的分类。” )
response = client.responses.parse( model=“gpt-5.4-mini”, input=“Classify the query: brown coffee table”, text_format=QueryClassification, )
print(response.output_parsed.message)
输出:Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
`
这种方法有效。但在大规模场景下,有一种更便宜的方式可以用小型/笨模型实现。更不用说,存在可发送内容的长度上限。
幸运的是,有一种简单模式可以让LLM分类变得非常顺畅。
只需让一个笨LLM为你的查询凭空捏造看似合理的假分类:
`hallucination_prompt = f""" 你的任务是为搜索查询创造新颖的、从未见过的家具、家居用品或五金分类。
产品分类可能看起来像:
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
以下是需要生成分类的查询:
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
嗯,这没什么用。
实际上它极其有用。你现在可以将它解析到真实词汇中。