domain.product.parse_product_intent - 商品意图识别
parse_product_intent 调用模型,联合用户文字与可选的参考图,将商品需求解析为四组硬条件和软偏好以及待确认项,不查询商品库。条件值使用固定的类型化结构,区分标量和列表,并保留字符串、数字、布尔等值类型。本文介绍该算子的参数、输出结构和使用示例。
调用方式
通过 ds.mmops.product.parse_product_intent() 调用,接收并返回 Ray Dataset。算子根据调用方提供的字段目录,将每行商品需求解析为必须具备、最好具备、最好不具备、必须不具备四组条件,以及无法可靠执行的待确认项;不查询商品库。条件中的值使用固定的条件值(ConditionValue)与带类型标签的标量(TaggedScalar)结构表示。使用前请确认已满足离线算子概述中的前提条件。
输入数据
|
列名 |
类型 |
说明 |
|
|
str |
原始商品需求。 |
|
|
struct | None |
可选。前序定位结果的完整对象,纯文字需求可不设此列。 |
|
|
str |
裁剪图或原图的 URI。 |
|
|
str |
前序定位状态,取值为 cropped 或 kept_original。 |
|
|
struct |
前序定位详情。 |
|
|
string | None |
场景类型,取值为 product、worn、lifestyle 或 unclear。 |
|
|
string | None |
定位的目标名称。 |
|
|
list[float] | None |
原图上坐标范围为 0~1000 的 xyxy 框。 |
|
|
list[int] | None |
包含边距的像素 xyxy 裁剪框。 |
|
|
str |
前序定位或保留原图的原因。 |
reference_image 通常来自 localize_product_image 的四个结果列,需显式组装为 struct 后传入。
参数说明
仅以 _column 结尾的参数读取每行的列,其余参数是本次调用共享的业务常量。选择器为 None 时的含义见具体参数说明,不代表字符串列名。
业务参数
|
参数 |
类型 |
默认值 |
说明 |
|
|
str |
|
读取每行需求原文的列名。 |
|
|
str | None |
|
有值时读取每行的定位结果 struct;为 |
|
|
dict |
无(必填) |
非空的全局字段目录。具体字段请参见下文的字段目录参数。 |
|
|
str | None |
|
全局解释提示,不能覆盖固定的输出结构及字段目录约束。 |
|
|
list[str] | None |
|
需要显式保留的输入顶层列名。与结果列重名时报错,需先用 Ray Data 原生的 |
字段目录参数
field_catalog 以字段名为键,每个字段支持以下配置:
|
参数 |
类型 |
默认值 |
说明 |
|
|
str |
无(必填) |
字段类型,取值为 string、number、integer、boolean 或 array。 |
|
|
list[str] |
无(必填) |
允许使用的正向谓词,例如 eq、in、contains。 |
|
|
list |
无(可选) |
允许的值域。 |
|
|
dict |
|
精确值的别名。 |
|
|
str |
|
array 类型字段使用 contains 时的元素类型。 |
调度参数
|
参数 |
类型 |
默认值 |
说明 |
|
|
int |
|
每个调度批次的输入行数,不改变单行的业务语义。 |
|
|
int |
|
Ray 执行并发度,取值为正整数。 |
|
|
float |
|
每个执行任务申请的 Ray CPU 资源,取值须大于 0。 |
输出数据
每行输入对应一行结果。默认仅返回下列结果列,可通过 keep_columns 显式保留指定的输入列,与结果列重名时须先用 Ray Data 原生的 rename_columns 改名。四组条件中的每一项都使用固定的条件(Condition)结构,业务值使用带类型标签的结构;数字、布尔和列表之间不互相强制转换,也不会将整项写成 JSON 字符串。
|
列名 |
类型 |
说明 |
|
|
list[Condition] |
必须具备,用于正向硬筛选。空组为 |
|
|
list[Condition] |
最好具备,用于加分。空组为 |
|
|
list[Condition] |
最好不具备,命中时降权。空组为 |
|
|
list[Condition] |
必须不具备,命中时排除。空组为 |
|
|
list[struct] |
待确认项,包括未映射、有歧义、有冲突的条件或参考图处理失败。 |
|
|
string |
待确认的用户原文。 |
|
|
string |
无法可靠执行的原因。 |
Condition:固定的单层 OR 容器
|
字段 |
类型 |
说明 |
|
|
string |
必填,取值为 atom 或 any。 |
|
|
Atom | None |
|
|
|
list[Atom] | None |
|
组内的多个 Condition 按 AND 解释;kind=any 中的多个 Atom 按 OR 解释。any 不递归,因此输出的 Arrow Schema 层级固定且有限。空条件组用 [] 表示,不能用 kind=any, any=[] 表示。
Atom:字段目录中的一个谓词
|
字段 |
类型 |
说明 |
|
|
string |
必填, |
|
|
string |
必填,字段目录允许的正向谓词。 |
|
|
ConditionValue |
必填,按下表保存标量或列表。 |
|
|
string |
必填,用户原文中的连续片段。 |
ConditionValue:标量或标量列表
|
字段 |
类型 |
说明 |
|
|
string |
必填,取值为 scalar 或 list。 |
|
|
TaggedScalar | None |
|
|
|
list[TaggedScalar] | None |
|
TaggedScalar:显式值类型
|
字段 |
类型 |
说明 |
|
|
string |
必填,取值为 null、bool、int、float、string、date、datetime、time 或 bigint。 |
|
|
bool | None |
仅在 |
|
|
int64 | None |
仅在 |
|
|
float64 | None |
仅在 |
|
|
string | None |
|
TaggedScalar 的五个字段始终存在,未激活的载荷字段均为 null。type=null 时四个载荷字段均为 null,表示显式空值;外层 null 只用于未启用的分支,不代表业务空值。0、false 和空字符串均不等于缺失。bigint 的十进制载荷保留整数精度,不是 JSON 文本;日期类标签只用于已知的真实日期类型,不会根据普通字符串推测。
编码结构不会扩大字段目录允许的业务值。类型标签按实际校验后的值确定,例如 number 类型的字段返回整数时仍标记为 int,不强制转为 float。字段目录校验、别名归一和硬条件冲突处理完成后才进行列编码;field_catalog 参数仍使用普通的业务标量和列表,无需改写为 TaggedScalar。原本不允许的 null、嵌套列表或字段值,不会因为存在对应的编码字段而被自动接受。
三种编码样例
以下样例仅用于说明结构,是手工编写的静态样例,不表示模型一定会选择相同的条件分解。
样例 1:标量条件,color eq 黑色。
{
"kind": "atom",
"atom": {
"field": "color",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "黑色"
},
"items": null
},
"source_text": "黑色"
},
"any": null
}
样例 2:列表条件,color in [黑色, 白色]。
{
"kind": "atom",
"atom": {
"field": "color",
"op": "in",
"value": {
"kind": "list",
"scalar": null,
"items": [
{
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "黑色"
},
{
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "白色"
}
]
},
"source_text": "黑色或白色"
},
"any": null
}
样例 3:单层 OR,category eq 手提包,或 color eq 黑色。any 中只放 Atom。
{
"kind": "any",
"atom": null,
"any": [
{
"field": "category",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "手提包"
},
"items": null
},
"source_text": "手提包"
},
{
"field": "color",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "黑色"
},
"items": null
},
"source_text": "黑色"
}
]
}
使用示例
示例中的 /shared 是集群所有 Worker 节点均可读写的共享挂载目录。parse_product_intent 调用模型,使用执行节点环境中的 MMOPS_MODEL_BASE_URL、MMOPS_TEXT_MODEL(图片模型使用 MMOPS_VISION_MODEL)和 MMOPS_MODEL_API_KEY 环境变量,应由集群运行环境安全注入。环境变量说明,请参见离线算子概述。
以下请求包含字符串、数值和布尔字段。每项返回值仍保留各自的类型,写入 Parquet 时使用上述固定结构。
import ray
from mmops.ray import enable_accessor
ray.init()
enable_accessor()
field_catalog = {
"category": {"type": "string", "values": ["手提包", "运动鞋"], "operators": ["eq", "in"]},
"color": {"type": "string", "values": ["黑色", "白色"], "operators": ["eq", "in"]},
"price": {"type": "number", "operators": ["lte"]},
"in_stock": {"type": "boolean", "operators": ["eq"]},
}
ds = ray.data.from_items([
{"query_id": "Q-001", "query": "必须是黑色手提包"},
{"query_id": "Q-002", "query": "黑色或白色都可以,价格不超过500,必须有现货"},
])
result = ds.mmops.product.parse_product_intent(
query_column="query", field_catalog=field_catalog,
reference_image_column=None, keep_columns=["query_id"],
)
# 四个条件组是 list[Condition];按 kind 读取 atom 或 any。
# Atom.value.kind 决定读取 scalar 或 items;TaggedScalar.type 决定载荷槽。
# 不再把 condition["value"] 当作原始字符串,也不把所有值转成同一种类型。
result = result.select_columns([
"query_id", "must_have", "better_have", "better_not_have", "must_not_have", "unresolved",
])
result.write_parquet("/shared/output/product-parse_product_intent/metadata")
示例返回结果
以下为与上述调用对应的返回结构示例,模型结果以实际输入及配置为准。算子返回 Dataset,take() 仅取少量行到本地查看;多行结果不保证跨分区的顺序。
示例展示两条输入对应的完整类型容器,字符串、整数、布尔值与列表保持各自的类型。行序不能作为关联依据,请使用 query_id 对应输入。
import json
rows = result.take(2)
print(json.dumps(rows, ensure_ascii=False, indent=2))
[
{
"query_id": "Q-001",
"must_have": [
{
"kind": "atom",
"atom": {
"field": "category",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "手提包"
},
"items": null
},
"source_text": "手提包"
},
"any": null
},
{
"kind": "atom",
"atom": {
"field": "color",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "黑色"
},
"items": null
},
"source_text": "黑色"
},
"any": null
}
],
"better_have": [],
"better_not_have": [],
"must_not_have": [],
"unresolved": []
},
{
"query_id": "Q-002",
"must_have": [
{
"kind": "atom",
"atom": {
"field": "color",
"op": "in",
"value": {
"kind": "list",
"scalar": null,
"items": [
{
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "黑色"
},
{
"type": "string",
"bool_value": null,
"int_value": null,
"float_value": null,
"text_value": "白色"
}
]
},
"source_text": "黑色或白色都可以"
},
"any": null
},
{
"kind": "atom",
"atom": {
"field": "price",
"op": "lte",
"value": {
"kind": "scalar",
"scalar": {
"type": "int",
"bool_value": null,
"int_value": 500,
"float_value": null,
"text_value": null
},
"items": null
},
"source_text": "价格不超过500"
},
"any": null
},
{
"kind": "atom",
"atom": {
"field": "in_stock",
"op": "eq",
"value": {
"kind": "scalar",
"scalar": {
"type": "bool",
"bool_value": true,
"int_value": null,
"float_value": null,
"text_value": null
},
"items": null
},
"source_text": "必须有现货"
},
"any": null
}
],
"better_have": [],
"better_not_have": [],
"must_not_have": [],
"unresolved": []
}
]
使用限制
-
四组条件始终为列表,组内默认按 AND 解释;单层 OR 使用
kind=any的固定 Condition 容器。 -
负向组中仍保存正向谓词,禁止使用 neq、not、not_in、not_contains 构成双重否定。例如
must_not_have中的 color eq 白色表示排除白色;其中的 any[P, Q] 命中任意一项即排除。 -
下游必须确认
must_have中的条件已知为真、must_not_have中的条件已知为假;未知结果既不能通过硬条件,也不能获得软偏好的加分。 -
有冲突或未解析的硬性要求须先确认,不能静默放宽。非法的原子条件进入
unresolved;同一标量字段的多值冲突或正负冲突等硬条件冲突也会移入unresolved。模型返回的条件组不是列表时,报 INVALID_MODEL_OUTPUT 错误。 -
仅当文字明确引用图片属性时,才会根据图片中的可见证据生成条件。图片读取失败时保留到
unresolved;不能猜测材质、SKU、真伪或其他隐藏属性。 -
reference_image必须是原始的定位结果,不能将status伪装为 cropped;参考对象结构错误时直接报错。 -
模型请求或 JSON 解析失败属于执行错误,不会以空结果伪装成功。