From 535c8a0a202ad637e03fbf24c9482a666c487e85 Mon Sep 17 00:00:00 2001 From: YUAN Date: Mon, 7 Sep 2026 12:11:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20T21=20=E7=9C=9F=E5=BA=93=E8=81=94?= =?UTF-8?q?=E8=B0=83=E4=B8=89=E5=9D=91=E2=80=94=E2=80=94=E2=91=A0Milvus=20?= =?UTF-8?q?Lite=20=E6=96=87=E4=BB=B6=E5=BA=93=E8=B7=A8=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E9=87=8D=E5=BC=80=20collection=20released,=20ensure=5Fcollecti?= =?UTF-8?q?on=20=E8=A1=A5=E6=98=BE=E5=BC=8F=20load(=E5=8D=95=E6=B5=8B?= =?UTF-8?q?=E5=90=8C=E8=BF=9B=E7=A8=8B=E8=A6=86=E7=9B=96=E4=B8=8D=E5=88=B0?= =?UTF-8?q?);=20=E2=91=A1faiss=20=E4=B8=8D=E6=94=AF=E6=8C=81=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E8=B7=AF=E5=BE=84(faiss=20C++=20IO),=20=E6=9C=AC?= =?UTF-8?q?=E6=9C=BA=20.env=20=E6=94=B9=E8=8B=B1=E6=96=87=E7=BB=9D?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84=20C:/Users/YUAN/.jinrong/milvus/,?= =?UTF-8?q?=20.env.example=20=E5=8A=A0=E8=AD=A6=E5=91=8A;=20=E2=91=A2build?= =?UTF-8?q?=5Fkb=20=E7=9B=B4=E8=B7=91=20sys.path=20=E5=BC=95=E5=AF=BC?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=A0=B9(=E5=90=8C=20rebuild=5Falerts=20?= =?UTF-8?q?=E5=85=88=E4=BE=8B);=20=E7=9C=9F=E5=BA=93=2024=20=E5=9D=97?= =?UTF-8?q?=E5=85=A5=E5=BA=93+=E6=A3=80=E7=B4=A2=E9=AA=8C=E6=94=B6?= =?UTF-8?q?=E9=80=9A=E8=BF=87(=E5=9F=BA=E9=87=91=E7=94=B3=E8=B4=AD=20Top3?= =?UTF-8?q?=20=E5=91=BD=E4=B8=AD=E7=94=B3=E8=B5=8E=E8=A7=84=E5=88=99,=20?= =?UTF-8?q?=E6=BA=AF=E6=BA=90=E5=AE=8C=E6=95=B4),=20425=20=E7=BB=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + app/service/milvus_service.py | 8 +++++++- scripts/kb/build_kb.py | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 29569a3..ba7d722 100644 --- a/.env.example +++ b/.env.example @@ -18,6 +18,7 @@ NEO4J_USER=neo4j NEO4J_PASSWORD= # Milvus Lite +# 注意:路径含中文会触发 Milvus Lite(faiss) 文件 IO 失败,Windows 中文用户目录请改纯英文绝对路径 MILVUS_URI=./data/milvus.db # Ollama Embedding diff --git a/app/service/milvus_service.py b/app/service/milvus_service.py index 967038f..5b83ed0 100644 --- a/app/service/milvus_service.py +++ b/app/service/milvus_service.py @@ -64,20 +64,26 @@ def _kb_schema(dim: int): def ensure_collection(client: MilvusClient, dim: int | None = None) -> None: - """幂等建集合(exists 跳过;不存在则建 schema + 向量索引)。 + """幂等建集合 + 确保加载(exists → load;不存在 → 建 schema + 索引 + load)。 索引 AUTOINDEX + COSINE:Milvus Lite 支持 AUTOINDEX(自动选型), COSINE 语义直观且 bge-m3 未归一化时也正确。dim 缺省取 settings.embed_dim, 与 embedding 服务同源——两边配置天然一致,不必各配一份。 + + load 口径(2026-09-07 实测坑):Milvus Lite 文件库**跨进程重开**后 + collection 处于 released 状态,直接 search 报 MilvusException(code=101); + 单测同进程覆盖不到,须每次 ensure 时显式 load_collection。 """ dim = dim or settings.embed_dim if client.has_collection(COLLECTION_NAME): + client.load_collection(COLLECTION_NAME) return index = client.prepare_index_params() index.add_index(field_name="embedding", index_type="AUTOINDEX", metric_type="COSINE") client.create_collection( COLLECTION_NAME, schema=_kb_schema(dim), index_params=index ) + client.load_collection(COLLECTION_NAME) def insert_chunks(client: MilvusClient, rows: list[dict]) -> int: diff --git a/scripts/kb/build_kb.py b/scripts/kb/build_kb.py index a14f3fc..bab834f 100644 --- a/scripts/kb/build_kb.py +++ b/scripts/kb/build_kb.py @@ -23,6 +23,10 @@ import re import sys from pathlib import Path +# 项目根加入 path(直跑脚本时 sys.path[0] 为脚本目录,须自行引导;同 rebuild_alerts) +ROOT = Path(__file__).resolve().parents[2] +sys.path.insert(0, str(ROOT)) + # 允许 doc_type 白名单(03-milvus-collections.md §2.1 冻结枚举) DOC_TYPES = ("prospectus", "fee", "rule", "risk")