01
PilotDeck 的白盒记忆结构与 Milvus 3.0 映射
工作空间过滤与长文本存储:根据 WorkSpace 限定检索范围,同时保存记忆内容及其元数据。 Entity TTL:为临时记忆设置过期时间,测试能否自动清理,同时保留长期记忆。 服务端排序:验证置顶状态、置信度和更新时间能否直接参与查询排序。 Snapshots:在 Dream Mode 合并记忆前创建快照,测试合并出错后能否恢复先前的记忆状态。
02
建表与写入:工作空间隔离与 Entity TTL 自动过期
from pymilvus import DataType, MilvusClientclient = MilvusClient(uri="http://192.168.1.1:19531")schema = client.create_schema(auto_id=False)schema.add_field("mem_id", DataType.VARCHAR, max_length=64, is_primary=True)schema.add_field("workspace_id", DataType.VARCHAR, max_length=64, is_partition_key=True)schema.add_field("is_pinned", DataType.INT8) # 1 代表置顶,0 代表普通schema.add_field("confidence", DataType.FLOAT)schema.add_field("updated_at", DataType.INT64)schema.add_field("expire_at", DataType.TIMESTAMPTZ, nullable=True)schema.add_field("content", DataType.VARCHAR, max_length=65535)schema.add_field("embedding", DataType.FLOAT_VECTOR, dim=1024)index_params = client.prepare_index_params()index_params.add_index(field_name="embedding",index_type="HNSW",metric_type="COSINE",params={"M": 16, "efConstruction": 200},)client.create_collection(collection_name="pilot_mem_v1",schema=schema,index_params=index_params,properties={"ttl_field": "expire_at"},)client.create_alias(collection_name="pilot_mem_v1", alias="pilot_mem_active")
rows = [{"mem_id": "mem_pin_01","workspace_id": "ws_chip_report","is_pinned": 1,"confidence": 0.98,"updated_at": 1760774400,"expire_at": None, # 传 None (NULL) 代表永不过期"content": "数据图表统一使用深灰配色,保留原始出处链接。","embedding": embed("数据图表统一使用深灰配色..."),},{"mem_id": "mem_norm_02","workspace_id": "ws_chip_report","is_pinned": 0,"confidence": 0.92,"updated_at": 1760774600,"expire_at": None,"content": readme_long_doc, # 23,225 字节的 Markdown 文档"embedding": embed(readme_long_doc),},{"mem_id": "mem_ttl_temp_03","workspace_id": "ws_chip_report","is_pinned": 0,"confidence": 0.65,"updated_at": 1760774700,"expire_at": "2026-10-18T09:41:11Z", # 写入后 4 秒过期"content": "临时对话片段:用户正在调试本地端口 3001。","embedding": embed("临时对话片段..."),},]client.insert(collection_name="pilot_mem_active", data=rows)
03
白盒面板与检索:服务端 order_by 与分组统计
panel_list = client.query(collection_name="pilot_mem_active",filter='workspace_id == "ws_chip_report"',output_fields=["mem_id", "is_pinned", "confidence", "updated_at"],order_by=["is_pinned:desc", "confidence:desc", "updated_at:desc"],limit=5,offset=0,)
mem_pin_01(is_pinned=1, confidence=0.98)mem_frag_015(is_pinned=1, confidence=0.70)mem_norm_02(is_pinned=0, confidence=0.92)mem_frag_019(is_pinned=0, confidence=0.90, updated_at=1760775019)mem_frag_014(is_pinned=0, confidence=0.90, updated_at=1760775014)
search_hits = client.search(collection_name="pilot_mem_active",data=[query_vector],filter='workspace_id == "ws_chip_report" and confidence >= 0.6',limit=5,order_by_fields=[{"field": "is_pinned", "order": "desc"},{"field": "updated_at", "order": "desc"},],output_fields=["mem_id", "is_pinned", "confidence", "updated_at"],)
counts = client.query(collection_name="pilot_mem_active",filter="confidence > 0",group_by_fields=["workspace_id"],output_fields=["workspace_id", "count(*)"],)# 实测输出: [{'workspace_id': 'ws_chip_report', 'count(*)': 22}, {'workspace_id': 'ws_podcast_ops', 'count(*)': 1}]
04
Dream Mode 整合与快照回滚实测
client.flush(collection_name="pilot_mem_active")client.create_snapshot(collection_name="pilot_mem_active",snapshot_name="snap_ws_chip_1792316487",description="Safety point before Dream Mode compaction",)
client.insert(collection_name="pilot_mem_active", data=[bad_merged_row])client.delete(collection_name="pilot_mem_active",ids=[f"mem_frag_{i:03d}" for i in range(1, 21)],)
import time# 1. 将快照从原表恢复至新表 pilot_mem_v2job_id = client.restore_snapshot(snapshot_name="snap_ws_chip_1792316487",source_collection_name="pilot_mem_v1",target_collection_name="pilot_mem_v2",)# 2. 轮询恢复状态(Pending -> Executing -> Completed)while True:state = client.get_restore_snapshot_state(job_id=job_id)if state.state == "RestoreSnapshotCompleted":breakif state.state == "RestoreSnapshotFailed":raise RuntimeError(state.reason)time.sleep(0.05)# 3. 加载新表并原子切换业务别名client.load_collection(collection_name="pilot_mem_v2")client.alter_alias(collection_name="pilot_mem_v2", alias="pilot_mem_active")
写在最后
用 Partition Key 隔离不同工作空间; 用 TIMESTAMPTZ + ttl_field 自动淘汰短期碎片记忆; 用 order_by 与 group_by_fields 在服务端直接完成置顶排序和工作空间统计; 用 create_snapshot、restore_snapshot 配合 alter_alias,在几秒钟内无损回退错误的后台记忆合并。
作者介绍
Zilliz黄金写手:尹珉
阅读推荐Milvus 3.0 Embedding模型升级教程:从向量回填到灰度切流为什么我们放弃了Kafka、Pulsar,让WoodPecker承担Milvus3.0的写入工作Aquila语义去重精读:训练数据里的语义重复,白白烧掉了多少 GPU 时?从条件匹配到相似检索:同程旅行慧行搜索的火车换乘方案向量化实践AI 科研平台Consensus,如何基于Zilliz Cloud构建4 亿文献的学术检索系统