lanny xu commited on
Commit
c0c60d3
·
1 Parent(s): 4672f48

optimize query speed

Browse files
Files changed (2) hide show
  1. document_processor.py +13 -2
  2. run_server.py +14 -3
document_processor.py CHANGED
@@ -288,6 +288,7 @@ class DocumentProcessor:
288
  try:
289
  # 准备连接参数
290
  connection_args = {}
 
291
 
292
  # 优先使用 URI
293
  if MILVUS_URI and len(MILVUS_URI.strip()) > 0:
@@ -345,6 +346,16 @@ class DocumentProcessor:
345
  print(f"⚠️ 显式连接尝试失败: {e}")
346
  # 继续尝试,也许 LangChain 内部能处理
347
 
 
 
 
 
 
 
 
 
 
 
348
  # 初始化 Milvus 连接 (不删除旧数据)
349
  # 注意:由于我们已经手动建立了全局连接 'default',
350
  # 这里我们将 connection_args 简化为仅指向该 alias,
@@ -355,8 +366,8 @@ class DocumentProcessor:
355
  connection_args={"alias": "default"}, # ✅ 复用已建立的连接
356
  index_params={
357
  "metric_type": "L2",
358
- "index_type": MILVUS_INDEX_TYPE,
359
- "params": MILVUS_INDEX_PARAMS
360
  },
361
  search_params={
362
  "metric_type": "L2",
 
288
  try:
289
  # 准备连接参数
290
  connection_args = {}
291
+ is_local_file = False
292
 
293
  # 优先使用 URI
294
  if MILVUS_URI and len(MILVUS_URI.strip()) > 0:
 
346
  print(f"⚠️ 显式连接尝试失败: {e}")
347
  # 继续尝试,也许 LangChain 内部能处理
348
 
349
+ # 确定索引类型
350
+ # Milvus Lite (本地模式) 仅支持 FLAT, IVF_FLAT, AUTOINDEX,不支持 HNSW
351
+ final_index_type = MILVUS_INDEX_TYPE
352
+ final_index_params = MILVUS_INDEX_PARAMS
353
+
354
+ if is_local_file and MILVUS_INDEX_TYPE == "HNSW":
355
+ print("⚠️ 检测到 Milvus Lite (本地模式),HNSW 索引不受支持,自动切换为 AUTOINDEX")
356
+ final_index_type = "AUTOINDEX"
357
+ final_index_params = {} # AUTOINDEX 不需要复杂参数
358
+
359
  # 初始化 Milvus 连接 (不删除旧数据)
360
  # 注意:由于我们已经手动建立了全局连接 'default',
361
  # 这里我们将 connection_args 简化为仅指向该 alias,
 
366
  connection_args={"alias": "default"}, # ✅ 复用已建立的连接
367
  index_params={
368
  "metric_type": "L2",
369
+ "index_type": final_index_type,
370
+ "params": final_index_params
371
  },
372
  search_params={
373
  "metric_type": "L2",
run_server.py CHANGED
@@ -12,9 +12,13 @@ import re
12
  import shutil
13
 
14
  def install_ngrok():
15
- """安装 pyngrok"""
16
- print("🔧 正在安装 pyngrok...")
17
- subprocess.check_call([sys.executable, "-m", "pip", "install", "pyngrok"])
 
 
 
 
18
 
19
  def run_server():
20
  """在后台运行服务器"""
@@ -95,6 +99,13 @@ if __name__ == "__main__":
95
  import pyngrok
96
  except ImportError:
97
  install_ngrok()
 
 
 
 
 
 
 
98
 
99
  # 2. 启动 FastAPI
100
  server_thread = threading.Thread(target=run_server)
 
12
  import shutil
13
 
14
  def install_ngrok():
15
+ """安装 pyngrok 和 cloudflared"""
16
+ print("🔧 正在安装 Web 穿透工具...")
17
+ try:
18
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "pyngrok", "cloudflared"])
19
+ print("✅ 穿透工具安装完成")
20
+ except Exception as e:
21
+ print(f"⚠️ 安装穿透工具失败: {e}")
22
 
23
  def run_server():
24
  """在后台运行服务器"""
 
99
  import pyngrok
100
  except ImportError:
101
  install_ngrok()
102
+
103
+ # 检查 cloudflared 是否存在,如果不存在尝试安装
104
+ if not shutil.which("cloudflared"):
105
+ try:
106
+ __import__("cloudflared")
107
+ except ImportError:
108
+ install_ngrok()
109
 
110
  # 2. 启动 FastAPI
111
  server_thread = threading.Thread(target=run_server)