在 Chroma 中,$eq 表示等于(equal,也可以直接给值,省略 $eq 符号,例如 {"cat":"销售"}),$ne 表示不等(not equal)。
示例:
print("\n--- 等于 ---")
show(col.get(where={"cat": "售后"}))
print("\n--- 等于 ---")
show(col.get(where={"cat": {"$eq": "售后"}})) # 等价写法
print("\n--- 不等于 ---")
show(col.get(where={"cat": {"$ne": "售后"}}))输出结果:
--- 等于 ---
id=r1, document=退款流程 refund, metadata={'is_public': True, 'score': 0.9, 'cat': '售后', 'tags': ['a', 'b'], 'year': 2020}
id=r4, document=退货政策, metadata={'is_public': False, 'cat': '售后', 'score': 0.3, 'tags': ['b', 'd'], 'year': 2024}
--- 等于 ---
id=r1, document=退款流程 refund, metadata={'is_public': True, 'tags': ['a', 'b'], 'cat': '售后', 'year': 2020, 'score': 0.9}
id=r4, document=退货政策, metadata={'year': 2024, 'tags': ['b', 'd'], 'cat': '售后', 'is_public': False, 'score': 0.3}
--- 不等于 ---
id=r2, document=发票 invoice, metadata={'cat': '财务', 'is_public': False, 'tags': ['c'], 'score': 0.5, 'year': 2022}
id=r3, document=物流 logistics, metadata={'score': 0.7, 'year': 2024, 'tags': ['a', 'd'], 'is_public': False, 'cat': '物流'}布尔和数值也一样:
print("\n--- 布尔值 ---")
show(col.get(where={"is_public": True}))
print("\n--- 数值 ---")
show(col.get(where={"year": 2024}))输出结果:
--- 布尔值 ---
id=r1, document=退款流程 refund, metadata={'cat': '售后', 'score': 0.9, 'tags': ['a', 'b'], 'is_public': True, 'year': 2020}
--- 数值 ---
id=r3, document=物流 logistics, metadata={'is_public': False, 'tags': ['a', 'd'], 'score': 0.7, 'year': 2024, 'cat': '物流'}
id=r4, document=退货政策, metadata={'year': 2024, 'cat': '售后', 'is_public': False, 'tags': ['b', 'd'], 'score': 0.3}