Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
f26846e
1
Parent(s):
58f1304
optimize query speed
Browse files- document_processor.py +12 -2
document_processor.py
CHANGED
|
@@ -324,7 +324,7 @@ class DocumentProcessor:
|
|
| 324 |
|
| 325 |
# 显式建立全局连接 (修复 ConnectionNotExistException)
|
| 326 |
try:
|
| 327 |
-
from pymilvus import connections
|
| 328 |
print(f"🔌 尝试建立 pymilvus 全局连接 (Alias: default)...")
|
| 329 |
# 移除旧连接(如果存在)以防参数变更
|
| 330 |
if connections.has_connection("default"):
|
|
@@ -332,6 +332,13 @@ class DocumentProcessor:
|
|
| 332 |
|
| 333 |
connections.connect(alias="default", **connection_args)
|
| 334 |
print("✅ pymilvus 全局连接建立成功")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
except ImportError:
|
| 336 |
print("⚠️ 未找到 pymilvus 库,跳过显式连接")
|
| 337 |
except Exception as e:
|
|
@@ -339,10 +346,13 @@ class DocumentProcessor:
|
|
| 339 |
# 继续尝试,也许 LangChain 内部能处理
|
| 340 |
|
| 341 |
# 初始化 Milvus 连接 (不删除旧数据)
|
|
|
|
|
|
|
|
|
|
| 342 |
self.vectorstore = Milvus(
|
| 343 |
embedding_function=self.embeddings,
|
| 344 |
collection_name=COLLECTION_NAME,
|
| 345 |
-
connection_args=
|
| 346 |
index_params={
|
| 347 |
"metric_type": "L2",
|
| 348 |
"index_type": MILVUS_INDEX_TYPE,
|
|
|
|
| 324 |
|
| 325 |
# 显式建立全局连接 (修复 ConnectionNotExistException)
|
| 326 |
try:
|
| 327 |
+
from pymilvus import connections, utility
|
| 328 |
print(f"🔌 尝试建立 pymilvus 全局连接 (Alias: default)...")
|
| 329 |
# 移除旧连接(如果存在)以防参数变更
|
| 330 |
if connections.has_connection("default"):
|
|
|
|
| 332 |
|
| 333 |
connections.connect(alias="default", **connection_args)
|
| 334 |
print("✅ pymilvus 全局连接建立成功")
|
| 335 |
+
|
| 336 |
+
# 检查集合是否存在 (提前检查,避免 LangChain 内部出错)
|
| 337 |
+
if utility.has_collection(COLLECTION_NAME, using="default"):
|
| 338 |
+
print(f"✅ 集合 {COLLECTION_NAME} 已存在")
|
| 339 |
+
else:
|
| 340 |
+
print(f"ℹ️ 集合 {COLLECTION_NAME} 不存在,将由 Milvus 类自动创建")
|
| 341 |
+
|
| 342 |
except ImportError:
|
| 343 |
print("⚠️ 未找到 pymilvus 库,跳过显式连接")
|
| 344 |
except Exception as e:
|
|
|
|
| 346 |
# 继续尝试,也许 LangChain 内部能处理
|
| 347 |
|
| 348 |
# 初始化 Milvus 连接 (不删除旧数据)
|
| 349 |
+
# 注意:由于我们已经手动建立了全局连接 'default',
|
| 350 |
+
# 这里我们将 connection_args 简化为仅指向该 alias,
|
| 351 |
+
# 避免 LangChain 再次尝试连接或因参数问题覆盖连接。
|
| 352 |
self.vectorstore = Milvus(
|
| 353 |
embedding_function=self.embeddings,
|
| 354 |
collection_name=COLLECTION_NAME,
|
| 355 |
+
connection_args={"alias": "default"}, # ✅ 复用已建立的连接
|
| 356 |
index_params={
|
| 357 |
"metric_type": "L2",
|
| 358 |
"index_type": MILVUS_INDEX_TYPE,
|